Browse Source

【内嵌支付】修复内嵌支付成功后弹窗显示

wanjun 6 months ago
parent
commit
aca60cfaf6

+ 119 - 87
PDF Office/PDF Master/Class/Purchase/DMG/KMPurchaseEmbeddedWindowController.swift

@@ -212,6 +212,9 @@ class KMPurchaseEmbeddedWindowController: NSWindowController {
     private var handler: ((Int) -> Void)!
     static var currentWindowController: KMPurchaseEmbeddedWindowController!
     private var embeddedPaymentPopWC: KMEmbeddedPaymentPopWC?
+    private var successfulPaymentsNumber = 0
+    private var license1 = ""
+    private var licenseAI1 = ""
     
     @objc static func currentFirstTrialWC(_ productId: String) -> KMPurchaseEmbeddedWindowController {
         if currentWindowController != nil {
@@ -605,11 +608,11 @@ class KMPurchaseEmbeddedWindowController: NSWindowController {
                     let aiBinding_discount = Float(KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.mac.product_3+com.brother.pdfreaderpro.ai.product_1")) ?? 94.98
                     let aiBinding_original = Float(KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.mac.product_3+com.brother.pdfreaderpro.ai.product_1")) ?? 94.98
                     let cross_discount = Float(KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.cross.platform.product_3")) ?? 119.98
-                    let cross_original = Float(KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.cross.platform.product_3")) ?? 119.98
+                    let cross_original = Float(KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.cross.platform.product_2")) ?? 119.98
                     discount2 = aiBinding_discount - discount1
                     original2 = aiBinding_original - original1
                     discount3 = cross_discount - discount1
-                    original3 = cross_original - original1
+                    original3 = cross_original
                     
                     listPrice = (original1 * Float(amountTextField1.stringValue)!)
                     paySum = (discount1 * Float(amountTextField1.stringValue)!)
@@ -626,13 +629,13 @@ class KMPurchaseEmbeddedWindowController: NSWindowController {
                 let aiBinding_discount = Float(KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.mac.product_3+com.brother.pdfreaderpro.ai.product_1")) ?? 94.98
                 let aiBinding_original = Float(KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.mac.product_3+com.brother.pdfreaderpro.ai.product_1")) ?? 94.98
                 let cross_discount = Float(KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.cross.platform.product_3")) ?? 119.98
-                let cross_original = Float(KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.cross.platform.product_3")) ?? 119.98
+                let cross_original = Float(KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.cross.platform.product_2")) ?? 119.98
                 discount2 = aiBinding_discount - discount1
                 original2 = aiBinding_original - original1
                 discount3 = cross_discount - discount1
-                original3 = cross_original - original1
+                original3 = cross_original
                 
-                listPrice = (original1 * Float(amountTextField1.stringValue)!) + (original3 * Float(amountTextField3.stringValue)!)
+                listPrice = (original1 * Float(amountTextField1.stringValue)!) + (discount3 * Float(amountTextField3.stringValue)!)
                 paySum = (discount1 * Float(amountTextField1.stringValue)!) + (discount3 * Float(amountTextField3.stringValue)!)
                 discount = listPrice - paySum
             } else if productType == .ipaPremiumAI {
@@ -678,11 +681,11 @@ class KMPurchaseEmbeddedWindowController: NSWindowController {
                     let aiBinding_discount = Float(KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.mac.product_3+com.brother.pdfreaderpro.ai.product_1")) ?? 94.98
                     let aiBinding_original = Float(KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.mac.product_3+com.brother.pdfreaderpro.ai.product_1")) ?? 94.98
                     let cross_discount = Float(KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.cross.platform.product_3")) ?? 119.98
-                    let cross_original = Float(KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.cross.platform.product_3")) ?? 119.98
+                    let cross_original = Float(KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.cross.platform.product_2")) ?? 119.98
                     discount2 = aiBinding_discount - discount1
                     original2 = aiBinding_original - original1
                     discount3 = cross_discount - discount1
-                    original3 = cross_original - original1
+                    original3 = cross_original
                     
                     listPrice = (original1 * Float(amountTextField1.stringValue)!) + (original2 * Float(amountTextField2.stringValue)!)
                     paySum = (discount1 * Float(amountTextField1.stringValue)!) + (discount2 * Float(amountTextField2.stringValue)!)
@@ -697,11 +700,11 @@ class KMPurchaseEmbeddedWindowController: NSWindowController {
                 let aiBinding_discount = Float(KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.mac.product_3+com.brother.pdfreaderpro.ai.product_1")) ?? 94.98
                 let aiBinding_original = Float(KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.mac.product_3+com.brother.pdfreaderpro.ai.product_1")) ?? 94.98
                 let cross_discount = Float(KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.cross.platform.product_3")) ?? 119.98
-                let cross_original = Float(KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.cross.platform.product_3")) ?? 119.98
+                let cross_original = Float(KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.cross.platform.product_2")) ?? 119.98
                 discount2 = aiBinding_discount - discount1
                 original2 = aiBinding_original - original1
                 discount3 = cross_discount - discount1
-                original3 = cross_original - original1
+                original3 = cross_original
                 
                 listPrice = (original1 * Float(amountTextField1.stringValue)!) + (original2 * Float(amountTextField2.stringValue)!) + (original3 * Float(amountTextField3.stringValue)!)
                 paySum = (discount1 * Float(amountTextField1.stringValue)!) + (discount2 * Float(amountTextField2.stringValue)!) + (discount3 * Float(amountTextField3.stringValue)!)
@@ -1033,7 +1036,7 @@ class KMPurchaseEmbeddedWindowController: NSWindowController {
     }
     
     @IBAction func paddlePopUpAction(_ sender: NSPopUpButton) {
-        buyAction(sender)
+        
     }
     
     @IBAction func couponClearButton(_ sender: NSButton) {
@@ -1507,7 +1510,7 @@ class KMPurchaseEmbeddedWindowController: NSWindowController {
         DispatchQueue.main.asyncAfter(deadline: .now() + 1) { [weak self] in
             guard let self = self else { return }
             if self.orderID == "" { return }
-            self._getOrderStatus(tradeNo: self.orderID, callback: { info, err in
+            self._getOrderStatus(tradeNo: self.orderID, callback: { [self] info, err in
                 if err == nil {
                     if let dataInfo = info {
                         if dataInfo.keys.contains("status") {
@@ -1528,20 +1531,42 @@ class KMPurchaseEmbeddedWindowController: NSWindowController {
                                 } else {
                                     self.orderID = ""
                                     self.stopPolling()
-                                    if dataInfo.keys.contains("license") {
-                                        let license = dataInfo["license"] as? [String]
-                                        if license!.count > 0 {
-                                            self.verificationActivate(license: license![0])
-                                            self.embeddedPaymentPopWC?.close()
-                                            self.embeddedPaymentPopWC = nil
+                                    if (dataInfo.keys.contains("license") && dataInfo.keys.contains("ai_license")) {
+                                        self.successfulPaymentsNumber = 2
+                                        if dataInfo.keys.contains("license") {
+                                            let license = dataInfo["license"] as? [String]
+                                            if license!.count > 0 {
+                                                self.verificationActivate(license: license![0])
+//                                                self.embeddedPaymentPopWC?.close()
+//                                                self.embeddedPaymentPopWC = nil
+                                            }
                                         }
-                                    }
-                                    if dataInfo.keys.contains("ai_license") {
-                                        let ai_license = dataInfo["ai_license"] as? [String]
-                                        if ai_license!.count > 0 {
-                                            self.verificationActivateAIInfo(license: ai_license![0])
-                                            self.embeddedPaymentPopWC?.close()
-                                            self.embeddedPaymentPopWC = nil
+                                        if dataInfo.keys.contains("ai_license") {
+                                            let ai_license = dataInfo["ai_license"] as? [String]
+                                            if ai_license!.count > 0 {
+                                                self.verificationActivateAIInfo(license: ai_license![0])
+//                                                self.embeddedPaymentPopWC?.close()
+//                                                self.embeddedPaymentPopWC = nil
+                                            }
+                                        }
+                                    } else {
+                                        self.successfulPaymentsNumber = 1
+                                        if dataInfo.keys.contains("license") {
+                                            let license = dataInfo["license"] as? [String]
+                                            if license!.count > 0 {
+                                                self.verificationActivate(license: license![0])
+//                                                self.embeddedPaymentPopWC?.close()
+//                                                self.embeddedPaymentPopWC = nil
+                                            }
+                                        } else if dataInfo.keys.contains("ai_license") {
+                                            let ai_license = dataInfo["ai_license"] as? [String]
+                                            if ai_license!.count > 0 {
+                                                if !AIInfoManager.default().aiInfoValid {
+                                                    self.verificationActivateAIInfo(license: ai_license![0])
+                                                }
+//                                                self.embeddedPaymentPopWC?.close()
+//                                                self.embeddedPaymentPopWC = nil
+                                            }
                                         }
                                     }
                                 }
@@ -1616,21 +1641,45 @@ class KMPurchaseEmbeddedWindowController: NSWindowController {
                 self._trackEvent_paid()
             }
 
+            self.successfulPaymentsNumber -= 1
             if self.paymentMethod == .wxpay || self.paymentMethod == .alipay {
                 self.qrCodeImageView.isHidden = true
                 self.successfullyView.isHidden = false
+                
+                if self.successfulPaymentsNumber == 0 {
+                    if let blockSelf = self.activityAlertViewController {
+                        blockSelf.alertTitle = NSLocalizedString("Successful Activation!", comment: "")
+                        blockSelf.alertMessage = NSLocalizedString("Congratulations! You can start using PDF Reader Pro AI features.", comment: "")
+                        
+                        var frame = self.window?.frame
+                        frame!.origin.y -= blockSelf.view.frame.size.height-frame!.size.height
+                        frame!.origin.x -= (blockSelf.view.frame.size.width-frame!.size.width)/2.0
+                        frame!.size.width = blockSelf.view.frame.size.width
+                        frame!.size.height = blockSelf.view.frame.size.height
+                        self.mainBox.contentView = blockSelf.view
+                        self.window?.setFrame(frame!, display: true, animate: true)
+                        
+                        self.embeddedPaymentPopWC?.close()
+                        self.embeddedPaymentPopWC = nil
+                    }
+                }
             } else {
-                if let blockSelf = self.activityAlertViewController {
-                    blockSelf.alertTitle = NSLocalizedString("Successful Activation!", comment: "")
-                    blockSelf.alertMessage = NSLocalizedString("Congratulations! You can start using PDF Reader Pro AI features.", comment: "")
-                    
-                    var frame = self.window?.frame
-                    frame!.origin.y -= blockSelf.view.frame.size.height-frame!.size.height
-                    frame!.origin.x -= (blockSelf.view.frame.size.width-frame!.size.width)/2.0
-                    frame!.size.width = blockSelf.view.frame.size.width
-                    frame!.size.height = blockSelf.view.frame.size.height
-                    self.mainBox.contentView = blockSelf.view
-                    self.window?.setFrame(frame!, display: true, animate: true)
+                if self.successfulPaymentsNumber == 0 {
+                    if let blockSelf = self.activityAlertViewController {
+                        blockSelf.alertTitle = NSLocalizedString("Successful Activation!", comment: "")
+                        blockSelf.alertMessage = NSLocalizedString("Congratulations! You can start using PDF Reader Pro AI features.", comment: "")
+                        
+                        var frame = self.window?.frame
+                        frame!.origin.y -= blockSelf.view.frame.size.height-frame!.size.height
+                        frame!.origin.x -= (blockSelf.view.frame.size.width-frame!.size.width)/2.0
+                        frame!.size.width = blockSelf.view.frame.size.width
+                        frame!.size.height = blockSelf.view.frame.size.height
+                        self.mainBox.contentView = blockSelf.view
+                        self.window?.setFrame(frame!, display: true, animate: true)
+                        
+                        self.embeddedPaymentPopWC?.close()
+                        self.embeddedPaymentPopWC = nil
+                    }
                 }
             }
         }
@@ -1644,47 +1693,6 @@ class KMPurchaseEmbeddedWindowController: NSWindowController {
         let infoDic = ["cdkey": license]
         VerificationManager.default().activateDevice(withInfo: infoDic) { status, info, error in
             if error != nil {
-//                let tTitle0 = NSLocalizedString("The license you have entered is not correct. Please enter valid license.", comment: "")
-//                let tTitle1 = NSLocalizedString("If you have already purchased PDF Reader Pro for Mac, you should find your license in an email confirmation.", comment: "")
-//                var message = String(format: "%@\n\n%@", tTitle0, tTitle1)
-//                if let err = error as? NSError {
-//                    if ActivityErrorType(UInt32(err.code)) == ActivityErrorTypeCDKeyEmpty {
-//                        message = String(format: "%@\n\n%@", NSLocalizedString("License number can not be empty.", comment:""), "")
-//                    } else if ActivityErrorType(UInt32(err.code)) == ActivityErrorTypeCDKeyNotExist {
-//                        message = String(format: "%@\n\n%@", NSLocalizedString("License number does not exist.", comment:""), "")
-//                    } else if ActivityErrorType(UInt32(err.code)) == ActivityErrorTypeOutNumber {
-//                        message = String(format: "%@\n\n%@", NSLocalizedString("The devices connected to the license number has outnumbered.", comment:""), "")
-//                    } else if ActivityErrorType(UInt32(err.code)) == ActivityErrorTypeOutNumber {
-//                        let tMsg0 = NSLocalizedString("The devices connected to the license number has outnumbered.", comment: "")
-//                        let tMsg1 = NSLocalizedString("Please deactivate license on one of them, to use PDF Reader Pro on this computer", comment: "")
-//                        message = String(format: "%@\n\n%@", tMsg0, tMsg1)
-//                    } else if ActivityErrorType(UInt32(err.code)) == ActivityErrorTypeNetworkDisable {
-//                        message = NSLocalizedString("The network is unavailable.", comment: "")
-//                    } else if ActivityErrorType(UInt32(err.code)) == ActivityErrorTypeCDKeyExpire {
-//                        message = NSLocalizedString("The license number expired.", comment: "")
-//                    } else if ActivityErrorType(UInt32(err.code)) == ActivityErrorTypeInvalidInfo {
-//                        message = NSLocalizedString("Activation error. Please try to input correct license, or contact us.", comment: "")
-//                    } else if ActivityErrorType(UInt32(err.code)) == ActivityErrorTypeNotMatchProduct {
-//                        message = NSLocalizedString("The license number is wrong, please input right license which is for this app.", comment: "")
-//                    } else if ActivityErrorType(UInt32(err.code)) == ActivityErrorTypeFormatError {
-//                        message = NSLocalizedString("The license format is wrong. Please input correct license.", comment: "")
-//                    }
-//                    if (ActivityErrorType(UInt32(err.code)) == ActivityErrorTypeUnknow) || (ActivityErrorType(UInt32(err.code)) == ActivityErrorTypeOutNumber) {
-//                        let alert = NSAlert()
-//                        alert.alertStyle = .critical
-//                        alert.messageText = NSLocalizedString("Activation Error", comment: "")
-//                        alert.informativeText = NSLocalizedString("Contact Us", comment: "")
-//                        alert.addButton(withTitle: NSLocalizedString("Try Again", comment: ""))
-//                        if alert.responds(to: #selector(alert.beginSheetModal(for:completionHandler:))) {
-//                            alert.beginSheetModal(for: self.window!, completionHandler: nil)
-//                        } else {
-//                            alert.runModal()
-//                        }
-//                        return
-//                    } else {
-//                        
-//                    }
-//                }
                 let alert = NSAlert()
                 alert.alertStyle = .critical
                 alert.messageText = NSLocalizedString("Activation Error", comment: "")
@@ -1700,21 +1708,45 @@ class KMPurchaseEmbeddedWindowController: NSWindowController {
                     self._trackEvent_paid()
                 }
 
+                self.successfulPaymentsNumber -= 1
                 if self.paymentMethod == .wxpay || self.paymentMethod == .alipay {
                     self.qrCodeImageView.isHidden = true
                     self.successfullyView.isHidden = false
+                    
+                    if self.successfulPaymentsNumber == 0 {
+                        if let blockSelf = self.activityAlertViewController {
+                            blockSelf.alertTitle = NSLocalizedString("Successful Activation!", comment: "")
+                            blockSelf.alertMessage = NSLocalizedString("Congratulations! You can start using PDF Reader Pro features.", comment: "")
+                            
+                            var frame = self.window?.frame
+                            frame!.origin.y -= blockSelf.view.frame.size.height-frame!.size.height
+                            frame!.origin.x -= (blockSelf.view.frame.size.width-frame!.size.width)/2.0
+                            frame!.size.width = blockSelf.view.frame.size.width
+                            frame!.size.height = blockSelf.view.frame.size.height
+                            self.mainBox.contentView = blockSelf.view
+                            self.window?.setFrame(frame!, display: true, animate: true)
+                            
+                            self.embeddedPaymentPopWC?.close()
+                            self.embeddedPaymentPopWC = nil
+                        }
+                    }
                 } else {
-                    if let blockSelf = self.activityAlertViewController {
-                        blockSelf.alertTitle = NSLocalizedString("Successful Activation!", comment: "")
-                        blockSelf.alertMessage = NSLocalizedString("Congratulations! You can start using PDF Reader Pro features.", comment: "")
-                        
-                        var frame = self.window?.frame
-                        frame!.origin.y -= blockSelf.view.frame.size.height-frame!.size.height
-                        frame!.origin.x -= (blockSelf.view.frame.size.width-frame!.size.width)/2.0
-                        frame!.size.width = blockSelf.view.frame.size.width
-                        frame!.size.height = blockSelf.view.frame.size.height
-                        self.mainBox.contentView = blockSelf.view
-                        self.window?.setFrame(frame!, display: true, animate: true)
+                    if self.successfulPaymentsNumber == 0 {
+                        if let blockSelf = self.activityAlertViewController {
+                            blockSelf.alertTitle = NSLocalizedString("Successful Activation!", comment: "")
+                            blockSelf.alertMessage = NSLocalizedString("Congratulations! You can start using PDF Reader Pro features.", comment: "")
+                            
+                            var frame = self.window?.frame
+                            frame!.origin.y -= blockSelf.view.frame.size.height-frame!.size.height
+                            frame!.origin.x -= (blockSelf.view.frame.size.width-frame!.size.width)/2.0
+                            frame!.size.width = blockSelf.view.frame.size.width
+                            frame!.size.height = blockSelf.view.frame.size.height
+                            self.mainBox.contentView = blockSelf.view
+                            self.window?.setFrame(frame!, display: true, animate: true)
+                            
+                            self.embeddedPaymentPopWC?.close()
+                            self.embeddedPaymentPopWC = nil
+                        }
                     }
                 }
             }