Quantcast
Jump to content

Integrating Samsung IAP in Your Unity Game


Recommended Posts

2021-05-11-01-banner.jpg

The 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 Helper 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:

  1. Consumable: can be used only one time and re-purchasable
  2. Non-consumable: can be used any number of times and not re-purchasable
  3. 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 the 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.


2021-05-11-01-01.jpgFigure 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:

  1. Import the Samsung IAP Unity plugin package into the project. In Unity, click Assets -> Import Package -> Custom Package and select the downloaded plugin package.
  2. You can now see the Plugins folder under your Assets folder and the “SamsungIAP.cs” script at Assets/Plugins/Script.
  3. Copy or move the “SamsungIAP.cs” script into the default scripts folder (where all the scripts are kept together) of your project 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”.
  4. 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”.
  5. Click on the “SamsungIAP” game object and check whether the IAP functionality is enabled in the Inspector, as shown below:

2021-05-11-01-02-v2.jpgFigure 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 the Seller Portal

To process/test the Samsung IAP operations, both your game and any IAP items need to be registered in the Seller Portal. Follow the steps below:

  1. 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.
  2. Save your Unity project and build the APK file. In Unity, go to File -> Build Settings and then click the Build button.
  3. 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.
  4. You can add testers (non-licensed and licensed) in the Binary tab of the Seller Portal while registering your game in the manner covered in the previous step. Licensed testers are not be 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 “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 codes into this “player.cs” script to enable all the Samsung IAP functions for this project. Follow the steps below:

  1. Add the following line at the beginning to access the Samsung IAP libraries in this script.
    using Samsung; 
    
  2. Call the GetOwnedList() method whenever the game launches, by adding the following line at the beginning of the Start() method. Learn more about the GetOwnedList() method here.
  3. After the processing of the GetOwnedList() method is completed, the OnGetOwnedList 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, ID, etc.) 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:

  1. Declare a button variable and a dropdown variable in the beginning of the “player.cs” script.
    public Button getProductsButton;
    public Dropdown itemList;
    
  2. Add a listener method for the “Available Items” button at the end of the Start() method.
    getProductsButton.onClick.AddListener(OnGetProductsButton);
    
  3. To initiate the GetProductsDetails() method, we need to implement the listener OnGetProductsButton() method.
    void OnGetProductsButton(){
             //get all the product details
             SamsungIAP.Instance.GetProductsDetails("", OnGetProductsDetails); 
    }  
    
  4. 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);
                       }
                  }
             }
    }
    

2021-05-11-01-03.jpgFigure 3: Showing the available IAP items in the game.


The information of 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.

View the full blog at its source

