Skip to content

Commit

Permalink
Applying changes from SeanTB's PR (robotmedia#150 on robotmedia repo)
Browse files Browse the repository at this point in the history
As single commit, as the original is messy.
  • Loading branch information
Milan Cermak committed Aug 18, 2016
1 parent 3278a50 commit 2570e28
Showing 1 changed file with 43 additions and 37 deletions.
80 changes: 43 additions & 37 deletions RMStore/RMStore.m
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,15 @@ @implementation RMStore {
NSMutableDictionary *_products;
NSMutableSet *_productsRequestDelegates;
NSMutableSet *_productsRequests;

NSMutableArray *_restoredTransactions;

NSInteger _pendingRestoredTransactionsCount;
NSMutableSet *_pendingRestoredTransactionIds;
BOOL _restoredCompletedTransactionsFinished;

SKReceiptRefreshRequest *_refreshReceiptRequest;
void (^_refreshReceiptFailureBlock)(NSError* error);
void (^_refreshReceiptSuccessBlock)();

void (^_restoreTransactionsFailureBlock)(NSError* error);
void (^_restoreTransactionsSuccessBlock)(NSArray* transactions);
}
Expand All @@ -157,6 +156,7 @@ - (instancetype) init
_productsRequestDelegates = [NSMutableSet set];
_productsRequests = [NSMutableSet set];
_restoredTransactions = [NSMutableArray array];
_pendingRestoredTransactionIds = [NSMutableSet set];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
}
return self;
Expand Down Expand Up @@ -217,12 +217,12 @@ - (void)addPayment:(NSString*)productIdentifier
{
payment.applicationUsername = userIdentifier;
}

RMAddPaymentParameters *parameters = [[RMAddPaymentParameters alloc] init];
parameters.successBlock = successBlock;
parameters.failureBlock = failureBlock;
_addPaymentParameters[productIdentifier] = parameters;

[[SKPaymentQueue defaultQueue] addPayment:payment];
}

Expand All @@ -240,9 +240,9 @@ - (void)requestProducts:(NSSet*)identifiers
delegate.successBlock = successBlock;
delegate.failureBlock = failureBlock;
[_productsRequestDelegates addObject:delegate];

SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:identifiers];
productsRequest.delegate = delegate;
productsRequest.delegate = delegate;
[_productsRequests addObject:productsRequest];

[productsRequest start];
Expand All @@ -257,7 +257,7 @@ - (void)restoreTransactionsOnSuccess:(void (^)(NSArray *transactions))successBlo
failure:(void (^)(NSError *error))failureBlock
{
_restoredCompletedTransactionsFinished = NO;
_pendingRestoredTransactionsCount = 0;
[_pendingRestoredTransactionIds removeAllObjects];
_restoredTransactions = [NSMutableArray array];
_restoreTransactionsSuccessBlock = successBlock;
_restoreTransactionsFailureBlock = failureBlock;
Expand All @@ -270,7 +270,7 @@ - (void)restoreTransactionsOfUser:(NSString*)userIdentifier
{
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];
Expand Down Expand Up @@ -310,11 +310,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];
numberFormatter.numberStyle = NSNumberFormatterCurrencyStyle;
numberFormatter.locale = product.priceLocale;
NSString *formattedString = [numberFormatter stringFromNumber:product.price];
return formattedString;
}

#pragma mark Observers
Expand Down Expand Up @@ -395,7 +395,7 @@ - (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
{
RMStoreLog(@"restore transactions finished");
_restoredCompletedTransactionsFinished = YES;

[self notifyRestoreTransactionFinishedIfApplicableAfterTransaction:nil];
}

Expand Down Expand Up @@ -481,7 +481,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];
Expand Down Expand Up @@ -528,7 +528,7 @@ + (BOOL)hasPendingDownloadsInTransaction:(SKPaymentTransaction*)transaction
- (void)didPurchaseTransaction:(SKPaymentTransaction *)transaction queue:(SKPaymentQueue*)queue
{
RMStoreLog(@"transaction purchased with product %@", transaction.payment.productIdentifier);

if (self.receiptVerifier != nil)
{
[self.receiptVerifier verifyTransaction:transaction success:^{
Expand All @@ -547,23 +547,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];
Expand All @@ -572,9 +572,15 @@ - (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)
{
[_pendingRestoredTransactionIds addObject:productIdentifier];
}

if (self.receiptVerifier != nil)
{
[self.receiptVerifier verifyTransaction:transaction success:^{
Expand Down Expand Up @@ -634,18 +640,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];
Expand All @@ -657,9 +663,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)
Expand Down Expand Up @@ -714,7 +720,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
Expand Down Expand Up @@ -756,17 +762,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);
Expand Down

0 comments on commit 2570e28

Please sign in to comment.