2
0
Эх сурвалжийг харах

added wrapper for payment discount

Jan Remes 6 жил өмнө
parent
commit
e33f21767d

+ 1 - 5
SwiftyStoreKit/PaymentQueueController.swift

@@ -125,11 +125,7 @@ class PaymentQueueController: NSObject, SKPaymentTransactionObserver {
         
         
         if #available(iOS 12.2, tvOS 12.2, *) {
         if #available(iOS 12.2, tvOS 12.2, *) {
             
             
-            if let obj = payment.paymentDiscount, !(obj is SKPaymentDiscount) {
-                assert(false, "Unrecognized type provided for payment discount. Only SKPaymentDiscount is supported")
-            }
-            
-            if let discount = payment.paymentDiscount as? SKPaymentDiscount {
+            if let discount = payment.paymentDiscount?.discount as? SKPaymentDiscount {
                 skPayment.paymentDiscount = discount
                 skPayment.paymentDiscount = discount
             }
             }
         }
         }

+ 10 - 2
SwiftyStoreKit/PaymentsController.swift

@@ -28,8 +28,7 @@ import StoreKit
 struct Payment: Hashable {
 struct Payment: Hashable {
     let product: SKProduct
     let product: SKProduct
     
     
-    /// SKPaymentDiscount
-    let paymentDiscount: AnyObject?
+    let paymentDiscount: PaymentDiscount?
     let quantity: Int
     let quantity: Int
     let atomically: Bool
     let atomically: Bool
     let applicationUsername: String
     let applicationUsername: String
@@ -49,6 +48,15 @@ struct Payment: Hashable {
     }
     }
 }
 }
 
 
+public struct PaymentDiscount {
+    let discount: AnyObject
+    
+    @available(iOS 12.2, *)
+    init(discount: SKPaymentDiscount) {
+        self.discount = discount
+    }
+}
+
 class PaymentsController: TransactionController {
 class PaymentsController: TransactionController {
 
 
     private var payments: [Payment] = []
     private var payments: [Payment] = []

+ 2 - 2
SwiftyStoreKit/SwiftyStoreKit.swift

@@ -64,7 +64,7 @@ public class SwiftyStoreKit {
         }
         }
     }
     }
 
 
-    fileprivate func purchase(product: SKProduct, quantity: Int, atomically: Bool, applicationUsername: String = "", simulatesAskToBuyInSandbox: Bool = false, paymentDiscount: AnyObject? = nil, completion: @escaping (PurchaseResult) -> Void) {
+    fileprivate func purchase(product: SKProduct, quantity: Int, atomically: Bool, applicationUsername: String = "", simulatesAskToBuyInSandbox: Bool = false, paymentDiscount: PaymentDiscount? = nil, completion: @escaping (PurchaseResult) -> Void) {
         paymentQueueController.startPayment(Payment(product: product, paymentDiscount: paymentDiscount, quantity: quantity, atomically: atomically, applicationUsername: applicationUsername, simulatesAskToBuyInSandbox: simulatesAskToBuyInSandbox) { result in
         paymentQueueController.startPayment(Payment(product: product, paymentDiscount: paymentDiscount, quantity: quantity, atomically: atomically, applicationUsername: applicationUsername, simulatesAskToBuyInSandbox: simulatesAskToBuyInSandbox) { result in
             
             
             completion(self.processPurchaseResult(result))
             completion(self.processPurchaseResult(result))
@@ -170,7 +170,7 @@ extension SwiftyStoreKit {
      *  - Parameter product: optional discount to be applied. Must be SKProductPayment type
      *  - Parameter product: optional discount to be applied. Must be SKProductPayment type
      *  - Parameter completion: handler for result
      *  - Parameter completion: handler for result
      */
      */
-    public class func purchaseProduct(_ product: SKProduct, quantity: Int = 1, atomically: Bool = true, applicationUsername: String = "", simulatesAskToBuyInSandbox: Bool = false, paymentDiscount: AnyObject? = nil, completion: @escaping ( PurchaseResult) -> Void) {
+    public class func purchaseProduct(_ product: SKProduct, quantity: Int = 1, atomically: Bool = true, applicationUsername: String = "", simulatesAskToBuyInSandbox: Bool = false, paymentDiscount: PaymentDiscount? = nil, completion: @escaping ( PurchaseResult) -> Void) {
         
         
         sharedInstance.purchase(product: product, quantity: quantity, atomically: atomically, applicationUsername: applicationUsername, simulatesAskToBuyInSandbox: simulatesAskToBuyInSandbox, paymentDiscount: paymentDiscount, completion: completion)
         sharedInstance.purchase(product: product, quantity: quantity, atomically: atomically, applicationUsername: applicationUsername, simulatesAskToBuyInSandbox: simulatesAskToBuyInSandbox, paymentDiscount: paymentDiscount, completion: completion)
     }
     }