For the system API calls, in general, the validations are being done at two levels on Alipay side.
The first level is at the API gateway level, where it does certain sanity checks such as verifying the signature, verifying if the partner ID is valid or whether the partner has permission to call this particular API, etc. If the validation fails, Alipay would return the appropriate error codes which are classified as the 'API Gateway Error Codes' as in the below.
Once it passes the validations at the API gateway level, the API request would be dispatched to the internal system for further processing, which it would be subjected to the validations of the business logic. The corresponding error codes returned are classified as the 'Business Error Codes".
Business Logic Errors
Error Codes
Descriptions
TRADE_NOT_EXIST
Trade not exist. out_trade_no or Alipay trade_no is incorrect
ILLEGAL_SIGN
Illegal signature.
ILLEGAL_DYN_MD5_KEY
Dynamic key information is incorrect.
ILLEGAL_ENCRYPT
Encryption is incorrect.
ILLEGAL_ARGUMENT
Parameter is incorrect.
ILLEGAL_SERVICE
Service parameter is incorrect.
ILLEGAL_USER
User ID is incorrect.
ILLEGAL_PARTNER
Partner ID is incorrect.
ILLEGAL_EXTERFACE
Interface configuration is incorrect.
ILLEGAL_PARTNER_EXTERFACE
Partner’s interface information is incorrect.
ILLEGAL_SECURITY_PROFILE
Matching private key configuration has not been found.
ILLEGAL_AGENT
Agency ID is incorrect.
ILLEGAL_SIGN_TYPE
The signature type is incorrect.
ILLEGAL_CHARSET
The character set is illegal.
ILLEGAL_CLIENT_IP
Client IP address is illegal
HAS_NO_PRIVILEGE
Has no right to visit.
ILLEGAL_DIGEST_TYPE
Digest type is illegal
ILLEGAL_DIGEST
Digest is illegal
ILLEGAL_FILE_FORMAT
File format is illegal
ILLEGAL_ENCODING
Encoding type is illegal
EXTERFACE_IS_CLOSED
API is closed
ILLEGAL_REQUEST_REFERER
Anti-phishing checks illegal request
ILLEGAL_ANTI_PHISHING_KEY
Anti-phishing checks illegal timeframe
ANTI_PHISHING_KEY_TIMEOUT
Anti-phishing checks timeframe timeout
ILLEGAL_EXTER_INVOKE_IP
IP Anti-phishing checks illegal IP
System Errors
When system error occurs, please contact Alipay Technical Support to assist the error repair..
Returned result
Description
SYSTEM_ERROR
Alipay system failed to process the request due to temporary internal glitch.
SESSION_TIMEOUT
Session timeout
ILLEGAL_TARGET_SERVICE
Wrong target service
ILLEGAL_ACCESS_SWITCH_SYSTEM
Merchant is not allowed to visit system of this type.
EXTERFACE_IS_CLOSED
The interface has been closed.
Payment Type
Type
Description
01
Coupon Fee Pre-payment
02
Coupon Fee Payment
03
Reminding Payment
04
Automatic Sending Goods
1
Merchandise
2
Service Purchase
3
Online Auction
4
Donation
5
Post Fee Compensation
6
Bonus
7
Funds Purchase
8
Air Ticket
9
Go Dutch
10
Group Purchase
11
Electronic Ticket
12
Lottery Ticket
13
Auction
14
Mobile Payment
15
Flowers & Gifts
16
Agent Electronic Ticket
17
Party Membership Dues
18
Foreign Exchange
19
Automatic Charge
20
Refund of Overseas Payment
21
Refund of Instant Payment
22
Business Deposit
24
Cash Gift
25
Rent
26
Motopay
23
Shopping Chart
27
Escrow Payment of Group Purchase
Transaction Status
Status
Description
WAIT_BUYER_PAY
Transaction awaits user payment.
WAIT_SELLER_SEND_GOODS
Transaction awaits seller sending goods.
WAIT_BUYER_CONFIRM_GOODS
Transaction awaits buyer confirming goods.
TRADE_FINISHED
Transaction is finished successfully.
TRADE_CLOSED
Transaction is closed during processing (finished, not successfully)
WAIT_SYS_CONFIRM_PAY
Transaction awaits system conforming payment, please do not send goods.
WAIT_SYS_PAY_SELLER
Buyer confirm goods, transaction awaits system paying to seller.
TRADE_REFUSE
Transaction refused.
TRADE_REFUSE_DEALING
Transaction refusing.
TRADE_CANCEL
Transaction canceled.
TRADE_PENDING
Pending Transaction.
TRADE_SUCCESS
Transaction complete, and available for refund
BUYER_PRE_AUTH
Buyer has paid. (IVR Payment)
COD_WAIT_SELLER_SEND_GOODS
Transaction awaits seller sending goods. (COD)
COD_WAIT_BUYER_PAY
Transaction awaits user payment. (COD)
COD_WAIT_SYS_PAY_SELLER
Buyer confirm goods, transaction awaits system paying to seller. (COD)
Additional Trade Status
Status
Description
ZHIFUBAO_CONFIRM
Custom Service confirms goods for buyer.
ZHIFUBAO_CANCEL_FP
Custom Service cancels instant payment for buyer.
DAEMON_CONFIRM_CANCEL_PRE_AUTH
Expiration Program cancels pre authorization.
DAEMON_CONFIRM_CLOSE
Expiration Program cancels transaction as buyer did not pay.
To verify whether all API requests and responses are correctly handled, and whether user experiences are as expected, you can test Alipay payment features integrated with your applications in sandbox environment before going live in the production environment.
Sandbox environment is an environment where you can mimic the characteristics of the production environment and create simulated responses from all APIs the application relies on before going live. You can make API tests based on your own requirements including making a transaction, cancel, or refund a transaction, and so on. Before you access the Alipay Sandbox Portal to start the test, you need to make some preparations. See Prerequisites for details.
Before you test Alipay payment features in sandbox, you need to get the following preparations:
Download the demo code. If you haven’t done this, see demo code.
Get an Alipay merchant account. If you don’t have one, watch the video below to get yourself an Alipay merchant account and a quick overview of Alipay sandbox portal.
Use the Alipay sandbox gateway for testing. The URL of the sandbox gateway is: https://mapi.alipaydev.com/gateway.do?
Alipay sandbox test accounts
There are two types of test account: merchant test account and buyer test account.
Merchant test accounts:
Find the merchant test account information in the Alipay sandbox portal under Sandbox Accounts > Merchant. More than one test accounts are provided, and the accounts are sorted by payment feature. Find the correct one to use according to the payment feature you want to test. For example, to test the Alipay Auto Debit feature, use the account information provided under Alipay Auto Debit.
Login password: Use this password to login to the Alipay for Business website in the sandbox environment.
Signature key: Only MD5 signature is supported in the sandbox environment. RSA signature is not supported at this time.
Use the buyer test account to login to the Alipay sandbox app.
The buyer test account information can be found in the Alipay sandbox portal under Sandbox Accounts > Buyer.
Account balance: You can click Top Up to top up the buyer test account.
Download the Alipay sandbox app
The Alipay sandbox app supports only Android at this time.
1) In the Alipay sandbox portal, click Alipay Sandbox App from the menu on the left.
2) Take one of the following steps:
Scan the QR code displayed on the page with a QR code reader on your Android device. This will download and install the sandbox app to your device.
Click the download link to download the sandbox app to your computer.
To log in to the Alipay sandbox app, use the buyer test account and login password that are provided in the portal under Sandbox Accounts > Buyer.
To generate a digital signature, normally a key is required to sign the data. You must prepare the MD5 private key or the RSA/DSA private and public key pair to generate and verify a digital signature.
MD5 sign type
MD5 private key is required for generating and verifying MD5 signatures. The MD5 secret key is the 32-byte string which is composed of English letters and numbers. You can log in to the Global Portal to view the private key:
Log in with your user ID.
Click My Technical Service and enter your payment password. If you don't know your payment password, please contact Global Merchant Business Support
Check your MD5 Key. For example, the following graphic is an example of an MD5 Key:
RSA/DSA sign type
An RSA/DSA key pair contains the private key and the public key. The private key is required for generating the signature, while the public key is used for verifying the signature. The following steps assume that you are using RSA sign type, similar steps applied for generating and uploading DSA key pair.
Generating the private/public key pair
Many tools can be used to generate the RSA key pair. The following example illustrates the steps to generate the RSA key pair by using OpenSSL.
Install OpenSSL
For linux system, use the following command:
sudo apt-get install openssl
For windows system, download and then install OpenSSL from OpenSSL site.
Generate RSA key pair.
For linux system, use the following command:
After that, you can see two files under current folder, rsaprivatekey.pem and rsapublickey.pem. The former is the private key and the latter is the public key.
Notes:
For Java developers, remove the header, footer, carriage return, and space from the pkcs8 private key output in the console.
After creating a private key with openssl, if you use JAVA, you need to transform the private key into PKCS8 format; if you use .NET or PHP, no need to transform the private key into PKCS8 format.
-----BEGIN PUBLIC KEY-----MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDQWiDVZ7XYxa4CQsZoB3n7bfxLDkeGKjyQPt2FUtm4TWX9OYrd523iw6UUqnQ+Evfw88JgRnhyXadp+vnPKP7unormYQAfsM/CxzrfMoVdtwSiGtIJB4pfyRXjA+KL8nIa2hdQy5nLfgPVGZN4WidfUY/QpkddCVXnZ4bAUaQjXQIDAQAB-----END PUBLIC KEY-----
Exchange the public key
You need to exchange your public key with Alipay. Contact Global Merchant Technical Support and provide your PID and public key information. Alipay will then make configurations accordingly, and provide you Alipay public key.
For a transaction that has been successfully paid, the customer can request the merchant for refunding as long as the refunding period is still valid, and the merchant can make use of the refunding interface to complete the refunding, as illustrated.
For the integration of the refunding service, we would like to highlight:
The refunding service name is: alipay.acquire.overseas.spot.refund(REFUND);
To refund a transaction, the interface REVERSE is only applicable at the same day of the payment (GMT +8, Beijing time); on the other hand, the interface REFUND is applicable as long as the refunding period has not expired yet;
the refunding of a transaction can be full or partial, i.e. the refunding amount can respectively be the same as or less than the original transaction amount that has been paid; furthermore, for a transaction, multiple refunding request is allowed provided the sum of the amount of the multiple refunding request is less than or equal with the original transaction amount.
Except for "sign" and "sign_type", all other parameters used need to be signed.
Parameters without value don't need to be transmitted, nor to be included in the data to be signed;
At signing, the character set used to change the character into byte stream must be consistent with that specified in _input_charset;
If the parameter _input_charset is transmitted, it shall also be included in the data to be signed.
MD5 sign type
After the pre-sign string is generated:
Append the MD5 secret key to the pre-sign string to generate a new string.
Calculate the new string with the MD5 signature algorithm (by using the MD5 signature function).
The result 32-byte string is the signature, which is used as the value of the “sign” parameter.
RSA/DSA sign type
After the pre-sign string is generated, perform the following steps to generate the signature:
Use the RSA/DSA algorithm and the merchant private key to generate the signature.
Encode the signature to a string.
Then, use the string as the value of the “sign” parameter.
MD5 sign type
After receiving the character string of the response or notification from Alipay system, similar to the steps taken in Signing the data, append the MD5 secret key to the character string to generate a new string. Then, calculate this new string with the MD5 signature algorithm. After the 32-byte signature result string is generated, verify whether the value is equal to the value passed in the sign parameter. If Yes, the verification is passed.
RSA/DSA sign type
After receiving a response or notification, perform the following steps to verify the signature:
Use the RSA/DSA algorithm to calculate a message digest.
Use the RSA/DSA public key to de-sign the signature (the value of the sign field) to a message digest.
Compare the two message digests obtained in step 2 and step 3. If the digests are the same, then it indicates that the signed data has not been changed.