Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should add store payment iOS 11 support #257

Merged
merged 3 commits into from
Aug 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions SwiftyStoreKit/PaymentQueueController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ class PaymentQueueController: NSObject, SKPaymentTransactionObserver {
}
paymentQueue.finishTransaction(skTransaction)
}

var shouldAddStorePaymentHandler: ShouldAddStorePaymentHandler?

// MARK: SKPaymentTransactionObserver
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
Expand Down Expand Up @@ -202,4 +204,8 @@ class PaymentQueueController: NSObject, SKPaymentTransactionObserver {

}

func paymentQueue(_ queue: SKPaymentQueue, shouldAddStorePayment payment: SKPayment, for product: SKProduct) -> Bool {

return shouldAddStorePaymentHandler?(payment, product) ?? false
}
}
2 changes: 2 additions & 0 deletions SwiftyStoreKit/SwiftyStoreKit+Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion SwiftyStoreKit/SwiftyStoreKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class SwiftyStoreKit {

private let productsInfoController: ProductsInfoController

private let paymentQueueController: PaymentQueueController
fileprivate let paymentQueueController: PaymentQueueController

fileprivate let receiptVerificator: InAppReceiptVerificator

Expand Down Expand Up @@ -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 {
Expand Down
38 changes: 38 additions & 0 deletions SwiftyStoreKitTests/PaymentQueueControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down