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

   <dependency>
       <groupId>com.alipay.sdk</groupId>
       <artifactId>alipay-sdk-java</artifactId>
       <version>4.10.29.ALL</version>
   </dependency>

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

//Construct client
CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
//Set gateway address
certAlipayRequest.setServerUrl("https://openapi.alipay.com/gateway.do");
//Set app Id
certAlipayRequest.setAppId(app_id);
//Set application private key
certAlipayRequest.setPrivateKey(privateKey);
//Set request format, fixed value json
certAlipayRequest.setFormat("json");
//Set character set
certAlipayRequest.setCharset(charset);
//Set signature type
certAlipayRequest.setSignType(sign_type);
//Set application public key certificate path
certAlipayRequest.setCertPath(app_cert_path);
//Set Alipay public key certificate path
certAlipayRequest.setAlipayPublicCertPath(alipay_cert_path);
//Set up Alipay certificate path
certAlipayRequest.setRootCertPath(alipay_root_cert_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. alipay.trade.app.pay
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");
model.setOutTradeNo(outtradeno);
model.setTimeoutExpress("30m");
model.setTotalAmount("0.01");
model.setProductCode("QUICK_MSECURITY_PAY");
request.setBizModel(model);
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) {
        e.printStackTrace();
}

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