-
Notifications
You must be signed in to change notification settings - Fork 452
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix to ensure restoreTransactions completion callback is called #150
base: master
Are you sure you want to change the base?
Changes from 5 commits
d448533
67bf17b
b3f8822
1396cbb
858fb43
ddab329
303b1e9
3153a69
d1737ca
811d820
2d493ee
344a5d7
58c63e4
86bc310
576560f
551a7e2
968349b
a7e52f0
6a35cf8
fc805bd
d548cd8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -133,16 +133,16 @@ @implementation RMStore { | |
NSMutableDictionary *_addPaymentParameters; // HACK: We use a dictionary of product identifiers because the returned SKPayment is different from the one we add to the queue. Bad Apple. | ||
NSMutableDictionary *_products; | ||
NSMutableSet *_productsRequestDelegates; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please refrain from making style changes. It makes the PR harder to review, and -unless done globally- makes the code style heterogenous. |
||
NSMutableArray *_restoredTransactions; | ||
NSInteger _pendingRestoredTransactionsCount; | ||
|
||
NSMutableSet *_pendingRestoredTransactionIds; | ||
BOOL _restoredCompletedTransactionsFinished; | ||
|
||
SKReceiptRefreshRequest *_refreshReceiptRequest; | ||
void (^_refreshReceiptFailureBlock)(NSError* error); | ||
void (^_refreshReceiptSuccessBlock)(); | ||
|
||
void (^_restoreTransactionsFailureBlock)(NSError* error); | ||
void (^_restoreTransactionsSuccessBlock)(NSArray* transactions); | ||
} | ||
|
@@ -155,6 +155,7 @@ - (id) init | |
_products = [NSMutableDictionary dictionary]; | ||
_productsRequestDelegates = [NSMutableSet set]; | ||
_restoredTransactions = [NSMutableArray array]; | ||
_pendingRestoredTransactionIds = [NSMutableSet set]; | ||
[[SKPaymentQueue defaultQueue] addTransactionObserver:self]; | ||
} | ||
return self; | ||
|
@@ -188,16 +189,16 @@ - (void)addPayment:(NSString*)productIdentifier | |
} | ||
|
||
- (void)addPayment:(NSString*)productIdentifier | ||
success:(void (^)(SKPaymentTransaction *transaction))successBlock | ||
failure:(void (^)(SKPaymentTransaction *transaction, NSError *error))failureBlock | ||
success:(void (^)(SKPaymentTransaction *transaction))successBlock | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same. |
||
failure:(void (^)(SKPaymentTransaction *transaction, NSError *error))failureBlock | ||
{ | ||
[self addPayment:productIdentifier user:nil success:successBlock failure:failureBlock]; | ||
} | ||
|
||
- (void)addPayment:(NSString*)productIdentifier | ||
user:(NSString*)userIdentifier | ||
success:(void (^)(SKPaymentTransaction *transaction))successBlock | ||
failure:(void (^)(SKPaymentTransaction *transaction, NSError *error))failureBlock | ||
user:(NSString*)userIdentifier | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same. |
||
success:(void (^)(SKPaymentTransaction *transaction))successBlock | ||
failure:(void (^)(SKPaymentTransaction *transaction, NSError *error))failureBlock | ||
{ | ||
SKProduct *product = [self productForIdentifier:productIdentifier]; | ||
if (product == nil) | ||
|
@@ -215,12 +216,12 @@ - (void)addPayment:(NSString*)productIdentifier | |
{ | ||
payment.applicationUsername = userIdentifier; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same. |
||
RMAddPaymentParameters *parameters = [[RMAddPaymentParameters alloc] init]; | ||
parameters.successBlock = successBlock; | ||
parameters.failureBlock = failureBlock; | ||
_addPaymentParameters[productIdentifier] = parameters; | ||
|
||
[[SKPaymentQueue defaultQueue] addPayment:payment]; | ||
} | ||
|
||
|
@@ -230,18 +231,18 @@ - (void)requestProducts:(NSSet*)identifiers | |
} | ||
|
||
- (void)requestProducts:(NSSet*)identifiers | ||
success:(RMSKProductsRequestSuccessBlock)successBlock | ||
failure:(RMSKProductsRequestFailureBlock)failureBlock | ||
success:(RMSKProductsRequestSuccessBlock)successBlock | ||
failure:(RMSKProductsRequestFailureBlock)failureBlock | ||
{ | ||
RMProductsRequestDelegate *delegate = [[RMProductsRequestDelegate alloc] init]; | ||
delegate.store = self; | ||
delegate.successBlock = successBlock; | ||
delegate.failureBlock = failureBlock; | ||
[_productsRequestDelegates addObject:delegate]; | ||
|
||
SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:identifiers]; | ||
productsRequest.delegate = delegate; | ||
productsRequest.delegate = delegate; | ||
|
||
[productsRequest start]; | ||
} | ||
|
||
|
@@ -251,23 +252,23 @@ - (void)restoreTransactions | |
} | ||
|
||
- (void)restoreTransactionsOnSuccess:(void (^)(NSArray *transactions))successBlock | ||
failure:(void (^)(NSError *error))failureBlock | ||
failure:(void (^)(NSError *error))failureBlock | ||
{ | ||
_restoredCompletedTransactionsFinished = NO; | ||
_pendingRestoredTransactionsCount = 0; | ||
[_pendingRestoredTransactionIds removeAllObjects]; | ||
_restoredTransactions = [NSMutableArray array]; | ||
_restoreTransactionsSuccessBlock = successBlock; | ||
_restoreTransactionsFailureBlock = failureBlock; | ||
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions]; | ||
} | ||
|
||
- (void)restoreTransactionsOfUser:(NSString*)userIdentifier | ||
onSuccess:(void (^)(NSArray *transactions))successBlock | ||
failure:(void (^)(NSError *error))failureBlock | ||
onSuccess:(void (^)(NSArray *transactions))successBlock | ||
failure:(void (^)(NSError *error))failureBlock | ||
{ | ||
NSAssert([[SKPaymentQueue defaultQueue] respondsToSelector:@selector(restoreCompletedTransactionsWithApplicationUsername:)], @"restoreCompletedTransactionsWithApplicationUsername: not supported in this iOS version. Use restoreTransactionsOnSuccess:failure: instead."); | ||
_restoredCompletedTransactionsFinished = NO; | ||
_pendingRestoredTransactionsCount = 0; | ||
[_pendingRestoredTransactionIds removeAllObjects]; | ||
_restoreTransactionsSuccessBlock = successBlock; | ||
_restoreTransactionsFailureBlock = failureBlock; | ||
[[SKPaymentQueue defaultQueue] restoreCompletedTransactionsWithApplicationUsername:userIdentifier]; | ||
|
@@ -289,7 +290,7 @@ - (void)refreshReceipt | |
} | ||
|
||
- (void)refreshReceiptOnSuccess:(RMStoreSuccessBlock)successBlock | ||
failure:(RMStoreFailureBlock)failureBlock | ||
failure:(RMStoreFailureBlock)failureBlock | ||
{ | ||
_refreshReceiptFailureBlock = failureBlock; | ||
_refreshReceiptSuccessBlock = successBlock; | ||
|
@@ -307,11 +308,11 @@ - (SKProduct*)productForIdentifier:(NSString*)productIdentifier | |
|
||
+ (NSString*)localizedPriceOfProduct:(SKProduct*)product | ||
{ | ||
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init]; | ||
numberFormatter.numberStyle = NSNumberFormatterCurrencyStyle; | ||
numberFormatter.locale = product.priceLocale; | ||
NSString *formattedString = [numberFormatter stringFromNumber:product.price]; | ||
return formattedString; | ||
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same. |
||
numberFormatter.numberStyle = NSNumberFormatterCurrencyStyle; | ||
numberFormatter.locale = product.priceLocale; | ||
NSString *formattedString = [numberFormatter stringFromNumber:product.price]; | ||
return formattedString; | ||
} | ||
|
||
#pragma mark Observers | ||
|
@@ -392,7 +393,7 @@ - (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue | |
{ | ||
RMStoreLog(@"restore transactions finished"); | ||
_restoredCompletedTransactionsFinished = YES; | ||
|
||
[self notifyRestoreTransactionFinishedIfApplicableAfterTransaction:nil]; | ||
} | ||
|
||
|
@@ -478,7 +479,7 @@ - (void)didFinishDownload:(SKDownload*)download queue:(SKPaymentQueue*)queue | |
{ | ||
SKPaymentTransaction *transaction = download.transaction; | ||
RMStoreLog(@"download %@ for product %@ finished", download.contentIdentifier, transaction.payment.productIdentifier); | ||
|
||
[self postNotificationWithName:RMSKDownloadFinished download:download userInfoExtras:nil]; | ||
|
||
const BOOL hasPendingDownloads = [self.class hasPendingDownloadsInTransaction:transaction]; | ||
|
@@ -525,7 +526,7 @@ + (BOOL)hasPendingDownloadsInTransaction:(SKPaymentTransaction*)transaction | |
- (void)didPurchaseTransaction:(SKPaymentTransaction *)transaction queue:(SKPaymentQueue*)queue | ||
{ | ||
RMStoreLog(@"transaction purchased with product %@", transaction.payment.productIdentifier); | ||
|
||
if (self.receiptVerificator != nil) | ||
{ | ||
[self.receiptVerificator verifyTransaction:transaction success:^{ | ||
|
@@ -544,23 +545,23 @@ - (void)didPurchaseTransaction:(SKPaymentTransaction *)transaction queue:(SKPaym | |
- (void)didFailTransaction:(SKPaymentTransaction *)transaction queue:(SKPaymentQueue*)queue error:(NSError*)error | ||
{ | ||
SKPayment *payment = transaction.payment; | ||
NSString* productIdentifier = payment.productIdentifier; | ||
NSString* productIdentifier = payment.productIdentifier; | ||
RMStoreLog(@"transaction failed with product %@ and error %@", productIdentifier, error.debugDescription); | ||
|
||
if (error.code != RMStoreErrorCodeUnableToCompleteVerification) | ||
{ // If we were unable to complete the verification we want StoreKit to keep reminding us of the transaction | ||
[queue finishTransaction:transaction]; | ||
} | ||
|
||
RMAddPaymentParameters *parameters = [self popAddPaymentParametersForIdentifier:productIdentifier]; | ||
if (parameters.failureBlock != nil) | ||
{ | ||
parameters.failureBlock(transaction, error); | ||
} | ||
|
||
NSDictionary *extras = error ? @{RMStoreNotificationStoreError : error} : nil; | ||
[self postNotificationWithName:RMSKPaymentTransactionFailed transaction:transaction userInfoExtras:extras]; | ||
|
||
if (transaction.transactionState == SKPaymentTransactionStateRestored) | ||
{ | ||
[self notifyRestoreTransactionFinishedIfApplicableAfterTransaction:transaction]; | ||
|
@@ -569,9 +570,12 @@ - (void)didFailTransaction:(SKPaymentTransaction *)transaction queue:(SKPaymentQ | |
|
||
- (void)didRestoreTransaction:(SKPaymentTransaction *)transaction queue:(SKPaymentQueue*)queue | ||
{ | ||
RMStoreLog(@"transaction restored with product %@", transaction.originalTransaction.payment.productIdentifier); | ||
|
||
_pendingRestoredTransactionsCount++; | ||
NSString *productIdentifier = transaction.originalTransaction.payment.productIdentifier; | ||
RMStoreLog(@"transaction restored with product %@", productIdentifier); | ||
|
||
if(productIdentifier) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: if (productIdentifier) |
||
[_pendingRestoredTransactionIds addObject:productIdentifier]; | ||
} | ||
if (self.receiptVerificator != nil) | ||
{ | ||
[self.receiptVerificator verifyTransaction:transaction success:^{ | ||
|
@@ -631,18 +635,18 @@ - (void)didDownloadSelfHostedContentForTransaction:(SKPaymentTransaction *)trans | |
- (void)finishTransaction:(SKPaymentTransaction *)transaction queue:(SKPaymentQueue*)queue | ||
{ | ||
SKPayment *payment = transaction.payment; | ||
NSString* productIdentifier = payment.productIdentifier; | ||
NSString* productIdentifier = payment.productIdentifier; | ||
[queue finishTransaction:transaction]; | ||
[self.transactionPersistor persistTransaction:transaction]; | ||
|
||
RMAddPaymentParameters *wrapper = [self popAddPaymentParametersForIdentifier:productIdentifier]; | ||
if (wrapper.successBlock != nil) | ||
{ | ||
wrapper.successBlock(transaction); | ||
} | ||
|
||
[self postNotificationWithName:RMSKPaymentTransactionFinished transaction:transaction userInfoExtras:nil]; | ||
|
||
if (transaction.transactionState == SKPaymentTransactionStateRestored) | ||
{ | ||
[self notifyRestoreTransactionFinishedIfApplicableAfterTransaction:transaction]; | ||
|
@@ -654,9 +658,9 @@ - (void)notifyRestoreTransactionFinishedIfApplicableAfterTransaction:(SKPaymentT | |
if (transaction != nil) | ||
{ | ||
[_restoredTransactions addObject:transaction]; | ||
_pendingRestoredTransactionsCount--; | ||
[_pendingRestoredTransactionIds removeObject:transaction.payment.productIdentifier]; | ||
} | ||
if (_restoredCompletedTransactionsFinished && _pendingRestoredTransactionsCount == 0) | ||
if (_restoredCompletedTransactionsFinished && _pendingRestoredTransactionIds.count == 0) | ||
{ // Wait until all restored transations have been verified | ||
NSArray *restoredTransactions = [_restoredTransactions copy]; | ||
if (_restoreTransactionsSuccessBlock != nil) | ||
|
@@ -711,7 +715,7 @@ - (void)request:(SKRequest *)request didFailWithError:(NSError *)error | |
|
||
- (void)addProduct:(SKProduct*)product | ||
{ | ||
_products[product.productIdentifier] = product; | ||
_products[product.productIdentifier] = product; | ||
} | ||
|
||
- (void)postNotificationWithName:(NSString*)notificationName download:(SKDownload*)download userInfoExtras:(NSDictionary*)extras | ||
|
@@ -748,17 +752,17 @@ - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProdu | |
RMStoreLog(@"products request received response"); | ||
NSArray *products = [NSArray arrayWithArray:response.products]; | ||
NSArray *invalidProductIdentifiers = [NSArray arrayWithArray:response.invalidProductIdentifiers]; | ||
|
||
for (SKProduct *product in products) | ||
{ | ||
RMStoreLog(@"received product with id %@", product.productIdentifier); | ||
[self.store addProduct:product]; | ||
} | ||
|
||
[invalidProductIdentifiers enumerateObjectsUsingBlock:^(NSString *invalid, NSUInteger idx, BOOL *stop) { | ||
RMStoreLog(@"invalid product with id %@", invalid); | ||
}]; | ||
|
||
if (self.successBlock) | ||
{ | ||
self.successBlock(products, invalidProductIdentifiers); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I remember having to shorten the description for some reason.
In any case, would you mind taking this change out of the PR?