From e8b6627c80827b025cc3187d720f62e167d53bcf Mon Sep 17 00:00:00 2001 From: Leszek Kaczor Date: Thu, 16 Jul 2015 15:53:06 +0200 Subject: [PATCH 1/2] Fixing restore purchases callbacks --- Classes/SCPStoreKitManager.m | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/Classes/SCPStoreKitManager.m b/Classes/SCPStoreKitManager.m index 12c7085..4b5e0e4 100755 --- a/Classes/SCPStoreKitManager.m +++ b/Classes/SCPStoreKitManager.m @@ -128,6 +128,16 @@ - (void)restorePurchasesPaymentTransactionStateRestored:(PaymentTransactionState #pragma mark - SKPaymentTransactionObserver methods +- (void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error { + if (self.paymentTransactionStateFailedBlock) + self.paymentTransactionStateFailedBlock(error); +} + +- (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue { + if (self.paymentTransactionStateRestoredBlock) + self.paymentTransactionStateRestoredBlock(queue.transactions); +} + - (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions { if([transactions count] > 0) @@ -193,14 +203,6 @@ - (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)tran } } -- (void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error -{ - if(_failureBlock) - { - _failureBlock(error); - } -} - - (void)request:(SKRequest *)request didFailWithError:(NSError *)error { if(_failureBlock) From b559ddf843cf7d23de52b735ae8b90c047388336 Mon Sep 17 00:00:00 2001 From: Leszek Kaczor Date: Fri, 30 Oct 2015 13:54:59 +0100 Subject: [PATCH 2/2] Adding method to configure payment blocks --- Classes/SCPStoreKitManager.h | 9 +++++++++ Classes/SCPStoreKitManager.m | 28 ++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/Classes/SCPStoreKitManager.h b/Classes/SCPStoreKitManager.h index 92cde61..52c734b 100755 --- a/Classes/SCPStoreKitManager.h +++ b/Classes/SCPStoreKitManager.h @@ -28,6 +28,15 @@ typedef void(^PaymentTransactionStateRestored)(NSArray *transactions); - (void)requestProductsWithIdentifiers:(NSSet *)productsSet productsReturnedSuccessfully:(ProductsReturnedSuccessfully)productsReturnedSuccessfullyBlock invalidProducts:(InvalidProducts)invalidProductsBlock failure:(Failure)failureBlock; +- (void)configurePaymentTransactionStatePurchasing:(PaymentTransactionStatePurchasing)paymentTransactionStatePurchasingBlock paymentTransactionStatePurchased:(PaymentTransactionStatePurchased)paymentTransactionStatePurchasedBlock paymentTransactionStateFailed:(PaymentTransactionStateFailed)paymentTransactionStateFailedBlock paymentTransactionStateRestored:(PaymentTransactionStateRestored)paymentTransactionStateRestoredBlock failure:(Failure)failureBlock; + +/** + * Only if configure payment transaction blocks before calling that method + * + * @param product the product to buy + */ +- (void)requestPaymentForProduct:(SKProduct *)product; + - (void)requestPaymentForProduct:(SKProduct *)product paymentTransactionStatePurchasing:(PaymentTransactionStatePurchasing)paymentTransactionStatePurchasingBlock paymentTransactionStatePurchased:(PaymentTransactionStatePurchased)paymentTransactionStatePurchasedBlock paymentTransactionStateFailed:(PaymentTransactionStateFailed)paymentTransactionStateFailedBlock paymentTransactionStateRestored:(PaymentTransactionStateRestored)paymentTransactionStateRestoredBlock failure:(Failure)failureBlock; - (void)restorePurchasesPaymentTransactionStateRestored:(PaymentTransactionStateRestored)paymentTransactionStateRestoredBlock paymentTransactionStateFailed:(PaymentTransactionStateFailed)paymentTransactionStateFailedBlock failure:(Failure)failureBlock; diff --git a/Classes/SCPStoreKitManager.m b/Classes/SCPStoreKitManager.m index 4b5e0e4..2c7aeb4 100755 --- a/Classes/SCPStoreKitManager.m +++ b/Classes/SCPStoreKitManager.m @@ -82,6 +82,34 @@ - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProdu } } +- (void)configurePaymentTransactionStatePurchasing:(PaymentTransactionStatePurchasing)paymentTransactionStatePurchasingBlock paymentTransactionStatePurchased:(PaymentTransactionStatePurchased)paymentTransactionStatePurchasedBlock paymentTransactionStateFailed:(PaymentTransactionStateFailed)paymentTransactionStateFailedBlock paymentTransactionStateRestored:(PaymentTransactionStateRestored)paymentTransactionStateRestoredBlock failure:(Failure)failureBlock { + + self.paymentTransactionStatePurchasingBlock = paymentTransactionStatePurchasingBlock; + self.paymentTransactionStatePurchasedBlock = paymentTransactionStatePurchasedBlock; + self.paymentTransactionStateFailedBlock = paymentTransactionStateFailedBlock; + self.paymentTransactionStateRestoredBlock = paymentTransactionStateRestoredBlock; + self.failureBlock = failureBlock; + + [[SKPaymentQueue defaultQueue] addTransactionObserver:self]; +} + +- (void)requestPaymentForProduct:(SKProduct *)product { + SKPayment *payment = [SKPayment paymentWithProduct:product]; + + if([SKPaymentQueue canMakePayments]) + { + [[SKPaymentQueue defaultQueue] addTransactionObserver:self]; + [[SKPaymentQueue defaultQueue] addPayment:payment]; + } + else + { + if(self.failureBlock) + { + self.failureBlock([NSError errorWithDomain:SCPStoreKitDomain code:SCPErrorCodePaymentQueueCanNotMakePayments errorDescription:@"SKPaymentQueue can not make payments" errorFailureReason:@"Has the SKPaymentQueue got any uncompleted purchases?" errorRecoverySuggestion:@"Finish all transactions"]); + } + } +} + - (void)requestPaymentForProduct:(SKProduct *)product paymentTransactionStatePurchasing:(PaymentTransactionStatePurchasing)paymentTransactionStatePurchasingBlock paymentTransactionStatePurchased:(PaymentTransactionStatePurchased)paymentTransactionStatePurchasedBlock paymentTransactionStateFailed:(PaymentTransactionStateFailed)paymentTransactionStateFailedBlock paymentTransactionStateRestored:(PaymentTransactionStateRestored)paymentTransactionStateRestoredBlock failure:(Failure)failureBlock { self.paymentTransactionStatePurchasingBlock = paymentTransactionStatePurchasingBlock;