Home

Awesome

InApp Billing for Android npm version Build Status

React Native Billing is built to provide an easy interface to InApp Billing on Android, accomplished by wrapping anjlab's InApp Billing library.

import InAppBilling from "react-native-billing";

async purchase() {
  try {
    await InAppBilling.open();
    const details = await InAppBilling.purchase("android.test.purchased");
    console.log("You purchased: ", details);
  } catch (err) {
    console.log(err);
  } finally {
    await InAppBilling.close();
  }
}

async checkSubscription() {
    try {
    await InAppBilling.open();
    // If subscriptions/products are updated server-side you
    // will have to update cache with loadOwnedPurchasesFromGoogle()
    await InAppBilling.loadOwnedPurchasesFromGoogle();
    const isSubscribed = await InAppBilling.isSubscribed("myapp.productId")
    console.log("Customer subscribed: ", isSubscribed);
  } catch (err) {
    console.log(err);
  } finally {
    await InAppBilling.close();
  }
}

Installation and linking

  1. npm install --save react-native-billing or yarn add react-native-billing
  2. react-native link react-native-billing

With this, the linkcommand will do most of the heavy lifting for native linking. But, you will still need add your Google Play license key to the strings.xml (step 5). If you are using a React Native version less than v0.18 you will also have to do step 4.3 (override onActivityResult).

Manual installation Android

  1. npm install --save react-native-billing
  2. Add the following in android/setting.gradle
...
include ':react-native-billing', ':app'
project(':react-native-billing').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-billing/android')
  1. And the following in android/app/build.gradle
...
dependencies {
    ...
    compile project(':react-native-billing')
}
  1. Update MainActivity or MainApplication depending on React Native version.
  1. Add your Google Play license key as a line to your android/app/src/main/res/values/strings.xml with the name RNB_GOOGLE_PLAY_LICENSE_KEY. For example:
<string name="RNB_GOOGLE_PLAY_LICENSE_KEY">YOUR_GOOGLE_PLAY_LICENSE_KEY_HERE</string>

Alternatively, you can add your license key as a parameter when registering the InAppBillingBridgePackage, like so:

.addPackage(new InAppBillingBridgePackage("YOUR_LICENSE_KEY"))

or for React Native 29+

new InAppBillingBridgePackage("YOUR_LICENSE_KEY")

Testing with static responses

If you want to test with static responses, you can use reserved productids defined by Google. These are:

If you want to test with these productids, you will have to use a null license key. This is because your actual license key will not validate when using these productids.

In order to do this send in null as parameter, along with your Activity-instance, when registering the package: .addPackage(new InAppBillingBridgePackage(null, this))

See the Google Play docs for more info on static responses.

For instance to purchase and consume the static android.test.purchased products, with async/await (you can chain the promise) :

// To be sure the service is close before opening it
async pay() {
  await InAppBilling.close();
  try {
    await InAppBilling.open();
    if (!await InAppBilling.isPurchased(productId)) {
      const details = await InAppBilling.purchase(productId);
      console.log('You purchased: ', details);
    }
    const transactionStatus = await InAppBilling.getPurchaseTransactionDetails(productId);
    console.log('Transaction Status', transactionStatus);
    const productDetails = await InAppBilling.getProductDetails(productId);
    console.log(productDetails);
  } catch (err) {
    console.log(err);
  } finally {
    await InAppBilling.consumePurchase(productId);
    await InAppBilling.close();
  }
}

Testing with your own In-app products

Testing with static responses is limited, because you are only able to test the purchase function. Therefore, testing with real In-app products is recommended. But before that is possible, you need to do the following:

Important: You can only test on a physical Android device, not from an emulator.

Handle Canceled Subscriptions

Call InAppBilling.getSubscriptionTransactionDetails(productId) and check the details.autoRenewing flag. It will be set to false once subscription gets cancelled. Also notice, that you will need to call periodically InAppBilling.loadOwnedPurchasesFromGoogle() method in order to update purchase/subscription information from the Google-servers.

Javascript API

All methods return a Promise.

open()

Important: Opens the service channel to Google Play. Must be called (once!) before any other billing methods can be called.

InAppBilling.open().then(() => InAppBilling.purchase("android.test.purchased"));

close()

Important: Must be called to close the service channel to Google Play, when you are done doing billing related work. Failure to close the service channel may degrade the performance of your app.

InAppBilling.open()
  .then(() => InAppBilling.purchase("android.test.purchased"))
  .then(details => {
    console.log("You purchased: ", details);
    return InAppBilling.close();
  });

loadOwnedPurchasesFromGoogle()

Refreshes the internal purchases & subscriptions status cache.

InAppBilling.loadOwnedPurchasesFromGoogle().then(...);

purchase(productId)

Parameter(s)
Returns:
InAppBilling.purchase("android.test.purchased").then(details => {
  console.log(details);
});

consumePurchase(productId)

Parameter(s)
Returns:
InAppBilling.consumePurchase('your.inapp.productid').then(...);

subscribe(productId)

Parameter(s)
Returns:
InAppBilling.subscribe("your.inapp.productid").then(details => {
  console.log(details);
});

isSubscribed(productId)

Parameter(s)
Returns:
InAppBilling.isSubscribed('your.inapp.productid').then(...);

updateSubscription(oldProductIds, productId)

Parameter(s)
Returns:
InAppBilling.updateSubscription(['subscription.p1m', 'subscription.p3m'], 'subscription.p12m').then(...)

isPurchased(productId)

Parameter(s)
Returns:
InAppBilling.isPurchased('your.inapp.productid').then(...);

isOneTimePurchaseSupported()

Returns:
InAppBilling.isOneTimePurchaseSupported().then(...);

isValidTransactionDetails(productId)

Validates if the transaction for the productId has a valid signature.

Parameter(s)
Returns:
InAppBilling.isValidTransactionDetails("your.inapp.productid").then(isValid => {
  console.log(isValid);
});

listOwnedProducts()

Returns:
InAppBilling.listOwnedProducts().then(...);

listOwnedSubscriptions()

Returns:
InAppBilling.listOwnedSubscriptions().then(...);

getProductDetails(productId)

Important: Use this to query managed products. Subscriptions require the use of getSubscriptionDetails.

Parameter(s)
Returns:
InAppBilling.getProductDetails('your.inapp.productid').then(...);

getProductDetailsArray(productIds)

Parameter(s)
Returns:
InAppBilling.getProductDetailsArray(['your.inapp.productid', 'your.inapp.productid2']).then(...);

getSubscriptionDetails(productId)

Parameter(s)
Returns:
InAppBilling.getSubscriptionDetails('your.inapp.productid').then(...);

getSubscriptionDetailsArray(productIds)

Parameter(s)
Returns:
InAppBilling.getSubscriptionDetailsArray(['your.inapp.productid', 'your.inapp.productid2']).then(...);

getPurchaseTransactionDetails(productId)

Parameter(s)
Returns:
InAppBilling.getPurchaseTransactionDetails("your.inapp.productid").then(
  details => {
    console.log(details);
  }
);

getSubscriptionTransactionDetails(productId)

Parameter(s)
Returns:
InAppBilling.getSubscriptionTransactionDetails("your.inapp.productid").then(
  details => {
    console.log(details);
  }
);