feat: 新增分账功能

This commit is contained in:
hucs
2024-09-04 14:43:22 +08:00
parent 438d6755f5
commit c0b60d0769
4 changed files with 71 additions and 27 deletions

View File

@@ -313,6 +313,8 @@ public class PlayOrderInfoEntity extends BaseEntity<PlayOrderInfoEntity> {
*/
private String excludeHistory;
private BigDecimal profitSharingAmount;
public PlayOrderInfoEntity() {

View File

@@ -161,4 +161,6 @@ public class SysTenantEntity extends BaseEntity<SysTenantEntity> {
private String chongzhichenggongTemplateId;
private String xindingdanshoulitongzhiTemplateId;
private Integer profitsharingRate = 0;
}

View File

@@ -202,6 +202,9 @@ public class SysTenantServiceImpl extends ServiceImpl<SysTenantMapper, SysTenant
if (sysUserMapper.checkUserNameUnique(sysTenantEntity.getUserName()) > 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());
}

View File

@@ -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;
// 参数示例
//<xml>
// <appid><![CDATA[wx846289ab0b2e9f89]]></appid>
// <bank_type><![CDATA[OTHERS]]></bank_type>
// <cash_fee><![CDATA[1]]></cash_fee>
// <fee_type><![CDATA[CNY]]></fee_type>
// <is_subscribe><![CDATA[N]]></is_subscribe>
// <mch_id><![CDATA[1616729997]]></mch_id>
// <nonce_str><![CDATA[1692338418306]]></nonce_str>
// <openid><![CDATA[oNHh06-KauVEor1BfSWsBr2Xil-I]]></openid>
// <out_trade_no><![CDATA[gameno20230818140018j5kc]]></out_trade_no>
// <result_code><![CDATA[SUCCESS]]></result_code>
// <return_code><![CDATA[SUCCESS]]></return_code>
// <sign><![CDATA[639C5B46EB11B42A354E38A5F96683CC]]></sign>
// <time_end><![CDATA[20230818140028]]></time_end>
// <total_fee>1</total_fee>
// <trade_type><![CDATA[JSAPI]]></trade_type>
// <transaction_id><![CDATA[4200001910202308187726172728]]></transaction_id>
//</xml>
}
private void profitSharing(WxPayService wxPayService, Map<String, String> 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));
}
}