Integrating Samsung IAP in Your Unity Game
Md. Abulkalam Azad
Engineer, Samsung Developer Program
Galaxy Store is one of the top app stores to sell your Android games in many different countries. You can also sell various in-app purchase (IAP) items inside your games using the Samsung IAP SDK. As many of you now use the Unity engine to develop your games, Samsung has introduced a Unity plugin for the Samsung IAP SDK that enables you to implement IAP features. Follow the steps outlined in this blog to easily implement the Unity plugin into your project and utilize the Samsung IAP functionalities.
Prerequisites
It is assumed you are already familiar with the Samsung IAP procedure. If not, please read the IAP SDK programming guide carefully before proceeding further. After that, download the Samsung IAP Unity plugin package and go through its documentation. To avoid compatibility issues, make sure you meet the system requirements.
There are three types of IAP items:
- Consumable: can be used only one time and re-purchasable
- Non-consumable: can be used any number of times and not re-purchasable
- Subscription: can be used any number of times while it is active
For this example, we have developed a basic coin collecting game in Unity for Android and added UI buttons that allow users to buy IAP items (consumable and non-consumable) and a subscription. The “Buy Super Jump” button initiates purchasing a super jump item from Galaxy Store using the Samsung IAP SDK. Super jump is a consumable item which enables the player to jump higher than normal. Similarly, the “Upgrade Player” button initiates purchasing a player upgrade, which is a non-consumable item. This blog only covers consumable and non-consumable purchases, we’ll discuss subscriptions in a future blog.
Figure 1: Preview of the sample game developed in Unity.
Note: You are required to develop your game/application in Unity beforehand to integrate the IAP Unity plugin into it.
Integrate the Samsung IAP Unity plugin
After creating the game in Unity, you need to enable Samsung IAP functionalities in your project. Follow the steps below:
- Import the Samsung IAP Unity plugin package into the project. In Unity, click Assets -> Import Package -> Custom Package and select the downloaded plugin package.
- You can now see the Plugins folder under your Assets folder and the “SamsungIAP.cs” script at Assets/Plugins/Script.
- Copy or move the “SamsungIAP.cs” script into the default scripts folder of your project (where all the scripts are kept together) so that other scripts can access it easily. If you don’t already have a scripts folder, create a new one and keep all your project scripts together along with “SamsungIAP.cs.”
- Create an empty game object in the Hierarchy tab and drag-and-drop the “SamsungIAP.cs” script onto it. In our sample project, we have renamed the game object as “SamsungIAP.”
- Click on the “SamsungIAP” game object and check if the IAP functionality is enabled in the Inspector, as shown below:
Figure 2: Samsung IAP is enabled for the project.
Set the IAP operation mode
IAP supports three operational modes. The production mode is for enabling billing for item purchases and the other two are for testing IAP functions without billing the game users for item purchases. The default operation mode is set to OPERATION_MODE_TEST with the return value as Success, but you can set the return value to Failure instead, or switch to OPERATION_MODE_PRODUCTION by checking (√) the Production Build checkbox in the Inspector as shown in figure 2. You can learn more about the IAP operation modes and how they work from here.
Register the game and IAP items in Seller Portal
To process/test the Samsung IAP operations, both your game and any IAP items need to be registered in Seller Portal. Follow the steps below:
- Ensure you have switched the platform of your game to Android and the package name is different from the apps registered in other app stores. You can rename the package name of your project from Player Settings -> Other Settings.
- Save your Unity project and build the APK file. In Unity, go to File -> Build Settings and then click the Build button.
- Follow the steps listed in Register an app and in-app items in Seller Portal and complete the registration of your game and IAP items accordingly. For our sample game, we have registered a consumable and a non-consumable item with the IDs “BuySuperJump” and “BuyUpgradedPlayer” respectively. Keep the item IDs in mind as they will be required when initiating the purchases.
- You can add testers (non-licensed and licensed) in the Binary tab of Seller Portal while registering your game in the manner covered in the previous step. Licensed testers are not charged for purchasing any IAP items. You can register the licensed testers in your Seller Portal profile. See IAP Testing for more information.
Get previously purchased items
Make sure to retrieve any previously purchased non-consumable and unconsumed items every time the user starts the game. Use the GetOwnedList()
method of the IAP plugin to get information about the items the user has already purchased. However, please note there is a script called “player.cs” in our project which is added to the main player game object as a component. From now on we will be editing the code in “player.cs” to enable all the Samsung IAP functions for this project. Follow the steps below:
-
Add the following line at the beginning to access the Samsung IAP libraries in this script.
using Samsung;
-
Call the
GetOwnedList()
method whenever the game launches by adding the following line at the beginning of theStart()
method. Learn more about theGetOwnedList()
method here.SamsungIAP.Instance.GetOwnedList(ItemType.all, OnGetOwnedList);
-
After the processing of the
GetOwnedList()
method is completed, theOnGetOwnedList
callback is triggered, which receives information about the specified purchased items and API call processing. We need to implement this callback method under the same class as in the following;void OnGetOwnedList(OwnedProductList _ownedProductList){ if(_ownedProductList.errorInfo != null){ if(_ownedProductList.errorInfo.errorCode == 0){// 0 means no error if(_ownedProductList.results != null){ foreach(OwnedProductVo item in _ownedProductList.results){ if(item.mConsumableYN == "Y"){ //consume the consumable items and OnConsume callback is triggered afterwards SamsungIAP.Instance.ConsumePurchasedItems(item.mPurchaseId, OnConsume); } if(item.mItemId == "BuySuperJump"){ superJump++; } else if(item.mItemId == "BuyUpgradedPlayer"){ playerMaterial = Resources.Load<Material>("playerMaterial"); MeshRenderer meshRenderer = GetComponent<MeshRenderer>(); meshRenderer.material = playerMaterial; } } } } }
As you can see, some actions have been taken inside the game depending on the respective item IDs. For example, the super jump counter has been increased and the material of the player gets changed. If there is any consumable item which has not been reported as consumed, then the ConsumePurchasedItems()
method is invoked. We describe this method in the next section.
Consume purchased consumable items
Use the ConsumePurchasedItems()
method to report the purchased consumable item as consumed, which enables the item to be purchased again. See Acknowledge a purchased consumable item to understand this process better. When the process of the ConsumePurchasedItems()
method in the previous section is finished, the item data and processing results are returned to the OnConsume
callback method. We need to implement this method in the same way under the same class as we implemented the OnGetOwnedList
method earlier.
void OnConsume(ConsumedList _consumedList){
if(_consumedList.errorInfo != null){
if(_consumedList.errorInfo.errorCode == 0){
if(_consumedList.results != null){
foreach(ConsumeVo item in _consumedList.results){
if(item.mStatusCode == 0){
//successfully consumed and ready to be purchased again.
}
}
}
}
}
}
Get purchasable IAP items
The users may want to see details of the available IAP items in the store for the game. The GetProductsDetails()
method helps to retrieve detailed information (for example, item name, price, or ID) about the IAP items registered in your game that are available for users to purchase. There is a UI button “Available Items” in our sample game for querying the purchasable items. After clicking this button, brief information for each item is presented in a simple dropdown list next to the button (see figure 3). To get the list of available items:
-
Declare a button variable and a dropdown variable in the beginning of the “player.cs” script.
public Button getProductsButton; public Dropdown itemList;
-
Add a listener method for the “Available Items” button at the end of the
Start()
method.getProductsButton.onClick.AddListener(OnGetProductsButton);
-
To initiate the
GetProductsDetails()
method, we need to implement the listenerOnGetProductsButton()
method.void OnGetProductsButton(){ //get all the product details SamsungIAP.Instance.GetProductsDetails("", OnGetProductsDetails); }
-
After the processing is completed on the server side, the
OnGetProductsDetails
callback is triggered, which contains information about the available IAP items. Implement this callback method and add information of each item to the dropdown method so that the users can see them easily. In the example, we show only the item name and price.void OnGetProductsDetails(ProductInfoList _productList){ if (_productList.errorInfo != null){ if (_productList.errorInfo.errorCode == 0){// 0 means no error if (_productList.results != null){ itemList.ClearOptions(); List<string> optionItems = new List<string>(); int i = 1; foreach (ProductVo item in _productList.results){ string temp = i+ ". " + item.mItemName + ": $ " + item.mItemPrice; optionItems.Add(temp); i++; } itemList.AddOptions(optionItems); } } } }
Figure 3: Showing the available IAP items in the game.
The information about all IAP items is shown in the dropdown menu as a list. You can show only one specific item or more items by specifying their IDs in the GetProductsDetails()
method if you want. Learn more about the method here.
Purchase IAP items
There are two UI buttons (see figures 1 and 3) in our sample game, namely “Buy Super Jump” and “Upgrade Player,” for purchasing consumable and non-consumable items, respectively. The variables for these two buttons are declared in the beginning of the Start()
method and two listener methods: OnBuySuperJumpButton()
and OnUpgradePlayerButton()
are added at the end of the Start()
method of “player.cs” script. Consequently, tapping on these buttons invokes the corresponding methods in the script. Follow the steps below to complete in-app purchasing:
- To enable the “Buy Super Jump” and the “Upgrade Player” buttons purchasing a super jump and a player upgrade, we need to instantiate the
StartPayment()
method inside the button listeners with the corresponding item IDs.void OnBuySuperJumpButton(){ //purchase a consumable item SamsungIAP.Instance.StartPayment("BuySuperJump", "", OnPayment); } void OnUpgradePlayerButton(){ //purchase a non-consumable item SamsungIAP.Instance.StartPayment("BuyUpgradedPlayer", "", OnPayment); }
- After the payment processing is completed, the
OnPayment
callback is triggered, which contains information about the purchased item, the transaction, and API call processing. We need to implement this callback method and act according to the item IDs as in the following:void OnPayment(PurchasedInfo _purchaseInfo){ if(_purchaseInfo.errorInfo != null){ if(_purchaseInfo.errorInfo.errorCode == 0){ if(_purchaseInfo.results != null){ //your purchase is successful if(_purchaseInfo.results.mConsumableYN == "Y"){ //consume the consumable items SamsungIAP.Instance.ConsumePurchasedItems(_purchaseInfo.results.mPurchaseId, OnConsume); } if(_purchaseInfo.results.mItemId == "BuySuperJump"){ superJump++; } else if(_purchaseInfo.results.mItemId == "BuyUpgradedPlayer"){ playerMaterial = Resources.Load<Material>("playerMaterial"); MeshRenderer meshRenderer = GetComponent<MeshRenderer>(); meshRenderer.material = playerMaterial; } } } } }
- For the
ConsumePurchasedItems()
method, we have already implemented theOnConsume
listener method.
In this way, in-app purchasing is fully implemented for both consumable and non-consumable items. Next, build the project, run it on your Galaxy device, and check that the IAP works flawlessly. In addition, you may update the APK of your game in Seller Portal and submit a beta version for more IAP testing. See the Test Guide to learn more about testing. Do not forget to switch the IAP operation mode to OPERATION_MODE_PRODUCTION before submitting the game to be published.
Conclusion
This tutorial explains the entire procedure of integrating the Samsung IAP Unity plugin and using the IAP functionalities for a sample game. Therefore, we hope that you can make use of this tutorial and develop an in-app purchase enabled game for Galaxy Store using Unity and sell your game items successfully to generate revenue.
Follow Up
This site has many resources for developers looking to build for and integrate with Samsung devices and services. Stay in touch with the latest news by creating a free account or by subscribing to our monthly newsletter. Visit the Marketing Resources page for information on promoting and distributing your apps. Finally, our developer forum is an excellent way to stay up-to-date on all things related to the Galaxy ecosystem.