Article Directory
Forwarded from the article of CSDN remote penetration: Java Alipay sandbox environment payment, SDK interface remote debugging [intranet penetration]
1. Test environment
- Maven
- Spring boot
- Jdk 1.8
2. Local configuration
Obtain Alipay payment Java SDK, maven project can choose maven version, ordinary java project can be downloaded from GitHub, here is an example of maven
SDK download link: https://doc.open.alipay.com/docs/doc.htm?treeId=193&articleId=103419&docType=1
Select the latest SDK version
Copy the maven coordinates and import them into the pom file of the spring boot project
Open the development documentation: https://opendocs.alipay.com/open/028r8t?scene=22
Here is just a separate test of the computer website payment unified order and payment page interface.
Write an interface, for the convenience of demonstration, create a get request, and write the order information in the code
//支付宝支付接口
@Controller
@RequestMapping("/pay")
public class AlipayTest {
private String serverUrl="https://openapi.alipaydev.com/gateway.do";
private String appId="2021000122601685";
private String privateKey="MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCCk5evhVhZL3kaWEN4ACD+xHoTfn0RqR7aoh2esSTHeca8bV77z4KYUYXH/76uG1ufeYX8CsI68fMTTIxTcvA1F/vctjCkyAS3fjo+DTPpQ5tZpXvJjbNhwXJwrNov/44Dsy727N+b6CAmz5C6vkNipTdekUlHO+6EJmNgMo+ZgYuoEvinP/1JLST6IpcYj/QjGtG8cYStDlx6C3P2Xk7Jv//rOsMbem6bO5v7zZU4E29lVn5mZucxyPqDfRgyz2H++EaNxDRRh6cdVxwntEImpn9Tap+Jv1tTEddI7pOnXqsOwy5lOEJ6r36g48OyXWOLum2x9bxZUs/6tg96VAgMBAAECggEAN7StlqTltHuJNQ2hBe9eQSjKRJmI3y8gQc5C6ZLLNSHXzselaEQHNd6mMjarPhg6zUV8wPH/wuvbsFOcF9tmMkFmxBiyjnGwV1pjYVmafMkpwh7jWkLk9UBLPTEbKN2+wqbuq08Pjc7et2cywRXjn8SrhQ6F47dZBspgkTDQ+PTpQ4unHjUeQVz55K2VitReDZuTNWPCj2DT7UHDaItFTVtG0oNd1mVtcmeS+4Na7WS/3dusUef2v8fpTuTYcNN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+cEOnEZcfP3TxeKReAH379bHUVQOz7h3uH0eDcFmi3GjwPBB9lmcJdTI6qD/XObiWJID1e+XYzVgNKC+DXfJ2YN8NEuyRH5Zr862s3C6xd8BAYU1VW9ZfyAssQxILCOwQZ0qDOOq48GAhzDWwPG8oteJX7+lcH1TrnG01SmPiZqkHX6uUW2ye+fZkbW9qjtyujNnADIWNb/D1R2qgAcHsuOVV4hniRQIDAQAB";
//创建订单接口
@ResponseBody
@GetMapping("/create")
public String create(){
//创建连接
AlipayClient alipayClient = new DefaultAlipayClient(serverUrl,appId,privateKey,"json", "UTF-8",alipayPublicKey,"RSA2");
AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
//订单信息
JSONObject bizContent = new JSONObject();
bizContent.put("out_trade_no", "202100012260165");
bizContent.put("total_amount", 5.00);
bizContent.put("subject", "测试");
bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");
request.setBizContent(bizContent.toString());
AlipayTradePagePayResponse response = null;
try {
response = alipayClient.pageExecute(request);
} catch (AlipayApiException e) {
e.printStackTrace();
}
if(response.isSuccess()){
//成功
return response.getBody();
} else {
//失败
return "调用失败";
}
}
}
Note the configuration parameters:
- The value of the serverUrl parameter:
- The value of the appId parameter:
- The values of privateKey and alipayPublicKey:
After configuring the parameter values, then we start the springboot project. The tomcat configured here uses port 8080
After the startup is successful, open the browser to access the interface address we wrote above: http://localhost:8080/pay/create , the Alipay interface appears to indicate success. If it fails, check the configured parameters!
3. Intranet penetration
The above service of calling SDK to create a payment order runs locally. The limitation is that only local projects can call the interface for testing payment. If you want to call it remotely, you need to expose the service to the public network.
Cpolar is an intranet penetration tool that supports http/https/tcp protocols, does not limit traffic, is easy to operate, does not require public network IP, and does not require routers, and can easily expose services to public network access.
cpolar official website: https://www.cpolar.com/
3.1 Download and install cpolar intranet penetration
Visit the cpolar official website, register an account, and download and install the cpolar client. For details, please refer to the documentation tutorial to download and install.
3.2 Create a tunnel
After cpolar is successfully installed, we access the local port 9200 on the browser and log in to the web ui interface of Cpolar: http://localhost:9200 .
Click Tunnel Management on the left dashboard - Create Tunnel. Since port 8080 is configured in tomcat, we need to create an http tunnel pointing to port 8080:
- Tunnel name: can be customized, be careful not to duplicate the existing tunnel name
- Protocol: http protocol
- Local address: 8080
- Domain name type: choose a random domain name for free
- Region: Select China top
click创建
Click the status on the left dashboard - list of online tunnels, you can see that the tunnel just created has generated a corresponding public network address, one http protocol, one https protocol (without the cumbersome steps of configuring ssl certificate), and set it to copy think down
4. Test public network access
Open the browser and enter the copied 公网地址+接口路径
to visit, and the Alipay page appears to indicate that the visit is successful
5. Configure a fixed second-level subdomain name
Since the above tunnel created by using cpolar uses a random public network address, the address will change within 24 hours, which is not conducive to long-term remote access. Therefore, we can configure a second-level subdomain name for it, which is a fixed address and will not change randomly [ps: cpolar.cn has been filed]
Note that the cpolar package needs to be upgraded to the basic package or above. [cpolar.cn has been filed]
5.1 Reserve a second-level subdomain
Visit the official website of Cpolar , log in to the background of the official website of cpolar, click Reserved on the left, and find the reserved second-level subdomain name:
- Region: Select China VIP
- Second-level domain name: can be customized
- Description: Remarks, which can be customized
click保留
Prompt that the subdomain name is reserved successfully, copy the reserved second-level subdomain name
5.2 Configure the second-level subdomain name
Visit http://127.0.0.1:9200/ , log in to the cpolar web UI management interface, click the tunnel management on the left dashboard - tunnel list, find the payment interface test tunnel, and click edit on the right
Modify the tunnel information, and configure the successfully reserved second-level subdomain name into the tunnel
- Domain name type: select a second-level subdomain name
- Sub Domain: Fill in the reserved sub-domain name
click更新
After updating, open the online tunnel list again, and you can see that the tunnel has become a fixed second-level subdomain name, copy the domain name
6. Use a fixed second-level subdomain name for access
Open the browser and enter the configuration that has just been successfully configured to 固定二级子域名+接口路径
access, and the Alipay page appears to indicate that the access is successful
Fill in the account name and payment password test, and log in to the console to view the account name and payment password
Select the balance of the test account for payment, and the payment password can be seen in the console
payment successful