Web/WAP
In this topic, you'll learn how to integrate the card payment SDK into a web or WAP client so that you can render cashier pages in a computer browser or a mobile browser.
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.
- Properly configure keys.
- Configure a URL for receiving an asynchronous notification.
- Install npm (Node Package Manager).
- Properly handle compatibility issues: Provide corresponding polyfills for Internet Explorer and other old browser versions. We recommend that you use babel-preset-env to address browser compatibility issues when you build a project.
- Use the following recommended browser versions:
- For mobile browsers:
- iOS 9 and above
- Android 5.0 and above
- For computer browsers, use the following recommended versions:
- For mobile browsers:
Integrate the card payment SDK by following these steps:
Import the SDK package
You can use either of the following methods to add an SDK package:
- Import the Web SDK package into your project by using npm: Input the install command in the command line interface.
- Use CDN resources: Use the script tag in a HTML file.
Create an SDK instance by using the AMSCheckout()
method:
- Create a config object: Required. The object must contain all of the following configuration parameters:
- locale: An optional string. 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
-
- environment: A required string. It is used to pass in environmental information. Valid values are:
sandbox
: sandbox environmentprod
: production environment
- mode: A required string. It is used to specify the display mode of the cashier page. A valid value is:
component
: used to display pre-built payment method components in your application.
- networkMode: A required string. It is used to specify a network mode. A valid value is:
session
: In this mode, your server only needs to send a request through the createPaymentSession API once to communicate the payment data between your client and the Alipay server.
- analytics: an optional object. It is used to configure and analyze data. It contains the value:
- enabled: an optional Boolean. It defaults to
true
, which indicates that you allow the SDK to upload and analyze operational data to deliver a better service. If you do not allow the data to be uploaded and analyzed, specify it asfalse
.
- enabled: an optional Boolean. It defaults to
onLog
: Optional. It is a callback function that is used to generate the error information about log and API exceptions during the execution of the SDK.onEventCallback
: Optional. It is a callback function that is used to generate the end event about payment exceptions during the execution of the SDK, such as a payment failure and a 3D verification failure.
- locale: An optional string. It is used to pass in language information. Valid values are listed as follows. If other values are passed, English is used by default:
- 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. After you receive the response from the API call, use the value of the paymentSessionData parameter to complete Step 4.
Create a component for collecting payment factors by using the createComponent
method in the instantiated object:
- Create a configuration object by using the paymentSessionData parameter: Pass the complete data in the paymentSessionData parameter obtained in the response through the createPaymentSession API to the paymentSessionData parameter.
- Call the
createComponent
method in the instantiated object. The return value isPromise
, and the content is the instantiated object. The DOM node in the payment component can render the card payment methods according to the environmental parameters and display them on the current page.
The card payment SDK provides the following event codes:
SDK_INTERNAL_ERROR
: represents one of the following cases:- The data returned from the server does not follow JSON format. Contact Alipay Technical Support for help.
- The browser does not support the postMessage communication mode. Replace or update the browser.
- The iFrame element cannot be created. Replace or update the browser, or try again later.
SDK_INIT_PARAMETER_ERROR
: represents one of the following cases:- The environmental information passed during initialization is incorrect. Check the value of the environment parameter.
- The mode information passed during initialization is incorrect. Check the value of the mode parameter.
- The language information passed during initialization is incorrect. Check the value of the local parameter.
- The network information passed during initialization is incorrect. Check the value of the networkMode parameter.
- The
onlog
method passed during initialization is incorrect. Check whetheronlog
is a method.
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.