
Security News
TypeScript is Porting Its Compiler to Go for 10x Faster Builds
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
react-native-billing
Advanced tools
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();
}
}
npm install --save react-native-billing
or yarn add react-native-billing
react-native link react-native-billing
With this, the link
command 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
).
npm install --save react-native-billing
android/setting.gradle
...
include ':react-native-billing', ':app'
project(':react-native-billing').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-billing/android')
android/app/build.gradle
...
dependencies {
...
compile project(':react-native-billing')
}
React Native version >= 0.29
Edit MainApplication.java
.
import com.idehub.Billing.InAppBillingBridgePackage;
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
// add package here
new InAppBillingBridgePackage()
);
}
React Native version < 0.29
Edit MainActivity.java
. Step 4.3 is only required if you are using a lower React Native version than 18.0 and/or your MainActivity
class does not inherit from ReactActivity
.
import com.idehub.Billing.InAppBillingBridgePackage;
.addPackage(new InAppBillingBridgePackage())
onActivityResult
:@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
mReactInstanceManager.onActivityResult(requestCode, resultCode, data);
}
Larger example:
// Step 1; import package:
import com.idehub.Billing.InAppBillingBridgePackage;
public class MainActivity extends Activity implements DefaultHardwareBackBtnHandler {
...
@Override
protected void onCreate(Bundle savedInstanceState) {
...
mReactInstanceManager = ReactInstanceManager.builder()
.setApplication(getApplication())
.setBundleAssetName("index.android.bundle")
.setJSMainModuleName("index.android")
.addPackage(new MainReactPackage())
// Step 2; register package
.addPackage(new InAppBillingBridgePackage())
.setUseDeveloperSupport(BuildConfig.DEBUG)
.setInitialLifecycleState(LifecycleState.RESUMED)
.build();
...
}
// Step 3: For RN < v0.18, override onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
mReactInstanceManager.onActivityResult(requestCode, resultCode, data);
}
...
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")
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 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:
getProductDetails
function to see if it's the product is retrieved.applicationId
(normally your package name) and versionCode
set in android/app/build.gradle
.Important: You can only test on a physical Android device, not from an emulator.
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.
All methods return a Promise
.
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"));
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();
});
Refreshes the internal purchases & subscriptions status cache.
InAppBilling.loadOwnedPurchasesFromGoogle().then(...);
InAppBilling.purchase("android.test.purchased").then(details => {
console.log(details);
});
InAppBilling.consumePurchase('your.inapp.productid').then(...);
InAppBilling.subscribe("your.inapp.productid").then(details => {
console.log(details);
});
InAppBilling.isSubscribed('your.inapp.productid').then(...);
InAppBilling.updateSubscription(['subscription.p1m', 'subscription.p3m'], 'subscription.p12m').then(...)
InAppBilling.isPurchased('your.inapp.productid').then(...);
InAppBilling.isOneTimePurchaseSupported().then(...);
Validates if the transaction for the productId has a valid signature.
InAppBilling.isValidTransactionDetails("your.inapp.productid").then(isValid => {
console.log(isValid);
});
InAppBilling.listOwnedProducts().then(...);
InAppBilling.listOwnedSubscriptions().then(...);
Important: Use this to query managed products. Subscriptions require the use of getSubscriptionDetails
.
InAppBilling.getProductDetails('your.inapp.productid').then(...);
InAppBilling.getProductDetailsArray(['your.inapp.productid', 'your.inapp.productid2']).then(...);
InAppBilling.getSubscriptionDetails('your.inapp.productid').then(...);
InAppBilling.getSubscriptionDetailsArray(['your.inapp.productid', 'your.inapp.productid2']).then(...);
InAppBilling.getPurchaseTransactionDetails("your.inapp.productid").then(
details => {
console.log(details);
}
);
InAppBilling.getSubscriptionTransactionDetails("your.inapp.productid").then(
details => {
console.log(details);
}
);
FAQs
React Native bridge for InApp Billing on Android
The npm package react-native-billing receives a total of 103 weekly downloads. As such, react-native-billing popularity was classified as not popular.
We found that react-native-billing demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.