Quick Steps Bazaar In-App Billing Easy Guide

The objective of this tutorial is to explain the least steps required to implement Bazaar in-app billing  (IAB) in your Android app. If you want to do more complicated tasks and know more about in-app billing process, you can read other provided documents.

1: Example Android App for In-app Billing

You can use TrivialDrive example app, which is made by Google, to understand the basics of in-app billing and to copy some util files from it into Eclipse (see next step).

In order to make this example applicable for Bazaar, we changed 2 lines of this example. You can see TrivialDrive's source code here or download it here. Also you can view our 2 lines changes in this commit.

2: Adding the IInAppBillingService.aidl and Util Files to the Project

Add IInAppBillingService.aidl file and util folder from the TrivialDrive example to your project.

Note: You might need to change package name of files inside util package based on their new location.
Migrating from Google Play: If you have already implemented Google Play in-app billing in your app, instead of this step, you can just edit IabHelper.java class as you can see in this commit.

After completing this step, your folder structure in Eclipse should look like this:

Folder Structure in Eclipse

3: Adding Bazaar Billing Permission to the Manifest File

In order to support in-app billing in your Android app, a new permission line must be added to the project’s AndroidManifest.xml file. Add the billing permission as follows to AndroidManifest.xml file:

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

4: Obtaining the Public Key (RSA Key) for the Application

Go to CafeBazaar's developers' panel. Upload your apk, but don’t submit app review request. 

Go to Dealer Apps page. By clicking on the RSA Key in the Actions tab, Base64-encoded RSA public key for the application will be shown. Save it for step 7.

5: Products

In developers' panel under the Applications tab, click on the Enter button related to your app. Go to Products/Services page. Add new product with unique Sku. Remember this Sku for step 6.

6: Variables

Before calling onCreate method of seller activity (activity which does in-app billing) add below lines and  fill them with your app specific values.

// Debug tag, for logging
static final String TAG = "";

// SKUs for our products: the premium upgrade (non-consumable)
static final String PRODUCT_SKU = "";

// (arbitrary) request code for the purchase flow
static final int RC_REQUEST = ;

// The helper object
IabHelper mHelper;

7: Inside onCreate Method

In the onCreate of your seller activity, add the following lines:

String base64EncodedPublicKey = "";
// You can find it in your Bazaar console, in the Dealers section. 
// It is recommended to add more security than just pasting it in your source code;
mHelper = new IabHelper(this, base64EncodedPublicKey);

Log.d(TAG, "Starting setup.");
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
    public void onIabSetupFinished(IabResult result) {
        Log.d(TAG, "Setup finished.");

        if (!result.isSuccess()) {
            // Oh noes, there was a problem.
            Log.d(TAG, "Problem setting up In-app Billing: " + result);
        // Hooray, IAB is fully set up!

8: Listeners

Codes inside util handle the common tasks of in-app billing for all apps. You as a developer need to have your app specific listeners to handle different events related to in-app billing.

Below is an example implementation for handling Bazaar's response to not consumed purchased products of user and also for handling finished purchase event.

IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
    public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
        Log.d(TAG, "Query inventory finished.");
        if (result.isFailure()) {
            Log.d(TAG, "Failed to query inventory: " + result);
        else {
            Log.d(TAG, "Query inventory was successful.");
            // does the user have the premium upgrade?
            mIsPremium = inventory.hasPurchase(SKU_PREMIUM);

            // update UI accordingly

            Log.d(TAG, "User is " + (mIsPremium ? "PREMIUM" : "NOT PREMIUM"));

        Log.d(TAG, "Initial inventory query finished; enabling main UI.");

IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
    public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
        if (result.isFailure()) {
            Log.d(TAG, "Error purchasing: " + result);
        else if (purchase.getSku().equals(SKU_PREMIUM)) {
            // give user access to premium content and update the UI

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data);

    // Pass on the activity result to the helper for handling
    if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
        super.onActivityResult(requestCode, resultCode, data);
    } else {
        Log.d(TAG, "onActivityResult handled by IABUtil.");

9: Purchasing Product

Below code will direct user to Bazaar's purchase dialog. After finishing purchase flow you will be informed by mPurchaseFinishedListener listener.

mHelper.launchPurchaseFlow(this, PRODUCT_SKU, RC_REQUEST, mPurchaseFinishedListener, "payload-string");

10: onDestroy

Unbind the service in onDestroy:

public void onDestroy() {
    if (mHelper != null) mHelper.dispose();
    mHelper = null;

You’re done!