Java docking with WeChat payment: JSAPI payment (WeChat public account payment)

This article is [WeChat JSAPI Payment] article, which mainly explains how merchants connect to WeChat payment, and the simple version is tested


1. Preparation before JSAPI payment access

1. JSAPI payment first requires registration and certification of a public account (about 300 yuan a year)

WeChat public account registration

2. Apply to become a merchant. There are many types of merchants (ordinary merchants, service providers, special merchants, etc.). Apply according to your own needs.

Merchant application

3. Association between WeChat public account and merchant

        3.1. Log in to the WeChat official account and associate the merchant account with the application account

         3.2. Associated merchant number

 Click Add and complete the binding according to the instructions.............

4. Obtain appId and merchant number

        4.1. Log in to the WeChat official account , enter the [Basic Configuration] directory, and get the appId of the official account, as shown in the figure

         4.2. Log in to the WeChat Pay merchant account , enter [Account Center->Account Information->WeChat Pay Merchant ID, and get the WeChat Pay merchant account,  as shown in the figure

 5. Log in to the WeChat merchant platform , enter the [Account Center > API Security] directory, and set the APIV3 key. If there are official documents, I will not go into details here.

2. Code snippets

1.Introduce Maven dependencies

<dependency>
  <groupId>com.github.wechatpay-apiv3</groupId>
  <artifactId>wechatpay-java</artifactId>
  <version>0.2.7</version>
</dependency>

2. Backend business request interface

Modify and use it according to your own business needs. The parameters are based on your own needs. The code is as follows (JSAPI example): 

     /** 商户号*/
    @Value("${wx.mchId}")
    private String mchId;

    /** 公众号appid*/
    @Value("${wx.appId}")
    private String appId;

    /** 商户APIV3密钥*/
    @Value("${wx.apiV3Key}")
    private String apiV3Key;

    /**微信回调地址*/
    @Value("${wx.v3PayNotifyUrl}")
    private String v3PayNotifyUrl;

    /** 商户证书序列号 */
    @Value("${wx.merchantSerialNumber}")
    private String merchantSerialNumber;

    public static RSAAutoCertificateConfig config = null ;
    public static JsapiServiceExtension service = null ;

@GetMapping("/prepay")
    public PrepayWithRequestPaymentResponse WeChartPay(String amountString) {
      
        String openId = "oI*******************iGiA";
        // 订单号
        String orderUuid = IdUtils.getUUID();
        //元转换为分
        Integer amountInteger = Integer.valueOf(AmountUnitConversionUtil.changeYuanAndFen(amountString));
        //私钥文件路径(本地自己测试看自己的私钥文件存放路径)
        String filePath ="***/***/***/apiclient_key.pem";//测试环境可放到resource目录下

        // 一个商户号只能初始化一个配置,否则会因为重复的下载任务报错
        if (config == null) {
            config =new RSAAutoCertificateConfig.Builder()
                            .merchantId(mchId)
                            .privateKeyFromPath(filePath)
                            .merchantSerialNumber(merchantSerialNumber)
                            .apiV3Key(apiV3Key)
                            .build();
        }
        // 构建service
        if (service == null) {
            service = new JsapiServiceExtension.Builder().config(config).build();
        }

        //组装预约支付的实体
        // request.setXxx(val)设置所需参数,具体参数可见Request定义
        PrepayRequest request = new PrepayRequest();
        //计算金额
        Amount amount = new Amount();
        amount.setTotal(amountInteger);
        amount.setCurrency("CNY");
        request.setAmount(amount);
        //公众号appId
        request.setAppid(appId);
        //商户号
        request.setMchid(mchId);
        //支付者信息
        Payer payer = new Payer();
        payer.setOpenid(openId);
        request.setPayer(payer);
        //描述
        request.setDescription("支付测试");
        //微信回调地址,需要是https://开头的,必须外网可以正常访问
        //本地测试可以使用内网穿透工具,网上很多的
        request.setNotifyUrl(v3PayNotifyUrl);
        //订单号
        request.setOutTradeNo(orderUuid);
        // 加密
        PrepayWithRequestPaymentResponse payment = service.prepayWithRequestPayment(request);
        //默认加密类型为RSA
        payment.setSignType("MD5");
       //返回数据,前端调起支付
    return payment;

3. The front-end calls up the payment request method

function onBridgeReady(){
   WeixinJSBridge.invoke(
      'getBrandWCPayRequest', {
         "appId":"wx2421b1c4370ec43b",     //公众号ID,由商户传入     
         "timeStamp":"1395712654",         //时间戳,自1970年以来的秒数     
         "nonceStr":"e61463f8efa94090b1f366cccfbbb444", //随机串     
         "package":"prepay_id=u802345jgfjsdfgsdg888",     
         "signType":"MD5",         //微信签名方式:     
         "paySign":"70EA570631E4BB79628FBCA90534C63FF7FADD89" //微信签名 
      },
      function(res){
      if(res.err_msg == "get_brand_wcpay_request:ok" ){
      // 使用以上方式判断前端返回,微信团队郑重提示:
            //res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。
      } 
   }); 
}
if (typeof WeixinJSBridge == "undefined"){
   if( document.addEventListener ){
       document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
   }else if (document.attachEvent){
       document.attachEvent('WeixinJSBridgeReady', onBridgeReady); 
       document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
   }
}else{
   onBridgeReady();
}

Summarize


The above is a WeChat JSAPI payment test implemented based on the official documents. I won’t go into details about the WeChat callback interface. Leave a message if you need it. If you have any questions, you can leave a message to discuss and learn together!

Guess you like

Origin blog.csdn.net/m0_57007247/article/details/130083768