Alipay, China's leading third-party online payment solutionAlipay, China's leading third-party online payment solution

Android

In this topic, you'll learn how to integrate the pop-up payment experience SDK into an Android client. This will help you to render cashier pages in a mobile application.

 

Prerequisites

Before the integration, ensure that you have completed the following tasks:

  • Install the latest version of Android Studio.
  • Target at least API level 19 or later.
  • Use Gradle 4.1 or later.
  • Configure a physical machine or simulator to run your application.

 

Key integration steps

Integrate the SDK by the following steps:

1

Integrate the SDK package

Merchant client

To integrate the SDK package, please refer to Integrate the SDK Package for Android.

 

 

2

Create an SDK instance by using AMSCashierPayment

Merchant client

The process of creating an SDK instance includes the following steps:

  1. Create a config object: Required. The object must contain all of the following configuration parameters:
    • setLocale: Optional. String type. It is used to pass in language information. Valid values are listed as follows. If other values are passed, English is used by default:
      • en_US: English
      • pt_BR: Portuguese
      • ko_KR: Korean
      • es_ES: Spanish
      • fr_FR: French
      • nl_NL: Dutch
      • it_IT: Italian
      • de_DE: German
      • zh_CN: Simplified Chinese
      • zh_HK: Traditional Chinese
    • setOption: Optional, used to set whether to adopt the default loading style and whether to use a sandbox environment. Supported values include the following:
      • "sandbox", "true": Sandbox environment.
      • "sandbox", "false": Production Environment.
      • "showLoading", "true": Use the default loading style.
      • "showLoading", "false": Do not use the default loading style.
      • "notRedirectAfterComplete", "true": Optional, Boolean type. The default value is false, which means it will jump back to your page after the payment is completed. The same applies when the value is empty. true value means there will be no jump after the payment is completed. You need to use the customer event code to control the card binding to complete the subsequent process. Please note that the payment result event code returned by the client is only used as a reference for the jump operation of the client page. For transaction status updates, please refer to the results returned by the server's notifyPayment or inquiryPayment API.
      • "merchantAppointmentParam": Optional object used for setting custom features. It contains a value below:

        • storedCard: Optional object used for setting custom features of stored cards. It contains a value below:
          • ​​needCVV: Optional Boolean. The default value is false, which indicates that the user do not need to input CVV number during the payment process. The same applies when the value is empty. true value means the user need to input CVV number during the payment process.
  2. Create an instance of the onCheckoutListener API for handling corresponding events in subsequent processes, including the following method:
    • onEventCallback: Required. A callback function that returns various types of data or values, including event codes, cardPaymentResultInfo parameter for card payment, and popRiskDecisionResultInfo parameter for risk control. See the References for further information.
  3. Set the instance of the onCheckoutListener API to the configuration instance for executing event callbacks.
  4. Instantiate the AMSCashierPayment.
Create an SDK instance:
Java
Editor
3

Initiate a createPaymentSession request

Merchant server

The merchant client monitors the click events of the payment button itself. When the buyer selects the payment method and clicks the payment button, the merchant client initiates createPaymentSession request to the APO server. Once the response to the createPaymentSession request is received, the paymentSessionData value in the response is used in Step 4.

For card token payment mode with bound cards, you need to pass in the paymentMethod.paymentMethodId field to the card token returned to you by APO after the buyer binds the card. The way to obtain the card token is as follows:

  • Independent card binding: Obtain the card token through the card.cardToken field in the asset binding result notification sent to you by APO.
  • Non-independent card binding: Obtain the card token through the paymentResultInfo.cardToken field in the payment result notification sent to you by APO or the response to actively query the payment result.

Note: When calling the createPaymentSession API, the value of the request parameter paymentRedirectUrl needs to use the URL Scheme corresponding to the page you provide after the payment is completed.

The following createPaymentSession request example only includes the required fields and some optional fields:
JSON: No card bound
JSON: Token payment
Editor
Response example for the createPaymentSession request:
JSON
Editor
4

Create a payment element collection component using the createComponent method in the instance object.

Merchant client
  • Call the createComponent() method and pass in the following parameters:
    • activity: Required. Activity type object, used to contain contextual parameter information for the current page.
    • sessionData: Required. String type. Pass in the complete data of the paymentSessionData field obtained from the response of the createPaymentSession request into the sessionData parameter.
  • Free resources of the SDK component by calling the onDestroy() method in the instance object. You need to unmount a component in the following cases:

    • When the user exits the checkout page, free the component resources created in the createPaymentSession .
    • When the user initiates multiple payments, free the component resources created in the previous createPaymentSession .
Create a configuration object:
Java
Editor
References

The References offers callback data and code samples for integration steps. For further details, refer to the References.