Alipay app payment server access (certificate mode)

Alipay has paid a lot of access documents. Because of the key configuration error, it has been returning to the 4000 error code for a half afternoon.

The overall development flow chart is as follows:

1. First create the APP and sign the APP payment capability

Official documents: Preparation before access

This process needs to fill in and certify some company information. The signing rate of Alipay is 6%.

2 first generate and configure the public key certificate according to the official document

reference resources: How to generate and configure public key certificate 

First, download the Alipay open platform development assistant to get the csr file, select the "interface signing method" in the application information - "public key certificate" - upload the csr file, select the.csr file of directory Alipay open platform development assistant /csr, upload the success, and download the "public key certificate" (appCertPublicKey_). Appid data.Crt) and Alipay public key certificate (alipayCertPublicKey_) RSA2.crt), Alipay certificate. alipayRootCert.crt );

3 server development

Reference: Official Documents Example (certificate) of APP payment order information generated by JAVA server SDK

1) At pom.xml Add the latest alipay sdk dependency to


2) JAVA server SDK generates APP payment order information example (certificate)

//Construct client
CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
//Set gateway address
//Set app Id
//Set application private key
//Set request format, fixed value json
//Set character set
//Set signature type
//Set application public key certificate path
//Set Alipay public key certificate path
//Set up Alipay certificate path
//Construct client
AlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);

//Instantiate the request class corresponding to the specific API corresponding to the class name and interface name.
AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
//The SDK has encapsulated the public parameters. Only the business parameters need to be passed in here. The following methods are the model input methods (model and biz) of the SDK_ Biz if content exists at the same time_ content). 
AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
model.setBody("I'm the test data");
model.setSubject("App Payment test Java");
request.setNotifyUrl("Asynchronous address that can be accessed by merchant's extranet");
try {
        //This is different from the normal interface call. SDK execute is used
        AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
        System.out.println(response.getBody());//That is, orderString can directly request to the client without further processing.
    } catch (AlipayApiException e) {

Attention pit:

1) The request types of certificate and public key are different. The certificate is CertAlipayRequest;

2) The request method of certificate and public key is different. The certificate is SDK execute method;

3) The value of productCode parameter is "quick" when app pays_ MSECURITY_ PAY";

4) The application private key refers to the domain name in the CSR folder of the previous step_ The other three certificate paths are the absolute paths of the three. crt certificates downloaded in the previous step.

If the server requests the interface successfully, it will return a & concatenated parameter string with a signature sign value

4 troubleshooting

The most common is ALIN10146 troubleshooting , the client will return "the system is busy, please try again later".

The most common reason is the public-private key pairing error. Please refer to the official documents for using the certificate How to verify key certificates match To test.

Keywords: SDK Java JSON xml

Added by dujed on Sat, 27 Jun 2020 08:18:19 +0300