Kaynağa Gözat

feat(pay): 添加微信分账功能

- 新增 Profitsharing、ProfitSharingRequest、Receiver 类用于分账相关数据处理
- 添加 ProfitsharingAddReceiverBo 类实现分账逻辑
- 在 payController 中添加测试接口
- 增加 RelationTypeEnum 枚举定义分账
wzq 1 hafta önce
ebeveyn
işleme
a6c97c0172

+ 25 - 0
national-motion-module-system/national-motion-system-biz/src/main/java/org/jeecg/modules/pay/entity/ProfitSharingRequest.java

@@ -0,0 +1,25 @@
+package org.jeecg.modules.pay.entity;
+
+import lombok.Data;
+import lombok.experimental.Accessors;
+
+import java.io.Serializable;
+import java.util.List;
+
+@Data
+@Accessors(chain = true)
+public class ProfitSharingRequest implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    private String nonce_str;
+    private String sign_type = "HMAC-SHA256";
+
+    private String sub_mchid;
+    private String appid;
+    private String sub_appid;
+    private String transaction_id; // 微信支付订单号
+    private String out_order_no;    // 分账单号
+    private List<Receiver> receivers;
+    private boolean unfreeze_unsplit;
+}

+ 119 - 0
national-motion-module-system/national-motion-system-biz/src/main/java/org/jeecg/modules/pay/entity/Profitsharing.java

@@ -0,0 +1,119 @@
+package org.jeecg.modules.pay.entity;
+ 
+import java.lang.reflect.Field;
+import java.util.HashMap;
+import java.util.Map;
+
+
+/**
+ * 分账协议
+ * 
+ * @author libaibai
+ * @version 1.0 2020年10月19日
+ */
+public class Profitsharing {
+ 
+	private String mch_id; //
+	private String sub_mch_id; //
+	private String appid;
+ 
+//	private String nonce_str = UUIDUtil.getUUID19();
+	private String sign;
+ 
+	private String transaction_id; // 微信订单号
+ 
+	private String out_order_no; // 分账单号
+ 
+	private String receivers; // 分账接收方列表
+ 
+	public Profitsharing(String mch_id, String sub_mch_id, String appid, String transaction_id, String out_order_no, String receivers, String key) {
+		this.mch_id = mch_id;
+		this.sub_mch_id = sub_mch_id;
+		this.appid = appid;
+		this.transaction_id = transaction_id;
+		this.out_order_no = out_order_no;
+		this.receivers = receivers;
+//		this.sign = Signature.getSignSha(toMap(), key);
+	}
+ 
+	public String getMch_id() {
+		return mch_id;
+	}
+ 
+	public void setMch_id(String mch_id) {
+		this.mch_id = mch_id;
+	}
+ 
+	public String getSub_mch_id() {
+		return sub_mch_id;
+	}
+ 
+	public void setSub_mch_id(String sub_mch_id) {
+		this.sub_mch_id = sub_mch_id;
+	}
+ 
+	public String getAppid() {
+		return appid;
+	}
+ 
+	public void setAppid(String appid) {
+		this.appid = appid;
+	}
+ 
+//	public String getNonce_str() {
+//		return nonce_str;
+//	}
+//
+//	public void setNonce_str(String nonce_str) {
+//		this.nonce_str = nonce_str;
+//	}
+ 
+	public String getSign() {
+		return sign;
+	}
+ 
+	public void setSign(String sign) {
+		this.sign = sign;
+	}
+ 
+	public String getTransaction_id() {
+		return transaction_id;
+	}
+ 
+	public void setTransaction_id(String transaction_id) {
+		this.transaction_id = transaction_id;
+	}
+ 
+	public String getOut_order_no() {
+		return out_order_no;
+	}
+ 
+	public void setOut_order_no(String out_order_no) {
+		this.out_order_no = out_order_no;
+	}
+ 
+	public String getReceivers() {
+		return receivers;
+	}
+ 
+	public void setReceivers(String receivers) {
+		this.receivers = receivers;
+	}
+ 
+	public Map<String, Object> toMap() {
+		Map<String, Object> map = new HashMap<String, Object>();
+		Field[] fields = this.getClass().getDeclaredFields();
+		for (Field field : fields) {
+			Object obj;
+			try {
+				obj = field.get(this);
+				if (obj != null) {
+					map.put(field.getName(), obj);
+				}
+			} catch (IllegalArgumentException e) {
+			} catch (IllegalAccessException e) {
+			}
+		}
+		return map;
+	}
+}

