无关风月
2025-09-01 43e82ea6be043159401551036990e8132ac312d0
ruoyi-service/ruoyi-other/src/main/java/com/ruoyi/other/util/payment/wx/WechatPayService.java
@@ -87,7 +87,7 @@
        params.put("out_trade_no", orderNumber);
        params.put("total_fee", String.valueOf(i) );
        params.put("spbill_create_ip", "221.182.45.100"); // 实际应用中应获取客户端IP
        params.put("notify_url", "http://221.182.45.100:8084"+callbackPath);
        params.put("notify_url", "http://47.120.5.122:8080"+callbackPath);
        params.put("trade_type", "NATIVE");
        // 生成签名
        String sign = weixinSignature(params);
@@ -110,6 +110,76 @@
        resultMap.put("code",orderNumber);
        return resultMap;
    }
    /**
     * 统一下单
     * @param orderNumber 订单号
     * @param totalFee 总金额(分)
     * @param body 商品描述
     * @param openid 用户openid
     * @return 预支付订单信息
     */
    public R unifiedOrderApplet(String orderId,String orderNumber, String totalFee, String body, String openid, String callbackPath) throws Exception {
        int i = new BigDecimal(totalFee).multiply(new BigDecimal("100")).intValue();
        String hostAddress = null;
        try {
            hostAddress = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        // 构建请求参数
        Map<String, String> params = new HashMap<>();
        params.put("appid", wechatPayConfig.getAppId());
        params.put("mch_id", wechatPayConfig.getMchId());
        params.put("nonce_str", generateNonceStr());
        params.put("body", body);
        params.put("out_trade_no", orderNumber);
        params.put("total_fee", String.valueOf(i) );
        params.put("spbill_create_ip", "221.182.45.100"); // 实际应用中应获取客户端IP
        params.put("notify_url", "http://47.120.5.122:8080"+callbackPath);
        params.put("trade_type", "JSAPI");
        params.put("openid", openid);
        // 生成签名
        String sign = weixinSignature(params);
        params.put("sign", sign);
        // 将参数转换为XML
        String xmlParams = XMLUtil.mapToXml(params).replaceFirst("^<\\?xml.+?\\?>\\s*", "");
        // 发送请求到微信支付统一下单接口
        String url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
        String result = HttpUtil.post(url, xmlParams);
        // 解析返回结果
        Map<String, String> resultMap = XMLUtil.xmlToMap(result);
        // 验证签名
        if (!verifySign(resultMap, wechatPayConfig.getKey())) {
            return R.fail("微信支付签名验证失败");
//            throw new Exception("微信支付签名验证失败");
        }
        if (!resultMap.get("return_code").equals("SUCCESS")) {
            return R.fail(resultMap.get("return_msg"));
        }
        // 构建小程序支付所需参数
        Map<String, String> payParams = new HashMap<>();
        payParams.put("appId", wechatPayConfig.getAppId());
        payParams.put("timeStamp", String.valueOf(System.currentTimeMillis() / 1000));
        payParams.put("nonceStr", generateNonceStr());
        payParams.put("package", "prepay_id=" + resultMap.get("prepay_id"));
        payParams.put("signType", "MD5");
        // 生成支付签名
        String paySign = weixinSignature(payParams);
        payParams.put("paySign", paySign);
        //给前端标识
        payParams.put("payMethod","1");
        payParams.put("orderId", orderId);
        return R.ok(JSON.toJSONString(payParams));
    }
    /**
     * 微信下单的签名算法
     * @param map