Link to comment
Share on other sites



  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Popular Days

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Similar Topics

    • By BGR
      The Galaxy Z Fold 4 is Samsung’s next flagship phone, and the foldable will introduce several significant improvements over its predecessor. And, if new leaks are accurate, the Galaxy Z Fold 4 will start at double the storage compared to its predecessor, or 512GB of built-in memory. The flash upgrade is welcome on a flagship device that will most certainly lack support for microSD storage expansion. That also means the most expensive Fold 4 model will go up to 1TB in storage space.
      But it’s not all good news, as storage upgrades don’t come cheap.
      Galaxy Z Fold 4 storage leaks
      If you thought the Galaxy Z Fold 4 series will not deliver a major product update, the wave of Fold 4 leaks should have made you change your mind by now. According to reports from trusted sources, the new foldable will be lighter and thinner than its predecessor.
      Not only that, but the handset will get an update to the aspect ratio, which will make it easier to use when folded. Moreover, the hinge crease might be milder than before.
      More importantly, the Galaxy Z Fold 4 will deliver a big camera update that should address criticisms regarding the Fold 3’s camera. Also important are the Fold 4 specs, with a recent report indicating that the new Samsung handset will feature the latest Snapdragon 8+ Gen 1 processor. That’s good news to anyone familiar with the Galaxy S22 performance debacle.
      Galaxy Z Fold 4 storage options leaked in Korea. Image source: handphone.or.kr But that recently leaked specs sheet indicated the Fold 4 will come in two storage configurations: 256GB and 512GB. That’s in line with the previous model and a logical move for Samsung. That 256GB entry-level tier is double what you get on most smartphones. And that tier would let Samsung keep in place last year’s price, but that’s just speculation.
      A new finding from Korea indicates that Samsung will make 512GB and 1TB versions of the Galaxy Z Fold 4. That’s according to a website that seems to be run by the Korea Association for ICT Promotion.
      According to Redditors, that’s a website that manages information about lost cellphones in Korea. But the database also includes a hidden search service for smartphone model numbers, including unreleased devices.
      What about the price?
      The listings on the website only show two storage tiers for the Galaxy Z Fold 4: 512GB and 1TB. While some of the Fold 4 models in the list do not have storage designators, there is no entry for a 256GB model.
      We’re just speculating for the time being, but if the information in this database is accurate, then Samsung might double the storage on its next-gen foldable flagship.
      On the other hand, the 256GB storage tier is critical. That’s the kind of storage space you should expect from the cheapest Fold 4 model. Put differently, a bump to 512GB might also help Samsung justify a price increase for the new Fold 4.
      Galaxy Z Fold 4 design render: Foldable screen (top), cover screen and camera details (bottom). Image source: @OnLeaks and Smartprix One of the best things about the Galaxy Z Fold 3 is that the entry price dropped. The phone starts at $1,799, and that’s before all the crazy deals come in. Samsung did its best to lower the cost, offering buyers plenty of sales and the ability to trade in multiple gadgets.
      Nobody could blame Samsung for keeping that entry price in place or raising it, given the larger economic context. But it’ll be interesting to see if Samsung is ready to sell a 512GB Galaxy Z Fold 4 for $1,799. This would essentially give buyers a free storage upgrade.
      Again, we’re assuming there’s no 256GB version of the Fold 4. But there’s no proof that Samsung dropped the 256GB tier for the Fold line.
      Samsung will unveil the Fold 4 on August 10th, according to a leaker. We should learn more details about the new foldable’s storage options well before then.
      The post Samsung might give the Galaxy Z Fold 4 a major storage upgrade appeared first on BGR.
      View the full article
    • By BGR
      After the Galaxy S22 series’ semi-disastrous launch, Samsung probably can’t wait to unveil its next flagship phones. These will be foldable devices, however. They’re the successors of last year’s Galaxy Z Fold 3 and Flip 3. The Fold 4 and Flip 4 have appeared in several leaks already, and we know what to expect from the handsets’ designs and specs sheets. And a leaker just revealed the purported launch, preorder, and release dates for the Galaxy Z Fold 4 and Flip 4.
       
      The Fold 4 and Flip 4 will look like their predecessors, but fans of foldable handsets can expect several significant design changes.
      Samsung’s Galaxy Z Fold 4 design is the more exciting of the two devices. The phone will be shorter and wider than the Fold 3. It’ll still have a massive internal foldable display. But it should be more comfortable to use the handset when folded because the external display will have a better aspect ratio.
      The Fold 4 is also going to be lighter and thinner than its predecessor. Samsung will supposedly use a new hinge mechanism to reduce the phone’s weight.
      The same leaker who provided most of the aforementioned Galaxy Z Fold 4 details also listed the handset’s main specs. The main takeaway is that the Fold 4 will feature a brand new chip, the Snapdragon 8 Plus Gen 1 that Qualcomm just unveiled.
      This implies that all the Fold 4 models will pack the same System-on-Chip (SoC), which is good news for fans. Poor Exynos 2200 performance hurt the Galaxy S22 this year, and many people think Samsung should drop its latest SoC as soon as possible.
      Ice Universe has provided plenty of tidbits about the Fold 4 and Flip 4 in the past. But he never revealed the launch and release date details for the two Galaxy Z phones.
      Thankfully, we now have that information from a different prominent leaker, YouTuber Jon Prosser.
      Galaxy Z Fold 4 release date
      The YouTuber took to Twitter to discuss several unreleased Samsung products, including the new foldables. Prosser revealed launch, preorder, and release dates in a series of tweets. He mentioned the Galaxy Z Fold 4, Flip 4, a Lavander version of the Galaxy S22, as well as upcoming Galaxy Watch 5 and Watch 5 Pro wearables.
      If Prosser’s sources are correct, Samsung will hold its Galaxy Z Fold 4 launch event on August 10th. That’s when the company should unveil the Flip 4 and its new 2022 wearable generation. Preorders for all these products should start on the same day.
      Then, the Galaxy Z Fold 4 and the rest of these new devices will apparently get the same release date: August 26th.
      The leaker said that the Galaxy Z Fold 4 will come in Phantom Black, Green, and Beige. The Flip 4 will be available in colorway options at launch, including Graphite, Bora Purple, Pink Gold, and Blue.
      The Galaxy Watch 5 will come in 40mm and 44mm sizes, which sound familiar. Both models are said to be available in Phantom Black or Silver. The smaller model will get a Pink option, while the 44mm will get a Sapphire (blueish) colorway. The Watch 5 Pro will also reportedly come in Phantom Black and Silver, but it’ll be slightly larger at 46mm.
      Prosser did not provide pricing information for any of these devices, but at least we have an apparent release date. However, considering that Samsung went out of its way to make last year’s foldables more affordable, we’d expect a similar strategy for the Galaxy Z Fold 4 and Flip 4.
      The prices should leak as we get closer to the launch event.
      The post Samsung Galaxy Z Fold 4 release date just leaked appeared first on BGR.
      View the full article





×
×
  • Create New...