Skip to main content

Ready to market your stellar app?

Samsung Developer Program is your gateway to app monetization success.

Learn More

Samsung Developer Program

Purchasing Commercial and Consumable Items

Purchasing Commercial Items

OpenIABLibrary_Integration_PurchasingCommercialIems_Diagram.png

 

Purchasing Consumable Items

The Buy Gas feature of this application is of a consumable type. Once the gas runs out, you will need to repurchase. Multiple purchases can be done with this type of commercial item. 

 

Purchasing a consumable item
  1. In the Trivial Drive UI, tap BUY GAS (C).
  2. The button tap invokes the function onBuyGasButtonClicked() of MainActivity.java, which calls OpenIabHelper.launchPurchaseFlow(). InAppConfig.SKU_GAS is specified as a parameter.
MainActivity.java
public void onBuyGasButtonClicked(View arg0) {
    Log.d(TAG, "Buy gas button clicked.");
    
    if (setupDone == null) {
        complain("Billing Setup is not completed yet");
        return;
    }
    
    if (!setupDone) {
        complain("Billing Setup failed");
        return;
    }
    
    // launch the gas purchase UI flow
    // We will be notified of completion via mPurchaseFinishedListener
    
    setWaitScreen(true);
    Log.d(TAG, "Launching purchase flow for gas.");
    
    /* TODO: 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_GAS, RC_REQUEST, mPurchaseFinishedListener, payload);
}

 

  1. OpenIabHelper.launchPurchaseFlow() calls the launchPurchaseFlow() function of AppStoreInAppBillingService.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);
}

 

  1. 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);
    Itent 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);
}

 

  1.  After a successful purchase, the result is sent back to MainActivity.java via the IabHelper.OnIabPurchaseFinishedListener.
MainActivity.java
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
    public void onIabPurchaseFinished(IabResult iabResult, Purchase purchase) {
        ...
        if (purchase.getSku().equals(InAppConfig.SKU_GAS)) {
                // Bought 1/4 tank of gas. So consume it.
            Log.d(TAG, "Purchase is gas. Starting gas consumption.");
            mHelper.consumeAsync(purchase, mConsumeFinishedListener);
        }
        ...
    }
};    

  • Was this article helpful?