From 5dc40fcd64b0513150f1d8335ab849e6d8cdc28e Mon Sep 17 00:00:00 2001
From: 无关风月 <443237572@qq.com>
Date: 星期五, 04 七月 2025 19:42:49 +0800
Subject: [PATCH] 支付版本更新 根据资金流向使用V2或V3服务商版本支付

---
 cloud-server-other/src/main/java/com/dsh/other/util/PayMoneyUtil.java |  130 +++++++++++++++++++++++++++++++-----------
 1 files changed, 95 insertions(+), 35 deletions(-)

diff --git a/cloud-server-other/src/main/java/com/dsh/other/util/PayMoneyUtil.java b/cloud-server-other/src/main/java/com/dsh/other/util/PayMoneyUtil.java
index 1395bc1..d0200ee 100644
--- a/cloud-server-other/src/main/java/com/dsh/other/util/PayMoneyUtil.java
+++ b/cloud-server-other/src/main/java/com/dsh/other/util/PayMoneyUtil.java
@@ -12,6 +12,10 @@
 import com.alipay.api.msg.MsgHandler;
 import com.alipay.api.request.*;
 import com.alipay.api.response.*;
+import com.dsh.other.util.wx.PartnerAppPrepay;
+import com.dsh.other.util.wx.WXPayUtility;
+import com.dsh.other.util.wx.WeChatV3SignUtil;
+import com.dsh.other.util.wx.WxV3PayConfig;
 import lombok.Synchronized;
 import org.apache.commons.collections.map.HashedMap;
 import org.bouncycastle.jce.provider.BouncyCastleProvider;
@@ -72,9 +76,7 @@
     private String certPath = "/usr/playpai/cert/weixin/apiclient_cert.p12";//微信证书
 
 
