Skip to content

Commit

Permalink
Merge pull request #257 from bizz84/feature/should-add-store-payment
Browse files Browse the repository at this point in the history
Should add store payment iOS 11 support
  • Loading branch information
bizz84 authored Aug 21, 2017
2 parents 821d863 + ffd6825 commit 3a8dc2f
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 1 deletion.
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

0 comments on commit 3a8dc2f

Please sign in to comment.