diff --git a/SwiftyStoreKit/PaymentQueueController.swift b/SwiftyStoreKit/PaymentQueueController.swift index 0ac196e2..5fb47d6b 100644 --- a/SwiftyStoreKit/PaymentQueueController.swift +++ b/SwiftyStoreKit/PaymentQueueController.swift @@ -121,6 +121,13 @@ class PaymentQueueController: NSObject, SKPaymentTransactionObserver { let skPayment = SKMutablePayment(product: payment.product) skPayment.applicationUsername = payment.applicationUsername skPayment.quantity = payment.quantity + +#if os(iOS) || os(tvOS) + if #available(iOS 8.3, tvOS 9.0, *) { + skPayment.simulatesAskToBuyInSandbox = payment.simulatesAskToBuyInSandbox + } +#endif + paymentQueue.add(skPayment) paymentsController.append(payment) diff --git a/SwiftyStoreKit/PaymentsController.swift b/SwiftyStoreKit/PaymentsController.swift index a9916e0e..1fad4eed 100644 --- a/SwiftyStoreKit/PaymentsController.swift +++ b/SwiftyStoreKit/PaymentsController.swift @@ -30,6 +30,7 @@ struct Payment: Hashable { let quantity: Int let atomically: Bool let applicationUsername: String + let simulatesAskToBuyInSandbox: Bool let callback: (TransactionResult) -> Void var hashValue: Int { diff --git a/SwiftyStoreKit/SwiftyStoreKit.swift b/SwiftyStoreKit/SwiftyStoreKit.swift index 70d3442d..481fe063 100644 --- a/SwiftyStoreKit/SwiftyStoreKit.swift +++ b/SwiftyStoreKit/SwiftyStoreKit.swift @@ -46,11 +46,11 @@ public class SwiftyStoreKit { return productsInfoController.retrieveProductsInfo(productIds, completion: completion) } - fileprivate func purchaseProduct(_ productId: String, quantity: Int = 1, atomically: Bool = true, applicationUsername: String = "", completion: @escaping ( PurchaseResult) -> Void) { + fileprivate func purchaseProduct(_ productId: String, quantity: Int = 1, atomically: Bool = true, applicationUsername: String = "", simulatesAskToBuyInSandbox: Bool = false, completion: @escaping ( PurchaseResult) -> Void) { retrieveProductsInfo(Set([productId])) { result -> Void in if let product = result.retrievedProducts.first { - self.purchase(product: product, quantity: quantity, atomically: atomically, applicationUsername: applicationUsername, completion: completion) + self.purchase(product: product, quantity: quantity, atomically: atomically, applicationUsername: applicationUsername, simulatesAskToBuyInSandbox: simulatesAskToBuyInSandbox, completion: completion) } else if let error = result.error { completion(.error(error: SKError(_nsError: error as NSError))) } else if let invalidProductId = result.invalidProductIDs.first { @@ -61,14 +61,14 @@ public class SwiftyStoreKit { } } - fileprivate func purchase(product: SKProduct, quantity: Int, atomically: Bool, applicationUsername: String = "", completion: @escaping (PurchaseResult) -> Void) { + fileprivate func purchase(product: SKProduct, quantity: Int, atomically: Bool, applicationUsername: String = "", simulatesAskToBuyInSandbox: Bool = false, completion: @escaping (PurchaseResult) -> Void) { guard SwiftyStoreKit.canMakePayments else { let error = NSError(domain: SKErrorDomain, code: SKError.paymentNotAllowed.rawValue, userInfo: nil) completion(.error(error: SKError(_nsError: error))) return } - paymentQueueController.startPayment(Payment(product: product, quantity: quantity, atomically: atomically, applicationUsername: applicationUsername) { result in + paymentQueueController.startPayment(Payment(product: product, quantity: quantity, atomically: atomically, applicationUsername: applicationUsername, simulatesAskToBuyInSandbox: simulatesAskToBuyInSandbox) { result in completion(self.processPurchaseResult(result)) }) @@ -159,9 +159,9 @@ extension SwiftyStoreKit { * - Parameter applicationUsername: an opaque identifier for the user’s account on your system * - Parameter completion: handler for result */ - public class func purchaseProduct(_ productId: String, quantity: Int = 1, atomically: Bool = true, applicationUsername: String = "", completion: @escaping (PurchaseResult) -> Void) { + public class func purchaseProduct(_ productId: String, quantity: Int = 1, atomically: Bool = true, applicationUsername: String = "", simulatesAskToBuyInSandbox: Bool = false, completion: @escaping (PurchaseResult) -> Void) { - sharedInstance.purchaseProduct(productId, quantity: quantity, atomically: atomically, applicationUsername: applicationUsername, completion: completion) + sharedInstance.purchaseProduct(productId, quantity: quantity, atomically: atomically, applicationUsername: applicationUsername, simulatesAskToBuyInSandbox: simulatesAskToBuyInSandbox, completion: completion) } /** @@ -172,9 +172,9 @@ extension SwiftyStoreKit { * - Parameter applicationUsername: an opaque identifier for the user’s account on your system * - Parameter completion: handler for result */ - public class func purchaseProduct(_ product: SKProduct, quantity: Int = 1, atomically: Bool = true, applicationUsername: String = "", completion: @escaping ( PurchaseResult) -> Void) { + public class func purchaseProduct(_ product: SKProduct, 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, completion: completion) + sharedInstance.purchase(product: product, quantity: quantity, atomically: atomically, applicationUsername: applicationUsername, simulatesAskToBuyInSandbox: simulatesAskToBuyInSandbox, completion: completion) } /** diff --git a/SwiftyStoreKitTests/PaymentQueueControllerTests.swift b/SwiftyStoreKitTests/PaymentQueueControllerTests.swift index aa6c4f57..b3e9095a 100644 --- a/SwiftyStoreKitTests/PaymentQueueControllerTests.swift +++ b/SwiftyStoreKitTests/PaymentQueueControllerTests.swift @@ -29,11 +29,12 @@ import StoreKit @testable import SwiftyStoreKit extension Payment { - init(product: SKProduct, quantity: Int, atomically: Bool, applicationUsername: String, callback: @escaping (TransactionResult) -> Void) { + init(product: SKProduct, quantity: Int, atomically: Bool, applicationUsername: String, simulatesAskToBuyInSandbox: Bool, callback: @escaping (TransactionResult) -> Void) { self.product = product self.quantity = quantity self.atomically = atomically self.applicationUsername = applicationUsername + self.simulatesAskToBuyInSandbox = simulatesAskToBuyInSandbox self.callback = callback } } @@ -300,6 +301,6 @@ class PaymentQueueControllerTests: XCTestCase { func makeTestPayment(productIdentifier: String, quantity: Int = 1, atomically: Bool = true, callback: @escaping (TransactionResult) -> Void) -> Payment { let testProduct = TestProduct(productIdentifier: productIdentifier) - return Payment(product: testProduct, quantity: quantity, atomically: atomically, applicationUsername: "", callback: callback) + return Payment(product: testProduct, quantity: quantity, atomically: atomically, applicationUsername: "", simulatesAskToBuyInSandbox: false, callback: callback) } } diff --git a/SwiftyStoreKitTests/PaymentsControllerTests.swift b/SwiftyStoreKitTests/PaymentsControllerTests.swift index d6e5bab3..36fe3dd3 100644 --- a/SwiftyStoreKitTests/PaymentsControllerTests.swift +++ b/SwiftyStoreKitTests/PaymentsControllerTests.swift @@ -240,7 +240,7 @@ class PaymentsControllerTests: XCTestCase { func makeTestPayment(product: SKProduct, atomically: Bool = true, callback: @escaping (TransactionResult) -> Void) -> Payment { - return Payment(product: product, quantity: 1, atomically: atomically, applicationUsername: "", callback: callback) + return Payment(product: product, quantity: 1, atomically: atomically, applicationUsername: "", simulatesAskToBuyInSandbox: false, callback: callback) } func makeTestPayment(productIdentifier: String, atomically: Bool = true, callback: @escaping (TransactionResult) -> Void) -> Payment {