【Latest and most detailed】How to access Alipay interface from computer website

introduction

Alipay official computer website payment quick access document: https://docs.open.alipay.com/270/105899/ ;
Next, I will give a detailed description of this quick access document.


1. Preparation for accessing the Alipay interface

First of all, let me explain that when I made a computer website to access the Alipay interface, I used the sandbox environment to debug it first, and took the java version as an example. You need to register an enterprise account and log in to Alipay:
https://open.alipay.com/platform/home.htm ;

After logging in, there is a "Developer Center" button in the upper left corner. After clicking in, there is a "Sandbox Environment" button on the left, click to enter the sandbox environment, and conduct Alipay interface development in the sandbox environment (in the sandbox environment) After debugging the Alipay interface in the environment, you only need to modify the configuration information in AlipayConfig.java to use it):
write picture description here

1.1 Configuring the sandbox environment

1) First, you need to generate the RSA2 (SHA256) application public key, and configure the RSA2 (SHA256) application public key. Note here that there is no need to configure the RSA (SHA1) key.
(For details on generating the application public key of RSA2, please refer to: https://docs.open.alipay.com/291/105971 )
write picture description here

2)! ! ★★★ Note that in the sandbox environment, Alipay's gateway is different from the real gateway! Attention is needed here!

For more information on configuring the sandbox environment, please refer to Alipay's official sandbox environment usage documentation:
https://docs.open.alipay.com/200/105311

1.2 Download SDK&Demo

1) The computer website payment demo I downloaded is the java version, as well as the PHP version and the .NET version. The download address is:
https://docs.open.alipay.com/270/106291/

2) After downloading the demo, open the readme.txt inside, and follow the instructions inside to complete it step by step. There is only one point to explain here, that is, you need to download the SDK in the demo, and put some jar packages in the SDK compressed package into the project, and you are done.

write picture description here

2. Access Alipay

The sandbox environment has been configured and the demo and SDK have been downloaded. The next thing to do is to configure the AlipayConfig.java file, run the index.jsp file, check the effect, and then modify the index.jsp file according to the needs of your website.

2.1 Configure AlipayConfig.java

Open the AlipayConfig.java file, you can see that you need to configure app_id, RSA2 private key, Alipay public key, server asynchronous notification page path, page jump synchronous notification page path, and Alipay gateway. Among them, the signature method and character encoding format do not need to be configured. This is written by itself.

1) Configure app_id, this app_id is the APPID in the sandbox application

2) Configure the RSA2 private key and Alipay public key. The RSA2 private key is the private key generated in the front, and the Alipay public key can be viewed directly after uploading the application public key.

3) Configure the server asynchronous notification page path, ★★★ Note: This asynchronous notification page path must be accessible from the public network, not the intranet! (For the understanding of asynchronous notification, you can turn to 2.2 to view the interface calling process.), I personally suggest that you can use the peanut shell software to bind the ip of the machine for testing.

4) Configure the page path of the page jump synchronization notification page, this does not require a public network, only the local machine can access it (the understanding of synchronization notification also goes to 2.2 to view the interface call process)

5) Alipay gateway, ★★★ This must be noted, the Alipay gateway using the sandbox environment and the Alipay gateway not using the sandbox environment are different!

Note: Some places need to configure PID, this PID is the merchant UID

2.2 Interface call process

write picture description here

Here is a detailed description of asynchronous callbacks and synchronous callbacks.

In simple terms, asynchronous callback means that the user does not jump to the page, but executes the asynchronous callback page (that is, the java code in .jsp). It is important to note here that because it will not jump to the page, so The jsp or js code inside will not be executed, only the java code will be executed. However, the struts2+spring framework I use here, if the action is called in the jsp page, an error will occur when the action calls the service layer.

Therefore, when I configure the asynchronous callback page, I execute the action code directly through the url, and pass:

PrintWriter out = ServletActionContext.getResponse().getWriter();
HttpServletRequest request = ServletActionContext.getRequest(); 

Here I will introduce, because Alipay must use "out.println" to print out the success when the asynchronous callback is made, and the Alipay server will determine that the transaction is completed. Therefore, you need to define out first. Before defining out, you need to get the response. Next, let's introduce the response:

1. Understand the response

服务器在接收和解释客户端的请求消息后,服务器会返回给客户端一个HTTP响应消息,我们称之为响应(response)。其实也是一个按照http协议的规则拼接而成的一个字符串

In this action, the merchant's order number, transaction status, payment amount, seller id, and merchant's own id are verified to prevent hackers from modifying parameters such as the payment amount, thereby causing losses to the website. Moreover, no errors will occur when the action calls the service layer and the service layer calls the dao layer!

Synchronous callback is easier to understand, that is, after Alipay completes payment, it will jump to the synchronous callback page you configured in AlipayConfig, this page can be written by yourself, it does not need to be on the public network!

3. Use the sandbox account to test the Alipay interface

In the sandbox environment column, there is a sandbox account, use the buyer account and login password of the sandbox account for test payment

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325501948&siteId=291194637