Purchase Nonconsumable Items
The Premium Car feature of this application is a nonconsumable item. This means that this item can be purchased only once. The premium car does not expire and can be reused an unlimited number of times.
- In the Trivial Drive UI, click UPGRADE MY CAR (NC).
- The button click invokes the function
onUpgradeAppButtonClicked()
ofMainActivity.java
, which callsOpenIabHelper.launchPurchaseFlow()
.InAppConfig.SKU_PREMIUM
is specified as a parameter.
MainActivity.java public void onUpgradeAppButtonClicked(View arg0) { Log.d(TAG, "Upgrade button clicked; launching purchase flow for upgrade."); if (setupDone == null) { complain("Billing Setup is not completed yet"); return; } if (!setupDone) { complain("Billing Setup failed"); return; } setWaitScreen(true); /* TO DO: For security, generate your payload here for verification. See the comments on verifyDeveloperPayload() for more info. Since this is a SAMPLE, we just use an empty string. But on a production app, you should carefully generate this.*/ String payload = " "; mHelper.launchPurchaseFlow(this, InAppConfig.SKU_PREMIUM, RC_REQUEST, mPurchaseFinishedListener, payload); }
OpenIabHelper.launchPurchaseFlow()
calls thelaunchPurchaseFlow()
function ofAppStoreInAppBillingService.java
.
OpenIabHelper.java public void launchPurchaseFlow(Activity act, @NotNull String sku, int requestCode, IabHelper.OnIabPurchaseFinishedListener listener, String extraData) { Logger.d("Purchase flow for INAPP Item started"); launchPurchaseFlow(act, sku, ITEM_TYPE_INAPP, requestCode, listener, extraData); }
OpenIabHelper.java public void launchPurchaseFlow(Activity act, @NotNull String sku, String itemType, int requestCode, IabHelper.OnIabPurchaseFinishedListener listener, String extraData) { checkSetupDone("launchPurchaseFlow"); appStoreBillingService.launchPurchaseFlow(act, SkuManager.getInstance().getStoreSku(appstore.getAppStoreName(), sku), itemType, requestCode, listener, extraData); }
AppStoreInAppBillingService.launchPurchaseFlow()
is invoked, which starts the billing service for the chosen app store. For Samsung,SamsungAppsBillingService.launchPurchaseFlow()
is invoked.
SamsungAppsBillingService.java public void launchPurchaseFlow(@NotNull Activity activity, @NotNull String sku, String itemType, int requestCode, OnIabPurchaseFinishedListener listener, String extraData) { String itemGroupId = getItemGroupId(sku); String itemId = getItemId(sku); Bundle bundle = new Bundle(); bundle.putString(KEY_NAME_THIRD_PARTY_NAME, activity.getPackageName()); /* If 12-digit itemGroupId is specified in SKU mapping, bundle will include itemGroupId. If itemGroupId is not specified, third-party package name will be used. (Samsung IAP v3.0) */ if (!itemGroupId.isEmpty()) { bundle.putString(KEY_NAME_ITEM_GROUP_ID, itemGroupId); } bundle.putString(KEY_NAME_ITEM_ID, itemId); Logger.d("launchPurchase: itemGroupId = ", itemGroupId, ", itemId = ", itemId); ComponentName cmpName = new ComponentName(SamsungApps.IAP_PACKAGE_NAME, PAYMENT_ACTIVITY_NAME); Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); intent.setComponent(cmpName); intent.putExtras(bundle); mRequestCode = requestCode; mPurchaseListener = listener; purchasingItemType = itemType; mItemGroupId = itemGroupId; mExtraData = extraData; Logger.d("Request code: ", requestCode); activity.startActivityForResult(intent, requestCode); }
- After a successful purchase, the result is sent back to
MainActivity.java
through theIabHelper.OnIabPurchaseFinishedListener
.
MainActivity.java IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() { public void onIabPurchaseFinshed(IabResult result, Purchase purchase) { ... else if (purchase.getSku().equals(InAppConfig.SKU_PREMIUM)) { // Bought the premium upgrade! Log.d(TAG, "Purchase is premium upgrade. Congratulating user."); alert(R.string.premium_msg); mIsPremium = true; updateUi(); setWaitScreen(false); } } };