Переглянути джерело

【会员系统】DMG版本,内嵌支付相关逻辑

wangshuai 1 тиждень тому
батько
коміт
5fbf6b44c1

+ 103 - 53
PDF Office/PDF Master/Class/Purchase/DMG/KMPurchaseEmbeddedWindowController.swift

@@ -1626,7 +1626,54 @@ class KMPurchaseEmbeddedWindowController: NSWindowController {
                              discountFlag: getOneTimePurchaseDiscountFlag(),
                              couponCode: couponCode,
                              num: pdfCount) { success, result in
-                //写定时器查询结果
+                if success {
+                    if let dataInfo = result {
+                        self.orderID = result?.createOrder?.orderId ?? ""
+                        let page_pay_url = result?.createOrder?.payHref ?? ""
+                        let qrCode = result?.createOrder?.qrCode ?? ""
+                        if self.paymentMethod == .paddle || self.paymentMethod == .paypal {
+                            if self.orderID == "" || page_pay_url == "" {
+                                DispatchQueue.main.async {
+                                    let alert = NSAlert()
+                                    alert.alertStyle = .critical
+                                    alert.messageText = NSLocalizedString("Please check if the information is wrong or the network is error.", comment: "")
+                                    alert.addButton(withTitle: NSLocalizedString("Yes", comment: ""))
+                                    alert.runModal()
+                                }
+                            } else {
+                                self.openWebView(page_pay_url)
+                            }
+                        } else if self.paymentMethod == .wxpay || self.paymentMethod == .alipay {
+                            if self.orderID == "" || qrCode == "" {
+                                DispatchQueue.main.async {
+                                    let alert = NSAlert()
+                                    alert.alertStyle = .critical
+                                    alert.messageText = NSLocalizedString("Please check if the information is wrong or the network is error.", comment: "")
+                                    alert.addButton(withTitle: NSLocalizedString("Yes", comment: ""))
+                                    alert.runModal()
+                                }
+                            } else {
+                                self.scanCode(qrCode)
+                            }
+                        }
+                    } else {
+                        DispatchQueue.main.async {
+                            let alert = NSAlert()
+                            alert.alertStyle = .critical
+                            alert.messageText = NSLocalizedString("Please check if the information is wrong or the network is error.", comment: "")
+                            alert.addButton(withTitle: NSLocalizedString("Yes", comment: ""))
+                            alert.runModal()
+                        }
+                    }
+                } else {
+                    DispatchQueue.main.async {
+                        let alert = NSAlert()
+                        alert.alertStyle = .critical
+                        alert.messageText = NSLocalizedString("Please check if the information is wrong or the network is error.", comment: "")
+                        alert.addButton(withTitle: NSLocalizedString("Yes", comment: ""))
+                        alert.runModal()
+                    }
+                }
             }
         } else {
             var price = _product_Info.price
@@ -1666,44 +1713,43 @@ class KMPurchaseEmbeddedWindowController: NSWindowController {
                                    discountFlag: getPurchaseDiscountFlag(),
                                    couponCode: couponCode,
                                    num: pdfCount) { success, result in
-                //写定时器查询结果
+                if success {
+                    if let dataInfo = result {
+                        self.orderID = result?.createOrder?.orderId ?? ""
+                        let page_pay_url = result?.createOrder?.payHref ?? ""
+                        if self.orderID == "" || page_pay_url == "" {
+                            DispatchQueue.main.async {
+                                let alert = NSAlert()
+                                alert.alertStyle = .critical
+                                alert.messageText = NSLocalizedString("Please check if the information is wrong or the network is error.", comment: "")
+                                alert.addButton(withTitle: NSLocalizedString("Yes", comment: ""))
+                                alert.runModal()
+                            }
+                        } else {
+                            if self.paymentMethod == .paddle || self.paymentMethod == .paypal {
+                                self.openWebView(page_pay_url)
+                            }
+                        }
+                    } else {
+                        DispatchQueue.main.async {
+                            let alert = NSAlert()
+                            alert.alertStyle = .critical
+                            alert.messageText = NSLocalizedString("Please check if the information is wrong or the network is error.", comment: "")
+                            alert.addButton(withTitle: NSLocalizedString("Yes", comment: ""))
+                            alert.runModal()
+                        }
+                    }
+                } else {
+                    DispatchQueue.main.async {
+                        let alert = NSAlert()
+                        alert.alertStyle = .critical
+                        alert.messageText = NSLocalizedString("Please check if the information is wrong or the network is error.", comment: "")
+                        alert.addButton(withTitle: NSLocalizedString("Yes", comment: ""))
+                        alert.runModal()
+                    }
+                }
             }
         }
