xuhy
2025-01-02 b94fffb01c94100b34e2908b4fe79c1c554b0912
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.ruoyi.system.vx;
 
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.payments.nativepay.NativePayService;
import com.wechat.pay.java.service.payments.nativepay.model.Amount;
import com.wechat.pay.java.service.payments.nativepay.model.PrepayRequest;
import com.wechat.pay.java.service.payments.nativepay.model.PrepayResponse;
 
/** Native 支付下单为例 */
public class QuickStart {
 
    /** 商户号 */
    public static String merchantId = "1665330417";
    /** 商户API私钥路径 */
    public static String privateKeyPath = "E:\\cert\\ershou\\apiclient_key.pem";
    /** 商户证书序列号 */
    public static String merchantSerialNumber = "7EEA04429B006E12AAA421C002EC48BBEED5BE94";
    /** 商户APIV3密钥 */
    public static String apiV3Key = "TA2npSNWmS0GcB0tFFRWA94rm1M0iSFs";
 
    public static void main(String[] args) {
        // 使用自动更新平台证书的RSA配置
        // 一个商户号只能初始化一个配置,否则会因为重复的下载任务报错
        Config config =
                new RSAAutoCertificateConfig.Builder()
                        .merchantId(merchantId)
                        .privateKeyFromPath(privateKeyPath)
                        .merchantSerialNumber(merchantSerialNumber)
                        .apiV3Key(apiV3Key)
                        .build();
        // 构建service
        NativePayService service = new NativePayService.Builder().config(config).build();
        // request.setXxx(val)设置所需参数,具体参数可见Request定义
        PrepayRequest request = new PrepayRequest();
        Amount amount = new Amount();
        amount.setTotal(100);
        request.setAmount(amount);
        request.setAppid("wx98563d0ec9cf21c8");
        request.setMchid("1665330417");
        request.setDescription("测试商品标题");
        request.setNotifyUrl("https://notify_url");
        request.setOutTradeNo("out_trade_no_001fafasdadsadws");
        // 调用下单方法,得到应答
        PrepayResponse response = service.prepay(request);
        // 使用微信扫描 code_url 对应的二维码,即可体验Native支付
        System.out.println(response.getCodeUrl());
    }
}