-
Sell digital content in Android through Google Play In-app Billing Service Version 3
about 10 years ago
about 10 years ago
Products which you can sell in your application falls into these categories :
1.Standard in-app products(one time billing) eg: Premium Upgrade, Ads free version of the app. 2.Subscription(recurring,automated billing) eg: Game currency, music voucher.
Install Google Play Billing library
If you are using non-Eclipse IDE:
a. Create the following directory /src/com/android/vending/billing and copy the
InAppBillingService.aidl file into this directory.
a. String PURCHASE_PRODUCT_ID is the product unique id (i.e SKU ) which is shown in the in_app products tab in your Google Play Developer console account.
b. String base64EncodedPublicKey is a public license key which is a base64 string which can be found in the Services and Apis tab in your Google Play developer Console account.
- IabHelper mHelper;
- final String PURCHASE_PRODUCT_ID = "YOUR_PRODUCT_UNIQUE_ID";
- /*or, you can also use this
- final String PURCHASE_PRODUCT_ID = "android.test.purchased";
- for testing purposes
- */
- int PURCHASE_REQUEST_CODE = 1001;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_in_app_billing);
- String base64EncodedPublicKey = "YOUR_PUBLIC_LICENSE_KEY";
- // compute your public key and store it in base64EncodedPublicKey
- //
- mHelper = new IabHelper(this, base64EncodedPublicKey);
- mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
- public void onIabSetupFinished(IabResult result) {
- if (!result.isSuccess()) {
- // Oh noes, there was a problem.
- Log.d(TAG, "Problem setting up In-app Billing: " + result);
- mHelper=null;
- // Setup failed !! do what you want to do next.
- }
- else {
- // Hooray, IAB is fully set up!
- Log.d(TAG, "Hooray, IAB is fully set up!");
- List additionalSkuList = new ArrayList();
- additionalSkuList.add(PURCHASE_PRODUCT_ID);
- mHelper.queryInventoryAsync(true, additionalSkuList, mQueryFinishedListener);
- }
- }
- });
- }
- IabHelper.QueryInventoryFinishedListener
- mQueryFinishedListener = new IabHelper.QueryInventoryFinishedListener() {
- public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
- if (result.isFailure()) {
- // handle error
- return;
- } else {
- String price =
- inventory.getSkuDetails(PURCHASE_PRODUCT_ID).getPrice();
- String title =
- inventory.getSkuDetails(PURCHASE_PRODUCT_ID).getTitle();
- boolean isPurchased = inventory.hasPurchase(PURCHASE_PRODUCT_ID);
- // update the UI
- Log.d(TAG, "product price = " + price + " title = " + title);
- if (!isPurchased) {
- mHelper.launchPurchaseFlow(InAppBillingActivity.this, PURCHASE_PRODUCT_ID, PURCHASE_REQUEST_CODE,
- mPurchaseFinishedListener, "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ");
- } else {
- //product is already purchased
- 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(PURCHASE_PRODUCT_ID)) {
- MyUtility.setAdsFreeVersion(InAppBillingActivity.this, true);
- Log.d(TAG, "user has purchased " + purchase.getSku());
- }
- }
- };
- @Override
- public void onDestroy() {
- super.onDestroy();
- //unbind from the In-app Billing service when you are done with your activity
- if (mHelper != null) mHelper.dispose();
- mHelper = null;
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data);
- if (mHelper == null) return;
- // Pass on the activity result to the helper for handling
- if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
- // not handled, so handle it ourselves (here's where you'd
- // perform any handling of activity results not related to in-app
- // billing...
- super.onActivityResult(requestCode, resultCode, data);
- } else {
- Log.d(TAG, "onActivityResult handled by IABUtil.");
- }
- }
IabHelper mHelper; final String PURCHASE_PRODUCT_ID = "YOUR_PRODUCT_UNIQUE_ID"; /*or, you can also use this final String PURCHASE_PRODUCT_ID = "android.test.purchased"; for testing purposes */ int PURCHASE_REQUEST_CODE = 1001; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_in_app_billing); String base64EncodedPublicKey = "YOUR_PUBLIC_LICENSE_KEY"; // compute your public key and store it in base64EncodedPublicKey // mHelper = new IabHelper(this, base64EncodedPublicKey); mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() { public void onIabSetupFinished(IabResult result) { if (!result.isSuccess()) { // Oh noes, there was a problem. Log.d(TAG, "Problem setting up In-app Billing: " + result); mHelper=null; // Setup failed !! do what you want to do next. } else { // Hooray, IAB is fully set up! Log.d(TAG, "Hooray, IAB is fully set up!"); List additionalSkuList = new ArrayList(); additionalSkuList.add(PURCHASE_PRODUCT_ID); mHelper.queryInventoryAsync(true, additionalSkuList, mQueryFinishedListener); } } }); } IabHelper.QueryInventoryFinishedListener mQueryFinishedListener = new IabHelper.QueryInventoryFinishedListener() { public void onQueryInventoryFinished(IabResult result, Inventory inventory) { if (result.isFailure()) { // handle error return; } else { String price = inventory.getSkuDetails(PURCHASE_PRODUCT_ID).getPrice(); String title = inventory.getSkuDetails(PURCHASE_PRODUCT_ID).getTitle(); boolean isPurchased = inventory.hasPurchase(PURCHASE_PRODUCT_ID); // update the UI Log.d(TAG, "product price = " + price + " title = " + title); if (!isPurchased) { mHelper.launchPurchaseFlow(InAppBillingActivity.this, PURCHASE_PRODUCT_ID, PURCHASE_REQUEST_CODE, mPurchaseFinishedListener, "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ"); } else { //product is already purchased 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(PURCHASE_PRODUCT_ID)) { MyUtility.setAdsFreeVersion(InAppBillingActivity.this, true); Log.d(TAG, "user has purchased " + purchase.getSku()); } } }; @Override public void onDestroy() { super.onDestroy(); //unbind from the In-app Billing service when you are done with your activity if (mHelper != null) mHelper.dispose(); mHelper = null; } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data); if (mHelper == null) return; // Pass on the activity result to the helper for handling if (!mHelper.handleActivityResult(requestCode, resultCode, data)) { // not handled, so handle it ourselves (here's where you'd // perform any handling of activity results not related to in-app // billing... super.onActivityResult(requestCode, resultCode, data); } else { Log.d(TAG, "onActivityResult handled by IABUtil."); } }
You have successfully implemented In-app billing service.
To Test In-app billing service click here
Note: We have not implemented how to consume a purchased product in this blog.
0 Comment(s)