-//        self._buyProduct(productID, count: pdfCount, discountId: couponCode, payment: paymentMethod, license: licenseCode, email: emailTextField.stringValue) { [weak self] info, err in
-//            guard let self = self else { return }
-//            if err != nil {
-//                DispatchQueue.main.async {
-//                    let alert = NSAlert()
-//                    alert.alertStyle = .critical
-//                    alert.messageText = NSLocalizedString("Please check if the information is wrong or the network is error.", comment: "")
-//                    alert.addButton(withTitle: NSLocalizedString("Yes", comment: ""))
-//                    alert.runModal()
-//                }
-//                return
-//            }
-//            if let dataInfo = info {
-//                self.orderID = dataInfo["trade_no"] as! String
-//                let page_pay_url = dataInfo["page_pay_url"] as! String
-//                if self.paymentMethod == .paddle || self.paymentMethod == .paypal {
-//                    if page_pay_url != "" {
-//                        self.openWebView(page_pay_url)
-//                    }
-//                } else if self.paymentMethod == .wxpay || self.paymentMethod == .alipay {
-//                    if page_pay_url != "" {
-//                        self.scanCode(page_pay_url)
-//                    }
-//                }
-//            } else {
-//                // 数据错误
-//                DispatchQueue.main.async {
-//                    let alert = NSAlert()
-//                    alert.alertStyle = .critical
-//                    alert.messageText = NSLocalizedString("Please check if the information is wrong or the network is error.", comment: "")
-//                    alert.addButton(withTitle: NSLocalizedString("Yes", comment: ""))
-//                    alert.runModal()
-//                }
-//            }
-//        }
     }
     
     func getPurchasePaymentMethod() -> Int {
@@ -2178,6 +2224,11 @@ class KMPurchaseEmbeddedWindowController: NSWindowController {
         startPolling()
     }
     
+    private func convertBase64StringToNSImage(base64String: String) -> NSImage? {
+        // 待补充base64转NSImage方法
+        return nil
+    }
+    
     private func scanCode(_ urlPath: String) -> Void {
         DispatchQueue.main.async { [weak self] in
             guard let self = self else { return }
@@ -2190,21 +2241,7 @@ class KMPurchaseEmbeddedWindowController: NSWindowController {
                 self.scanLabel.stringValue = NSLocalizedString("Scan QR Code with Alipay", comment: "")
                 self.payTypeImageView.image = NSImage(named: "EmbeddedPayment28")
             }
-
-            if let url = URL(string: urlPath) {
-                URLSession.shared.dataTask(with: url) { data, response, error in
-                    if let data = data, let image = NSImage(data: data) {
-                        DispatchQueue.main.async {
-                            self.qrCodeImageView.image = image
-                            self.startPolling()
-                        }
-                    } else {
-                        print("Failed to load image or there was an error: \(error?.localizedDescription ?? "Unknown error")")
-                    }
-                }.resume()
-            } else {
-                print("Invalid URL.")
-            }
+            self.payTypeImageView.image = convertBase64StringToNSImage(base64String: urlPath)
         }
     }
     
@@ -2290,6 +2327,19 @@ class KMPurchaseEmbeddedWindowController: NSWindowController {
         }
     }
     
