Skip to main content
Samsung Developer Program

Purchasing Commercial Items

After getting the currently available commercial items and their related data, for example, current price, your service app can present the items to the end-user for purchase. Item presentation can also be influenced by getting the list of previously user-purchased items.

Service code must integrate OpenIAB library API calls that is specific to the type of item the user wants to purchase.

Purchasing Consumable Items

The Buy Fuel feature of this application is a consumable item. Once the fuel runs out, you must repurchase. You can make multiple purchases of consumable items. 

OpenIAB_Integration_PurchaseCommercialItems_APICallSequence.png

To purchase a consumable item:
  1. In the City Flyer UI, click Buy Fuel.
  2. The button click invokes the function BuyFuel() of Shop.cs, which calls OpenIAB.purchaseProduct() and passes the SKU of the item (SKU_PLANE_FUEL).
Shop.cs
// Called on the onClick() method of the Buy Fuel button
// Starts the purchase flow for fuel

public void BuyFuel(){
    OpenIAB.purchaseProduct(SKU_PLANE_FUEL);
}
  1. OpenIAB.purchaseProduct() calls the purchaseProduct() function of OpenIAB_Android.cs for Android devices. In this call and others, the item SKU is passed as the parameter SKU. The parameter developerPayload is assigned a blank string because the developerPayload feature is not supported by Samsung IAP. 
OpenIAB.cs
        /**
         * Purchases the product with the given sku and developerPayload
         * @param product ID
         * @param developerPayload to verify transaction
         */
public static void purchaseProduct(string sku, string developerPayload = ""){
    billing.purchaseProduct(sku, developerPayload);
}
  1. OpenIAB_Android.purchaseProduct calls purchaseProduct() of UnityPlugin.java via the method Call().
OpenIAB_Android.cs
public void purchaseProduct(string sku, string developerPayload = ""){
    ...
    plugin.Call("purchaseProduct", sku, developerPayload);
}
  1. UnityPlugin.java accesses the library-level classes that provide the functions for buying the items. After the item purchase process is complete, onIabPurchaseFinished() is invoked.  
  • When the item purchase process is successful:
  1. onIabPurchaseFinished() sends the message back to Shop.cs.
UnityPlugin.java
        /** 
         * Callback for when a purchase is finished
         */

IabHelper.OnIabPurchaseFinishedListener _purchaseFinishedListener = new
IabHelper.OnIabPurchaseFinishedListener(){
    public void onIabPurchaseFinished(Iab Result result, Purchase purchase){
        ...
        UnityPlayer.UnitySendMessage(EVENT_MANAGER, PURCHASE_SUCCEEDED_CALLBACK, jsonPurchase);
    }
};
  1. This message is received by OnPurchaseSucceeded. It performs the corresponding action based on the item purchased by the user. To learn more about the Purchase object, see the OpenIAB API Reference. 
Shop.cs
// Listener callback if the purchase is successful

private void OnPurchaseSucceeded(Purchase purchase){
    // Check what was purchased and update the game
    switch (purchase.Sku){
        case SKU_PLANE_FUEL:
            FuelController.fuelAmount += 25;
            if (!infoCanvas.activeSelf){
                infoCanvas.SetActive (true);
                infoLog.text = "Added 25 Fuel!";
            }
            break;
    }
}
  • When the item purchase process fails OnPurchaseFailed() is invoked. For error response codes, see the OpenIAB API Reference
Shop.cs
// Listener callback if the purchase fails

private void OnPurchaseFailed(int errorCode, string error)
    {
        if (!infoCanvas.activeSelf){
            infoCanvas.SetActive (true);
            infoLog.text = error;
        }
    }

Purchasing Non-Consumable Items

The Buy Plane feature is a non-consumable item. This means that this item can be purchased only once. The purchased plane does not expire and can be reused an unlimited number of times. 

To purchase a non-consumable item: 
  1. In the City Flyer UI, click Buy Plane.
  2. The button click invokes the function BuyPlane() of Shop.cs, which calls OpenIAB.purchaseProduct() and passes the SKU of the item (SKU_CHANGE_PLANE).
Shop.cs
// Listener callback if the purchase is successful

private void OnPurchaseSucceeded(Purchase purchase){
    switch (purchase.Sku){
        // Check what was purchased and update the game
        if (!infoCanvas.activeSelf){
            infoCanvas.SetActive (true);
            infoLog.text = "Plane changed to F-16!";
        }
        oldPlane.SetActive (false);
        newPlane[0].SetActive (true);
        break;
    }
}

Shop.cs
// Called on the onClick() method of the Buy Plane button
// Starts the purchase flow for a new airplane

public void BuyPlane(){
    OpenIAB.purchaseProduct(SKU_CHANGE_PLANE);
}

Plane purchases follow the same sequence as Buy Fuel, starting with the function BuyPlane(), and ending with case SKU_CHANGE_PLANE in OnPurchaseSucceeded()

Purchasing Non-Recurring Subscription Items

The Buy Ghost Mode feature of this application is a non-recurring subscription item. This means that this item can be purchased one time and is delivered on a scheduled basis for a specified duration. Specify the duration when you add the item in the Samsung Seller Office.

To purchase a non-recurring item:
  1. In the City Flyer UI, click Buy Ghost Mode.
  2. The button click invokes the function GhostMode() of Shop.cs, which calls OpenIAB.purchaseProduct() and passes the SKU of the item (SKU_GHOST_MODE).
Shop.cs
// Called on the onClick() method of the Buy Ghost Mode button
// Starts the purchase flow for Ghost Mode

public void GhostMode(){
    OpenIAB.purchaseSubscription(SKU_GHOST_MODE);
}

Shop.cs
// Listener callback if the purchase is successful

private void OnPurchaseSucceeded(Purchase purchase){
    // Check what was purchased and update the game
    case SKU_GHOST_MODE:
        if (!infoCanvas.activeSelf){
            infoCanvas.SetActive (true)
            infoLog.text = "Ghost Mode Active!";
        }
        isGhostModeEnabled = true;
        break;
    }
} 

Ghost Mode purchases follow the same sequence as Buy Fuel, starting with the function GhostMode(), and ending with case SKU_GHOST_MODE in OnPurchaseSucceeded().

  • Was this article helpful?