Spring Boot docks xorpay personal visa-free payment

Spring Boot docks xorpay personal visa-free payment

xorpay supports many payment methods, which is good news for personal webmasters. It is very convenient to use. However, the official demos are not very good. Here I will share the docking process: xorpay
Insert image description here

1. Preliminary preparation

First, you need to apply for WeChat payment. After the registration is completed, just follow the website prompts. If you have any questions, ask the customer service directly. The customer service is very good except for being slow to reply to messages.
The official website has customer service contact information

2. Create a controller and fill in the basic information

String name = "测试商品";    //商品名称
String payType = "native";      /支付方式,一般为 native
String price = "0.01";         //价格
String orderId = new Date().toString();      //平台订单号,需要唯一
String notifyUrl = "https://xorpay.com/main";       //回调地址,测试可以用这个,实际项目中需要修改
String appSecret = "youAppSecret";      //你得appSecret-xorpay后台查看

3. Set request body parameters

// 设置请求体参数
MultiValueMap<String, String> requestBody = new LinkedMultiValueMap<>();
requestBody.add("name", name);
requestBody.add("pay_type", payType);
requestBody.add("price", price);
requestBody.add("order_id", orderId);
requestBody.add("notify_url", notifyUrl);

4. Generate MD5 signature

// 拼接参数值
StringBuilder sb = new StringBuilder();
for (List<String> value : requestBody.values()) {
    
    
    sb.append(value.get(0));
}
sb.append(appSecret);
// 计算MD5签名
String sign =  DigestUtils.md5DigestAsHex(sb.toString().getBytes());

5. Complete parameters

requestBody.add("sign", sign);       //设置sign
requestBody.add("order_uid", "api-test-user");      //订单用户-可以不传
requestBody.add("more", "");        //订单其他信息,回调时原样传回-可以不传

6. Send a request

// 创建RestTemplate对象
RestTemplate restTemplate = new RestTemplate();
// 设置请求头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
// 发送POST请求
String url = "http://xorpay.com/api/pay/XXX";	//XXX需要替换成你的aid,xorpay后台查看
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST,
       		new HttpEntity<>(requestBody, headers), String.class);

7. Encapsulate the Vo class to receive return data

@Data
public class XorpayVo {
    
    
    private String status;
    private Map<String, String> info;
    private String expires_in;
    private String aoid;
}

8. Process the returned data

// 处理响应
if (response.getStatusCode().is2xxSuccessful()) {
    
    
    String responseBody = response.getBody();
    // 将字符串解析为JSONObject对象
    JSONObject jsonObject = JSONObject.parseObject(responseBody);
    // 将JSONObject对象转换为Map对象
    XorpayVo xorpayVo = jsonObject.toJavaObject(XorpayVo.class);
    return xorpayVo.getInfo().get("qr");
} else {
    
    
    return "";
}

9. Front-end page-index.html

<img />

<script src="https://cdn.bootcss.com/jquery/3.6.0/jquery.min.js"></script>
<script>
    $(document).ready(function() {
      
      
        $.get("/getQr/getSrc", function(data) {
      
      
            // 在成功回调函数中设置img元素的src属性
            $("img").attr("src", "http://xorpay.com/qr?data=" + data);
        });
    });
</script>

After the project is launched, visit: http://localhost:8080/indexl.html to see the payment QR code
Insert image description here

10. Source code acquisition

Follow the official account and replycheersReady to receive
Scan the QR code to follow

Guess you like

Origin blog.csdn.net/kkwyting/article/details/132690447