diff --git a/play-admin/src/main/java/com/starry/admin/modules/order/module/entity/PlayOrderInfoEntity.java b/play-admin/src/main/java/com/starry/admin/modules/order/module/entity/PlayOrderInfoEntity.java index 5c109e1..994d620 100644 --- a/play-admin/src/main/java/com/starry/admin/modules/order/module/entity/PlayOrderInfoEntity.java +++ b/play-admin/src/main/java/com/starry/admin/modules/order/module/entity/PlayOrderInfoEntity.java @@ -313,6 +313,8 @@ public class PlayOrderInfoEntity extends BaseEntity { */ private String excludeHistory; + private BigDecimal profitSharingAmount; + public PlayOrderInfoEntity() { diff --git a/play-admin/src/main/java/com/starry/admin/modules/system/module/entity/SysTenantEntity.java b/play-admin/src/main/java/com/starry/admin/modules/system/module/entity/SysTenantEntity.java index 9764b88..c32e661 100644 --- a/play-admin/src/main/java/com/starry/admin/modules/system/module/entity/SysTenantEntity.java +++ b/play-admin/src/main/java/com/starry/admin/modules/system/module/entity/SysTenantEntity.java @@ -161,4 +161,6 @@ public class SysTenantEntity extends BaseEntity { private String chongzhichenggongTemplateId; private String xindingdanshoulitongzhiTemplateId; + private Integer profitsharingRate = 0; + } diff --git a/play-admin/src/main/java/com/starry/admin/modules/system/service/impl/SysTenantServiceImpl.java b/play-admin/src/main/java/com/starry/admin/modules/system/service/impl/SysTenantServiceImpl.java index 336abf5..993ff4d 100644 --- a/play-admin/src/main/java/com/starry/admin/modules/system/service/impl/SysTenantServiceImpl.java +++ b/play-admin/src/main/java/com/starry/admin/modules/system/service/impl/SysTenantServiceImpl.java @@ -202,6 +202,9 @@ public class SysTenantServiceImpl extends ServiceImpl 0) { return R.error("管理员账号已存在,请重新设置!"); } + if (sysTenantEntity.getProfitsharingRate() < 0 || sysTenantEntity.getProfitsharingRate() > 30) { + throw new RuntimeException("分账抽成参数不得小于0,不得大于30"); + } if (StrUtil.isBlankIfStr(sysTenantEntity.getTenantId())) { sysTenantEntity.setTenantId(IdUtils.getUuid()); } diff --git a/play-admin/src/main/java/com/starry/admin/modules/weichat/controller/WxPlayController.java b/play-admin/src/main/java/com/starry/admin/modules/weichat/controller/WxPlayController.java index c3985d1..0b8dfa4 100644 --- a/play-admin/src/main/java/com/starry/admin/modules/weichat/controller/WxPlayController.java +++ b/play-admin/src/main/java/com/starry/admin/modules/weichat/controller/WxPlayController.java @@ -2,8 +2,14 @@ package com.starry.admin.modules.weichat.controller; import cn.hutool.core.util.StrUtil; +import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONObject; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse; +import com.github.binarywang.wxpay.bean.profitsharing.ProfitSharingReceiverRequest; +import com.github.binarywang.wxpay.bean.profitsharing.ProfitSharingReceiverResult; +import com.github.binarywang.wxpay.bean.profitsharing.ProfitSharingRequest; +import com.github.binarywang.wxpay.bean.profitsharing.ProfitSharingResult; import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest; import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderResult; import com.github.binarywang.wxpay.exception.WxPayException; @@ -17,6 +23,7 @@ import com.starry.admin.modules.custom.service.IPlayCustomUserInfoService; import com.starry.admin.modules.order.module.entity.PlayOrderInfoEntity; import com.starry.admin.modules.order.service.IPlayOrderInfoService; import com.starry.admin.modules.order.service.impl.PlayOrderInfoServiceImpl; +import com.starry.admin.modules.system.entity.SysTenantEntity; import com.starry.admin.modules.system.service.impl.SysTenantServiceImpl; import com.starry.admin.modules.weichat.service.WxCustomMpService; import com.starry.admin.utils.SecurityUtils; @@ -62,13 +69,6 @@ public class WxPlayController { private WxCustomMpService mpService; - //@GetMapping("/test") - //public String test() { - // SysTenantEntity tenant = tenantService.getById("7f7ecd75fd29464db2867982f88e8ef0"); - // tenantService.initRole(tenant); - // return WxPayNotifyResponse.success("成功"); - //} - /** * 微信支付-微信回调地址(https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=9_7&index=8) * @@ -122,6 +122,9 @@ public class WxPlayController { log.info("*********支付处理完成"); // 推送通知 mpService.sendBalanceMessage(orderInfoEntity); + // 请求分账 + WxPayService wxPayService = mpService.getWxPay(); + this.profitSharing(wxPayService, orderMap, orderInfoEntity, tenantService.selectSysTenantByTenantId(orderInfoEntity.getTenantId())); } catch (Exception e) { log.error("订单回调业务处理异常,xmlData={}", xmlData); } @@ -152,6 +155,9 @@ public class WxPlayController { if (StringUtils.isEmpty(money)) { throw new CustomException("请求参数错误,money不能为空"); } + if (new BigDecimal(money).compareTo(new BigDecimal("10")) < 0) { + throw new CustomException("充值金额不能小于10元"); + } String tenantId = SecurityUtils.getTenantId(); if (StrUtil.isBlankIfStr(tenantId)) { throw new CustomException("系统错误,租户ID获取失败"); @@ -159,7 +165,8 @@ public class WxPlayController { // 用户信息 PlayCustomUserInfoEntity customUserInfo = customUserInfoService.selectById(ThreadLocalRequestDetail.getCustomUserInfo().getId()); // 租户信息 -// SysTenantEntity entity = tenantService.selectSysTenantByTenantId(tenantId); + SysTenantEntity tenant = tenantService.selectSysTenantByTenantId(tenantId); + Boolean profitSharing = tenant.getProfitsharingRate() > 0; // 订单总金额,单位为分 long totalFee = getTotalFee(money); // 创建订单信息 @@ -178,6 +185,12 @@ public class WxPlayController { request.setSpbillCreateIp("101.43.206.16"); request.setNotifyUrl(wxPayService.getConfig().getNotifyUrl()); request.setBody("船票充值"); + if (profitSharing) { + // 添加分账方 + this.profitSharingAddreceiver(wxPayService); + request.setProfitSharing("Y"); + } + WxPayUnifiedOrderResult orderResult; try { //统一下单,商户系统先调用该接口在微信支付服务后台生成预支付交易单,返回正确的预支付交易会话标识后再在APP里面调起支付 @@ -204,6 +217,23 @@ public class WxPlayController { return R.ok(jsonObject); } + private void profitSharingAddreceiver(WxPayService wxPayService) { + ProfitSharingReceiverRequest request = new ProfitSharingReceiverRequest(); + request.setAppid(wxPayService.getConfig().getAppId()); + request.setMchId(wxPayService.getConfig().getMchId()); + String nonceStr = "dalfhh241lnandnsklajax"; + request.setNonceStr(nonceStr); + request.setSignType("HMAC-SHA256"); + request.setReceiver(new JSONObject().fluentPut("type", "MERCHANT_ID").fluentPut("account", "1681470208").fluentPut("name", "合肥经济技术开发区陪陪信息技术服务部(个体工商户)").fluentPut("relation_type", "SERVICE_PROVIDER").toString()); + try { + ProfitSharingReceiverResult result = wxPayService.getProfitSharingService().addReceiver(request); + log.info("请求添加分账结果:" + JSONObject.toJSONString(result)); + } catch (WxPayException e) { + throw new RuntimeException(e); + } + + } + /** * @param money 金额,单位-元 @@ -236,24 +266,31 @@ public class WxPlayController { throw new RuntimeException(e); } return map; - // 参数示例 - // - // - // - // - // - // - // - // - // - // - // - // - // - // - // 1 - // - // - // } + + + private void profitSharing(WxPayService wxPayService, Map orderMap, PlayOrderInfoEntity orderInfoEntity, SysTenantEntity tenant) throws WxPayException { + Boolean profitSharing = tenant.getProfitsharingRate() > 0; + if (!profitSharing) { + return; + } + log.info("开始进行分账。。。。。。。。。。。。。。。"); + long totalFee = getTotalFee(orderInfoEntity.getOrderMoney().toString()); + int amount = new BigDecimal(totalFee).multiply(BigDecimal.valueOf((double) tenant.getProfitsharingRate() / 100)).intValue(); + if (amount == 0) { + return; + } + ProfitSharingRequest request = new ProfitSharingRequest(); + request.setSignType("HMAC-SHA256"); + request.setTransactionId(orderMap.get("transaction_id")); + request.setOutOrderNo("ps" + orderMap.get("out_trade_no")); + + request.setReceivers(new JSONArray().fluentAdd(new JSONObject().fluentPut("type", "MERCHANT_ID").fluentPut("name", "合肥经济技术开发区陪陪信息技术服务部(个体工商户)").fluentPut("account", "1681470208").fluentPut("amount", amount)).toString()); + ProfitSharingResult result = wxPayService.getProfitSharingService().profitSharing(request); + log.info("分账结果:" + JSONObject.toJSONString(result)); + // 保存分账金额 + BigDecimal profitSharingAmount = new BigDecimal(amount).divide(new BigDecimal(100)); + orderInfoService.update(null, Wrappers.lambdaUpdate(PlayOrderInfoEntity.class).eq(PlayOrderInfoEntity::getId, orderInfoEntity.getId()).set(PlayOrderInfoEntity::getProfitSharingAmount, profitSharingAmount)); + } + }