Add Samsung In-App Purchase service to your app
add samsung in-app purchase service to your app objective learn how to integrate samsung in-app purchase iap service into your app so that users can purchase digital consumable and non-consumable items within the app registered in the galaxy store overview the samsung in-app purchase iap service offers developers a robust solution for handling digital purchases within mobile apps it ensures a smooth and secure experience when purchasing digital goods or items, managing subscriptions, or dealing with refunds and consumed items the iap sdk makes integrating the iap functionality into your app simple, allowing you to configure iap settings, retrieve item details, offer and sell items, and manage purchased items effortlessly to successfully sell in-app items, follow these four basic steps download and integrate the samsung iap sdk into your application request for commercial seller status in the samsung galaxy store seller portal upload your application’s binary file in the seller portal add in-app items to your app in-app items can be categorized into three types consumable, non-consumable, and subscription consumable these items can be used only once and can be repurchased multiple times by the app user non-consumable non-consumable items can be used any number of times and cannot be repurchased subscription with subscription items, app users can access them any number of times throughout the duration of their subscription for more information, go to samsung iap set up your environment you will need the following android studio latest version recommended samsung iap sdk latest version samsung galaxy device android 6 0 or higher samsung galaxy store seller portal commercial seller account sample code here is a sample code for you to start coding in this code lab download it and start your learning experience! in-app purchase turbobike sample code 1 3 mb start your project in android studio, click open to open an existing project locate the downloaded android project turbobike_blank_code from the directory and click ok register the app and its items in the seller portal to register the sample app along with the in-app items in the samsung galaxy store seller portal, follow these steps sign in using your commercial seller account in android studio, modify the package name of the sample app navigate to app > kotlin + java > com example turbobike view, and in the mainactivity java file, refactor the application name turbobike from the package name com example turbobike for all directories notethe package name com example turbobike is already registered in the seller portal to avoid any conflicts, rename it with a different package name build the apk from android studio and upload the binary to the seller portal once the testing process is complete and the app functions smoothly as intended, return to this step and upload the final apk file under the in app purchase tab, add three items named bike, booster, and fuel these are item ids of the in-app items created in the sample app where the bike is a non-consumable item, while both booster and fuel are consumable items when adding the bike item, uncheck this item can be purchased multiple times checkbox refer to the step-by-step guide for detailed instructions lastly, add a licensed tester to enable purchasing within the app edit your seller portal profile and include your samsung account in the license test field on the test device, sign in with the same samsung account initialize the samsung iap sdk before using the samsung iap sdk library, certain configurations are necessary, which are already applied in the sample code provided the samsung-iap-6 3 0 aar library is added to the app > libs folder, and included as a dependency in the module-level build gradle file dependencies { … implementation filetree dir 'libs', include ['* aar'] } these necessary permissions are already added in the androidmanifest xml file com samsung android iap permission billing to connect to iap and enable in-app item registration in seller portal android permission internet because iap uses the internet <uses-permission android name="com samsung android iap permission billing" /> <uses-permission android name="android permission internet" /> go to mainactivity java and in the oncreate function, create an instance of the samsung iap sdk to utilize the functionalities it offers set the operating mode to operation_mode_test to purchase items without payment and enable only licensed testers to test without incurring charges iaphelper = iaphelper getinstance getapplicationcontext ; iaphelper setoperationmode helperdefine operationmode operation_mode_test ; notebefore submitting the app for beta testing or release, change the operating mode to operation_mode_production get the list of all items owned by the user the getownedlist function returns a list of items that the user has previously purchased, including consumable items that have not been reported as consumed non-consumable items subscription items that are in free trial and active state call the getownedlist function from the iaphelper class also, check if there are any consumable items in the returned list and call the handleconsumableitems function with the purchase id of each consumable item iaphelper getownedlist iaphelper product_type_all, new ongetownedlistlistener { @override public void ongetownedproducts @nonnull errorvo errorvo, @nonnull arraylist<ownedproductvo> arraylist { if errorvo geterrorcode == iaphelper iap_error_none { for ownedproductvo item arraylist { if item getisconsumable { // consume the purchased item handleconsumableitems item getpurchaseid ; } } } else { log d "getownedproducts failed ", errorvo tostring ; } } } ; notecall getownedlist whenever launching the application to check for unconsumed items or subscription availability report consumable items as consumed mark the consumable items returned from getownedlist and those successfully purchased with startpayment as consumed by calling the consumepurchaseditems function iaphelper consumepurchaseditems itemid, new onconsumepurchaseditemslistener { @override public void onconsumepurchaseditems @nonnull errorvo errorvo, @nonnull arraylist<consumevo> arraylist { if errorvo geterrorcode == iaphelper iap_error_none { toast maketext getapplicationcontext ,"consumed successfully now you can purchase another item ", toast length_short show ; } else { toast maketext getapplicationcontext , "consume failed " + errorvo geterrorstring , toast length_short show ; } } } ; this makes the items available for repurchase, regardless of whether they are used or not in the sample app, consumable items cannot be used and it only stores the total count of the items purchased get item details to retrieve information about some or all in-app items registered to the app, use the getproductsdetails function by specifying an item id such as bike or booster, you can fetch details for a particular in-app item to obtain information about all in-app items available in the seller portal for the user, pass an empty string "" as the argument iaphelper getproductsdetails "bike, booster, fuel", new ongetproductsdetailslistener { @override public void ongetproducts @nonnull errorvo errorvo, @nonnull arraylist<productvo> arraylist { if errorvo geterrorcode == iaphelper iap_error_none { for productvo item arraylist { if item getitemid equals itemid { // set product details value to ui product settext "product name " + item getitemname ; price settext "product price " + item getitempricestring ; currency settext "currency " + item getcurrencycode ; // click continue button to purchase dialogbutton setonclicklistener dialogbtnlistener ; } } } else { toast maketext getapplicationcontext , "getproductdetails failed " + errorvo geterrorstring , toast length_short show ; } } } ; handle item purchase and payment process to initiate a purchase and complete the payment transaction process, use the startpayment function the result of the purchase is specified in the onpaymentlistener interface, which includes the detailed purchase information purchasevo in case of a successful transaction if there's an error during the payment process, an error code -1003 indicates that the non-consumable item is already purchased for further information on error responses, refer to the documentation on response codes iaphelper startpayment itemid, string valueof 1 , new onpaymentlistener { @override public void onpayment @nonnull errorvo errorvo, @nullable purchasevo purchasevo { if errorvo geterrorcode == iaphelper iap_error_none { log d "purchaseid " , purchasevo getpurchaseid ; // non-consumable item, can purchase single time if itemid equals "bike" { purchasebikebtn settext "already purchased" ; } // consumable item, can purchase multiple time else if itemid equals "booster" { handleconsumableitems purchasevo getpurchaseid ; // update booster count in ui boostercount+=1; boostercountertv settext "⚡ "+boostercount+" ev" ; }else if itemid equals "fuel" { handleconsumableitems purchasevo getpurchaseid ; // update fuel count in ui fuelcount+=1; fuelcountertv settext "⛽ "+ fuelcount+" ltr" ; } }else { // non-consumable item, already purchase if errorvo geterrorcode == -1003 { purchasebikebtn settext "already purchased" ; } } } } ; upon successfully purchasing a consumable item, the consumepurchaseditems function is called through the handleconsumableitems function the total number of purchased boosters and fuel is displayed on the app ui using the boostercountertv settext and fuelcountertv settext methods respectively run the app after building the apk, install the app on a samsung galaxy device test the app by making purchases the turbo bike can only be bought once, while either the booster or fuel can be purchased multiple times after purchasing in-app items, the app shows that the bike has already been purchased along with the number of boosters and fuel bought you're done congratulations! you have successfully achieved the goal of this code lab now, you can integrate samsung iap sdk into your application by yourself! if you are having trouble, you may download this file in-app purchase turbobike complete code 1 3 mb to learn more about developing apps with samsung iap sdk, visit developer samsung com/iap