Skip to main content
Samsung Developer Program

Initiating IAP Purchase and Payment and Displaying the Results

This section describes how to initiate the purchase of an in-app item offered by your Android app, have Samsung IAP guide the user through IAP purchase and payment, and return information about the results (either successful or failed). 

To initiate IAP purchase and payment and display the results:

Your app logic and code specifies a UI button that initiates IAP purchase and payment.
In [Your main activity].xml file, add code for a purchase UI button that:

  • Specifies the item ID of the item to be purchased.
  • Calls the method that makes the startPayment call.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp" >
      <!-- 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>

In the example code, the purchase UI button calls the doPurchase method when clicked.
 

Your app logic and code, upon being notified of the user clicking the purchase UI button, initiates IAP processing of the item to be purchased.
In [Your main activity].java file, add code that:

  • Specifies the IAP operating mode.
  • Specifies the purchase item ID.
    Note: EVERY item being purchased MUST use a valid ID issued by the Samsung Seller Office, even an item ID hard-coded in development code (ITEM_ID="Nuclear").
  • Creates a purchase instance (via getInstance).
  • Initiates IAP purchase and payment (via startPayment in the doPurchase method).
  • Implements the OnPaymentListener.
  • Specifies whether or not the results of IAP processing are to be displayed.
public class MainActivity extends Activity implements OnPaymentListener
{
    private static final int    MODE  =
                                   SamsungIapHelper.IAP_MODE_COMMERCIAL;
    // Please change the ITEM_ID for your application.
    // =================================================================
    private static final String ITEM_ID  = " Nuclear";
    // =================================================================
    private SamsungIapHelper    mIapHelper  = null;

       @Override
    protected void onCreate( Bundle savedInstanceState )
    {
        super.onCreate(savedInstanceState);
        setContentView( R.layout.activity_main );       
        mIapHelper = SamsungIapHelper.getInstance( this, MODE );
    }
   
    public void doPurchase( View _view )
    {
        mIapHelper.startPayment( ITEM_ID, true, this );
    }   

SamsungIapHelper is created by the onCreate method. In the example code, the item ID is hard-coded. However, in release code, item IDs are assigned by and retrieved from the Seller Office. When the example code is run, it will run under the commercial IAP operating mode (MODE=IAP_MODE_COMMERCIAL).

In the doPurchase method, the startPayment method is called, and the payment request is sent to IAP in just one line. In the startPayment method, the third argument (this) is explicitly inserted because the activity implements OnPaymentListener, which is the interface for the callback onPayment method that receives payment results.

The initialization and purchase and payment processes have been simplified and are now managed within the IAP Helper; only the payment result enters the onPayment interface. 

Note: startPayment ( String, String, Boolean, OnPaymentListener) has been deprecated. Instead, use: startPayment(String, boolean, OnPaymentListener).

The example specifies that the results are to be displayed (true in startPayment(ITEM_ID, true, this)). The next step specifies how the results are to be displayed.

When purchase results are to be displayed, your app logic and code specifies how to display successful and failed results of the IAP purchase and payment processing.
In [Your main activity].java file, add code that:

  • Specifies how successful and failed IAP processing results are to be displayed.
  • Specifies related processing (such as dialog box buttons).
    @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 is finished Successfully
            // =========================================================
            if( _errorVo.getErrorCode() ==
                                        SamsungIapHelper.IAP_ERROR_NONE )
            {
                alert.setTitle( _errorVo.getErrorString() );
                alert.setMessage( _purchaseVo.dump() );
            }
            // =========================================================
           
            // If Payment is failed
            // =========================================================
            else
            {
                alert.setTitle( "ERROR" );
                alert.setMessage( _errorVo.getErrorString() );
            }
            // =========================================================
        }
        else
        {
            alert.setTitle( "ERROR" );
            alert.setMessage( "ErrorVo is null" );
        }
       
        alert.show();
    }

In the example code, the onPayment method specifies that the results are to be displayed in an alert dialog box: Upon successful processing, PurchaseVo information is displayed, or upon failed processing, errorVo information (see Error Response Codes). After the user reviews the popup information, they click the setPositiveButton to return to your app purchase screen.

To test the IAP purchase and payment code:

Note: Run the test under both Development Test Mode (Success) and Development Test Mode (Failed).

Figure 4     Purchasing and Paying for an Item via IAP

  1. On a test device in Android Studio 1.1.0:
    1. Set the IAP operating mode (_mode) to the appropriate test mode: Developer Test Mode (Success), or Developer Test Mode (Failure).
    2. Run the purchasing code.
  2.  In the main screen, click Purchase One Item.
  3. In the Confirm Password screen:
    1. Enter your password.
    2. Click Confirm.
  4. In the payment method screen:
    1. Select the payment method.
    2. Click Buy. IAP completes the purchase processing.
  5. In the Item List screen:
    1. For the specified Development Test Mode, verify the appropriate popup is displayed.
      1. When IAP processing is successful, verify the successful purchase result popup is displayed.
      2. When IAP processing failed, verify the failure purchase result popup is displayed.
    2. In the purchase result popup:
      1. Click OK.
      2. Verify that the popup is removed and the Item List screen is active.
    3. In the Item List, click Confirm.
  6. Verify your app purchase screen is displayed and active.
  • Was this article helpful?