批量分账

This commit is contained in:
Guoqs
2025-04-15 17:04:17 +08:00
parent 14fe3961a1
commit f540468f33
3 changed files with 1474 additions and 44 deletions

View File

@@ -1,6 +1,7 @@
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.huifu.adapay.core.exception.BaseAdaPayException;
@@ -9,6 +10,7 @@ import com.huifu.adapay.model.Refund;
import com.jsowell.JsowellApplication;
import com.jsowell.adapay.common.DivMember;
import com.jsowell.adapay.common.PaymentConfirmInfo;
import com.jsowell.adapay.dto.PaymentConfirmParam;
import com.jsowell.adapay.dto.QueryConfirmReverseDTO;
import com.jsowell.adapay.dto.QueryPaymentConfirmDTO;
import com.jsowell.adapay.operation.PaymentReverseOperation;
@@ -18,7 +20,9 @@ import com.jsowell.adapay.response.PaymentReverseResponse;
import com.jsowell.adapay.response.QueryPaymentConfirmDetailResponse;
import com.jsowell.adapay.service.AdapayService;
import com.jsowell.adapay.vo.OrderSplitResult;
import com.jsowell.common.constant.Constants;
import com.jsowell.common.enums.ykc.ScenarioEnum;
import com.jsowell.common.util.AdapayUtil;
import com.jsowell.common.util.StringUtils;
import com.jsowell.pile.domain.AdapayMemberAccount;
import com.jsowell.pile.service.OrderBasicInfoService;
@@ -53,6 +57,8 @@ public class PaymentTestController {
String wechatAppId2 = "wx20abc5210391649c"; // 嘉佳充电
String adapayAppId = "app_d0c80cb1-ffc8-48cb-a030-fe9bec823aaa";
@Autowired
private AdapayService adapayService;
@@ -76,11 +82,11 @@ public class PaymentTestController {
List<String> list =new ArrayList<String>();
try {
String path = "src/test/resources/payment_ids";
FileReader fileReader =new FileReader(path);
BufferedReader bufferedReader =new BufferedReader(fileReader);
String str=null;
while((str=bufferedReader.readLine())!=null) {
if(str.trim().length()>2) {
FileReader fileReader = new FileReader(path);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String str = null;
while ((str = bufferedReader.readLine()) != null) {
if (str.trim().length() > 2) {
list.add(str);
}
}
@@ -111,27 +117,130 @@ public class PaymentTestController {
return paramMap;
}
public static void main(String[] args) {
List<String> orderCodeList = Lists.newArrayList();
orderCodeList.add("C44107428110");
orderCodeList.add("877828180449136640");
orderCodeList.add("OP86761657642_20231110055438");
orderCodeList.add("C80813283397_20231110055123");
for (String orderCode : orderCodeList) {
// 如果orderCode长度大于12并且有下划线, 则根据下划线切割只取第一部分
if (orderCode.length() > 12 && orderCode.contains("_")) {
orderCode = orderCode.substring(0, orderCode.indexOf("_"));
}
System.out.println(orderCode);
}
}
/**
* 从文件中读取paymentId和未分帐金额
*/
public List<Map<String, String>> getPaymentIdListAndAmountForFile() {
List<Map<String, String>> list = Lists.newArrayList();
try {
String path = "src/test/resources/paymentIdAndAmount";
FileReader fileReader = new FileReader(path);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String str = null;
while ((str = bufferedReader.readLine()) != null) {
if (str.trim().length() > 2) {
// str根据逗号切割
String[] strArr = str.split(",");
String paymentId = strArr[0];
String settleAmount = strArr[1];
String orderCode = strArr[2];
// 如果orderCode长度大于12并且有下划线, 则根据下划线切割只取第一部分
if (orderCode.length() > 12 && orderCode.contains("_")) {
orderCode = orderCode.substring(0, orderCode.indexOf("_"));
}
list.add(ImmutableMap.of("paymentId", paymentId, "settleAmount", settleAmount, "orderCode", orderCode));
}
}
} catch (Exception e) {
}
// System.out.println(list);
return list;
}
/**
* 从文件获取分账参数
*/
public Map<String, Object> getPaymentConfirmParamFromFile() {
Map<String, Object> paramMap = Maps.newHashMap();
// 待分账汇付会员id, 如需分给对应商户就填写正确的汇付会员id
String adapayMemberId = "0";
// 待分账订单信息, 需要重新分账的订单信息(针对未分账的订单)
List<Map<String, String>> mapList = getPaymentIdListAndAmountForFile();
JSONArray jsonArray = new JSONArray();
for (Map<String, String> map : mapList) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("orderCode", map.get("orderCode"));
jsonObject.put("settleAmount", map.get("settleAmount"));
jsonObject.put("paymentId", map.get("paymentId"));
jsonArray.add(jsonObject);
}
paramMap.put("adapayMemberId", adapayMemberId);
paramMap.put("paymentList", jsonArray);
return paramMap;
}
/**
* 批量执行分账
*/
@Test
public void batchCreatePaymentConfirm() {
// 获取分账参数
Map<String, Object> confirmParam = getPaymentConfirmParam();
// Map<String, Object> confirmParam = getPaymentConfirmParam();
Map<String, Object> confirmParam = getPaymentConfirmParamFromFile();
// 分账到指定汇付会员账户中
AdapayMemberAccount adapayMemberAccount = new AdapayMemberAccount();
adapayMemberAccount.setAdapayMemberId((String) confirmParam.get("adapayMemberId"));
// 分账订单信息
JSONArray paymentList = (JSONArray) confirmParam.get("paymentList");
// 请求参数list
List<PaymentConfirmParam> paramList = new ArrayList<>();
for (int i = 0; i < paymentList.size(); i++) {
JSONObject jsonObject = (JSONObject) paymentList.get(i);
BigDecimal confirmAmt = jsonObject.getBigDecimal("settleAmount"); // 确认金额就是结算金额
String paymentId = jsonObject.getString("paymentId"); // 支付id
String orderCode = jsonObject.getString("orderCode"); // 订单编号
// 延时分账使用确认交易API
PaymentConfirmResponse paymentConfirmResponse = adapayService.createPaymentConfirmRequest(paymentId,
adapayMemberAccount, confirmAmt, orderCode, wechatAppId1);
System.out.println(JSON.toJSONString(paymentConfirmResponse));
System.out.println("paymentId:" + paymentId + ", orderCode:" + orderCode + ", settleAmount:" + confirmAmt);
DivMember divMember = new DivMember();
divMember.setMemberId(adapayMemberAccount.getAdapayMemberId());
divMember.setAmount(AdapayUtil.formatAmount(confirmAmt));
divMember.setFeeFlag(Constants.Y);
PaymentConfirmParam param = PaymentConfirmParam.builder()
.paymentId(paymentId)
.divMemberList(Lists.newArrayList(divMember))
.confirmAmt(confirmAmt)
.orderCode(orderCode)
.wechatAppId(wechatAppId1)
.build();
paramList.add(param);
}
// 执行分账方法
if (!paramList.isEmpty()) {
for (PaymentConfirmParam paymentConfirmParam : paramList) {
// 延时分账使用确认交易API
PaymentConfirmResponse paymentConfirmResponse = adapayService.createPaymentConfirmRequest(paymentConfirmParam);
// status为failed, error_code为payment_over_time_doing, error_msg为数据正在处理中请稍后再试, 则重试最多重试2次
if (paymentConfirmResponse.isFailed()) {
int count = 0;
while (count < 2) {
count++;
System.out.println("" + count + "次重试");
paymentConfirmResponse = adapayService.createPaymentConfirmRequest(paymentConfirmParam);
}
}
}
}
}
@@ -361,12 +470,15 @@ public class PaymentTestController {
System.out.println(JSON.toJSONString(orderSplitResult));
}
/**
* 查询未分帐金额
*/
@Test
public void queryPaymentConfirmDetailTest() throws BaseAdaPayException {
// 查询支付确认id
QueryPaymentConfirmDTO dto = new QueryPaymentConfirmDTO();
dto.setPaymentId("002212024121307453510713429549121368064");
dto.setPaymentId("002212023102515344310563156645282902016");
dto.setWechatAppId(wechatAppId1);
// 查询分账信息
QueryPaymentConfirmDetailResponse response = adapayService.queryPaymentConfirmList(dto);

File diff suppressed because it is too large Load Diff