import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONObject; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.huifu.adapay.core.exception.BaseAdaPayException; import com.huifu.adapay.model.PaymentReverse; 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.QueryConfirmReverseDTO; import com.jsowell.adapay.dto.QueryPaymentConfirmDTO; import com.jsowell.adapay.operation.PaymentReverseOperation; import com.jsowell.adapay.response.ConfirmReverseResponse; import com.jsowell.adapay.response.PaymentConfirmResponse; 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.enums.ykc.ScenarioEnum; import com.jsowell.common.util.StringUtils; import com.jsowell.pile.domain.AdapayMemberAccount; import com.jsowell.pile.service.OrderBasicInfoService; import org.apache.commons.collections4.CollectionUtils; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; import java.util.Map; /** * 专用处理汇付支付相关 */ @ActiveProfiles("dev") @SpringBootTest(classes = JsowellApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @RunWith(SpringRunner.class) public class PaymentTestController { protected final Logger logger = LoggerFactory.getLogger(this.getClass()); String wechatAppId1 = "wxbb3e0d474569481d"; // 万车充 String wechatAppId2 = "wx20abc5210391649c"; // 嘉佳充电 @Autowired private AdapayService adapayService; @Autowired private OrderBasicInfoService orderBasicInfoService; public List getPaymentIdList() { List resultList = Lists.newArrayList(); // List paymentIdList1 = getPaymentIdList1(); List paymentIdListForFile = getPaymentIdListForFile(); resultList.addAll(paymentIdListForFile); return resultList; } /** * 从文件中读取paymentId * @return * @throws IOException */ public List getPaymentIdListForFile() { List list =new ArrayList(); 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) { list.add(str); } } } catch (Exception e) { } // System.out.println(list); return list; } /** * 获取分账参数 */ public Map getPaymentConfirmParam() { Map paramMap = Maps.newHashMap(); // 待分账汇付会员id, 如需分给对应商户就填写正确的汇付会员id String adapayMemberId = "AM44542905"; // 待分账订单信息, 需要重新分账的订单信息(针对未分账的订单) String jsonArrayString = "[" + "{\"orderCode\":\"C27490025494\", \"settleAmount\":\"12.95\", \"paymentId\":\"002212025040121085510753131992504455168\"}" + "]"; JSONArray jsonArray = JSONArray.parseArray(jsonArrayString); paramMap.put("adapayMemberId", adapayMemberId); paramMap.put("paymentList", jsonArray); return paramMap; } /** * 批量执行分账 */ @Test public void batchCreatePaymentConfirm() { // 获取分账参数 Map confirmParam = getPaymentConfirmParam(); // 分账到指定汇付会员账户中 AdapayMemberAccount adapayMemberAccount = new AdapayMemberAccount(); adapayMemberAccount.setAdapayMemberId((String) confirmParam.get("adapayMemberId")); // 分账订单信息 JSONArray paymentList = (JSONArray) confirmParam.get("paymentList"); 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)); } } /** * 查询分账信息 * @throws BaseAdaPayException */ @Test public void queryCreateConfirmReverse() throws BaseAdaPayException { List paymentIdList = getPaymentIdList(); // 查询分账信息 List unSplitList = Lists.newArrayList(); // 未分帐 List splitList = Lists.newArrayList(); // 已分帐 BigDecimal total = BigDecimal.ZERO; // 总分账金额 BigDecimal totalWithdrawalAmt = BigDecimal.ZERO; // 实际到账金额汇总 BigDecimal totalFeeAmt = BigDecimal.ZERO; // 手续费金额汇总 List selfList = Lists.newArrayList(); Map map = Maps.newHashMap(); for (String paymentId : paymentIdList) { if (StringUtils.isBlank(paymentId)) { continue; } // 查询支付确认id QueryPaymentConfirmDTO dto = new QueryPaymentConfirmDTO(); dto.setPaymentId(paymentId); dto.setWechatAppId(wechatAppId1); // 查询分账信息 QueryPaymentConfirmDetailResponse response = adapayService.queryPaymentConfirmList(dto); if (response != null) { List confirms = response.getPaymentConfirms(); if (CollectionUtils.isEmpty(confirms)) { unSplitList.add(paymentId); } else { splitList.add(paymentId); for (PaymentConfirmInfo confirm : confirms) { if (queryConfirmReverseStatus(confirm)) { System.out.println("支付确认id:" + confirm.getId() + "撤销了。。。"); continue; } JSONObject jsonObject = JSON.parseObject(confirm.getDescription()); String adapayMemberId = jsonObject.getString("adapayMemberId"); if (StringUtils.isBlank(adapayMemberId)) { adapayMemberId = jsonObject.getString("adapayMemberIds"); } BigDecimal confirmAmt = new BigDecimal(confirm.getConfirmAmt()); // 本次确认金额 BigDecimal confirmedAmt = new BigDecimal(confirm.getConfirmedAmt()); // 已确认金额 BigDecimal feeAmt = new BigDecimal(confirm.getFeeAmt()); // 手续费 // 汇总已确认金额 total = total.add(confirmedAmt); // 汇总手续费金额 totalFeeAmt = totalFeeAmt.add(feeAmt); // 汇总可提现金额 totalWithdrawalAmt = totalWithdrawalAmt.add(confirmAmt).subtract(feeAmt); // confirm List divMembers = confirm.getDivMembers(); System.out.println("confirm:" + JSON.toJSONString(divMembers)); for (DivMember divMember : divMembers) { // 放map map.merge(divMember.getMemberId(), new BigDecimal(divMember.getAmount()), BigDecimal::add); } if (StringUtils.equals(adapayMemberId, "0")) { selfList.add(paymentId); } } } } else { unSplitList.add(paymentId); } } System.out.println("=================未分账:" + JSON.toJSONString(unSplitList) + ", 数量:" + unSplitList.size()); System.out.println("=================已分账:" + JSON.toJSONString(map) + ", 总分账:" + total + ", 数量:" + splitList.size()); System.out.println("===============金额明细:" + "总到账金额:" + totalWithdrawalAmt + ", 总手续费:" + totalFeeAmt); System.out.println("===================自己:" + JSON.toJSONString(selfList) + ", 数量:" + selfList.size()); } /** * 查询支付撤销状态 * @param confirm * @return * @throws BaseAdaPayException */ private boolean queryConfirmReverseStatus(PaymentConfirmInfo confirm) throws BaseAdaPayException { boolean result = false; QueryConfirmReverseDTO dto = QueryConfirmReverseDTO.builder() .paymentConfirmId(confirm.getId()) .wechatAppId(wechatAppId1) .build(); ConfirmReverseResponse confirmReverseResponse = adapayService.queryConfirmReverse(dto); if (confirmReverseResponse.isSuccess()) { result = true; } return result; } /** * 批量支付确认撤销 * @throws BaseAdaPayException */ @Test public void testCreateConfirmReverse() throws BaseAdaPayException { List list = getPaymentIdList(); // 批量支付确认撤销 for (String paymentId : list) { // 查询支付确认id QueryPaymentConfirmDTO dto = new QueryPaymentConfirmDTO(); dto.setPaymentId(paymentId); dto.setWechatAppId(wechatAppId1); QueryPaymentConfirmDetailResponse response = adapayService.queryPaymentConfirmList(dto); if (response != null) { List confirms = response.getPaymentConfirms(); System.out.println("支付id:" + paymentId + ", 确认信息:" + JSON.toJSONString(confirms)); if (CollectionUtils.isNotEmpty(confirms)) { for (PaymentConfirmInfo confirm : confirms) { adapayService.createConfirmReverse(confirm.getId(), wechatAppId1); } } } } } /** * 查询退款信息 */ @Test public void queryRefundTest() { List list = getPaymentIdList(); // 查询退款信息 for (String paymentId : list) { Map refundParams = Maps.newHashMap(); refundParams.put("payment_id", paymentId); try { Map refund = Refund.query(refundParams, wechatAppId2); System.out.println("支付id:" + paymentId + ", 退款信息:" + JSON.toJSONString(refund)); System.out.println(); } catch (BaseAdaPayException e) { throw new RuntimeException(e); } } } /** * 查询支付撤销信息 */ @Test public void queryPaymentReverseTest() { List list = getPaymentIdList(); // 查询支付撤销信息 for (String paymentId : list) { try { Map reverse = Maps.newHashMap(); reverse.put("payment_id", paymentId); reverse.put("app_id", wechatAppId2); Map response = PaymentReverse.queryList(reverse, wechatAppId2); System.out.printf("支付id: %s, 支付撤销信息: %s%n", paymentId, JSON.toJSONString(response)); System.out.println(); } catch (BaseAdaPayException e) { throw new RuntimeException(e); } } } /** * 延迟分账未确认调撤销调撤销接口退款/部分退 */ @Test public void createPaymentReverseRequestTest() { String paymentId = "002212025040121085510753131992504455168"; BigDecimal refundAmount = new BigDecimal("13.33"); String memberId = "63111233"; String orderCode = "C27490025494"; // 延迟分账未确认调撤销调撤销接口退款 PaymentReverseOperation operation = new PaymentReverseOperation(); operation.setPaymentId(paymentId); operation.setReverseAmt(refundAmount); operation.setMerchantKey(wechatAppId1); operation.setMemberId(memberId); operation.setScenarioType(ScenarioEnum.ORDER.getValue()); operation.setOrderCode(orderCode); PaymentReverseResponse response = adapayService.createPaymentReverseRequest(operation); System.out.println(JSON.toJSONString(response)); } @Test public void createBalancePaymentRequestTest() { String outMemberId = "ACM42875164"; // 出账memberId String inMemberId = "0"; // 入账memberId String transAmt = "798.20"; // 金额 String title = "提取余额到自己账户"; // 标题 String desc = "2024年7月31日08点55分,售后需求:客户重新添加结算账户, 原账户余额无法提取, 由现下打款给客户"; // 描述 String wechatAppId = wechatAppId1; // 万车充id adapayService.createBalancePaymentRequest(outMemberId, inMemberId, transAmt, title, desc, wechatAppId); } /** * */ @Test public void createBalancePaymentRequestTest2() { String outMemberId = "0"; // 出账memberId String inMemberId = "ACM25158725"; // 入账memberId String transAmt = "42.7"; // 金额 String title = "订单金额补分账"; // 标题 String desc = "补C69401257710,C86364369573结算金额"; // 描述 String wechatAppId = wechatAppId1; // 万车充id adapayService.createBalancePaymentRequest(outMemberId, inMemberId, transAmt, title, desc, wechatAppId); } /** * 校验订单分账金额 */ @Test public void verifyOrderConfirmAmountTest() throws BaseAdaPayException { String orderCode = "C21960272918"; List paymentIds = Lists.newArrayList("002212024121307453510713429549121368064"); BigDecimal settleAmount = new BigDecimal("19.37"); String wechatAppId = wechatAppId1; OrderSplitResult orderSplitResult = orderBasicInfoService.verifyOrderConfirmAmount(paymentIds, orderCode, settleAmount, wechatAppId); System.out.println(JSON.toJSONString(orderSplitResult)); } @Test public void queryPaymentConfirmDetailTest() throws BaseAdaPayException { // 查询支付确认id QueryPaymentConfirmDTO dto = new QueryPaymentConfirmDTO(); dto.setPaymentId("002212024121307453510713429549121368064"); dto.setWechatAppId(wechatAppId1); // 查询分账信息 QueryPaymentConfirmDetailResponse response = adapayService.queryPaymentConfirmList(dto); System.out.println(JSON.toJSONString(response)); } }