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.
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.
Add IInAppBillingService.aidl file and util
folder from the TrivialDrive example to your project.
After completing this step, your folder structure in Eclipse should look like this:
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>
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.
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.
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;
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! mHelper.queryInventoryAsync(mGotInventoryListener); } });
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); return; } 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); return; } else if (purchase.getSku().equals(SKU_PREMIUM)) { // give user access to premium content and update the UI } } }; @Override 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."); } }
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");
Unbind the service in onDestroy:
@Override public void onDestroy() { super.onDestroy(); if (mHelper != null) mHelper.dispose(); mHelper = null; }
You’re done!