Skip to main content
Samsung Developer Program

Initiate and Verify In-App Item Purchases

How to start the purchase of an in-app item offered by your Android app and selected by the user, have IAP guide the user through purchase and payment transaction, and return information about the results (either successful or failed)

 

Initiate In-App Item Purchases

The example code in the first procedure demonstrates how to: set up a purchase UI button, submit a purchase request for a hard coded in-app item ID, receive the results of the purchase transaction, and display a results message. 

For more details about IAP Helper API elements, see:

 

To enable a user to purchase an in-app item:

  1. Add a purchase UI button that calls doPurchase() to the layout in the activity_main.xml file.
    <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android" 
        android:orientation="vertical" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"> 
        
        <!-- Purchase Button --> 
        <Button 
            android:id="@+id/btn_purchase_one_item" 
            android:layout_width="match_parent" 
            android:layout_height="wrap_content" 
            android:text="@string/purchase_one_item" 
            android:onClick="doPurchase" /> 
            
    </LinearLayout>


     
  2. Add the ID of the in-app item to be purchased, a purchase request, and result processing to the MainActivity.java class.
    ITEM_ID hard codes the ID of the in-app item. doPurchase() callsstartPayment() that specifies the in-app item purchase and sends a request to Samsung IAP. The fourth argument ('this') refers to the OnPaymentListener interface for the onPayment() callback method that receives in-app item information, and transaction and processing results. 
    public class MainActivity extends AppCompatActivity implements OnPaymentListener { 
        // Please change the ITEM_ID for your application. 
        // ======================================================================== 
        private static final String ITEM_ID = "Nuclear"; 
        // ======================================================================== 
        
        private IapHelper mIapHelper = null; 
        
        @Override 
        protected void onCreate(Bundle savedInstanceState) { 
            super.onCreate(savedInstanceState); 
            setContentView(R.layout.activity_main); 
            
            mIapHelper = IapHelper.getInstance(this); 
            mIapHelper.setOperationMode(OPERATION_MODE_PRODUCTION); 
        } 
        
        public void doPurchase(View _view) { 
            // If specified, the passThroughParam value must be generated you. 
            mIapHelper.startPayment(ITEM_ID, "pLKjLKjLJL87=76df56rf+4f5", true, this); 
        } 
        
        @Override 
        public void onPayment(ErrorVo _errorVO, PurchaseVo _purchaseVO) { 
        
        // Success or failure of payment is processed here. 
        
        }
    }


     
  3. Process and display the startPayment() purchase request results in an AlertDialog by implementing the onPayment() callback method in the OnPaymentListener interface.
    onPayment()gets startPayment() request results and sets up an alert. Based on the results, one of the following is displayed: PurchaseVo object information about the purchase in-app item and transaction when it succeeds, an ErrorVo object error message when the transaction is reported as failed (for example, the user did not authorize payment), or the message 'ErrorVo is null' when request status results were not received (for example, processing failed unexpectedly).
    @Override 
    public void onPayment(ErrorVo _errorVO, PurchaseVo _purchaseVO) { 
    
        AlertDialog.Builder alert = new AlertDialog.Builder(this); 
        
        alert.setPositiveButton(android.R.string.ok, 
                                new DialogInterface.OnClickListener() { 
            @Override 
            public void onClick(DialogInterface dialog, int which) { 
                                dialog.dismiss(); 
            } 
        }); 
        
    if (_errorVO != null) { 
            
        // If payment was successful. 
        // ================================================================ 
        if (_errorVO.getErrorCode() == IapHelper.IAP_ERROR_NONE) { 
            if (_purchaseVO != null) { 
                alert.setTitle(_errorVO.getErrorString()); 
                alert.setMessage(_purchaseVO.dump()); 
            } 
        } 
        // ================================================================ 
        
        // If payment failed. 
        // ================================================================ 
        else { 
            alert.setTitle("ERROR"); 
            alert.setMessage(_errorVO.getErrorString()); 
        } 
        // ================================================================ 
    } 
    else { 
        alert.setTitle("ERROR"); 
        alert.setMessage("ErrorVo is null");
    }

 

 

To test your purchase code: 

  • We recommend you run the test in both test modes: Developer Test Mode (Success) and Developer Test Mode (Failed).
  • In your Android app code:
    • Set the IAP operating mode to the appropriate test mode.
    • All item IDs must match the IDs of registered in-app items. 
  • Your Android app and the in-app items must be registered in Seller Office.
  • Run your code.
    Note: The following steps 1. to 6.a. apply when your code implements item purchase similar to the example code in the section above and the method implemented in the IAP5Sample code. Steps 6.b. to 7.a. apply to most code implementations.


InAppItem_Purchase.png

  1. In the main screen, click Purchase One Item.  
  2. In the Confirm Password screen, enter your Samsung account password.
  3. Click Confirm
  4. In the Purchase screen, select a payment method. 
  5. Click Buy
  6. When the payment and purchase transaction is successful:
    1. Verify that the confirmation popup is displayed.
      InAppItem_Purchase_SuccessfulPopup.png
       
    2. Review the PurchaseVo object received by OnPaymentListener and verify that it contains correct details of the in-app item and the transaction.
    3. Review the errorVo object received by OnPaymentListener and verify that it indicates the status IAP_ERROR_NONE.
       
  7. When the payment and purchase transaction fails:
    1. Review the errorVo object received by OnPaymentListener and verify that it indicates an error status.
       

 

Verify In-App Item Purchases

IAP Helper enables in-app item purchase verification that involves only your Android app (client verification).  

During a successful in-app item purchase and payment transaction, Samsung IAP assigns a unique purchaseId. Your Android app receives the purchase ID and additional purchase and in-app item information in the PurchaseVo object of the onPaymentListener interface.

However, in certain scenarios, your Android app may not receive the PurchaseVo object (for example, due to network issues), although the purchase and payment transaction were successful. Your Android app can get the same fundamental purchase information by making a getOwnList() request to get theOwnedProductVo object of the OnGetOwnedListListener interface. 

SamsungIAP_v5_Architecture_ClientPurchaseVerification.png

Your Android app can verify an in-app item purchase by using item data registered in Seller Office (which must be provided by means that are independent of Samsung IAP), information your app used to make the item's startPayment() request, and PurchaseVo or OwnedProductVo object information. 

Client verification can be based on the following and other data:

PurchaseVo and OwnedProductVo
Objects 

Condition

startPayment()
Request

Registered item data

mItemId matches    _itemId  
mPaymentId value is specified    
mItemPrice matches   Item's local price at the time of the purchase

 

Note: We recommend that your Android app and your app server perform the more secure server verification of item purchases.  For details, see Enhanced Verification of In-App Item Purchases.
 

  • Was this article helpful?