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 12c7085..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; @@ -128,6 +156,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 +231,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)