Skip to content

Commit

Permalink
Patch clear transaction (#1215)
Browse files Browse the repository at this point in the history
* Bug Fix clearTransaction promise

Added a promise to the clearTransaction function, to wait until it finishes clearing open transactions

#1120

* BigFix clearTransaction promise

Added a promise to the clearTransaction function, to wait until it finishes clearing open transactions

#1120
  • Loading branch information
andresordonezfm committed Dec 30, 2020
1 parent f620e80 commit c0652db
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
1 change: 1 addition & 0 deletions ios/RNIapIos.h
Expand Up @@ -8,6 +8,7 @@
NSMutableArray *validProducts;
SKPayment *promotedPayment;
SKProduct *promotedProduct;
NSInteger countPendingTransaction;
}
@end

Expand Down
31 changes: 27 additions & 4 deletions ios/RNIapIos.m
Expand Up @@ -255,11 +255,23 @@ - (BOOL)paymentQueue:(SKPaymentQueue *)queue shouldAddStorePayment:(SKPayment *)
}
}

RCT_EXPORT_METHOD(clearTransaction) {
NSArray *pendingTrans = [[SKPaymentQueue defaultQueue] transactions];
RCT_EXPORT_METHOD(clearTransaction:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject) {

NSLog(@"\n\n\n *** clear remaining Transactions. Call this before make a new transaction \n\n.");
for (int k = 0; k < pendingTrans.count; k++) {
[[SKPaymentQueue defaultQueue] finishTransaction:pendingTrans[k]];

NSArray *pendingTrans = [[SKPaymentQueue defaultQueue] transactions];
countPendingTransaction = (NSInteger)(pendingTrans.count);

if (countPendingTransaction > 0) {
[self addPromiseForKey:@"cleaningTransactions" resolve:resolve reject:reject];

for (SKPaymentTransaction *transaction in pendingTrans) {
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}

} else {
resolve(nil);
}
}

Expand Down Expand Up @@ -814,4 +826,15 @@ - (void)requestDidFinish:(SKRequest *)request {
}
}

-(void)paymentQueue:(SKPaymentQueue *)queue removedTransactions:(NSArray *)transactions {
NSLog(@"removedTransactions");
if (countPendingTransaction != nil && countPendingTransaction > 0) {
countPendingTransaction--;
if (countPendingTransaction == 0) {
[self resolvePromisesForKey:@"cleaningTransactions" value:nil];
countPendingTransaction = nil;
}
}
}

@end

0 comments on commit c0652db

Please sign in to comment.