Java Alipay sandbox environment payment, SDK interface remote debugging [intranet penetration]

Article directory

    • @[TOC]
        • 1. Test environment
        • 2. Local configuration
        • 2. Intranet penetration
        • 2.1 Download and install cpolar intranet penetration
        • 2.2 Create tunnel
        • 3. Test public network access
        • 4. Configure a fixed second-level subdomain name
        • 4.1 Reserve a second-level subdomain name
        • 4.2 Configure the second-level subdomain name
        • 5. Use fixed second-level subdomain names for access
1. Test environment
  • Maven
  • Spring boot
  • Jdk 1.8
2. Local configuration

Obtain Alipay payment Java SDK. For maven projects, you can choose the maven version. Ordinary java projects can be downloaded from GitHub. Here we take maven as an example.

SDK download address: https://doc.open.alipay.com/docs/doc.htm?treeId=193 & amp;articleId=103419 & amp;docType=1

2023011001

Choose the latest SDK version

2023011002

Copy the maven coordinates and import them into the spring boot project pom file

2023011003
2023011004

Open the development document: https://opendocs.alipay.com/open/028r8t?scene=22

This is just a separate test of the computer website payment unified collection order and payment page interface.

2023011005

Write an interface to facilitate demonstration, create a get request, and write the order information in the code

//Alipay payment interface
@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//rOsMbem6bO5v7zZU4E29lVn5 mZucxyPqDfRgyz2H + + EaNxDRRh6cdVxwntEImpn9Tap + Jv1tTEddI7pOnXqsOwy5lOEJ6r36g48OyXWOLum2x9bxZUs/6tg96VAgMBAAECggEAN7StlqTltHuJNQ2hBe9eQSjKRJmI3y8gQc5C6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Tw6Tw0MqDFX1/EfDJp2spv2rQDMZyKCJZwZ2QyZNz2yYdm1ukzxbJI/conKUSsujdhKEZZhchv2MWl 2moscwn";
    private String alipayPublicKey="MIIBIjANBgkqhkiG9QEFAAOCAQ8AMIIBCgKCAQEAhsM/TnpIff7e39eqsCLQwI8fBQ + 5lYcFjOrSw7p0ZsMETXXAS677MZTKw5JyHVTieg79708qvaYWZ58jYuViTqpBCnot + cEOnEZcf P3TxeKReAH379bHUVQOz7h3uH0eDcFmi3GjwPBB9lmcJdTI6qD/XObiWJID1e + lcH1TrnG01SmPiZqkHX6uUW2ye + fZkbW9qjtyujNnADIWNb/D1R2qgAcHsuOVV4hniRQIDAQAB";

    //Create order interface
    @ResponseBody
    @GetMapping("/create")
    public String create(){<!-- -->

        //Create connection
        AlipayClient alipayClient = new DefaultAlipayClient(serverUrl,appId,privateKey,"json", "UTF-8",alipayPublicKey,"RSA2");
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();

        //order information
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", "202100012260165");
        bizContent.put("total_amount", 5.00);
        bizContent.put("subject", "test");
        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()){<!-- -->

            //success
            return response.getBody();
        } else {<!-- -->
            //fail
           return "Call failed";
        }
    }
}

Pay attention to the configuration parameters:

  • The value of the serverUrl parameter:

2023011006

  • Value of appId parameter:

2023011007

  • The values of privateKey and alipayPublicKey:

2023011008
2023011009

After configuring the parameter values, we then start the springboot project. The tomcat configured here uses port 8080.

2023011010

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!

2023011011

2. Intranet penetration

The above service that calls SDK to create payment orders runs locally. The limitation is that only local projects can call the test payment interface. 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 simple to operate, does not require a public IP address, or a router, and can easily expose services to the public network for access.

cpolar official website: https://www.cpolar.com/

2.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.

20230116162253

2.2 Create tunnel

After cpolar is successfully installed, we access the local 9200 port on the browser and log in to Cpolar’s web ui interface: http://localhost:9200.

Click Tunnel Management on the left dashboard – Create Tunnel. Since tomcat is configured with port 8080, we need to create an http tunnel pointing to port 8080:

  • Tunnel name: Customizable, be careful not to duplicate the name of an existing tunnel
  • Protocol: http protocol
  • Local address: 8080
  • Domain name type: Choose a random domain name for free
  • Region: Select China top

Click Create

2023011012

Click the status of the dashboard on the left – Online tunnel list. You can see that the tunnel you just created has generated a corresponding public network address, an http protocol and an https protocol (avoiding the tedious steps of configuring the SSL certificate). Copy and think about it

2023011013

3. Test public network access

Open the browser and enter the copied public network address + interface path to access. The Alipay page appears to indicate successful access.

20230116161201

202301161609

4. Configure fixed second-level subdomain name

Since the tunnel created using cpolar above 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. This address is a fixed address and will not change randomly [ps: cpolar.cn has been filed]

Note that you need to upgrade the cpolar package to the basic package or above. [cpolar.cn has been registered]

4.1 Reserve a second-level subdomain name

Visit the Cpolar official website, log in to the cpolar official website backend, click Reserve on the left, and find the reserved second-level subdomain name:

  • Region: Select China VIP
  • Second-level domain name: can be customized
  • Description: Notes, which can be customized

Click Keep

2023011015

Prompt that the subdomain name is reserved successfully, copy the reserved second-level subdomain name

2023011016

4.2 Configure second-level subdomain names

Visit http://127.0.0.1:9200/, log in to the cpolar web UI management interface, click Tunnel Management – Tunnel List on the left dashboard, find the payment interface test tunnel, and click Edit on the right

2023011017

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 successfully reserved second-level subdomain name

Click Update

2023011018

After the update, open the online tunnel list again. At this time, you can see that the tunnel has become a fixed second-level subdomain name. Copy the domain name.

2023011019

5. Use fixed second-level subdomain names for access

Open the browser and enter the fixed second-level subdomain + interface path that was just configured successfully to access it. The Alipay page appears indicating that the access is successful.

2023011020

Fill in the account name and payment password to test, log in to the console with the account name and payment password to view

2023011021

Select the balance of the test account to make payment. The payment password can be seen on the console.

2023011022
2023011023

payment successful

2023011024