+    private func pollingResultNew() -> Void {
+        DispatchQueue.main.asyncAfter(deadline: .now() + 1) { [weak self] in
+            guard let self = self else { return }
+            if self.orderID == "" { return }
+            KMMemberCenterManager.manager.getStateByOrderId(orderId: self.orderID) { success, info in
+                if (success) {
+                    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "KMIAPSubscriptionLoadedNotification"), object: nil)
+                    self.stopPolling();
+                }
+            }
+        }
+    }
+    
     private func startPolling() {
         timer = Timer(timeInterval: interval, repeats: false) { [weak self] _ in
             guard let self = self else { return }
@@ -2309,7 +2359,7 @@ class KMPurchaseEmbeddedWindowController: NSWindowController {
         print("Performing polling task \(pollCount + 1)/\(maxPolls)")
         
         pollCount += 1
-        pollingResult()
+        pollingResultNew()
     }
     
     private func stopPolling() {

+ 8 - 4
PDF Office/PDF Master/MemberCenter/Model/KMMemberCenterManager.swift

@@ -1374,15 +1374,17 @@ class KMMemberCenterManager: NSObject {
             var thirdOrderNo = ""
             var orderId = ""
             var payHref = ""
+            var qrCode = ""
             if requestCode == 200 {
                 if let token = result["thirdOrderNo"] { thirdOrderNo = token as? String ?? thirdOrderNo}
                 if let token = result["orderId"] { orderId = token as? String ?? orderId}
                 if let token = result["payHref"] { payHref = token as? String ?? payHref}
-                let products = KMCreateOrderModel(thirdOrderNo: thirdOrderNo, orderId: orderId, payHref: payHref)
+                if let token = result["qrcode"] { qrCode = token as? String ?? qrCode}
+                let products = KMCreateOrderModel(thirdOrderNo: thirdOrderNo, orderId: orderId, payHref: payHref, qrCode: qrCode)
                 let result1 = KMMemberProductResult(code: requestCode, msg: message, createOrder: products)
                 complete(true, result1)
             } else {
-                let result1 = KMMemberProductResult(code: requestCode, msg: message, createOrder: KMCreateOrderModel(thirdOrderNo: thirdOrderNo, orderId: orderId, payHref: payHref))
+                let result1 = KMMemberProductResult(code: requestCode, msg: message, createOrder: KMCreateOrderModel(thirdOrderNo: thirdOrderNo, orderId: orderId, payHref: payHref, qrCode: qrCode))
                 complete(false, result1)
             }
         }
@@ -1433,15 +1435,17 @@ class KMMemberCenterManager: NSObject {
             var thirdOrderNo = ""
             var orderId = ""
             var payHref = ""
+            var qrCode = ""
             if requestCode == 200 {
                 if let token = result["thirdOrderNo"] { thirdOrderNo = token as? String ?? thirdOrderNo}
                 if let token = result["orderId"] { orderId = token as? String ?? orderId}
                 if let token = result["payHref"] { payHref = token as? String ?? payHref}
-                let products = KMCreateOrderModel(thirdOrderNo: thirdOrderNo, orderId: orderId, payHref: payHref)
+                if let token = result["qrcode"] { qrCode = token as? String ?? qrCode}
+                let products = KMCreateOrderModel(thirdOrderNo: thirdOrderNo, orderId: orderId, payHref: payHref, qrCode: qrCode)
                 let result1 = KMMemberProductResult(code: requestCode, msg: message, createOrder: products)
                 complete(true, result1)
             } else {
-                let result1 = KMMemberProductResult(code: requestCode, msg: message, createOrder: KMCreateOrderModel(thirdOrderNo: thirdOrderNo, orderId: orderId, payHref: payHref))
+                let result1 = KMMemberProductResult(code: requestCode, msg: message, createOrder: KMCreateOrderModel(thirdOrderNo: thirdOrderNo, orderId: orderId, payHref: payHref, qrCode: qrCode))
                 complete(false, result1)
             }
         }

+ 3 - 1
PDF Office/PDF Master/MemberCenter/Model/KMMemberProductResult.swift

@@ -187,10 +187,12 @@ class KMCreateOrderModel: NSObject {
     var thirdOrderNo    : String = ""   // 【必需】
     var orderId         : String = ""   // 【必需】
     var payHref         : String = ""   // 【必需】
-    init(thirdOrderNo: String, orderId: String, payHref: String) {
+    var qrCode          : String = ""   // 【必需】
+    init(thirdOrderNo: String, orderId: String, payHref: String, qrCode: String) {
         self.thirdOrderNo = thirdOrderNo
         self.orderId = orderId
         self.payHref = payHref
+        self.qrCode = qrCode
     }
 }