Implement in-app subscriptions using Samsung IAP
implement in-app subscriptions using samsung iap objective learn how to integrate the samsung in-app purchase sdk into your application so that users can purchase and upgrade subscriptions directly within the app overview the samsung in-app purchase iap service provides developers with a reliable solution for managing digital purchases within mobile applications it guarantees a smooth and secure experience for users when buying digital goods, managing subscriptions, or processing refunds and consumed products the iap sdk enables easy integration of the iap functionality into your app, such as configuring iap, retrieving product details, offering and selling products, and managing purchased products to successfully sell in-app products, 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 products to your app by integrating in-app purchases iap , your apps can sell in-app products, including subscriptions a subscription is a specific type of in-app product available for purchase through your app in the galaxy store when a user buys a subscription, it grants access for a set duration known as the subscription period or payment cycle at the end of this period, the subscription automatically renews, allowing the user to continue using the product for another subscription period and to be automatically billed with the subscription price 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 subscription bookspot sample code 1 5 mb start your project in android studio, click open to open an existing project locate the downloaded sample project and click ok register the app and its associated subscriptions in the seller portal to register the sample app along with the in-app products 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 bookspot view, and in the mainactivity java file, refactor the application name bookspot from the package name com example bookspot for all directories notethe package name com example bookspot is already registered in the seller portal to avoid any conflicts, rename it with a different package name next, open the app > manifests > androidmanifest xml file and check that all necessary permissions are present com samsung android iap permission billing to connect to iap and enable in-app product registration in the 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" /> 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 in the in app purchase tab, click on add new item and select subscription add the subscriptions with item ids as basic and standard these are the item ids of the subscriptions created in the sample app next, set the subscription price by clicking on set new price noteto learn more about pricing, see manage subscription pricing provide the details for the payment cycle, grace period, free trial period, and free trial/tiered pricing limit as shown below noteincluding a free trial promotion for your app is optional to test the functionality of the getpromotioneligibility api, you can add a free trial select all subscriptions you added and click on activate lastly, add a license 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 samsung-iap-6 5 0 aar , ensure that it 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'] } next, open the mainactivity java file in the oncreate function, create an instance of iaphelper and set the operation mode to operation_mode_test this mode enables only license testers to test the application 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 operation mode to operation_mode_production get product details and check the promotion eligibility to obtain information about subscriptions registered in the store related to your app, use the getproductsdetails api you can retrieve details about a specific subscription by providing the argument named itemid with values such as basic or standard you can use an empty string "" as the argument to obtain all product details iaphelper getproductsdetails itemid, new ongetproductsdetailslistener { @override public void ongetproducts @nonnull errorvo errorvo, @nonnull arraylist<productvo> productlist { if errorvo geterrorcode == iaphelper iap_error_none { for productvo item productlist { itemname settext item getitemname +" level course" ; itemtype settext "item type "+item gettype ; itemprice settext "item price "+item getitemprice +item getcurrencyunit ; itemduration settext "item duration "+item getsubscriptiondurationunit ; subscriptiondialogbutton setonclicklistener dialogbtnlistener ; getpromotioneligibility item getitemid ; } } else { log e "ongetproducts error ", errorvo geterrorstring ; } } } ; after getting the product details, check the promotion eligibility use the getpromotioneligibility api to return the pricing options for a subscription, including free trials and introductory prices that may apply to the user iaphelper getpromotioneligibility itemid, new ongetpromotioneligibilitylistener { @override public void ongetpromotioneligibility @nonnull errorvo errorvo, @nonnull arraylist<promotioneligibilityvo> pricinglist { if errorvo geterrorcode == iaphelper iap_error_none { for promotioneligibilityvo pricing pricinglist { itempricing settext "promotion eligibility "+pricing getpricing ; } } else { log e "ongetpromotioneligibility error ", errorvo geterrorstring ; } } } ; initiate the payment process and acknowledge the subscription to initiate a purchase and complete the payment transaction process, use the startpayment api the result of the purchase is specified through the onpaymentlistener interface, which provides detailed purchase information in the event of a successful transaction use obfuscatedaccountid and obfuscatedprofileid value is up to 64 bytes to detect fraudulent payments these values are returned in purchasevo that contains the purchase results you can find more details about it in the purchase an in-app product section of the programming guide once the app has granted entitlement to the user, notify samsung iap of the successful transaction using the acknowledgepurchases api additionally, call the handlechangeplan function to make the change plan button visible and to set the onclicklistener iaphelper startpayment itemid, obfuscatedaccountid, obfuscatedprofileid, new onpaymentlistener { @override public void onpayment @nonnull errorvo errorvo, @nullable purchasevo purchasevo { if errorvo geterrorcode == iaphelper iap_error_none && purchasevo != null { acknowledgepurchases purchasevo getpurchaseid ; handlechangeplan itemid ; } else { log e "onpayment error ", errorvo geterrorstring ; } } } ; use the acknowledgepurchases api as below iaphelper acknowledgepurchases purchaseid, errorvo, acknowledgedlist -> { if errorvo geterrorcode == iaphelper iap_error_none { for acknowledgevo item acknowledgedlist { log e "onacknowledgepurchases ", item getstatusstring ; } } else { log e "onacknowledgepurchases error ", errorvo geterrorstring ; } } ; manage changes to subscription plans the changesubscriptionplan api allows users to switch between different tiers of the same subscription changes can be categorized as follows upgrade - moving from a lower-priced tier to a higher-priced tier, or switching between tiers of equal value downgrade - transitioning from a higher-priced tier to a lower-priced tier you can use proration modes to set the payment and current subscription period settings there are four proration modes available instant_prorated_date, instant_prorated_charge, instant_no_proration, and deferred in this code lab, use instant_prorated_date so that the current subscription is changed instantly, allowing the user to start using the new subscription tier right away also, use obfuscatedaccountid and obfuscatedprofileid to detect fraudulent payments iaphelper changesubscriptionplan itemid, newitemid, helperdefine prorationmode instant_prorated_date, obfuscatedaccountid, obfuscatedprofileid, new onchangesubscriptionplanlistener { @override public void onchangesubscriptionplan @nonnull errorvo errorvo, @nullable purchasevo purchasevo { if errorvo geterrorcode == iaphelper iap_error_none && purchasevo != null { handlechangeplan newitemid ; updatechangeplanview newitemid ; } else { log e "onchangesubscriptionplan error ", errorvo geterrorstring ; } } } ; notefor more details on handling changes to subscription plans, see manage subscription plan changes retrieve and process the list of subscriptions the getownedlist api retrieves a list of in-app products that the user has previously purchased, including active subscriptions and free trials call the getownedlist api from the iaphelper class and obtain the results through the ongetownedlistlistener interface utilize the helperdefine product_type_subscription parameter to fetch only subscription data after acquiring the subscription list, check the acknowledgment status of each subscription using the getacknowledgedstatus function and check whether all of the subscriptions are being acknowledged by samsung iap if any of the subscription status is not acknowledged, then call the acknowledgepurchases function to notify the acknowledgement to samsung iap if the subscription price has changed in the seller portal, it may be necessary for existing subscribers to consent to the price increase before the next subscription period, depending on certain conditions to determine if consent is required from the subscriber, use the getpricechangemode and isconsented functions if consent is needed, call the handleconsent function to make the consent button visible, and set the onclicklistener for the button accordingly iaphelper getownedlist helperdefine product_type_subscription, new ongetownedlistlistener { @override public void ongetownedproducts @nonnull errorvo errorvo, @nonnull arraylist<ownedproductvo> ownedlist { if errorvo geterrorcode == iaphelper iap_error_none { for ownedproductvo item ownedlist { // check the acknowledgedstatus helperdefine acknowledgedstatus acknowledgedstatus = item getacknowledgedstatus ; if acknowledgedstatus equals helperdefine acknowledgedstatus not_acknowledged { acknowledgepurchases item getpurchaseid ; } // handle the price change if item getitemid equals item1 || item getitemid equals item2 { handlechangeplan item getitemid ; subscriptionpricechangevo subscriptionpricechangevo = item getsubscriptionpricechange ; if subscriptionpricechangevo != null && subscriptionpricechangevo getpricechangemode equals helperdefine pricechangemode price_increase_user_agreement_required && !subscriptionpricechangevo isconsented { handleconsent item getitemid , item getpurchaseid ; } } } } else { log e "getownlist error ", errorvo geterrorstring ; } } } ; to create a deep link to the consent page when needed, use the following code uri deeplinkuri = uri parse "samsungapps //subscriptiondetail?purchaseid="+purchasedid ; intent intent = new intent intent action_view, deeplinkuri ; startactivity intent ; run the app after building the apk, install the app on a samsung galaxy device to test the app, click on view details in the basic tab this displays the purchase details, including promotion eligibility, item type, duration, and price then, click on continue to subscribe in the samsung checkout pop-up, select your payment method and click the subscribe button a payment confirmation screen appears upon successful completion of the transaction when the change plan button appears, click on it a pop-up shows the changes in your subscription plan click next and then subscribe using your payment method a payment confirmation screen appears, and the view changes to the standard tab, where you can see the details of your new subscription noteto test and display the consent button, you must increase the price of any subscription in the seller portal the price update will be applied in the galaxy store after a waiting period of 7 days the subscription renews every 10 minutes and will expire in operation_mode_test mode to test the change in the subscription price, set the operation mode to operation_mode_production you're done congratulations! you have successfully achieved the goal of this code lab now, you can implement in-app subscriptions using samsung iap into your application by yourself! if you are having trouble, you may download this file in-app subscription bookspot complete code 1 5 mb to learn more about developing apps with samsung iap sdk, visit developer samsung com/iap