Skip to content

Latest commit

 

History

History
206 lines (163 loc) · 5.37 KB

README.md

File metadata and controls

206 lines (163 loc) · 5.37 KB

InApp Billing for Cafe Bazaar (Android)

React Native Cafe Bazaar is built to provide an easy interface to InApp Billing for Cafe Bazaar,

⚠️ Notice: This library is not being actively updated. You might want to use the RN library of the cafebazaar itself.

Installation

  1. npm install --save react-native-cafe-bazaar or yarn add react-native-cafe-bazaar
  2. Add the following in android/settings.gradle
...
include ':react-native-cafe-bazaar', ':app'
project(':react-native-cafe-bazaar').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-cafe-bazaar/android/app')
  1. And the following in android/app/build.gradle
...
dependencies {
    ...
    implementation project(':react-native-cafe-bazaar')
}
  1. Update MainActivity or MainApplication depending on React Native version.
  • React Native version >= 0.29 Edit MainApplication.java.
    1. Add import com.contoriel.cafebazaar.CafeBazaarPackage; in the top of the file.
    2. Register package:
    @Override
    protected List<ReactPackage> getPackages() {
      return Arrays.<ReactPackage>asList(
        new MainReactPackage(),
        // add package here
        new CafeBazaarPackage()
      );
    }
  1. Add your CafeBazaar Public key as a line to your android/app/src/main/res/values/strings.xml with the name CAFE_BAZAAR_PUBLIC_KEY. For example:
<string name="CAFE_BAZAAR_PUBLIC_KEY">YOUR_CAFE_BAZAAR_PUBLIC_KEY_HERE</string>

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

.addPackage(new CafeBazaarPackage("YOUR_CAFE_BAZAAR_PUBLIC_KEY_HERE"))

or for React Native 29+

new CafeBazaarPackage("YOUR_CAFE_BAZAAR_PUBLIC_KEY_HERE")

Add the billing permission as follows to AndroidManifest.xml file:

<uses-permission android:name="com.farsitel.bazaar.permission.PAY_THROUGH_BAZAAR"></uses-permission>

ON THE BAZAAR DEVELOPER PANEL

  1. Upload the application to the Developer Panel.
  1. Using the Enter button in In-app Billing column of the created app, go to In-app Billing Panel.

  2. In that app, create your in-app items

  3. Grab the application's public key (a base-64 string) You can find the application's public key in the Dealer Apps page for your application.

Javascript API

Most of methods returns a Promise.

open()

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

import CafeBazaar from 'react-native-cafe-bazaar'

...

CafeBazaar.open()
.then(() => CafeBazaar.purchase('YOUR_SKU','DEVELOPER_PAYLOAD',RC_REQUEST))
.catch(err => console.log('CafeBazaar err:', err))

close()

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

CafeBazaar.open()
.then(() => CafeBazaar.purchase('YOUR_SKU','DEVELOPER_PAYLOAD',RC_REQUEST))
.then((details) => {
  console.log(details)
  return CafeBazaar.close()
})
.catch(err => console.log('CafeBazaar err:', err))

purchase('YOUR_SKU','DEVELOPER_PAYLOAD',RC_REQUEST)

Parameter(s)
  • productSKU (required): String
  • developerPayload: String
  • rcRequest: Integer
Returns:
  • Details: JSONObject:
    • mDeveloperPayload:
    • mItemType:
    • mOrderId:
    • mOriginalJson:
    • mPackageName:
    • mPurchaseState:
    • mPurchaseTime:
    • mSignature:
    • mSku:
    • mToken:
CafeBazaar.purchase('YOUR_SKU','DEVELOPER_PAYLOAD',RC_REQUEST)
.then((details) => {
  console.log(details)
})
.catch(err => console.log('CafeBazaar err:', err))

consume('YOUR_SKU')

Parameter(s)
  • productSKU (required): String
Returns:
  • Details: JSONObject:
    • mDeveloperPayload:
    • mItemType:
    • mOrderId:
    • mOriginalJson:
    • mPackageName:
    • mPurchaseState:
    • mPurchaseTime:
    • mSignature:
    • mSku:
    • mToken:
CafeBazaar.consume('YOUR_SKU')
.then(...)
.catch(err => console.log('CafeBazaar err:', err))

loadOwnedItems()

Returns:
  • items: JSONArray:
CafeBazaar.loadOwnedItems()
.then((details) => {
  console.log(details)
})
.catch(err => console.log('CafeBazaar err:', err))

loadInventory([item1_SKU,item2_SKU,...])

Parameter(s)
  • productSKUs (required): Array
Returns:
  • mPurchaseMap: JSONObject
  • mSkuMap: JSONObject
CafeBazaar.loadInventory([])
.then(...)
.catch(err => console.log('CafeBazaar err:', err))

Use event listener

Below function dispatch Event instead of Promise and return value is same.

purchaseWithEvent('YOUR_SKU','DEVELOPER_PAYLOAD',RC_REQUEST)

consumeWithEvent('YOUR_SKU')

loadOwnedItemsWithEvent()

import {DeviceEventEmitter} from 'react-native';
...
  componentDidMount(){
    DeviceEventEmitter.addListener('CafeBazaar', function(e: Event) {
      // handle event.
      console.log(e);
    });
  }

BACK TO BAZAAR DEVELOPER PANEL

Upload the updated APK to Bazaar Developer Panel