This is a react-native link library project for in-app purchase for both android and ios platforms. The goal for this project is to have similar experience between the two platforms for in-app-purchase. Basically, android platform has more functions for in-app-purchase and is not our specific interests for this project. We are willing to share same in-app-purchase experience for both
android
andios
.
- Recently,
react-native-iap
version 3 has been updated very prompty for migration issues. Don't get suprised too much on why it is bumping up version so quickly these days.- Migrated to new
androidx
apis. - Migrated to new
android
billing client which is2.0.+
.- acknowledgePurchase has been added in
3.2.0
which is very important.
- acknowledgePurchase has been added in
- New purchase flow
- More is comming in ios
13+
.
- Migrated to new
- Migrated to Android X in
3.1.0
. Please check the Migration Guide. - Recommended to use
3.2.0
or above forreact-native-iap@3.0.+
users.- Now, you should acknowledge purchase with non-consumable and subscription purchase from
3.0.0
. See more about acknowledgePurchase. - If you are using version
3.0.0
~ <3.2.0
, please useacknowledgePurchase
via google-api-nodejs-client. You can use method likeandroidpublisher.purchases.subscriptions.acknowledge
.
- Now, you should acknowledge purchase with non-consumable and subscription purchase from
- Please refer to Blog.
- If you are using
react-native-iap
version below3.0.0
, please follow above readme.
Func | Param | Return | Description |
---|---|---|---|
initConnection | Promise<boolean> |
Init IAP module. On Android this can be called to preload the connection to Play Services. In iOS, it will simply call canMakePayments method and return value. |
|
getProducts | string[] Product IDs/skus |
Promise<Product[]> |
Get a list of products (consumable and non-consumable items, but not subscriptions). Note: On iOS versions earlier than 11.2 this method will also return subscriptions if they are included in your list of SKUs. This is because we cannot differentiate between IAP products and subscriptions prior to 11.2. |
getSubscriptions | string[] Subscription IDs/skus |
Promise<Subscription[]> |
Get a list of subscriptions. Note: On iOS versions earlier than 11.2 this method will also return products if they are included in your list of SKUs. This is because we cannot differentiate between IAP products and subscriptions prior to 11.2. |
getPurchaseHistory | Promise<Purchase[]> |
Gets an inventory of purchases made by the user regardless of consumption status (where possible) | |
getAvailablePurchases | Promise<Purchase[]> |
Get all purchases made by the user (either non-consumable, or haven't been consumed yet) | |
string Product ID/sku |
Promise<Purchase> |
Buy a product | |
requestPurchase | string Product ID/sku |
Promise<string> |
Request a purchase. purchaseUpdatedListener will receive the result. |
string Product ID/sku, number Quantity |
Promise<Purchase> |
Buy a product with a specified quantity (iOS only) | |
requestPurchaseWithQuantityIOS | string Product ID/sku, number Quantity |
Promise<Purchase> |
Buy a product with a specified quantity (iOS only). purchaseUpdatedListener will receive the result |
string Subscription ID/sku, string Old Subscription ID/sku (on Android), int Proration Mode (on Android) |
Promise<Purchase> |
Create (buy) a subscription to a sku. For upgrading/downgrading subscription on Android pass the second parameter with current subscription ID, on iOS this is handled automatically by store. You can also optionally pass in a proration mode integer for upgrading/downgrading subscriptions on Android | |
requestSubscription | string Subscription ID/sku, string Old Subscription ID/sku (on Android), int Proration Mode (on Android) |
Promise<string> |
Create (buy) a subscription to a sku. For upgrading/downgrading subscription on Android pass the second parameter with current subscription ID, on iOS this is handled automatically by store. You can also optionally pass in a proration mode integer for upgrading/downgrading subscriptions on Android |
clearTransactionIOS | void |
void |
Clear up the unfinished transanction which sometimes causes problem. Read more in below readme. |
clearProductsIOS | void |
void |
Clear all products, subscriptions in ios. Read more in below readme. |
acknowledgePurchaseAndroid | string purchase token, string developerPayload |
Promise<PurchaseResult> |
Acknowledge a product (on Android.) No-op on iOS. |
consumePurchaseAndroid | string purchase token, string developerPayload |
Promise<PurchaseResult> |
Consume a product (on Android.) No-op on iOS. |
endConnectionAndroid | Promise<void> |
End billing connection (on Android.) No-op on iOS. | |
consumeAllItemsAndroid | Promise<void> |
Consume all items in android so they are able to buy again (on Android.) No-op on iOS. | |
validateReceiptIos | object receiptBody, boolean isTest |
object or boolean result |
validate receipt for ios. |
validateReceiptAndroid | string packageName, string productId, string productToken, string accessToken, boolean isSubscription |
object or boolean result |
validate receipt for android. |
requestReceiptIOS | Promise<string> |
Get the current receipt (on iOS.) No-op on Android. |
https://www.npmjs.com/package/react-native-iap
https://github.com/dooboolab/react-native-iap
$ npm install react-native-iap --save
$ react-native link react-native-iap
- In XCode, in the project navigator, right-click
Libraries
➜Add Files to [your project's name]
- Go to
node_modules
➜react-native-iap
and addRNIap.xcodeproj
- In XCode, in the project navigator, select your project. Add
libRNIap.a
to your project'sBuild Phases
➜Link Binary With Libraries
- Run your project (
Cmd+R
)
- Open up
android/app/src/main/java/[...]/MainApplication.java
- Add
import com.dooboolab.RNIap.RNIapPackage;
to the imports at the top of the file - Add
new RNIapPackage()
to the list returned by thegetPackages()
method
- Append the following lines to
android/settings.gradle
:
include ':react-native-iap'
project(':react-native-iap').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-iap/android')
- Insert the following lines inside the dependencies block in
android/app/build.gradle
:
compile project(':react-native-iap')
- Add the following to the
<permission>
block inandroid/app/src/main/AndroidManifest.xml
:
<uses-permission android:name="com.android.vending.BILLING" />
To migrate to 3.1.0
you must migrate your Android app to Android X by following the Migrating to AndroidX Guide.
You can look in the RNIapExample folder to try the example. Below is basic implementation which is also provided in RNIapExample project.
First thing you should do is to define your items for iOS and android separately like defined below.
import * as RNIap from 'react-native-iap';
const itemSkus = Platform.select({
ios: [
'com.example.coins100'
],
android: [
'com.example.coins100'
]
});
To get a list of valid items, call getProducts()
. You can do it in componentDidMount(), or another area as appropriate for you app. Since a user may first start your app with a bad internet connection, then later have an internet connection, making preparing/getting items more than once may be a good idea. Like if the user has no IAPs available when the app first starts, you may want to check again when the user enters your IAP store.
async componentDidMount() {
try {
const products = await RNIap.getProducts(itemSkus);
this.setState({ products });
} catch(err) {
console.warn(err); // standardized err.code and err.message available
}
}
iOS | Android | Comment | |
---|---|---|---|
price |
✓ | ✓ | Will return localizedPrice on Android (default) or a string price (eg. 1.99 ) (iOS) |
productId |
✓ | ✓ | Returns a string needed to purchase the item later |
currency |
✓ | ✓ | Returns the currency code |
localizedPrice |
✓ | ✓ | Use localizedPrice if you want to display the price to the user so you don't need to worry about currency symbols. |
title |
✓ | ✓ | Returns the title Android and localizedTitle on iOS |
description |
✓ | ✓ | Returns the localized description on Android and iOS |
introductoryPrice |
✓ | ✓ | Formatted introductory price of a subscription, including its currency sign, such as €3.99. The price doesn't include tax. |
introductoryPricePaymentModeIOS |
✓ | The payment mode for this product discount. | |
introductoryPriceNumberOfPeriods |
✓ | An integer that indicates the number of periods the product discount is available. | |
introductoryPriceNumberOfPeriodsIOS |
✓ | An integer that indicates the number of periods the product discount is available. | |
introductoryPriceSubscriptionPeriod |
✓ | An object that defines the period for the product discount. | |
introductoryPriceSubscriptionPeriodIOS |
✓ | An object that defines the period for the product discount. | |
subscriptionPeriodNumberIOS |
✓ | The unit in string like DAY or WEEK or MONTH or YEAR. | |
subscriptionPeriodUnitIOS |
✓ | The unit number of subscription period. | |
subscriptionPeriodAndroid |
✓ | Subscription period, specified in ISO 8601 format. For example, P1W equates to one week, P1M equates to one month, P3M equates to three months, P6M equates to six months, and P1Y equates to one year. | |
introductoryPriceCyclesAndroid |
✓ | The number of subscription billing periods for which the user will be given the introductory price, such as 3. | |
introductoryPricePeriodAndroid |
✓ | The billing period of the introductory price, specified in ISO 8601 format. | |
freeTrialPeriodAndroid |
✓ | Trial period configured in Google Play Console, specified in ISO 8601 format. For example, P7D equates to seven days. |
When you are done with the billing, you should release it for android(READ). It is not needed in ios. No need to check platform either since nothing will happen in ios. This can be used in componentWillUnMount
.
componentWillUnmount() {
RNIap.endConnectionAndroid();
}
The flow of the
purchase
has been renewed by the founding in issue #307. I've decided to redesign thispurchase flow
not relying on thepromises
orcallback
. There are some reasons not to approach in this way.
- There may be more than one responses when requesting a payment.
- The purchase responses are
asynchronuous
which means request that's made beforehand may not complete at first. - The purchase may be pended and hard to track what has been done (example).
- Billing flow is more like and
events
rather thancallback
pattern.
Once you have called getProducts()
, and you have a valid response, you can call buyProduct()
. 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
.
import RNIap, {
ProductPurchase,
purchaseUpdatedListener,
purchaseErrorListener,
} from 'react-native-iap';
let purchaseUpdateSubscription;
componentDidMount() {
purchaseUpdateSubscription = purchaseUpdatedListener((purchase: ProductPurchase) => {
console.log('purchaseUpdatedListener', purchase);
this.setState({ receipt: purchase.transactionReceipt }, () => this.goNext());
});
purchaseErrorSubscription = purchaseErrorListener((error: PurchaseError) => {
console.log('purchaseErrorListener', error);
Alert.alert('purchase error', JSON.stringify(error));
});
}
componentWillMount() {
if (purchaseUpdateSubscription) {
purchaseUpdateSubscription.remove();
purchaseUpdateSubscription = null;
}
if (purchaseErrorSubscription) {
purchaseErrorSubscription.remove();
purchaseErrorSubscription = null;
}
}
Then define the method like below and call it when user press the button.
requestPurchase = async(sku) => {
try {
RNIap.requestPurchase(sku);
} catch (err) {
console.warn(err.code, err.message);
}
}
requestSubscription = async(sku) => {
try {
RNIap.requestSubscription(sku);
} catch (err) {
Alert.alert(err.message);
}
}
render() {
...
onPress={() => this.requestPurchase(product.productId)}
...
}
Most likely, you'll want to handle the 'store kit flow' (detailed here), 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.
In this scenario, the initial call to RNIap.buyProduct
would fail and you'd need to add addAdditionalSuccessPurchaseListenerIOS
to handle the successful purchase previously. We are planning to remove additionalSuccessPurchaseListenerIOS in future releases so avoid using it. Approach of new purchase flow will prevent such issue in #307 which was privided in 2.4.0+
.
In new android billing client which is 2.0.+
currently, you should acknowledge purchases or else they will be cancelled automatically after 3 days (or 5 minutes in license test environment). See example
project and get idea on how to handle these.
purchaseUpdateSubscription = purchaseUpdatedListener(async(purchase) => {
console.log('purchaseUpdatedListener', purchase);
if (purchase.purchaseStateAndroid === 1 && !purchase.isAcknowledgedAndroid) {
try {
const ackResult = await acknowledgePurchaseAndroid();
console.log('ackResult', ackResult);
} catch (ackErr) {
console.warn('ackErr', ackErr);
}
}
this.setState({ receipt: purchase.transactionReceipt }, () => this.goNext());
});
You can use getAvailablePurchases()
to do what's commonly understood as "restoring" purchases. Once an item is consumed, it will no longer be available in getAvailablePurchases()
and will only be available via getPurchaseHistory()
. However, this method has some caveats on Android -- namely, that purchase history only exists for the single most recent purchase of each SKU -- so your best bet is to track consumption in your app yourself. By default, all items that are purchased will not be consumed unless they are automatically consumed by the store (for example, if you create a consumable item for iOS.) This means that you must manage consumption yourself. Purchases can be consumed by calling consumePurchaseAndroid()
. If you want to consume all items, you have to iterate over the purchases returned by getAvailablePurchases()
.
getPurchases = async() => {
try {
const purchases = await RNIap.getAvailablePurchases();
let restoredTitles = '';
let coins = CoinStore.getCount();
purchases.forEach(purchase => {
if (purchase.productId == 'com.example.premium') {
this.setState({ premium: true });
restoredTitles += 'Premium Version';
} else if (purchase.productId == 'com.example.no_ads') {
this.setState({ ads: false });
restoredTitles += restoredTitles.length > 0 ? 'No Ads' : ', No Ads';
} else if (purchase.productId == 'com.example.coins100') {
CoinStore.addCoins(100);
await RNIap.consumePurchaseAndroid(purchase.purchaseToken);
}
})
Alert.alert('Restore Successful', 'You successfully restored the following purchases: ' + restoredTitles);
} catch(err) {
console.warn(err); // standardized err.code and err.message available
Alert.alert(err.message);
}
}
Returned purchases is an array of each purchase transaction with the following keys:
iOS | Android | Comment | |
---|---|---|---|
productId |
✓ | ✓ | The product ID for the product. |
transactionReceipt |
✓ | ✓ | receipt for ios and stringified JSON of the original purchase object for android. |
transactionId |
✓ | ✓ | A unique order identifier for the transaction. |
transactionDate |
✓ | ✓ | The time the product was purchased, in milliseconds since the epoch (Jan 1, 1970). |
purchaseToken |
✓ | A token that uniquely identifies a purchase for a given item and user pair. | |
autoRenewingAndroid |
✓ | Indicates whether the subscription renews automatically. If true, the subscription is active, and will automatically renew on the next billing date. If false, indicates that the user has canceled the subscription. | |
dataAndroid |
✓ | Original json for purchase data. | |
signatureAndroid |
✓ | String containing the signature of the purchase data that was signed with the private key of the developer. The data signature uses the RSASSA-PKCS1-v1_5 scheme. | |
autoRenewingAndroid |
✓ | boolean checking if subscription is autoRenewing . This won't get returned if this is no subscription purchase. |
|
isAcknowledgedAndroid |
✓ | boolean checking if purhcase has been acknowledged. |
|
purchaseStateAndroid |
✓ | number indicating purchase state in android . |
|
originalTransactionDateIOS |
✓ | For a transaction that restores a previous transaction, the date of the original transaction. | |
originalTransactionIdentifierIOS |
✓ | For a transaction that restores a previous transaction, the transaction identifier of the original transaction. |
You need to test with one sandbox account, because the account holds previous purchase history.
From react-native-iap@0.3.16
, we support receipt validation. For Android, you need separate json file from the service account to get the access_token
from google-apis
, therefore it is impossible to implement serverless. You should have your own backend and get access_token
. With access_token
you can simply call validateReceiptAndroid
method we implemented. Further reading is here.
Currently, serverless receipt validation is possible using validateReceiptIos
method. The first parameter, you should pass transactionReceipt
which returns after buyProduct
. The second parameter, you should pass whether this is test
environment. If true
, it will request to sandbox
and false
it will request to production
.
const receiptBody = {
'receipt-data': purchase.transactionReceipt,
'password': '******'
};
const result = await RNIap.validateReceiptIos(receiptBody, false);
console.log(result);
For further information, please refer to guide.
Sometimes you will need to get the receipt at times other than after purchase. For example, when a user needs to ask for permission to buy a product (Ask to buy
flow) or unstable internet connections. For these cases we have a convenience method requestReceiptIOS
which gets the latest receipt for the app at any given time. The response is base64 encoded.
Issue regarding valid products
- In iOS, generally you are fetching valid products at App launching process. If you fetch again, or fetch valid subscription, the products are added to the array object in iOS side (objective-c NSMutableArray). This makes unexpected behavior when you fetch with a part of product lists. (For example, if you have products of [A, B, C], and you call fetch function with only [A], this module returns [A, B, C]). This is weird, but it works.
- But, weird result is weird, so we made a new method which remove all valid products. If you need to clear all products, subscriptions in that array, just call
clearProducts()
, and do the fetching job again, and you will receive what you expected.
- You could only in
Android
inreact-native-iap
below version 3. However, now you should alwaysfetchProducts
first in both platforms. It is becauseandroid
billingClient has been updatedbillingFlowParams
to include SkuDetails insteadsku
string which is hard to share betweenreact-native
andandroid
. It happened incom.android.billingclient:billing:2.0.+
. Therefore we've planned to store items to be fetched inandroid
before requesting purchase fromreact-native
side. Therefore, you should always fetch list of items topurchase
before requesting purchase.
- Offical doc is here.
- I've developed this feature for other developers to contribute easily who are aware of these things. The doc says you can also get the
accessToken
via play console without any of your backend server. You can get this by following process.- Select your app > Services & APIs > "YOUR LICENSE KEY FOR THIS APPLICATION Base64-encoded RSA public key to include in your binary". reference.
- If you are facing
"You already own this item"
on developer(test) mode you might check related issue #126
- You should detach from
expo
and getexpokit
out of it. - Releated issue in #174.
-
Offical doc is here
-
Start the IAPPromotionObserver in
-[application:didFinishLaunchingWithOptions:]
your AppDelegate:// Add '#import "IAPPromotionObserver.h"' to your imports [IAPPromotionObserver startObserving];
-
Add an event listener for the
iap-promoted-product
event somewhere early in your app's lifecycle:// Import the `NativeModules` and `NativeEventEmitter` components from 'react-native' const { RNIapIos } = NativeModules; const IAPEmitter = new NativeEventEmitter(RNIapIos); IAPEmitter.addListener('iap-promoted-product', async () => { // Check if there's a persisted promoted product const productID = await RNIap.getPromotedProduct(); if (productID !== null) { // You may want to validate the product ID against your own SKUs try { await RNIap.buyPromotedProduct(); // This will trigger the App Store purchase process } catch(e) { console.warn(e); } } });
- Please try below and make sure you've done belows.
- Steps
- Completed an effective "Agreements, Tax, and Banking."
- Setup sandbox testing account in "Users and Roles."
- Signed into iOS device with sandbox account.
- Set up three In-App Purchases with the following status: i. Ready to Submit ii. Missing Metadata iii. Waiting for Review
- Enable "In-App Purchase" in Xcode "Capabilities" and in Apple Developer -> "App ID" setting. Delete app / Restart device / Quit "store" related processes in Activity Monitor / Xcode Development Provisioning Profile -> Clean -> Build.
- Related issues #256 , #263.
- Steps
- The
react-native link
script isn't perfect and sometimes broke. Please tryunlinking
andlinking
again. Or try manual installing.
getAvailablePurchases
is used only when you purchase a non-consumable product. This can be restored only. If you want to find out if a user subscribes the product, you should check the receipt which you should store in your own database. Apple suggests you handle this in your own backend to do things like what you are trying to achieve.
- After you have completed the setup and set your deployment target to iOS 12, FaceID and Touch to purchase will be activated by default in production. Please note that in development or TestFlight, it will NOT use FaceID / Touch to checkout because they are using the Sandbox environment.
react-native
is an open source project with MIT license. We are willing to maintain this repository to support devs to monetize around the world. Since IAP
itself is not perfect on each platform, we desperately need this project to be maintained. If you'd like to help us, please consider being with us in Open Collective.
Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]
Please be our Backers.
Please make sure to read the Contributing Guide before making a pull request. Thank you to all the people who helped to maintain and upgrade this project!