Explorar el Código

【内嵌支付】补充完善优惠卷相关逻辑

wanjun hace 6 meses
padre
commit
d5f059ada3

+ 49 - 37
PDF Office/PDF Master/Class/Purchase/DMG/KMPurchaseEmbeddedWindowController.swift

@@ -60,6 +60,9 @@ class KMPurchaseEmbeddedWindowController: NSWindowController {
     @IBOutlet weak var couponClearButton: NSButton!
     @IBOutlet weak var applyBox: NSBox!
     @IBOutlet weak var applyButton: NSButton!
+    @IBOutlet weak var coupomErrorLabel: NSTextField!
+    @IBOutlet weak var coupomErrorTopLayout: NSLayoutConstraint!
+    
     @IBOutlet weak var lastPriceLabel: NSTextField!
     @IBOutlet weak var lastPriceLabel1: NSTextField!
     @IBOutlet weak var discountView: NSView!
@@ -347,6 +350,10 @@ class KMPurchaseEmbeddedWindowController: NSWindowController {
         couponBox.borderColor = NSColor(named: "KMPurchaseCouponBoxColor") ?? NSColor.gray
         couponBox.fillColor = NSColor(named: "KMPurchaseCouponBoxFillColor") ?? NSColor.gray
         applyBox.borderColor = NSColor(named: "KMPurchaseApplyColor") ?? NSColor.black
+        coupomErrorLabel.textColor = NSColor(named: "KMPurchaseErrorColor")
+        coupomErrorLabel.font = NSFont.SFProTextRegularFont(11.0)
+        refreshCouponError(isReveal: false)
+        
         lastPriceLabel.stringValue = NSLocalizedString("List Price", comment: "")
         lastPriceLabel.textColor = NSColor(named: "KMPurchaseListPriceColor")
         lastPriceLabel.font = NSFont.SFProTextRegularFont(16.0)
@@ -494,7 +501,7 @@ class KMPurchaseEmbeddedWindowController: NSWindowController {
         if KMDMGProductsManager.shareInstance.productDatas == nil {
             let alert = NSAlert()
             alert.alertStyle = .critical
-            alert.messageText = NSLocalizedString("Connection Error", comment: "")
+            alert.messageText = NSLocalizedString("Error Information", comment: "")
             alert.informativeText = NSLocalizedString("Please make sure your internet connection is available.", comment: "")
             alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
             if alert.responds(to: #selector(alert.beginSheetModal(for:completionHandler:))) {
@@ -837,6 +844,8 @@ class KMPurchaseEmbeddedWindowController: NSWindowController {
             }
         }
         
+        applyButton.isEnabled = true
+        coupomTextField.isEnabled = true
         // 价格刷新
         priceRefresh()
     }
@@ -884,6 +893,8 @@ class KMPurchaseEmbeddedWindowController: NSWindowController {
             }
         }
         
+        applyButton.isEnabled = true
+        coupomTextField.isEnabled = true
         // 价格刷新
         priceRefresh()
     }
@@ -905,8 +916,8 @@ class KMPurchaseEmbeddedWindowController: NSWindowController {
         if coupomTextField.stringValue == "" {
             let alert = NSAlert()
             alert.alertStyle = .critical
-            alert.messageText = NSLocalizedString("Coupon Error", comment: "")
-            alert.informativeText = NSLocalizedString("Please enter the coupon.", comment: "")
+            alert.messageText = NSLocalizedString("Error Information", comment: "")
+            alert.informativeText = NSLocalizedString("Please enter a coupon code.", comment: "")
             alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
             if alert.responds(to: #selector(alert.beginSheetModal(for:completionHandler:))) {
                 alert.beginSheetModal(for: self.window!, completionHandler: nil)
@@ -915,6 +926,9 @@ class KMPurchaseEmbeddedWindowController: NSWindowController {
             }
             return
         }
+        self.refreshCouponError(isReveal: false)
+        applyButton.isEnabled = false
+        coupomTextField.isEnabled = false
         self._getDiscount(productId: productID, discountId: coupomTextField.stringValue) { [weak self] info, err in
             guard let self = self else { return }
             if err == nil {
@@ -922,30 +936,14 @@ class KMPurchaseEmbeddedWindowController: NSWindowController {
                     if dataInfo.keys.contains("status") {
                         let status = dataInfo["status"] as? String ?? ""
                         if status == "" {
-                            let alert = NSAlert()
-                            alert.alertStyle = .critical
-                            alert.messageText = NSLocalizedString("Connection error", comment: "")
-                            alert.informativeText = NSLocalizedString("Promotional volume exception please try again.", comment: "")
-                            alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
-                            if alert.responds(to: #selector(alert.beginSheetModal(for:completionHandler:))) {
-                                alert.beginSheetModal(for: self.window!, completionHandler: nil)
-                            } else {
-                                alert.runModal()
-                            }
+                            self.coupomErrorLabel.stringValue = NSLocalizedString("The coupon code is invalid. Please try again.", comment: "")
+                            self.refreshCouponError(isReveal: true)
                             return
                         }
                         if status == "unused" {
                             if dataInfo["discount"] is NSNull && dataInfo["price"] is NSNull {
-                                let alert = NSAlert()
-                                alert.alertStyle = .critical
-                                alert.messageText = NSLocalizedString("Connection error", comment: "")
-                                alert.informativeText = NSLocalizedString("Non-concessionary amount.", comment: "")
-                                alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
-                                if alert.responds(to: #selector(alert.beginSheetModal(for:completionHandler:))) {
-                                    alert.beginSheetModal(for: self.window!, completionHandler: nil)
-                                } else {
-                                    alert.runModal()
-                                }
+                                self.coupomErrorLabel.stringValue = NSLocalizedString("The coupon code is invalid. Please try again.", comment: "")
+                                self.refreshCouponError(isReveal: true)
                                 return
                             } else {
                                 if dataInfo.keys.contains("coupon_provider") {
@@ -956,6 +954,8 @@ class KMPurchaseEmbeddedWindowController: NSWindowController {
                                                 if limitCode.count > 0 {
                                                     if self.productID != limitCode {
                                                         self.couponClearButton.isHidden = false
+                                                        self.coupomErrorLabel.stringValue = NSLocalizedString("The coupon code is invalid. Please try again.", comment: "")
+                                                        self.refreshCouponError(isReveal: true)
                                                         return
                                                     }
                                                 }
@@ -979,25 +979,23 @@ class KMPurchaseEmbeddedWindowController: NSWindowController {
                             }
                         } else if status == "used" || status == "expired" {
                             self.couponClearButton.isHidden = false
+                            if status == "used" {
+                                self.coupomErrorLabel.stringValue = NSLocalizedString("The coupon code has been used, please change your coupon code.", comment: "")
+                            } else {
+                                self.coupomErrorLabel.stringValue = NSLocalizedString("The coupon code has expired.", comment: "")
+                            }
+                            self.refreshCouponError(isReveal: true)
                         }
                     } else {
                         if dataInfo["discount"] is NSNull && dataInfo["price"] is NSNull {
-                            let alert = NSAlert()
-                            alert.alertStyle = .critical
-                            alert.messageText = NSLocalizedString("Connection error", comment: "")
-                            alert.informativeText = NSLocalizedString("Non-concessionary amount.", comment: "")
-                            alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
-                            if alert.responds(to: #selector(alert.beginSheetModal(for:completionHandler:))) {
-                                alert.beginSheetModal(for: self.window!, completionHandler: nil)
-                            } else {
-                                alert.runModal()
-                            }
-                            return
+                            self.coupomErrorLabel.stringValue = NSLocalizedString("The coupon code is invalid. Please try again.", comment: "")
+                            self.refreshCouponError(isReveal: true)
                         } else {
                             self.couponClearButton.isHidden = true
                             if dataInfo["discount"] is NSNull {
                                 if dataInfo["price"] is NSNull {
-                                    
+                                    self.coupomErrorLabel.stringValue = NSLocalizedString("The coupon code is invalid. Please try again.", comment: "")
+                                    self.refreshCouponError(isReveal: true)
                                 } else {
                                     if let price1 = dataInfo["price"] {
                                         self.discountAmount = String(format: "%@", price1 as! CVarArg)
@@ -1015,6 +1013,8 @@ class KMPurchaseEmbeddedWindowController: NSWindowController {
                 self.priceRefresh()
             } else {
                 self.couponClearButton.isHidden = false
+                self.coupomErrorLabel.stringValue = NSLocalizedString("The coupon code is invalid. Please try again.", comment: "")
+                self.refreshCouponError(isReveal: true)
             }
         }
     }
@@ -1026,6 +1026,9 @@ class KMPurchaseEmbeddedWindowController: NSWindowController {
     @IBAction func couponClearButton(_ sender: NSButton) {
         coupomTextField.stringValue = ""
         couponClearButton.isHidden = true
+        applyButton.isEnabled = true
+        coupomTextField.isEnabled = true
+        refreshCouponError(isReveal: false)
     }
         
     @IBAction func buyAction(_ sender: NSButton) {
@@ -1047,7 +1050,7 @@ class KMPurchaseEmbeddedWindowController: NSWindowController {
             if !isNumeric(paddleTextField.stringValue) && paddleTextField.stringValue != "" {
                 let alert = NSAlert()
                 alert.alertStyle = .critical
-                alert.messageText = NSLocalizedString("Connection Error", comment: "")
+                alert.messageText = NSLocalizedString("Error Information", comment: "")
                 alert.informativeText = NSLocalizedString("Please enter the correct postcode.", comment: "")
                 alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
                 if alert.responds(to: #selector(alert.beginSheetModal(for:completionHandler:))) {
@@ -1089,7 +1092,11 @@ class KMPurchaseEmbeddedWindowController: NSWindowController {
                 licenseCode = license
             }
         }
-        self._buyProduct(productID, count: pdfCount, discountId: coupomTextField.stringValue, payment: paymentMethod, license: licenseCode, email: emailTextField.stringValue) { [weak self] info, err in
+        var couponCode = ""
+        if coupomErrorLabel.isHidden {
+            couponCode = coupomTextField.stringValue
+        }
+        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 {return}
             if let dataInfo = info {
@@ -1645,6 +1652,11 @@ class KMPurchaseEmbeddedWindowController: NSWindowController {
         image2BottomLayout.constant = licenseCodeView.isHidden ? 20.0 : 8.0
     }
     
+    private func refreshCouponError(isReveal: Bool) {
+        coupomErrorLabel.isHidden = !isReveal
+        coupomErrorTopLayout.constant = coupomErrorLabel.isHidden ? -CGRectGetHeight(coupomErrorLabel.bounds) : 8.0
+    }
+    
     // MARK: Show Methods
     
 //    @IBAction private func dismissSheet(_ sender: NSButton) {

+ 17 - 4
PDF Office/PDF Master/Class/Purchase/DMG/KMPurchaseEmbeddedWindowController.xib

@@ -28,6 +28,8 @@
                 <outlet property="billInformationLabel" destination="Plj-ou-oBV" id="O7I-qB-NrY"/>
                 <outlet property="cardButton" destination="6fV-kv-Kqb" id="72P-Yg-Dcy"/>
                 <outlet property="cardView" destination="X7Z-7L-aZu" id="Wcx-bf-H1g"/>
+                <outlet property="coupomErrorLabel" destination="yok-Ur-JfA" id="aXv-KQ-aWN"/>
+                <outlet property="coupomErrorTopLayout" destination="TGw-N6-2Ro" id="v4r-Gp-S14"/>
                 <outlet property="coupomTextField" destination="lA7-Px-ulG" id="Tbk-LM-yvS"/>
                 <outlet property="couponBox" destination="ECb-Ab-7Fq" id="h7u-Fk-i1A"/>
                 <outlet property="couponButton" destination="16N-22-eUq" id="ufx-sv-rRp"/>
@@ -995,7 +997,7 @@
                                             </constraints>
                                         </customView>
                                         <customView translatesAutoresizingMaskIntoConstraints="NO" id="2Hk-KG-FSr">
-                                            <rect key="frame" x="0.0" y="0.0" width="538" height="128"/>
+                                            <rect key="frame" x="0.0" y="0.0" width="538" height="144"/>
                                             <subviews>
                                                 <customView translatesAutoresizingMaskIntoConstraints="NO" id="Imv-vq-rsc">
                                                     <rect key="frame" x="0.0" y="72" width="538" height="20"/>
@@ -1082,7 +1084,7 @@
                                                     </constraints>
                                                 </customView>
                                                 <customView translatesAutoresizingMaskIntoConstraints="NO" id="o5K-ag-Vbf">
-                                                    <rect key="frame" x="0.0" y="100" width="538" height="28"/>
+                                                    <rect key="frame" x="0.0" y="116" width="538" height="28"/>
                                                     <subviews>
                                                         <button translatesAutoresizingMaskIntoConstraints="NO" id="16N-22-eUq">
                                                             <rect key="frame" x="0.0" y="6" width="45" height="16"/>
@@ -1176,16 +1178,27 @@
                                                         <constraint firstItem="KlW-6M-F8A" firstAttribute="top" secondItem="o5K-ag-Vbf" secondAttribute="top" id="syK-Nu-TdQ"/>
                                                     </constraints>
                                                 </customView>
+                                                <textField focusRingType="none" horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="yok-Ur-JfA">
+                                                    <rect key="frame" x="-2" y="100" width="542" height="16"/>
+                                                    <textFieldCell key="cell" title="Label" id="hyR-5a-ztf">
+                                                        <font key="font" usesAppearanceFont="YES"/>
+                                                        <color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
+                                                        <color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
+                                                    </textFieldCell>
+                                                </textField>
                                             </subviews>
                                             <constraints>
                                                 <constraint firstItem="KXb-Fc-Ihu" firstAttribute="leading" secondItem="2Hk-KG-FSr" secondAttribute="leading" id="2ND-n1-fYL"/>
+                                                <constraint firstItem="o5K-ag-Vbf" firstAttribute="top" secondItem="2Hk-KG-FSr" secondAttribute="top" id="5PF-A5-0wZ"/>
                                                 <constraint firstItem="o5K-ag-Vbf" firstAttribute="leading" secondItem="2Hk-KG-FSr" secondAttribute="leading" id="5wl-Gd-dUP"/>
-                                                <constraint firstItem="Imv-vq-rsc" firstAttribute="top" secondItem="o5K-ag-Vbf" secondAttribute="bottom" constant="8" id="HxS-Ie-ICF"/>
+                                                <constraint firstItem="Imv-vq-rsc" firstAttribute="top" secondItem="yok-Ur-JfA" secondAttribute="bottom" constant="8" id="6rn-4s-kCc"/>
+                                                <constraint firstAttribute="trailing" secondItem="yok-Ur-JfA" secondAttribute="trailing" id="JkG-PB-kLe"/>
+                                                <constraint firstItem="yok-Ur-JfA" firstAttribute="leading" secondItem="2Hk-KG-FSr" secondAttribute="leading" id="QH4-ve-KJF"/>
                                                 <constraint firstAttribute="trailing" secondItem="ZY4-l7-Ejn" secondAttribute="trailing" id="RMg-ar-mLp"/>
+                                                <constraint firstItem="yok-Ur-JfA" firstAttribute="top" secondItem="o5K-ag-Vbf" secondAttribute="bottom" id="TGw-N6-2Ro"/>
                                                 <constraint firstItem="Imv-vq-rsc" firstAttribute="leading" secondItem="2Hk-KG-FSr" secondAttribute="leading" id="Y0H-ih-Rgw"/>
                                                 <constraint firstItem="ZY4-l7-Ejn" firstAttribute="top" secondItem="KXb-Fc-Ihu" secondAttribute="bottom" constant="16" id="c55-3v-q3H"/>
                                                 <constraint firstAttribute="trailing" secondItem="KXb-Fc-Ihu" secondAttribute="trailing" id="iGT-gn-pAk"/>
-                                                <constraint firstAttribute="height" constant="128" id="j6Q-TK-M7b"/>
                                                 <constraint firstItem="KXb-Fc-Ihu" firstAttribute="top" secondItem="Imv-vq-rsc" secondAttribute="bottom" constant="8" id="oTP-4f-B8x"/>
                                                 <constraint firstAttribute="bottom" secondItem="ZY4-l7-Ejn" secondAttribute="bottom" id="seY-YL-vDW"/>
                                                 <constraint firstAttribute="trailing" secondItem="o5K-ag-Vbf" secondAttribute="trailing" id="thc-FV-DHJ"/>