+ 18 - 0
national-motion-module-system/national-motion-system-biz/src/main/java/org/jeecg/modules/pay/entity/Receiver.java

@@ -0,0 +1,18 @@
+package org.jeecg.modules.pay.entity;
+
+import lombok.Data;
+import lombok.experimental.Accessors;
+
+import java.io.Serializable;
+
+@Data
+@Accessors(chain = true)
+public class Receiver implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    private String type;          // MERCHANT_ID/PERSONAL_OPENID
+    private String account;       // 接收方账号
+    private Integer amount;       // 分账金额(分)
+    private String description;   // 描述
+}

+ 74 - 2
national-motion-module-system/national-motion-system-biz/src/main/java/org/jeecg/modules/pay/paytest/payController.java

@@ -1,15 +1,23 @@
 package org.jeecg.modules.pay.paytest;
 
 
+import com.alibaba.fastjson2.JSONObject;
+import lombok.AllArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.jeecg.modules.pay.config.WechatPayV3Utils;
+import org.jeecg.modules.pay.config.WechatUrlConstants;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
- 
 
+@Slf4j
 @RestController
+@AllArgsConstructor
 @RequestMapping(value = "/test")
 public class payController {
 
+    private final WechatPayV3Utils wechatPayV3Utils;
+
     @GetMapping(value = "/getPay")
     public String getPay(String orderSn,int total , String description) {
         PayMerchantUtil payMerchantUtil = new PayMerchantUtil();
@@ -19,6 +27,70 @@ public class payController {
             throw new RuntimeException(e);
         }
     }
- 
+
+    @GetMapping(value = "/test")
+    public JSONObject test() {
+        //通过订单ID查询流水号
+        String transaction_id  = "88888888888888888888";
+        try {
+            JSONObject res = wechatPayV3Utils.sendGet(WechatUrlConstants.PAY_V3_QUERY_PROFIT_SHARING+"P001001001"+"?sub_mchid="+ServiceProvider.sub_mchid+"&transaction_id="+transaction_id);
+            log.info("wechatPay res:{}", res.toString());
+            return res;
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
+
+//    @GetMapping(value = "/test")
+//    public JSONObject test() {
+//        //通过订单ID查询流水号
+//        String transaction_id  = "88888888888888888888";
+//        try {
+//            JSONObject res = wechatPayV3Utils.sendGet(WechatUrlConstants.PAY_V3_QUERY_PROFIT_SHARING+"P001001001"+"?sub_mchid="+ServiceProvider.sub_mchid+"&transaction_id="+transaction_id);
+//            log.info("wechatPay res:{}", res.toString());
+//            return res;
+//        } catch (Exception e) {
+//            e.printStackTrace();
+//        }
+//        return null;
+//    }
+
+//    @GetMapping(value = "/test")
+//    public JSONObject test(String out_order_no) {
+//        //1.参数封装
+//        /**
+//         * 分账接收方列表,不超过50个json对象,不能设置出资子商户作为分账接受方
+//         点击行前的+展开字段详情
+//         */
+//        List<Receiver> receivers = new ArrayList<>();
+//        //查询当前订单的分账接收方列表
+//        for (int i = 0; i < 1; i++) {
+//            Receiver receiver = new Receiver();
+//            receiver.setType("MERCHANT_ID")
+//                    .setAccount(ServiceProvider.sp_mchid)
+//                    .setAmount(1)
+//                    .setDescription("微信文档值得吐槽");
+//            receivers.add(receiver);
+//        }
+//        ProfitSharingRequest profitSharingRequest = new ProfitSharingRequest();
+//        profitSharingRequest
+//                .setNonce_str(RandomUtil.randomString(32))
+//                .setSign_type("HMAC-SHA256")
+//                .setAppid(ServiceProvider.sp_appid)
+//                .setSub_mchid(ServiceProvider.sub_mchid)
+//                .setTransaction_id("4200001042202104228993583353")
+//                .setOut_order_no("P001001001")
+//                .setReceivers(receivers)
+//                .setUnfreeze_unsplit(Boolean.TRUE);
+//        try {
+//            JSONObject res = wechatPayV3Utils.sendPost(WechatUrlConstants.PAY_V3_PROFIT_SHARING, JSONObject.from(profitSharingRequest));
+//            log.info("wechatPay res:{}", res.toString());
+//            return res;
+//        } catch (Exception e) {
+//            e.printStackTrace();
+//        }
+//        return null;
+//    }
 }
  

+ 70 - 0
national-motion-module-system/national-motion-system-biz/src/main/java/org/jeecg/modules/pay/routing/ProfitsharingAddReceiverBo.java

@@ -0,0 +1,70 @@
+package org.jeecg.modules.pay.routing;
+
+import cn.hutool.core.util.RandomUtil;
+import com.alibaba.fastjson2.JSONObject;
+import lombok.extern.slf4j.Slf4j;
+import org.jeecg.modules.pay.config.WechatPayV3Utils;
+import org.jeecg.modules.pay.config.WechatUrlConstants;
+import org.jeecg.modules.pay.entity.ProfitSharingRequest;
+import org.jeecg.modules.pay.entity.Receiver;
+import org.jeecg.modules.pay.paytest.ServiceProvider;
+
+import javax.annotation.Resource;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * 添加分账接收方
+ *
+ * @author libaibai
+ * @version 1.0 2020年10月19日
+ */
+@Slf4j
+public class ProfitsharingAddReceiverBo {
+
+    @Resource
+     private WechatPayV3Utils wechatPayV3Utils;
+
+    /**
+     * @func :微信单次分账
+     * @data: 2019-11-11
+     * @auther :lijinquan
+     */
+    public JSONObject wechatProfitSharing() {
+        //1.参数封装
+        /**
+         * 分账接收方列表,不超过50个json对象,不能设置出资子商户作为分账接受方
+         点击行前的+展开字段详情
+         */
+        List<Receiver> receivers = new ArrayList<>();
+        //查询当前订单的分账接收方列表
+        for (int i = 0; i < 1; i++) {
+            Receiver receiver = new Receiver();
+            receiver.setType("MERCHANT_ID")
+                    .setAccount(ServiceProvider.sp_mchid)
+                    .setAmount(1)
+                    .setDescription("微信文档值得吐槽");
+            receivers.add(receiver);
+        }
+        ProfitSharingRequest profitSharingRequest = new ProfitSharingRequest();
+        profitSharingRequest
+                .setNonce_str(RandomUtil.randomString(32))
+                .setSign_type("HMAC-SHA256")
+                .setAppid(ServiceProvider.sp_appid)
+                .setSub_mchid(ServiceProvider.sub_mchid)
+                .setTransaction_id("4200001042202104228993583353")
+                .setOut_order_no("P001001001")
+                .setReceivers(receivers)
+                .setUnfreeze_unsplit(true);
+        try {
+            JSONObject res = wechatPayV3Utils.sendPost(WechatUrlConstants.PAY_V3_JSAPI, JSONObject.from(profitSharingRequest));
+            log.info("wechatPay res:{}", res.toString());
+            return res;
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+
+        return null;
+    }
+
+}

+ 45 - 0
national-motion-module-system/national-motion-system-biz/src/main/java/org/jeecg/modules/pay/routing/RelationTypeEnum.java

@@ -0,0 +1,45 @@
+package org.jeecg.modules.pay.routing;
+
+/**
+ * 【与分账方的关系类型】子商户与接收方的关系。
+ */
+public enum RelationTypeEnum {
+    SERVICE_PROVIDER("SERVICE_PROVIDER",0, "服务商"),
+    STORE("STORE",1, "门店"),
+    STAFF("STAFF",1, "员工"),
+    STORE_OWNER("STORE_OWNER",1, "店主"),
+    PARTNER("PARTNER",1, "合作伙伴"),
+    HEADQUARTER("HEADQUARTER",1, "总部"),
+    BRAND("BRAND",1, "品牌方"),
+    DISTRIBUTOR("DISTRIBUTOR",1, "分销商"),
+    USER("USER",1, "供应商"),
+    SUPPLIER("SUPPLIER",1, "门店"),
+    CUSTOM("CUSTOM",1, "自定义")
+    ;
+    public static String getTypeByValue(String value) {
+        for (RelationTypeEnum c : RelationTypeEnum.values()) {
+            if (c.value.equals(value)) {
+                return c.type;
+            }
+        }
+        return null;
+    }
+
+    // 成员变量
+    private final String type;
+    private final Integer value;
+    private final String desc;
+
+    // 构造方法 ,赋值给成员变量
+    RelationTypeEnum(String type, Integer value, String desc) {
+        this.type = type;
+        this.value = value;
+        this.desc = desc;
+    }
+
+    //覆盖方法  :只能使用toString方法来输出枚举变量值
+    @Override
+    public String toString() {
+        return this.type + "_" + this.value+ "_" + this.desc;
+    }
+}