Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request #31 from jinSasaki/fix-transaction
Browse files Browse the repository at this point in the history
Fixed handling for transaction
  • Loading branch information
jinSasaki authored Oct 16, 2018
2 parents 0a72de5 + 078a3bd commit 3d5243c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 17 deletions.
8 changes: 3 additions & 5 deletions Sources/InAppPurchase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,13 @@ extension InAppPurchase: InAppPurchaseProvidable {
}

public func set(shouldAddStorePaymentHandler: ((_ product: Product) -> Bool)? = nil, handler: InAppPurchase.PurchaseHandler?) {
paymentProvider.set(shouldAddStorePaymentHandler: { [weak self] (queue, payment, product) -> Bool in
paymentProvider.set(shouldAddStorePaymentHandler: { [weak self] (_, payment, product) -> Bool in
let shouldAddStorePayment = shouldAddStorePaymentHandler?(Internal.Product(product)) ?? false
if shouldAddStorePayment {
self?.paymentProvider.addPaymentHandler(withProductIdentifier: payment.productIdentifier, handler: { (queue, result) in
self?.paymentProvider.addPaymentHandler(withProductIdentifier: payment.productIdentifier, handler: { (_, result) in
switch result {
case .success(let transaction):
InAppPurchase.handle(
queue: queue,
transaction: transaction,
handler: handler
)
Expand Down Expand Up @@ -127,11 +126,10 @@ extension InAppPurchase: InAppPurchaseProvidable {

// Add payment to App Store queue
let payment = SKPayment(product: product)
self?.paymentProvider.add(payment: payment, handler: { (queue, result) in
self?.paymentProvider.add(payment: payment, handler: { (_, result) in
switch result {
case .success(let transaction):
InAppPurchase.handle(
queue: queue,
transaction: transaction,
handler: handler
)
Expand Down
4 changes: 2 additions & 2 deletions Sources/Internal/Internal+InAppPurchase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ extension InAppPurchase {
let fallbackHandler: PaymentHandler = { (_, result) in
switch result {
case .success(let transaction):
handler?(.success(.purchased(transaction: Internal.PaymentTransaction(transaction))))
handle(transaction: transaction, handler: handler)
case .failure(let error):
handler?(.failure(error))
}
}
return fallbackHandler
}

internal static func handle(queue: SKPaymentQueue, transaction: SKPaymentTransaction, handler: InAppPurchase.PurchaseHandler?) {
internal static func handle(transaction: SKPaymentTransaction, handler: InAppPurchase.PurchaseHandler?) {
switch transaction.transactionState {
case .purchasing:
// Do nothing
Expand Down
15 changes: 5 additions & 10 deletions Tests/InAppPurchaseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -456,21 +456,19 @@ class InAppPurchaseTests: XCTestCase {
}

func testHandleWherePurchasing() {
let queue = StubPaymentQueue()
let payment = StubPayment(productIdentifier: "PRODUCT_001")
let transaction = StubPaymentTransaction(
transactionIdentifier: "TRANSACTION_001",
transactionState: .purchasing,
payment: payment
)

InAppPurchase.handle(queue: queue, transaction: transaction, handler: { _ in
InAppPurchase.handle(transaction: transaction, handler: { _ in
XCTFail()
})
}

func testHandleWherePurchased() {
let queue = StubPaymentQueue()
let payment = StubPayment(productIdentifier: "PRODUCT_001")
let originalTransaction = StubPaymentTransaction(
transactionIdentifier: "ORIGINAL_TRANSACTION_001",
Expand All @@ -485,7 +483,7 @@ class InAppPurchaseTests: XCTestCase {
)

let expectation = self.expectation()
InAppPurchase.handle(queue: queue, transaction: transaction, handler: { result in
InAppPurchase.handle(transaction: transaction, handler: { result in
switch result {
case .success(let state):
if case let .purchased(transaction) = state {
Expand All @@ -503,7 +501,6 @@ class InAppPurchaseTests: XCTestCase {
}

func testHandleWhereRestored() {
let queue = StubPaymentQueue()
let payment = StubPayment(productIdentifier: "PRODUCT_001")
let transaction = StubPaymentTransaction(
transactionIdentifier: "TRANSACTION_001",
Expand All @@ -512,7 +509,7 @@ class InAppPurchaseTests: XCTestCase {
)

let expectation = self.expectation()
InAppPurchase.handle(queue: queue, transaction: transaction, handler: { result in
InAppPurchase.handle(transaction: transaction, handler: { result in
switch result {
case .success(let state):
XCTAssertEqual(state, .restored)
Expand All @@ -525,7 +522,6 @@ class InAppPurchaseTests: XCTestCase {
}

func testHandleWhereDeferred() {
let queue = StubPaymentQueue()
let payment = StubPayment(productIdentifier: "PRODUCT_001")
let transaction = StubPaymentTransaction(
transactionIdentifier: "TRANSACTION_001",
Expand All @@ -534,7 +530,7 @@ class InAppPurchaseTests: XCTestCase {
)

let expectation1 = self.expectation()
InAppPurchase.handle(queue: queue, transaction: transaction, handler: { result in
InAppPurchase.handle(transaction: transaction, handler: { result in
switch result {
case .success(let state):
XCTAssertEqual(state, .deferred)
Expand All @@ -547,7 +543,6 @@ class InAppPurchaseTests: XCTestCase {
}

func testHandleWhereFailed() {
let queue = StubPaymentQueue()

let error = NSError(domain: "test", code: 500, userInfo: nil)
let payment = StubPayment(productIdentifier: "PRODUCT_001")
Expand All @@ -559,7 +554,7 @@ class InAppPurchaseTests: XCTestCase {
)

let expectation = self.expectation()
InAppPurchase.handle(queue: queue, transaction: transaction, handler: { result in
InAppPurchase.handle(transaction: transaction, handler: { result in
switch result {
case .success:
XCTFail()
Expand Down

0 comments on commit 3d5243c

Please sign in to comment.