-
-
-    public ResultUtil confirm(String smid,String code, String outTradeNo, String amount) {
+    public ResultUtil confirm(String smid, String code, String outTradeNo, String amount) {
 
         AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
                 aliAppid,
@@ -85,15 +87,15 @@
                 "RSA2");
         AlipayTradeSettleConfirmRequest request = new AlipayTradeSettleConfirmRequest();
         request.setBizContent("{" +
-                "  \"out_request_no\":\""+code+"\"," +
-                "  \"trade_no\":\""+outTradeNo+"\"," +
+                "  \"out_request_no\":\"" + code + "\"," +
+                "  \"trade_no\":\"" + outTradeNo + "\"," +
                 "  \"settle_info\":{" +
                 "    \"settle_detail_infos\":[" +
                 "      {" +
                 "        \"trans_in_type\":\"defaultSettle\"," +
-                "        \"settle_entity_id\":\""+smid+"\"," +
+                "        \"settle_entity_id\":\"" + smid + "\"," +
                 "        \"settle_entity_type\":\"SecondMerchant\"," +
-                "        \"amount\":"+amount+"," +
+                "        \"amount\":" + amount + "," +
                 "      }" +
                 "    ]" +
                 "  }," +
@@ -107,7 +109,7 @@
         } catch (AlipayApiException e) {
             e.printStackTrace();
         }
-        if(response.isSuccess()){
+        if (response.isSuccess()) {
             System.out.println("调用成功");
             return ResultUtil.success();
         } else {
@@ -115,8 +117,9 @@
             return ResultUtil.error("出现问题啦");
         }
     }
+
     // 属于平台的运营商 因为无需分账不冻结资金
-    public ResultUtil confirm1(String smid,String code, String outTradeNo, String amount) {
+    public ResultUtil confirm1(String smid, String code, String outTradeNo, String amount) {
         AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
                 aliAppid,
                 appPrivateKey,
@@ -126,15 +129,15 @@
                 "RSA2");
         AlipayTradeSettleConfirmRequest request = new AlipayTradeSettleConfirmRequest();
         request.setBizContent("{" +
-                "  \"out_request_no\":\""+code+"\"," +
-                "  \"trade_no\":\""+outTradeNo+"\"," +
+                "  \"out_request_no\":\"" + code + "\"," +
+                "  \"trade_no\":\"" + outTradeNo + "\"," +
                 "  \"settle_info\":{" +
                 "    \"settle_detail_infos\":[" +
                 "      {" +
                 "        \"trans_in_type\":\"defaultSettle\"," +
-                "        \"settle_entity_id\":\""+smid+"\"," +
+                "        \"settle_entity_id\":\"" + smid + "\"," +
                 "        \"settle_entity_type\":\"SecondMerchant\"," +
-                "        \"amount\":"+amount+"," +
+                "        \"amount\":" + amount + "," +
                 "      }" +
                 "    ]" +
                 "  }," +
@@ -148,7 +151,7 @@
         } catch (AlipayApiException e) {
             e.printStackTrace();
         }
-        if(response.isSuccess()){
+        if (response.isSuccess()) {
             System.out.println("调用成功");
             return ResultUtil.success();
         } else {
@@ -156,10 +159,11 @@
             return ResultUtil.error("出现问题啦");
         }
     }
+
     /**
      * 支付宝支付
      */
-    public ResultUtil alipay(String smid,String body, String subject, String passbackParams, String outTradeNo, String amount, String notifyUrl) {
+    public ResultUtil alipay(String smid, String body, String subject, String passbackParams, String outTradeNo, String amount, String notifyUrl) {
 ////        //构造client
 //        CertAlipayRequest certAlipayRequest = new CertAlipayRequest ();
 //        //设置网关地址
@@ -381,8 +385,8 @@
 
 
         Map<String, String> map = new HashMap<>();
-        System.err.println("返回码"+params);
-        if (params.get("trade_status").equals("TRADE_SUCCESS")){
+        System.err.println("返回码" + params);
+        if (params.get("trade_status").equals("TRADE_SUCCESS")) {
             String out_trade_no = params.get("out_trade_no");
             String subject = params.get("subject");
             String total_amount = params.get("total_amount");
@@ -395,7 +399,7 @@
             map.put("passback_params", passback_params);//回传参数
             System.err.println("回调map");
             return map;
-        }else{
+        } else {
             return null;
         }
     }
@@ -451,16 +455,7 @@
         map.put("mch_id", mchId);
         map.put("nonce_str", nonce_str);
         String temp = "";
-        if (body.split("-").length>1){
-            temp = body.split("-")[1];
-            map.put("body", body.split("-")[0]);
-        }else{
-            map.put("body", body);
-        }
-        if (StringUtils.hasLength(temp) && temp.equals("1")){
-            // 添加分账标识
-            map.put("profit_sharing", "Y");
-        }
+        map.put("body", body);
         map.put("attach", attach);//存储订单id
         map.put("out_trade_no", out_trade_no);//存储的订单code
         map.put("total_fee", i);
@@ -551,13 +546,74 @@
             return ResultUtil.error(map1.get("return_msg"), new JSONObject());
         }
     }
+
+    public ResultUtil weixinpayV3(String subMchid, String description, String outTradeNo, String notifyUrl, String totalFee, String attach) throws Exception {
+        int i = new BigDecimal(totalFee).multiply(new BigDecimal("100")).intValue();
+
+        // TODO: 请准备商户开发必要参数,参考:https://pay.weixin.qq.com/doc/v3/partner/4013080340
+        PartnerAppPrepay client = new PartnerAppPrepay(
+//                "1681873607",                  // 商户号,是由微信支付系统生成并分配给每个商户的唯一标识符,商户号获取方式参考https://pay.weixin.qq.com/doc/v3/partner/4013080340
+//                "55714944F7A7E52526F708280B176DCC838F371A",       // 商户API证书序列号,如何获取请参考https://pay.weixin.qq.com/doc/v3/partner/4013058924
+//                "E:\\wanpai\\1681873607_20250424_cert\\apiclient_key.pem", // 商户API证书私钥文件路径,本地文件路径
+//                "PUB_KEY_ID_0116818736072025042400351694002605",          // 微信支付公钥ID,如何获取请参考https://pay.weixin.qq.com/doc/v3/partner/4013038589
+//                "E:\\wanpai\\pub_key.pem"     // 微信支付公钥文件路径,本地文件路径
+
+                "1681873607",                  // 商户号,是由微信支付系统生成并分配给每个商户的唯一标识符,商户号获取方式参考https://pay.weixin.qq.com/doc/v3/partner/4013080340
+                "55714944F7A7E52526F708280B176DCC838F371A",       // 商户API证书序列号,如何获取请参考https://pay.weixin.qq.com/doc/v3/partner/4013058924
+                "/usr/playpai/server/wxV3/1681873607_20250424_cert/apiclient_key.pem", // 商户API证书私钥文件路径,本地文件路径
+                "PUB_KEY_ID_0116818736072025042400351694002605",          // 微信支付公钥ID,如何获取请参考https://pay.weixin.qq.com/doc/v3/partner/4013038589
+                "/usr/playpai/server/wxV3/pub_key.pem"     // 微信支付公钥文件路径,本地文件路径
+        );
+
+        PartnerAppPrepay.PartnerAPIv3CommonPrepayRequest request = new PartnerAppPrepay.PartnerAPIv3CommonPrepayRequest();
+        request.spAppid = appid;// appid
+        request.spMchid = WxV3PayConfig.Mch_ID;// 服务商商户号
+        request.subMchid = subMchid;// 子商户号
+        request.description = description;// 描述
+        request.outTradeNo = outTradeNo;// 订单号
+        request.notifyUrl = callbackPath+notifyUrl;// 回调地址
+        request.attach = attach;// 回调地址
+        request.amount = new PartnerAppPrepay.CommonAmountInfo();
+        request.amount.total = (long) i;// 金额 单位分
+        request.amount.currency = "CNY";
+        String prepayId = "";
+        Map<String, Object> map3 = new HashMap<>();
+        try {
+            PartnerAppPrepay.PartnerAPIv3AppPrepayResponse response = client.run(request);
+            // TODO: 请求成功,继续业务逻辑
+            System.err.println("微信申请成功,预支付ID: " + response.prepayId);
+            prepayId = response.prepayId;
+        } catch (WXPayUtility.ApiException e) {
+            // TODO: 请求失败,根据状态码执行不同的逻辑
+            e.printStackTrace();
+        }
+        map3.put("appid", appid);
+        map3.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000));
+        String nonce_str = UUIDUtil.getRandomCode(16);
+        map3.put("noncestr", nonce_str);
+        map3.put("prepayid", prepayId);
+        // 构造待签名字符串
+        String message = WeChatV3SignUtil.buildSignMessage(map3);
+        // 私钥路径(pem 文件)
+        String privateKeyPath = "/usr/playpai/server/wxV3/1681873607_20250424_cert/apiclient_key.pem";
+//      String privateKeyPath = "E:\\wanpai\\1681873607_20250424_cert\\apiclient_key.pem";
+        // 生成签名
+        String sign = WeChatV3SignUtil.signWithPrivateKey(message, privateKeyPath);
+        map3.put("sign", sign);
+        map3.put("package", "Sign=WXPay");
+        map3.put("partnerid", WxV3PayConfig.Mch_ID);// 服务商商户号
+        System.err.println(map3);
+        return ResultUtil.success(map3);
+    }
+
     /**
      * 发起分账
+     *
      * @param order 微信订单号
      * @return
      */
