微信支付V3对接过程

微信支付统一下单接口详解

参考:https://pay.weixin.qq.com/wiki/doc/api/app.php?chapter=4_2

统筹: 因为是做的微信支付,商户嵌入微信支付,所以不是被扫支付,系统先调用该统一下单接口在微信支付服务后台生成预支付交易单,返回正确的预支付交易回话标识后,再调用支付接口返回到手机端,然后等待回调即可;

统一下单接口:

URL地址:https://api.mch.weixin.qq.com/pay/unifiedorder

不要证书;

成功后,返回:状态码  return_code  : SUCCESS/FAIL

1. 手机向后台交互

入参:商品描述:body;总金额:total_fee;终端IP:spbill_create_ip;购买的商品ID:自定义;用户ID:自定义;(存储到自己的数据库中)

2. 后台准备数据向微信直接请求;

将存储好的公众账号ID:appid;商户号:mch_id,取出来;自己生成随机字符串建议格式为:WX年月日时分秒随机数;按照签名规则生成签名;

SortedMap<String, Object> map = new TreeMap<String, Object>();

        // 公众账号ID
        map.put("appid", Configure.getAppid());
        // 商户号
        map.put("mch_id", Configure.getMchid());


        // 随机字符串
        map.put("nonce_str", randomString);
        // 总金额(单位是分没有小数点)
        int money = new BigDecimal(amount).multiply(new BigDecimal(100)).intValue();
        map.put("total_fee", money + "");
        map.put("out_trade_no", out_trade_no);
        // 交易类型
        map.put("trade_type", Configure.getTradeType());
        // 商品描述
        map.put("body", body);
        map.put("spbill_create_ip", spbillCreateIp);
        // 回掉地址
        map.put("notify_url", Configure.NOTIFY_URL);
        map.put("sign", Signature.getSign(map));

        String requestXML = PayCommonUtil.getRequestXml(map); // xml格式的字符串

 public static String getRequestXml(SortedMap<String, Object> parameters) {
        StringBuffer sb = new StringBuffer();
        sb.append("<xml>");
        Set es = parameters.entrySet();
        Iterator it = es.iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            String k = (String) entry.getKey();
            String v = (String) entry.getValue();
            if ("attach".equalsIgnoreCase(k) || "body".equalsIgnoreCase(k) || "sign".equalsIgnoreCase(k)) {
                sb.append("<" + k + ">" + "<![CDATA[" + v + "]]></" + k + ">");
            } else {
                sb.append("<" + k + ">" + v + "</" + k + ">");
            }
        }
        sb.append("</xml>");
        return sb.toString();
    }

<xml>
   <appid>wx2421b1c4370ec43b</appid>
   <attach>支付测试</attach>
   <body>JSAPI支付测试</body>
   <mch_id>10000100</mch_id>
   <nonce_str>1add1a30ac87aa2db72f57a2375d8fec</nonce_str>
   <notify_url>http://wxpay.weixin.qq.com/pub_v2/pay/notify.v2.php</notify_url>
   <openid>oUpF8uMuAJO_M2pxb1Q9zNjWeS6o</openid>
   <out_trade_no>1415659990</out_trade_no>
   <spbill_create_ip>14.23.150.211</spbill_create_ip>
   <total_fee>1</total_fee>
   <trade_type>JSAPI</trade_type>
   <sign>0CB01533B8C1EF103065174F50BCA001</sign>
</xml> 

向微信的统一下单接口发送请求

public static String httpsRequest(String requestUrl, String requestMethod, String outputStr) {
        try {
            // 创建SSLContext对象,并使用我们指定的信任管理器初始化
            TrustManager[] tm = { new MyX509TrustManager() };
            SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
            sslContext.init(null, tm, new java.security.SecureRandom());
            // 从上述SSLContext对象中得到SSLSocketFactory对象
            SSLSocketFactory ssf = sslContext.getSocketFactory();
            URL url = new URL(requestUrl);
            HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
            conn.setSSLSocketFactory(ssf);
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            // 设置请求方式(GET/POST)
            conn.setRequestMethod(requestMethod);
            conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
            // 当outputStr不为null时向输出流写数据
            if (null != outputStr) {
                OutputStream outputStream = conn.getOutputStream();
                // 注意编码格式
                outputStream.write(outputStr.getBytes("UTF-8"));
                outputStream.close();
            }
            // 从输入流读取返回内容
            InputStream inputStream = conn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String str = null;
            StringBuffer buffer = new StringBuffer();
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            // 释放资源
            bufferedReader.close();
            inputStreamReader.close();
            inputStream.close();
            inputStream = null;
            conn.disconnect();
            return buffer.toString();
        } catch (ConnectException ce) {
            log.error("连接超时:{}", ce);
        } catch (Exception e) {
            log.error("https请求异常:{}", e);
        }
        return null;
    }

