From 4beed66d31755142dd69f0294fd8521f7797b66a Mon Sep 17 00:00:00 2001 From: xuhy <3313886187@qq.com> Date: 星期三, 03 九月 2025 16:39:36 +0800 Subject: [PATCH] bug修改 --- ruoyi-service/ruoyi-other/src/main/java/com/ruoyi/other/util/payment/wx/WechatPayService.java | 72 +++++++++++++++++++++++++++++++++++ 1 files changed, 71 insertions(+), 1 deletions(-) diff --git a/ruoyi-service/ruoyi-other/src/main/java/com/ruoyi/other/util/payment/wx/WechatPayService.java b/ruoyi-service/ruoyi-other/src/main/java/com/ruoyi/other/util/payment/wx/WechatPayService.java index 9541643..fa55680 100644 --- a/ruoyi-service/ruoyi-other/src/main/java/com/ruoyi/other/util/payment/wx/WechatPayService.java +++ b/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 -- Gitblit v1.7.1