-    public ResultUtil fenzhang(String order,BigDecimal amount,String merchantNumber,
-                               String nonce_str,String description) throws Exception {
+    public ResultUtil fenzhang(String order, BigDecimal amount, String merchantNumber,
+                               String nonce_str, String description) throws Exception {
         Map<String, Object> map = new HashMap<>();
         map.put("mch_id", mchId);
         map.put("appid", appid);
@@ -576,7 +632,7 @@
         JSONObject jsonObject = new JSONObject(body);
         JSONArray objects = new JSONArray();
         objects.add(jsonObject);
-        map.put("receivers",objects.toJSONString());
+        map.put("receivers", objects.toJSONString());
         String s = this.weixinSignature1(map);
         map.put("sign", s);
         String url = "https://api.mch.weixin.qq.com/secapi/pay/profitsharing";
@@ -601,7 +657,7 @@
         } catch (Exception e) {
             e.printStackTrace();
         }
-        System.err.println("分账请求"+body1);
+        System.err.println("分账请求" + body1);
         //将结果xml解析成map
         body1 = body1.replaceAll("<!\\[CDATA\\[", "");
         body1 = body1.replaceAll("]]>", "");
@@ -626,12 +682,14 @@
             return ResultUtil.error(map1.get("return_msg"), new JSONObject());
         }
     }
+
     /**
      * 发起分账回退
+     *
      * @param order 微信订单号
      * @return
      */
-    public ResultUtil fenzhangRefund(String order,BigDecimal amount,String merchantNumber,
+    public ResultUtil fenzhangRefund(String order, BigDecimal amount, String merchantNumber,
                                      String nonce_str,
                                      String fenzhangRefundNo) throws Exception {
         Map<String, Object> map = new HashMap<>();
@@ -668,7 +726,7 @@
         } catch (Exception e) {
             e.printStackTrace();
         }
-        System.err.println("分账金额回退"+body1);
+        System.err.println("分账金额回退" + body1);
         //将结果xml解析成map
         body1 = body1.replaceAll("<!\\[CDATA\\[", "");
         body1 = body1.replaceAll("]]>", "");
@@ -1336,13 +1394,15 @@
         }
         return null;
     }
+
     /**
      * sha256_HMAC加密
+     *
      * @param message 消息
      * @param secret  秘钥
      * @return 加密后字符串
      */
-    public  String sha256_HMAC(String message, String secret) {
+    public String sha256_HMAC(String message, String secret) {
         String hash = "";
         try {
             Mac sha256_HMAC = Mac.getInstance("HmacSHA256");

--
Gitblit v1.7.1