3. 获取到微信的相应,拼装数据格式为吊起支付接口;然后返回给手机;

String result = CommonUtil.httpsRequest(Configure.REFUND_QUERY_API, "POST", requestXML);

Map<String, Object> resultMap = XMLParser.getMapFromXML(result);

SortedMap<String, Object> map2 = new TreeMap<String, Object>();

 String code = resultMap.get("return_code").toString();
        if (StringUtils.equals("SUCCESS", code)) {


            String nonce_str = MD5.MD5Encode(System.currentTimeMillis() / 1000 + "", "utf-8");
            nonce_str = String.valueOf(System.currentTimeMillis() / 1000);


            map2.put("appid", resultMap.get("appid").toString());
            map2.put("noncestr", randomString);
            map2.put("package", "Sign=WXPay");
            map2.put("partnerid", resultMap.get("mch_id").toString());
            map2.put("prepayid", resultMap.get("prepay_id").toString());
            map2.put("timestamp", nonce_str);


            resultMap.clear(); // 很重要哦
            resultMap.put("sign", Signature.getSign(map2));
            resultMap.put("noncestr", randomString);
            resultMap.put("package", "Sign=WXPay");
            resultMap.put("timestamp", nonce_str);
            resultMap.put("appid", map2.get("appid").toString());
            resultMap.put("partnerid", map2.get("partnerid").toString());
            resultMap.put("prepayid", map2.get("prepayid").toString());
        }

好多人在这一步出现问题,原因是文档中没有提现这一步数据是怎么返回的,没有明确说明,后台返回给手机端数据,需要按照调起支付接口的请求参数格式;当然我也在这步卡了n久;无良的微信支付接口;

这一步应该是后台按照调起支付接口拼装数据包括sign,然后传给手机端,手机端调用该接口;

当然需要注意一点,就是这个过程中,不要传多余的值,只要这些数据;

值得一提的是微信的返回结果-2是用户取消,我只想说一句,用户取消你妹啊,明明是参数有问题,还用户取消,然后一点提示没有,我已经无力吐槽;大公司不在乎小人物的感受~~~~你不用拉倒~~~有的是人用~~~我们不在乎~~~

有人说:随机字符串,第二次传的要和第一次的是一个,反正我是这样做的,成功了;

4. 准备回调方法

   @RequestMapping(value = "/weixinRes", method = RequestMethod.POST)
    @ControllerSystemLog(description = "微信支付请求回调", type = SystemLogTypeEnum.USER)
    public void weixinRes() throws Exception {
        LoggerUtil.info(getClass(), getRequest().getParameterMap().toString());
        InputStream inStream = getRequest().getInputStream();
        ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = inStream.read(buffer)) != -1) {
            outSteam.write(buffer, 0, len);
        }
        LoggerUtil.info(getClass(), "~~~~~~~~~~~~~~~~付款成功~~~~~~~~~");
        outSteam.close();
        inStream.close();
        String result = new String(outSteam.toByteArray(), "utf-8");// 获取微信调用我们notify_url的返回信息
        Map<String, Object> map = XMLParser.getMapFromXML(result);
        for (Object keyValue : map.keySet()) {
            LoggerUtil.info(getClass(), keyValue + "=" + map.get(keyValue));
        }
        if (map.get("result_code").toString().equalsIgnoreCase("SUCCESS")) {
            try {
                this.orderService.save(map);
                getResponse().getWriter().write(PayCommonUtil.setXML("SUCCESS", "")); // 告诉微信服务器,我收到信息了,不要在调用回调action了
                LoggerUtil.info(getClass(), "-------------" + PayCommonUtil.setXML("SUCCESS", ""));
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            LoggerUtil.info(getClass(), "~~~~~~~~~~~~~~~~微信付款失败了~~~~~~~~~" + map.get("return_msg").toString());
        }
    }





