diff --git a/CHANGELOG.md b/CHANGELOG.md index ff245b5c..c477b62b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to this project will be documented in this file. +## 0.10.6 Add support for shouldAddStorePayment + +* Add support for the new SKPaymentTransactionObserver.shouldAddStorePayment method in iOS 11 + ## 0.10.5 Filter out transactions in purchasing state * Filter out all transactions with state == .purchasing early in purchase flows (related to [#169](https://github.com/bizz84/SwiftyStoreKit/issues/169), [#188](https://github.com/bizz84/SwiftyStoreKit/pull/188), [#179](https://github.com/bizz84/SwiftyStoreKit/issues/179)) * Sample app: print localized description when a purchase fails with `.unknown` error diff --git a/SwiftyStoreKit/PaymentQueueController.swift b/SwiftyStoreKit/PaymentQueueController.swift index 54220d8c..3cb3d91e 100644 --- a/SwiftyStoreKit/PaymentQueueController.swift +++ b/SwiftyStoreKit/PaymentQueueController.swift @@ -142,6 +142,8 @@ class PaymentQueueController: NSObject, SKPaymentTransactionObserver { } paymentQueue.finishTransaction(skTransaction) } + + var shouldAddStorePaymentHandler: ShouldAddStorePaymentHandler? // MARK: SKPaymentTransactionObserver func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) { @@ -202,4 +204,8 @@ class PaymentQueueController: NSObject, SKPaymentTransactionObserver { } + func paymentQueue(_ queue: SKPaymentQueue, shouldAddStorePayment payment: SKPayment, for product: SKProduct) -> Bool { + + return shouldAddStorePaymentHandler?(payment, product) ?? false + } } diff --git a/SwiftyStoreKit/SwiftyStoreKit+Types.swift b/SwiftyStoreKit/SwiftyStoreKit+Types.swift index b41d6160..f6d78e0d 100644 --- a/SwiftyStoreKit/SwiftyStoreKit+Types.swift +++ b/SwiftyStoreKit/SwiftyStoreKit+Types.swift @@ -76,6 +76,8 @@ public struct RestoreResults { public let restoreFailedPurchases: [(SKError, String?)] } +public typealias ShouldAddStorePaymentHandler = (_ payment: SKPayment, _ product: SKProduct) -> Bool + // MARK: Receipt verification // Info for receipt returned by server diff --git a/SwiftyStoreKit/SwiftyStoreKit.swift b/SwiftyStoreKit/SwiftyStoreKit.swift index 2a2800bd..f3104757 100644 --- a/SwiftyStoreKit/SwiftyStoreKit.swift +++ b/SwiftyStoreKit/SwiftyStoreKit.swift @@ -28,7 +28,7 @@ public class SwiftyStoreKit { private let productsInfoController: ProductsInfoController - private let paymentQueueController: PaymentQueueController + fileprivate let paymentQueueController: PaymentQueueController fileprivate let receiptVerificator: InAppReceiptVerificator @@ -204,6 +204,15 @@ extension SwiftyStoreKit { sharedInstance.finishTransaction(transaction) } + + /** + * Register a handler for SKPaymentQueue.shouldAddStorePayment delegate method in iOS 11 + */ + public static var shouldAddStorePaymentHandler: ShouldAddStorePaymentHandler? { + didSet { + sharedInstance.paymentQueueController.shouldAddStorePaymentHandler = shouldAddStorePaymentHandler + } + } } extension SwiftyStoreKit { diff --git a/SwiftyStoreKitTests/PaymentQueueControllerTests.swift b/SwiftyStoreKitTests/PaymentQueueControllerTests.swift index 8cd43197..93faa31a 100644 --- a/SwiftyStoreKitTests/PaymentQueueControllerTests.swift +++ b/SwiftyStoreKitTests/PaymentQueueControllerTests.swift @@ -249,6 +249,44 @@ class PaymentQueueControllerTests: XCTestCase { XCTAssertTrue(restorePurchasesCallbackCalled) XCTAssertTrue(completeTransactionsCallbackCalled) } + + // MARK: shouldAddStorePayment tests + func testPaymentQueue_when_shouldAddStorePaymentHandlerIsNil_then_shouldAddStorePaymentReturnsFalse() { + + let spy = PaymentQueueSpy() + + let paymentQueueController = PaymentQueueController(paymentQueue: spy) + + paymentQueueController.shouldAddStorePaymentHandler = nil + + XCTAssertFalse(paymentQueueController.paymentQueue(SKPaymentQueue(), shouldAddStorePayment: SKPayment(), for: SKProduct())) + } + + func testPaymentQueue_when_shouldAddStorePaymentHandlerReturnsTrue_then_shouldAddStorePaymentReturnsTrue() { + + let spy = PaymentQueueSpy() + + let paymentQueueController = PaymentQueueController(paymentQueue: spy) + + paymentQueueController.shouldAddStorePaymentHandler = { payment, product in + return true + } + + XCTAssertTrue(paymentQueueController.paymentQueue(SKPaymentQueue(), shouldAddStorePayment: SKPayment(), for: SKProduct())) + } + + func testPaymentQueue_when_shouldAddStorePaymentHandlerReturnsFalse_then_shouldAddStorePaymentReturnsFalse() { + + let spy = PaymentQueueSpy() + + let paymentQueueController = PaymentQueueController(paymentQueue: spy) + + paymentQueueController.shouldAddStorePaymentHandler = { payment, product in + return false + } + + XCTAssertFalse(paymentQueueController.paymentQueue(SKPaymentQueue(), shouldAddStorePayment: SKPayment(), for: SKProduct())) + } // MARK: Helpers func makeTestPaymentTransaction(productIdentifier: String, transactionState: SKPaymentTransactionState) -> TestPaymentTransaction {