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 card payment SDK into an Android client so that you can render cashier pages in a mobile application.

Prerequisites

Before integrating the card payment SDK, familiarize yourself with the Integration guide and Overview. This will enable you to understand the steps for integrating server APIs and notes for calling the APIs. Also, ensure that you have completed the following tasks:

  • Obtain a client ID in Alipay Developer Center.
  • Properly configure keys in Alipay Developer Center.
  • Install the latest version of Android Studio.
  • Target at least Android 4.4 (API level 19) or higher.
  • Use Gradle 4.1 or earlier.
  • Use AndroidX.
  • Configure a physical machine or simulator to run your application.
Key integration steps

Integrate the card payment SDK by following these steps:

1

Import the SDK package

Merchant client

After you download the resource package in Alipay Developer Center, perform the following steps:

  • Move the unzipped .aar file into the libs file of your project.
  • Declare the reference of the .aar file in the build.gradle file in the project.
  • Add dependencies to the build.gradle file.
Add dependencies to the build.gradle file.
build.gradle
Editor
2

Create an SDK instance by using the AMSCheckout()method:

Merchant client

Follow these steps to create an SDK instance:

  1. Create a configuration object: A required object. The object must contain all of the following configuration parameters:
    • locale: An optional string. It is used by the merchant client to identify the browser language and pass in the language information. The SDK uses the information to provide cashier pages in the corresponding language. Valid values are listed as follows. If other values are passed, English is used by default:
      • locale("es", "ES"): Spanish

      • locale("ko", "KR"): Korean

      • locale("pt", "BR"): Portuguese

      • locale("en", "US"): English

    • envType: A required string belonging to the AMSEnvTypeEnum type. It is used to pass in environmental information. Valid values are:
      • SANDBOX: sandbox environment
      • PROD: production environment
  2. Create an instance of the OnCheckoutListener API, which is used for event handling in the subsequent process. The API includes the following method:
    • OnPaymentEventCallback(): Required. It is a callback function that monitors payment events on cashier pages. It returns eventCode and message.
  3. Set the instance of the OnCheckoutListener API in that of the configuration object to execute event callbacks.
  4. Instantiate the AMSCheckout method.
Create an SDK instance:
Java
Editor
3

Send a createPaymentSession request to the Alipay server.

Merchant client

After the buyer selects a card payment method for payment, your client starts monitoring the payment button click event. When your client detects that the payment button is clicked, your server needs to send a createPaymentSession request to the Alipay server. After you receive the response from the API call, use the value of the paymentSessionData parameter to complete Step 4.

Note: In your createPaymentSession request, specify paymentRedirectUrl as the URL Scheme that you provide to redirect buyers to the payment result page upon payment completion.

The following sample for calling the createPaymentSession API includes mandatory parameters and several optional parameters.
JSON
Editor
The response sample of the API call is shown as follows:
JSON
Editor
4

Create a component for collecting payment factors by using the createComponent method in the instantiated object

Merchant client

Call the createComponent method and pass in the following parameters:

  • activity: A required object belonging to the Activity type. It is used to include the information about context parameters on the current page.
  • paymentType: A required string. The fixed value is CARD.
  • paymentSessionData: A required string. Pass the complete data in the paymentSessionData parameter obtained in the response through the createPaymentSession API to the paymentSessionData parameter.
  • appearance: A required parameter belonging to the AMSPaymentAppearance type. This parameter is used for extensions. A valid value is null.
Create a configuration object:
JavaScript
Editor
Reference
Event codes

The card payment SDK provides the following event codes:

  • SDK_INTERNAL_ERROR: A popup on a cashier page failed to be loaded.

  • SDK_CALL_URL_ERROR: Represents one of the following cases:

    • The redirection to the merchant page failed to be executed.

    • The parameter paymentRedirectUrl in your createPaymentSession request is not passed or passed incorrectly.

  • SDK_CREATECOMPONENT_ERROR: The card component failed to be created. Use the correct parameters to call the createComponent method again.

Code sample in key steps

The following code sample shows the key steps during integration. The sample does not include the code used for calling the createPaymentSession API.

Java
Editor