Skip to content

Commit ae9e0b5

Browse files
emilioicaihyochan
authored andcommitted
method to retrieve pending transactions (#663)
* added exported method getPendingTransactions * exposing requestPendingPurchasesIOS * Update README.md * added typings for requestPendingPurchasesIOS * Update index.js * typings for getPendingPurchasesIOS
1 parent 5f72fb1 commit ae9e0b5

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ _*deprecated_<br>~~`buySubscription(sku: string)`~~<ul><li>sku: subscription ID/
116116
`clearTransactionIOS()` | `void` | **iOS only**<br>Clear up the unfinished transanction which sometimes causes problem.<br>Read more in below README.
117117
`clearProductsIOS()` | `void` | **iOS only**<br>Clear all products and subscriptions.<br>Read more in below README.
118118
`requestReceiptIOS()` | `Promise<string>` | **iOS only**<br>Get the current receipt.
119+
`requestPendingPurchasesIOS()` | `Promise<ProductPurchase[]>` | **IOS only**<br>Gets all the transactions which are pending to be finished.
119120
`validateReceiptIos(body: Object, devMode: boolean)`<ul><li>body: receiptBody</li><li>devMode: isTest</li></ul> | `Object\|boolean` | **iOS only**<br>Validate receipt.
120121
`endConnectionAndroid()` | `Promise<void>` | **Android only**<br>End billing connection.
121122
`consumeAllItemsAndroid()` | `Promise<void>` | **Android only**<br>Consume all items so they are able to buy again.
@@ -126,6 +127,7 @@ _*deprecated_<br>~~`buySubscription(sku: string, prevSku?: string, mode?: number
126127
`requestSubscription(sku: string, prevSku?: string, mode?: number)`<ul><li>sku: subscription ID/sku</li><li>prevSku: old subscription ID/sku (optional)</li><li>mode: proration mode (optional)</li></ul> | `Promise<string>` | **Android only**<br>Create (buy) a subscription to a sku.<br>For upgrading/downgrading subscription on Android pass the second parameter with current subscription ID, on iOS this is handled automatically by store.<br>You can also optionally pass in a proration mode integer for upgrading/downgrading subscriptions on Android
127128
`validateReceiptAndroid(bundleId: string, productId: string, productToken: string, accessToken: string)`<br><ul><li>bundleId: the packageName</li><li>productId: productId</li><li>productToken: productToken</li><li>accessToken: accessToken</li><li>isSubscription: isSubscription</li></ul> | `Object\|boolean` | **Android only**<br>Validate receipt.
128129

130+
129131
</details>
130132

131133
Npm Module

index.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,3 +269,10 @@ export function purchaseErrorListener(fn: Function) : EmitterSubscription;
269269
* @returns {Promise<string>}
270270
*/
271271
export function requestReceiptIOS(): Promise<string>;
272+
273+
/**
274+
* Request all the pending transactions (IOS only)
275+
* @returns {Promise<ProductPurchase[]>}
276+
*/
277+
export function getPendingPurchasesIOS(): Promise<ProductPurchase[]>;
278+

index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,17 @@ export const requestReceiptIOS = () => {
480480
}
481481
};
482482

483+
/**
484+
* Get the pending purchases in IOS.
485+
* @returns {Promise<ProductPurchase[]>}
486+
*/
487+
export const getPendingPurchasesIOS = () => {
488+
if (Platform.OS === 'ios') {
489+
checkNativeiOSAvailable();
490+
return RNIapIos.getPendingTransactions();
491+
}
492+
};
493+
483494
/**
484495
* deprecated codes
485496
*/
@@ -521,6 +532,7 @@ export default {
521532
getSubscriptions,
522533
getPurchaseHistory,
523534
getAvailablePurchases,
535+
getPendingPurchasesIOS,
524536
consumeAllItemsAndroid,
525537
buySubscription,
526538
buyProduct,

ios/RNIapIos.m

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,32 @@ - (BOOL)shouldAddStorePayment:(SKPayment *)payment forProduct:(SKProduct *)produ
318318
[self finishTransactionWithIdentifier:transactionIdentifier];
319319
}
320320

321+
RCT_EXPORT_METHOD(getPendingTransactions:(RCTPromiseResolveBlock)resolve
322+
reject:(RCTPromiseRejectBlock)reject) {
323+
[self requestReceiptDataWithBlock:^(NSData *receiptData, NSError *error) {
324+
if (receiptData == nil) {
325+
resolve(nil);
326+
}
327+
else {
328+
NSArray<SKPaymentTransaction *> *transactions = [[SKPaymentQueue defaultQueue] transactions];
329+
NSMutableArray *output = [NSMutableArray array];
330+
331+
for (SKPaymentTransaction *item in transactions) {
332+
NSMutableDictionary *purchase = [NSMutableDictionary dictionaryWithObjectsAndKeys:
333+
@(item.transactionDate.timeIntervalSince1970 * 1000), @"transactionDate",
334+
item.transactionIdentifier, @"transactionId",
335+
item.payment.productIdentifier, @"productId",
336+
[receiptData base64EncodedStringWithOptions:0], @"transactionReceipt",
337+
nil
338+
];
339+
[output addObject:purchase];
340+
}
341+
342+
resolve(output);
343+
}
344+
}];
345+
}
346+
321347
#pragma mark ===== StoreKit Delegate
322348

323349
-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {

0 commit comments

Comments
 (0)