栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

微信Native支付接入教程(2022简洁版)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

微信Native支付接入教程(2022简洁版)

PC网站接入支付

微信支付支持完成域名ICP备案的网站接入支付功能。PC网站接入支付后,可以通过JSAPI支付或Native支付,自行开发生成二维码,用户使用微信“扫一扫”来完成支付。本次讲解Native支付的快捷接入教程。

接入文档网站:PC网站 - 微信支付接入指引 - 微信支付商户平台

一、简介

Native支付是指商户系统按微信支付协议生成支付二维码,用户再用微信“扫一扫”完成支付的模式。

应用场景

Native支付适用于PC网站、实体店单品或订单、媒体广告支付等场景

用户扫描商户展示在各种场景的二维码进行支付,具体操作流程如下:

步骤一 商户根据微信支付的规则,为不同商品生成不同的二维码(如图3.1),展示在各种场景,用于用户扫描购买。

步骤二 用户使用微信“扫一扫”(如图3.2)扫描二维码后,获取商品支付信息,引导用户完成支付(如图3.3)。

步骤三 用户确认支付,输入支付密码(如图3.4)。

步骤四 支付完成后会提示用户支付成功(如图3.5),商户后台得到支付成功的通知,然后进行发货处理。

 

 二、接入微信支付前需要准备的参数

appid 微信公众号、小程序、app或开放平台的唯一标识;mchid  微信支付分配的商户收款账号;mchSerialNo:商户证书序列号; privateKey 私钥文件
apiKey3 交易过程生成签名的apiv3密钥;

三、java版本代码实现步骤

1、初始化httpClient

public CloseableHttpClient httpClient(){
        String keyPath = WChantPay.keyPath;
        String mchid = WChantPay.mchid;
        String mchSerialNo = WChantPay.mchSerialNo;
        String apiKey3 = WChantPay.apiKey3;
        try{

// 加载商户私钥(privateKey:私钥字符串)
            //示例:私钥存储在文件
            PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(
                    new FileInputStream("C:\Users\Administrator\Desktop\1626431593_20220528_cert\apiclient_key.pem"));


            //PrivateKey merchantPrivateKey = PemUtil
            //	.loadPrivateKey(new ByteArrayInputStream(keyPath.getBytes("utf-8")));

// 加载平台证书(mchId:商户号,mchSerialNo:商户证书序列号,apiV3Key:V3密钥)
            AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier(
                    new WechatPay2Credentials(mchid, new PrivateKeySigner(mchSerialNo, merchantPrivateKey)),
                    apiKey3.getBytes("utf-8"));
            WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()
                    .withMerchant(mchid, mchSerialNo, merchantPrivateKey)
                    .withValidator(new WechatPay2Validator(verifier));
// 初始化httpClient
            CloseableHttpClient httpC =  builder.build();
            return httpC;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

2、Native下单

public String CreateOrder() throws Exception{
        HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/pay/transactions/native");
        // 请求body参数
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectMapper objectMapper = new ObjectMapper();
        ObjectNode rootNode = objectMapper.createObjectNode();
        //商户id
        rootNode.put("mchid", WChantPay.mchid)
                //小程序id
                .put("appid", WChantPay.appid)
                //描述
                .put("description", "wudehua")
                //微信通知回调地址
                .put("notify_url", "http://119.29.28.29/")
                //商户订单id
                .put("out_trade_no", "12345671");
        //如果前端直接传的是分此处不需要再转
        //int round = Math.round(wxpayDetail.getTotal() * 100);
        rootNode.putObject("amount")
                //支付金额,单位是(分)
                .put("total", 10);

        objectMapper.writeValue(bos, rootNode);
        httpPost.addHeader("Accept", "application/json");
        httpPost.addHeader("Content-type","application/json; charset=utf-8");
        httpPost.setEntity(new StringEntity(bos.toString("UTF-8")));


        //完成签名并执行请求
        CloseableHttpResponse response = httpClient().execute(httpPost);

        try {
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == 200) { //处理成功
                System.out.println("success,return body = " + EntityUtils.toString(response.getEntity()));
                return EntityUtils.toString(response.getEntity());
            } else if (statusCode == 204) { //处理成功,无返回Body
                System.out.println("success");
                return null;
            } else {
                System.out.println("failed,resp code = " + statusCode+ ",return body = " + EntityUtils.toString(response.getEntity()));

                throw new IOException("request failed");

            }
        } finally {
            response.close();

        }

    }

3、【客户端】生成支付二维码 

这一步在客户端实现,可以利用qrcode.js进行实现二维码的生成;

4、查询订单

public void QueryOrder() throws Exception {
   
  //请求URL
  URIBuilder uriBuilder = new URIBuilder("https://api.mch.weixin.qq.com/v3/pay/transactions/id/4200000745202011093730578574");
  uriBuilder.setParameter("mchid", mchId);
 
  //完成签名并执行请求
  HttpGet httpGet = new HttpGet(uriBuilder.build());
  httpGet.addHeader("Accept", "application/json");
  CloseableHttpResponse response = httpClient().execute(httpGet);
   
  try {
      int statusCode = response.getStatusLine().getStatusCode();
      if (statusCode == 200) {
          System.out.println("success,return body = " + EntityUtils.toString(response.getEntity()));
      } else if (statusCode == 204) {
          System.out.println("success");
      } else {
          System.out.println("failed,resp code = " + statusCode+ ",return body = " + EntityUtils.toString(response.getEntity()));
          throw new IOException("request failed");
      }
  } finally {
      response.close();
  }
}

5、关闭订单

public void CloseOrder() throws Exception {
     
    //请求URL
    HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/sdkphp12345678920201028112429/close");
    //请求body参数
    String reqdata ="{"mchid": ""+mchId+""}";
     
    StringEntity entity = new StringEntity(reqdata,"utf-8");
    entity.setContentType("application/json");
    httpPost.setEntity(entity);
    httpPost.setHeader("Accept", "application/json");
   
  //完成签名并执行请求
    CloseableHttpResponse response = httpClient().execute(httpPost);
    try {
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode == 200) {
            System.out.println("success,return body = " + EntityUtils.toString(response.getEntity()));
        } else if (statusCode == 204) {
            System.out.println("success");
        } else {
            System.out.println("failed,resp code = " + statusCode+ ",return body = " + EntityUtils.toString(response.getEntity()));
            throw new IOException("request failed");
        }
    } finally {
        response.close();
    }
  }
  

 简单单五步搞定微信支付,不懂的可以评论区留言,我会一一回复。

喜欢我的文章作品的话,记得点赞加收藏哈!,我会定时更新的。。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/939493.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号