Making a purchase
The flow of the
purchasehas been renewed by the founding in issue #307. I've decided to redesign thePurchase Flowto not rely onPromiseorCallback. There are some reasons not to approach in this way:
- There may be more than one response when requesting a payment.
- Purchases are inter-session
asynchronuousmeaning requests that are made may take several hours to complete and continue to exist even after the app has been closed or crashed. - The purchase may be pending and hard to track what has been done (example.
- Thus the Billing Flow is an
eventpattern rather than acallbackpattern.
Once you have called getProducts(), and you have a valid response, you can call requestPurchase(). Subscribable products can be purchased just like consumable products and users can cancel subscriptions by using the iOS System Settings.
Before you request any purchase, you should set purchaseUpdatedListener from react-native-iap. It is recommended that you start listening to updates as soon as your application launches. And don't forget that even at launch you may receive successful purchases that either completed while your app was closed or that failed to be finished, consumed or acknowledged due to network errors or bugs.
Then define the method like below and call it when user press the button.
New Purchase Flow#
Most likely, you'll want to handle the “store kit flow”[[2]][apple-store-kit-flow], which happens when a user successfully pays after solving a problem with his or her account – for example, when the credit card information has expired.
For above reason, we decided to remove and use buyProductrequestPurchase instead which doesn't rely on promise function. The purchaseUpdatedListener will receive the success purchase and purchaseErrorListener will receive all the failure result that occured during the purchase attempt.
Finishing a Purchase#
Purchases will keep being emitted to your purchaseUpdatedListener on every app relaunch until you finish the purchase.
Consumable purchases should be consumed by calling consumePurchaseAndroid() or finishTransactionIOS(). Once an item is consumed, it will be removed from getAvailablePurchases() so it is up to you to record the purchase into your database before calling consumePurchaseAndroid() or finishTransactionIOS().
Non-consumable purchases need to be acknowledged on Android, or they will be automatically refunded after a few days. Acknowledge a purchase when you have delivered it to your user by calling acknowledgePurchaseAndroid().
On iOS non-consumable purchases are finished automatically but this will change in the future so it is recommended that you prepare by simply calling finishTransactionIOS() on non-consumables as well.
finishTransaction() works for both platforms and is recommended since version 4.1.0 or later. Equal to finishTransactionIOS + consumePurchaseAndroid and acknowledgePurchaseAndroid.