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.
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.
Integrate the card payment SDK by following these steps:
Import the SDK package
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.
Create an SDK instance by using the AMSCheckout()
method:
Follow these steps to create an SDK instance:
- 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 environmentPROD
: production environment
- 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:
- 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.
- Set the instance of the OnCheckoutListener API in that of the configuration object to execute event callbacks.
- Instantiate the
AMSCheckout
method.
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.
Create a component for collecting payment factors by using the createComponent
method in the instantiated object
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
.
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 thecreateComponent
method again.
The following code sample shows the key steps during integration. The sample does not include the code used for calling the createPaymentSession API.