Skip to content

Commit

Permalink
Merge pull request #349 from bitjeep/develop
Browse files Browse the repository at this point in the history
Add a wrapper for simulatesAskToBuyInSandbox
  • Loading branch information
bizz84 authored Mar 4, 2018
2 parents 75c2024 + 7f0cbe3 commit 1a470a4
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 11 deletions.
7 changes: 7 additions & 0 deletions SwiftyStoreKit/PaymentQueueController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions SwiftyStoreKit/PaymentsController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 8 additions & 8 deletions SwiftyStoreKit/SwiftyStoreKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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))
})
Expand Down Expand Up @@ -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)
}

/**
Expand All @@ -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)
}

/**
Expand Down
5 changes: 3 additions & 2 deletions SwiftyStoreKitTests/PaymentQueueControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion SwiftyStoreKitTests/PaymentsControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 1a470a4

Please sign in to comment.