What is a good way of restoring and auto-restoring purchases?
See original GitHub issueCan someone chip in here and tell us what is the proper way to handle restore purchases flows?
[This code is not final!]
const setup = async () => {
try {
const status = await initConnection();
console.log(`Payment Service set up. Status: ${status}`);
} catch (e) {
console.log('Payment Service is disabled');
}
await automaticallyRestorePurchases();
purchaseUpdateSubscription = purchaseUpdatedListener(treatIAPPurchaseSuccess);
purchaseErrorSubscription = purchaseErrorListener(treatIAPPurchaseFailure);
console.log('Payment Service setup succeeded');
};
const destroy = async () => {
try {
if (purchaseUpdateSubscription) {
purchaseUpdateSubscription.remove();
purchaseUpdateSubscription = undefined;
}
if (purchaseErrorSubscription) {
purchaseErrorSubscription.remove();
purchaseErrorSubscription = undefined;
}
await endConnection();
console.log('Payment Service destroyed');
} catch {
/* Do nothing */
}
};
useEffect(() => {
setup();
return destroy;
}, []);
// ...
// Called manually
const restorePurchases = async () => {
console.log('[RESTORE] restorePurchases called');
if (!currentUser) {
return;
}
startFlow();
const purchases = await getPurchaseHistory();
console.log(`[RESTORE] getPurchaseHistory for user. Results: ${purchases?.length}`);
for (const purchase of purchases) {
console.log(purchase);
await treatIAPPurchaseSuccess(purchase);
}
justFinishFlow();
};
// Called after startConnection, automatically when app starts and is logged in or has just logged in.
const automaticallyRestorePurchases = async () => {
console.log('[AUTO-RESTORE] restorePurchases called');
if (Platform.OS === 'ios') {
// Skip on iOS because of user and password are requested
return;
} else {
await restorePurchases();
}
};
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:7
Top Results From Across the Web
Restoring Purchased Products - Apple Developer
Your app starts restoring completed transactions by calling the restoreCompletedTransactions() method of SKPaymentQueue . This call sends a request to the App ...
Read more >What does restore purchase mean? - Qonversion
When users purchase non-consumables, auto-renewable subscriptions, or non-renewing ... This method is Apple's recommended approach to restoring purchases.
Read more >ios - How to restore the correct transaction when using Auto ...
My solution: retrieve the receipt and validate it against your productIdentifiers. Using SKPaymentQueue.defaultQueue().
Read more >Restoring In-App Purchases - RevenueCat
Restoring purchases is a mechanism by which your user can restore their in-app purchases, reactivating any content that had previously been purchased from ......
Read more >iOS Restore Purchases Does Not work Correctly - Unity Forum
... I CAN get the purchases to restore, but it's spotty at best, and not a solution I can give to my users....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Here’s a pretty complete example:
The most important case nowadays is auto-renewable subscriptions.
For my case, I had 3 subscriptions for the same service (with less or more features), added in a single group in App Store Connect.
The only way I could find to make all of it work in react native is:
There is no other method other than getPurchaseHistory() that offers all the up to date transactions existing or old or made on other devices. I consider the other fetch purchases and consumable methods really really really situational and for app only validation (no backend validation) apps.
Ending on an actionable note… I would prefer if the demo app had a working restore purchases flow, but also a recommended way of automatically restoring purchases.