Alipay online and in-store payment products offer a set of APIs that provide the ability to integrate with Alipay. You can use thePOSTmethod to send HTTPS requests and receive responses accordingly.
Before you make any payments, it is important to understand how Alipay APIs work and how requests and responses are structured. This section presents general information (such as message structure and message transmission workflow)of online messages between your system and Alipay. A message refers to the request message or the response message.
Request structure
The following figure illustrates the request message structure:
Figure 1. Request structure
URL
The request URL ishttps://{domain name}/ams/api/{version}/{endpoint}, which has the following structure:
domain name: is the standarddomain nameassignedby the wallet backend.
version: is the APIversion, for example,v1orv2.
endpoint: is theendpointto the interface,for example,/{version}/payments/pay.
An interface can be uniquely identified by its endpoint. For example, the/v1/payments/payis different from/v2/payments/pay.
HTTPS method
POST
Request header
The request header mainly contains the following fields. Note: Field names are case-insensitive.
For details of each header field, see the following descriptions.
signatureRequired
signaturecontainskey-value pairs that are separated by comma (,). Each key-value pair is an equation, which is a key joined with its value with an equal sign (=). For details about how to generate a signature, see theGenerate a signaturesection.
The following keys can be configured:
algorithm: Specifies the digital signature algorithm that is used to generate the signature.RSA256is supported.
keyVersion: Specifies the key version that is used to generate or validate the signature. By default, the value is the latest version of the key associated withClient-Id.
signature: Contains the signature value of the request.
Content-Typeindicates the media type of the body of the request, as defined byRFC2616. In which,charsetis used for generating/validating a signature.
For example:
copy
Content-Type: application/json; charset=UTF-8
client-idRequired
client-idis used to identify a client, and is associated with the keys that are used for the signature. For details about how to obtain a client ID, seeAlipay Developers Center user guide.
request-timeRequired
Specify the timestamp when the request is sent. The value of this field must be accurate to milliseconds. Use the following methods of different programming languages to get the timestamp:
The request body contains the detailedrequest information in JSON format. Fields enclosed in the request body section vary depending on services. For more information,see instructions on the specific API specification.
Response structure
The following figure illustrates the response structure:
Figure 2. Response structure
Responseheader
The response header carries the information about the response, mainly containing the following fields.
For details of each header field, see the following descriptions.
signatureRequired
signaturecontainskey-value pairs that are separated by comma (,). Each key-value pair is an equation, which is a key joined with its value with an equal sign (=). For details about how to generate a signature, see theGenerate a signaturesection.
The following keys can be configured:
algorithm: Specifies the digital signature algorithm that is used to generate the signature.RSA256is supported.
keyVersion: Specifies the key version that is used to generate or validate the signature. By default, the value is the latest version of the key associated withclient-id.
signature: Contains the signature value of the request.
Content-Typeindicates the media type of the body of the request, as defined byRFC2616. In which,charsetis used for generating/validating a signature.
For example:
copy
Content-Type: application/json; charset=UTF-8
client-idRequired
client-idis used to identify a client, and is associated with the keys that are used for the signature. For details about how to obtain a client ID, seeAlipay Developers Center user guide.
Response-time Required
Specifies the time when the response is sent, as defined byISO 8601.
Note: This field must be accurate to seconds.
copy
response-time: 2019-04-04T14:08:56+05:30
Responsebody
The response body containsthe information responding to the client. Fields in this section vary depending on services. However, theresultfield, which indicates the result of an API call, is always contained.
When the result status (resultStatus) is failed, the result code (resultCode) is an error code, the result message (resultMessage) is an error message, which is used for troubleshooting. For more information about how to resolve errors, seeResult/Error codes in the specific API.
Field
Data type
Required
Description
resultStatus
String
No
Result status. Valid values are:
S : Successful
F : Failed
U : Unknown
resultCode
String (64)
No
Result code
resultMessage
String (256)
No
Result message that describes the result code and status in details
Table 4. Response body
Message transmission workflow
The whole interaction sequence is illustrated below:
Figure 3. Message transmission workflow
Overall procedure
Follow the overall procedure to call an API.
Preparations
To prevent some potential errors that you might get in the response, consider the following factors:
To prevent potential errors that you might get in the response, understandAPI idempotency.
Encode a request that contains special characters.
1. Construct a request
Construct arequest bycomplying with therequest structure, for example,by adding theclient-Id,request-time,signature, and other fields to the request header.
To ensure the message transmission security, perform the following security measureswhen constructing a request:
Sign the request message. Message signing and signature validation are required for all requests and responses.For more information, see sign a requestand validate the signature.
Encode the request to prevent errors or ambiguity that might be caused by special characters enclosed in the request. For details, seeMessage encoding.
2. Send a request
You can send a request with your preferred platform or tool, for example, via Postman or cURL command.
threeCheck the response
The response is usually returned in JSON or XML format.For details about the response, see theResponse structuresection. After you receive the response, validate the signature of the response.
fourCheck the status code
The response data varies depending on the services. However, theresultfield, which indicates the result of an API call, is always contained. If an error occurs, an error response is returned, where theresultobjectindicates the error code and error message for you to troubleshoot issues.