React Native Custom Billing is built to provide an easy interface to InApp Billing for CafeBazaar/Myket/IranApps/Google (Android),
$ npm install react-native-custom-billing --save
Mostly automatic installation (for React Native <= 0.59 only, React Native >= 0.60 skip this as auto-linking should work)
$ react-native link react-native-custom-billing
- Open up
android/app/src/main/java/[...]/MainApplication.java
- Add
import com.customBilling.reactnative.RNCustomBillingPackage;
to the imports at the top of the file - Add
new RNCustomBillingPackage()
to the list returned by thegetPackages()
method
- Append the following lines to
android/settings.gradle
:include ':react-native-custom-billing' project(':react-native-custom-billing').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-custom-billing/android')
- Insert the following lines inside the dependencies block in
android/app/build.gradle
:compile project(':react-native-custom-billing')
- Add your Vendor Name (CafeBazaar/Myket/IranApps/Google) as a line to your
android/app/src/main/res/values/strings.xml
with the nameRNCB_VENDOR_NAME
. For example:
<string name="RNCB_VENDOR_NAME">bazaar</string>
or
<string name="RNCB_VENDOR_NAME">myket</string>
or
<string name="RNCB_VENDOR_NAME">google</string>
or
<string name="RNCB_VENDOR_NAME">iranapps</string>
- Add your Vendor Public key as a line to your
android/app/src/main/res/values/strings.xml
with the nameRNCB_VENDOR_PUBLIC_KEY
. For example:
<string name="RNCB_VENDOR_PUBLIC_KEY">YOUR_CAFE_BAZAAR_PUBLIC_KEY</string>
or
<string name="RNCB_VENDOR_PUBLIC_KEY">YOUR_MYKET_PUBLIC_KEY</string>
or
<string name="RNCB_VENDOR_PUBLIC_KEY">YOUR_IRAN_APPS_PUBLIC_KEY</string>
or
<string name="RNCB_VENDOR_PUBLIC_KEY">YOUR_GOOGLE_PUBLIC_KEY</string>
- Add the billing permission as follows to AndroidManifest.xml file based on your Vendor (CafeBazaar/Myket/Google):
Cafe Bazaar: <uses-permission android:name="com.farsitel.bazaar.permission.PAY_THROUGH_BAZAAR"></uses-permission>
or
Myket: <uses-permission android:name="ir.mservices.market.BILLING"></uses-permission>
or
Iran Apps: <uses-permission android:name="ir.tgbs.iranapps.permission.BILLING"/>
or
Google: <uses-permission android:name="com.android.vending.BILLING"></uses-permission>
Most of methods returns a Promise
.
Important: Opens the service channel to RNCustomBilling. Must be called (once!) before any other billing methods can be called.
import RNCustomBilling from 'react-native-custom-billing'
...
RNCustomBilling.open()
.then(() => RNCustomBilling.purchase('YOUR_SKU','DEVELOPER_PAYLOAD',RC_REQUEST))
.catch(err => console.log('Vendor err:', err))
Important: Must be called to close the service channel to RNCustomBilling, when you are done doing billing related work. Failure to close the service channel may degrade the performance of your app.
RNCustomBilling.open()
.then(() => RNCustomBilling.purchase('YOUR_SKU','DEVELOPER_PAYLOAD',RC_REQUEST))
.then((details) => {
console.log(details)
return RNCustomBilling.close()
})
.catch(err => console.log('Vendor err:', err))
- productSKU (required): String
- developerPayload: String
- rcRequest: Integer
- Details: JSONObject:
- mDeveloperPayload:
- mItemType:
- mOrderId:
- mOriginalJson:
- mPackageName:
- mPurchaseState:
- mPurchaseTime:
- mSignature:
- mSku:
- mToken:
RNCustomBilling.purchase('YOUR_SKU','DEVELOPER_PAYLOAD',RC_REQUEST)
.then((details) => {
console.log(details)
})
.catch(err => console.log('RNCustomBilling err:', err))
- productSKU (required): String
- Details: JSONObject:
- mDeveloperPayload:
- mItemType:
- mOrderId:
- mOriginalJson:
- mPackageName:
- mPurchaseState:
- mPurchaseTime:
- mSignature:
- mSku:
- mToken:
RNCustomBilling.consume('YOUR_SKU')
.then(...)
.catch(err => console.log('Vendor err:', err))
- items: JSONArray:
RNCustomBilling.loadOwnedItems()
.then((details) => {
console.log(details)
})
.catch(err => console.log('Vendor err:', err))
- productSKUs (required): Array
- mPurchaseMap: JSONObject
- mSkuMap: JSONObject
RNCustomBilling.loadInventory([])
.then(...)
.catch(err => console.log('Vendor err:', err))
Below function dispatch Event instead of Promise and return value is same.
import {DeviceEventEmitter} from 'react-native';
...
componentDidMount(){
DeviceEventEmitter.addListener('Vendor', function(e: Event) {
// handle event.
console.log(e);
});
}