Integrate the Samsung In-App Purchase Orders API with Your Application
samsung in-app purchase (iap) offers developers a robust solution for handling digital transactions within mobile applications available on galaxy store. whether it is selling digital goods, handling subscriptions, or managing refunds, samsung iap is designed to offer a smooth, secure experience. the samsung iap orders api expands the scope of these benefits. you can fetch all the payments and refunds history according to specified dates. this content guides you through the essential components for implementing both the samsung iap and samsung iap orders apis. figure 1: sample application ui in this tutorial, we provide a sample application called book spot, which offers users the option to subscribe to their favorite books and consumable items, such as text fonts, for purchase. after purchase, users can consume the item. finally, developers can view all their payment and refund history on specific dates by calling the samsung iap orders api from the back-end server. prerequisites before implementing in-app purchases in your app, do the following to enable a smooth and effective execution of the process while developing your own application: integrate the samsung iap sdk into your application. for more information about the iap sdk integration, you can follow the integration of samsung iap services in android apps article. upload the application for beta testing on samsung galaxy store. a step-by-step guide with screenshots has been provided in the documentation. for more details, see the section “production closed beta test” on the test guide. finally, create items on the seller portal so that users can purchase or subscribe to them while using the application. for more details about the available items that the seller portal supports, see the programming guide. for the sample application, we have already completed these steps. some example items were already created in seller portal, such as books and fonts so that you can consume and subscribe to them while using this sample application. implementation of item purchase now that the application and items are ready, you can implement the purchase functionality in your application like in the sample below: when clicking "buy," the startpayment() method is called, specifying parameters for item id and the onpaymentlistener interface, which handles the results of the payment transaction. the onpayment() callback returns whether the purchase has succeeded or failed. the purchasevo object is instantiated and in case it is not null, it holds the purchase results. if the purchase is successful, then it validates the purchase showing its id. if the purchase is not successful, a purchaseerror message is shown. for more information, check the purchase an in-app item section. iaphelper.startpayment(itemid, string.valueof(1), new onpaymentlistener() { @override public void onpayment(@nonnull errorvo errorvo, @nullable purchasevo purchasevo) { if (purchasevo != null) { // purchase successful log.d("purchaseid" , purchasevo.getpurchaseid().tostring()); toast.maketext(getapplicationcontext() ,"purchase successfully", toast.length_short).show(); } else { log.d("purchaseerror" , errorvo.tostring()); toast.maketext(getapplicationcontext() ,"purchase failed", toast.length_short).show(); } } }); implementation of item consumption after successfully purchasing an item, the user can then consume it. in the sample code below, when "consumed" is selected, the consumepurchaseitems() triggers the consume functionality. this is necessary as items must be marked as consumed so they can be purchased again: the consumepurchaseitems() method is called specifying the parameters for purchaseid and the onconsumepurchaseditemslistener() interface, which handles the item data and results. this code also checks if consuming the purchased items succeeded or failed: if the errorvo parameter is not null and there is no error with the purchase, which can be verified with the iap_error_none response code, then the “purchase acknowledged” message is displayed. however, if there is an error, the errorvo parameter returns an error description with the geterrorstring() getter, along with the “acknowledgment failed” message. iaphelper.consumepurchaseditems(purchaseid, new onconsumepurchaseditemslistener() { @override public void onconsumepurchaseditems(@nonnull errorvo errorvo, @nonnull arraylist<consumevo>arraylist) { if (errorvo != null && errorvo.geterrorcode() == iaphelper.iap_error_none) { toast.maketext(getapplicationcontext() ,"purchase acknowledged", toast.length_short).show(); } else { toast.maketext(getapplicationcontext(), "acknowledgment failed: " + errorvo.geterrorstring(), toast.length_short).show(); } } }); implementation of item subscription besides purchasing and consuming items, you can also subscribe to them in your applications. similar to the validation done for the consumable item purchase, you validate the subscription with a purchase id if the purchase is successful. use the same code snippet specified for “item purchase.” for more information, check the implementation of item purchase section. implementation of the samsung iap orders api the samsung iap orders api is used to view all payments and refunds on a specific date. it does this by fetching the payments and refunds history within the date you specified. let’s implement the samsung iap orders api and create a server to listen to its notifications. through server-to-server communication, the api returns all orders data for the application. configuring the server you can develop a spring boot server for this purpose. here are the guidelines on how to set up this server: set up a spring boot project. for more information, follow the steps on developing your first spring boot application. set up your server endpoint: create a controller for the samsung iap orders api in an integrated development environment (ide) after importing the spring boot project you created. this helps managing all in-app order-related activities and processing them within your application. the controller receives post requests sent from samsung’s iap orders service ensuring the communication with your application. get payment and refund history to view all payments and refunds: you must make a post request to the samsung iap orders api endpoint with the required headers specified below. if you specify a date, all the payment history for this date is returned. otherwise, it only returns all the data from the day before the current date. api endpoint: https://devapi.samsungapps.com/iap/seller/orders method: post headers: add the following fields to the request header. for more information, see the create an access token page, which helps you understand how to create the access token in detail. the token is used for authorization. you can also get the service account id by clicking the assistance > api service tabs on seller portal. for more details, read the section create a service account and visit seller portal. header name description required/optional values content-type format of the request body required application/json authorization authorization security header required bearer: access_token service account id this id can be created in seller portal and is used to generate the json web token (jwt) required service-account-id parameters: the following parameters can be used to build your post request. name type required/optional description sellerseq string required your seller deeplink, which is found in your profile in seller portal and consists of a 12-digit number. packagename string optional used to view payment and refund data. you can provide the application package name. when a package name is not specified, the data for all applications is shown. requestdate string optional specify a date from which to view the payment and refund data. if the date is not specified, the data from a day before your current date is returned. continuationtoken string optional use this if you want to check if there is a continuation for the data on the next page. if there is no more data, the response is null. to implement rest api support, add the following okhttp library dependencies to your application's build.gradle file: implementation 'com.squareup.okhttp3:okhttp: version' implementation 'com.google.code.gson:gson: version' a detailed description of the request items can be found in the request section of the samsung iap orders api documentation. for more information on the server communication, see samsung iap server api. here is a brief summary of the code below: a post request is mapped to the /orders url, which logs the request. the previously described parameters containing the data you specified are formatted in a json body using the string.format() method. the outgoing request is logged in a json body format. a requestbody is instantiated containing the json data, formatted for an http request to be sent to the server with the specified token and service account id. this code also handles multiple results your request can return: the onfailure() method is called when the network request fails for some reason, providing any error details using the ioexception exception. if the request succeeds, the onresponse() method returns the response body or any response exception found. @restcontroller @requestmapping(value = "/iap", method = requestmethod.post) public class orderscontroller { private final okhttpclient client = new okhttpclient(); @getmapping("/orders") public void sendtoserver() { system.out.println("post request received"); // log the request // define parameters values, use according to your requirement // string packagename = "com.example.app_name "; // string requestdate = "20240615"; // string continuationtoken = "xxxxxxxxxxx…….xxxxxx"; string sellerseq = "0000000xxxxx"; // create the json body, use packagename, requestdate, continuationtoken according to your requirement string jsonbody = string.format( "{\"sellerseq\":\"%s\"}", sellerseq ); // create the request body requestbody body = requestbody.create(jsonbody, mediatype.parse("application/json; charset=utf-8")); // access token string token = "0djt9yzryukdogbvulxxxxxx"; // build the request request request = new request.builder() .url("https://devapi.samsungapps.com/iap/seller/orders") .post(body) .addheader("authorization","bearer " + token) .addheader("service-account-id", "85412253-21b2-4d84-8ff5-xxxxxxxxxxxx") .addheader("content-type", "application/json") .build(); client.newcall(request).enqueue(new callback() { @override public void onfailure(@notnull call call, @notnull ioexception e) { system.err.println("request failed: " + e.getmessage()); } @override public void onresponse(@notnull call call, @notnull response response) throws ioexception { if (response.issuccessful()) { string responsebody = response.body().string(); system.out.println("response: " + responsebody); } else { system.err.println("unexpected response code: " + response.code()); system.err.println("response body: " + response.body().string()); } response.close(); // close the response body } }); } } congratulations! you have just built the spring boot server to handle api post requests using the okhttpclient to manage http requests and responses for your sample application. example response as previously mentioned, a json-formatted response is returned to your request. for detailed descriptions of each response body element, see the “response” section of the samsung iap orders api documentation. the following output format is a sample in which only some of the response-body data is presented. in this case, the continuationtoken parameter key returns null because there is no continuation for the data on the next page. the orderitemlist parameter key lists all the orders with specific details, such as orderid, countryid, packagename, among others. { "continuationtoken": null, "orderitemlist": [ { "orderid": "s20230210kr019xxxxx", "purchaseid": "a778b928b32ed0871958e8bcfb757e54f0bc894fa8df7dd8dbb553cxxxxxxxx", "contentid": "000005059xxx", "countryid": "usa", "packagename": "com.abc.xyz" }, { "orderid": "s20230210kr019xxxxx", "purchaseid": "90a5df78f7815623eb34f567eb0413fb0209bb04dad1367d7877edxxxxxxxx", "contentid": "000005059xxx", "countryid": "usa", "packagename": "com.abc.xyz" }, ] } usually, the responses contain all the relevant information about user purchases, such as the in-app item title, price, and payment status. therefore, you can use the information and create views for an easier order management. noteif the iap operating mode is configured to test mode, the api response is empty. this is because the api is configured to operate and return a response only in production mode. conclusion you have learned how to implement item purchase, consumption, and registration, as well as how to integrate the samsung iap orders api and configure a server to fetch all the payment and refund history within specific dates. integrating the samsung iap orders api functionality into your server is an essential step in managing your application payments history to ensure a seamless experience to users. now, you can implement the samsung iap orders api into your application to track all payments, refunds and make your business more manageable. related resources for additional information on this topic, see the resources below: download the android sample application source code add samsung in-app purchase service to your app samsung iap orders api integration of samsung iap services in android apps