Explorar o código

added optional SKPaymentDiscount to be applied

Jan Remes %!s(int64=6) %!d(string=hai) anos
pai
achega
72bd918129

+ 11 - 0
SwiftyStoreKit/PaymentQueueController.swift

@@ -123,6 +123,17 @@ class PaymentQueueController: NSObject, SKPaymentTransactionObserver {
         skPayment.applicationUsername = payment.applicationUsername
         skPayment.quantity = payment.quantity
         
+        if #available(iOS 12.2, tvOS 12.2, *) {
+            
+            if let obj = payment.discount, !(obj is SKPaymentDiscount) {
+                assert(false, "Unrecognized type provided for payment discount. Only SKPaymentDiscount is supported")
+            }
+            
+            if let discount = payment.discount as? SKPaymentDiscount {
+                skPayment.paymentDiscount = discount
+            }
+        }
+        
 #if os(iOS) || os(tvOS)
         if #available(iOS 8.3, tvOS 9.0, *) {
             skPayment.simulatesAskToBuyInSandbox = payment.simulatesAskToBuyInSandbox

+ 3 - 0
SwiftyStoreKit/PaymentsController.swift

@@ -27,6 +27,9 @@ import StoreKit
 
 struct Payment: Hashable {
     let product: SKProduct
+    
+    /// SKPaymentDiscount
+    let discount: NSObject?
     let quantity: Int
     let atomically: Bool
     let applicationUsername: String

+ 17 - 2
SwiftyStoreKit/SwiftyStoreKit.swift

@@ -64,8 +64,8 @@ public class SwiftyStoreKit {
         }
     }
 
-    fileprivate func purchase(product: SKProduct, quantity: Int, atomically: Bool, applicationUsername: String = "", simulatesAskToBuyInSandbox: Bool = false, completion: @escaping (PurchaseResult) -> Void) {
-        paymentQueueController.startPayment(Payment(product: product, quantity: quantity, atomically: atomically, applicationUsername: applicationUsername, simulatesAskToBuyInSandbox: simulatesAskToBuyInSandbox) { result in
+    fileprivate func purchase(product: SKProduct, quantity: Int, atomically: Bool, applicationUsername: String = "", simulatesAskToBuyInSandbox: Bool = false, discount: NSObject? = nil, completion: @escaping (PurchaseResult) -> Void) {
+        paymentQueueController.startPayment(Payment(product: product, discount: discount, quantity: quantity, atomically: atomically, applicationUsername: applicationUsername, simulatesAskToBuyInSandbox: simulatesAskToBuyInSandbox) { result in
             
             completion(self.processPurchaseResult(result))
         })
@@ -173,6 +173,21 @@ extension SwiftyStoreKit {
         
         sharedInstance.purchase(product: product, quantity: quantity, atomically: atomically, applicationUsername: applicationUsername, simulatesAskToBuyInSandbox: simulatesAskToBuyInSandbox, completion: completion)
     }
+    
+    /**
+     *  Purchase a product with discount
+     *  - Parameter product: product to be purchased
+     *  - Parameter product: optional discount to be applied
+     *  - Parameter quantity: quantity of the product to be purchased
+     *  - Parameter atomically: whether the product is purchased atomically (e.g. finishTransaction is called immediately)
+     *  - Parameter applicationUsername: an opaque identifier for the user’s account on your system
+     *  - Parameter completion: handler for result
+     */
+    @available(iOS 12.2, tvOS 12.2, *)
+    public class func purchaseProductWithDiscount(_ product: SKProduct, discount: SKPaymentDiscount?, quantity: Int = 1, atomically: Bool = true, applicationUsername: String = "", simulatesAskToBuyInSandbox: Bool = false, completion: @escaping ( PurchaseResult) -> Void) {
+        
+        sharedInstance.purchase(product: product, quantity: quantity, atomically: atomically, applicationUsername: applicationUsername, simulatesAskToBuyInSandbox: simulatesAskToBuyInSandbox, discount: discount, completion: completion)
+    }
 
     /**
      *  Restore purchases