微信支付 V3 接口的开发指南和技术实现主要包括以下几个核心环节: 1. **前期准备**:首先需要注册微信支付商户号,并获取商户号和商户私钥。在微信商户平台生成 API v3 密钥,并下载证书用于后续的签名和验证过程。 2. **配置证书**:将下载的证书配置到项目中,并设置商户号和商户私钥。这些信息通常会用于生成请求签名,确保请求的完整性和安全性。 3. **构建请求参数**:对于每一个微信支付接口,需要构建完整的请求参数,包括接口地址、请求方法(GET/POST)、请求头(Header)以及请求体(Body)。请求头中需要包含 `Authorization` 字段,该字段的值是通过签名算法生成的签名串。 4. **生成签名**:签名是微信支付 V3 接口安全机制的重要组成部分。签名算法通常基于商户私钥和请求内容生成。签名生成后需要添加到请求头中,以便微信服务器进行验证。 5. **发送请求**:使用 HTTP 客户端(如 Apache HttpClient 或 Guzzle)发送构建好的请求。在发送请求时,需要确保所有参数正确无误,并且签名已正确附加。 6. **解析响应**:接收到微信支付服务器的响应后,需要对响应内容进行解析。响应内容通常为 JSON 格式,包含支付结果、交易状态等信息。同时,还需要验证响应的签名,确保响应确实来自微信服务器,防止中间人攻击。 7. **处理支付结果**:根据解析后的响应内容,处理支付成功或失败的逻辑。例如,在支付成功后更新订单状态,在支付失败时提示用户重新支付。 在开发过程中,可以借助官方提供的 SDK 简化开发工作。例如,微信支付提供了适用于不同语言的 SDK,如 Java SDK 和 Python SDK,内部封装了部分常用方法,开发者只需按照官方 Demo 调用相关接口即可[^2]。 此外,微信支付 V3 接口还支持多种支付场景,包括小程序支付、H5 支付、Native 支付等。每种支付场景的接口调用方式略有不同,开发者需要根据实际需求选择合适的支付方式。 以下是一个简单的 Java 示例代码,展示如何使用 Apache HttpClient 发送带有签名的请求: ```java import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.util.EntityUtils; import java.util.*; public class WeChatPayV3 { private static final String MCH_ID = "your_merchant_id"; private static final String PRIVATE_KEY = "your_private_key"; private static final String API_V3_KEY = "your_api_v3_key"; private static final String CERT_SERIAL_NO = "your_certificate_serial_number"; public static String generateSignature(String method, String url, String body) { // 实现签名生成逻辑 return "generated_signature"; } public static void main(String[] args) throws Exception { String url = "https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi"; String body = "{" + "\"mchid\":\"" + MCH_ID + "\"," + "\"out_trade_no\":\"20231001123456\"," + "\"appid\":\"your_appid\"," + "\"description\":\"Test Payment\"," + "\"amount\":{" + "\"total\":1," + "\"currency\":\"CNY\"" + "}," + "\"payer\":{" + "\"openid\":\"user_openid\"" + "}" + "}"; String method = "POST"; String signature = generateSignature(method, url, body); try (CloseableHttpClient httpClient = HttpClients.createDefault()) { HttpPost httpPost = new HttpPost(url); httpPost.setHeader("Content-Type", "application/json"); httpPost.setHeader("Authorization", "WECHATPAY2-SHA256-RSA2048 " + signature); httpPost.setHeader("Wechatpay-Serial", CERT_SERIAL_NO); httpPost.setEntity(new StringEntity(body)); try (CloseableHttpResponse response = httpClient.execute(httpPost)) { String responseBody = EntityUtils.toString(response.getEntity()); System.out.println("Response: " + responseBody); } } } } ``` 在上述代码中,`generateSignature` 方法用于生成签名,`main` 方法中构建了请求并发送至微信支付服务器。收到响应后,程序会打印出响应内容。 为了确保接口的安全性,建议开发者在开发过程中严格遵循微信支付官方文档的要求,尤其是在签名生成和证书管理方面。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值