Files
jsowell-charger-web/jsowell-admin/src/test/java/PaymentTestController.java

438 lines
16 KiB
Java
Raw Normal View History

2024-11-08 17:36:24 +08:00
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
2024-11-08 18:35:55 +08:00
import com.google.common.collect.Sets;
2024-11-08 17:36:24 +08:00
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.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.PaymentReverseResponse;
import com.jsowell.adapay.response.QueryPaymentConfirmDetailResponse;
import com.jsowell.adapay.service.AdapayService;
import com.jsowell.common.enums.ykc.ScenarioEnum;
import com.jsowell.common.util.StringUtils;
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;
2024-11-08 17:36:51 +08:00
/**
* 专用处理汇付支付相关
*/
2024-11-08 17:36:24 +08:00
@ActiveProfiles("pre")
@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;
public List<String> getPaymentIdList() {
List<String> resultList = Lists.newArrayList();
// List<String> paymentIdList1 = getPaymentIdList1();
List<String> paymentIdListForFile = getPaymentIdListForFile();
resultList.addAll(paymentIdListForFile);
return resultList;
}
/**
* 从文件中读取paymentId
* @return
* @throws IOException
*/
public List<String> getPaymentIdListForFile() {
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) {
list.add(str);
}
}
} catch (Exception e) {
}
// System.out.println(list);
return list;
}
2024-11-08 18:35:55 +08:00
public static void main(String[] args) {
List<String> list1 = Lists.newArrayList(
"C84866578627",
"C69407733440",
"C46698893183",
"C23481086913",
"C46631353044",
"C82315862441",
"C48515444163",
"C88894523461",
"C63539142496",
"C88227376749",
"C69808468000",
"C29756635462",
"C48171513017",
"C82929694510",
"C42286214889",
"C40531240168",
"C42818195903",
"C44772182853",
"C63995701102",
"C84653836282",
"C82929004110",
"C42248888826",
"C42871512014",
"C61630741271",
"C44120948533",
"C86510728306",
"C23698728789",
"C40725164803",
"C46426468297",
"C80876734677",
"C65208404269",
"C48131292456",
"C27264263390",
"C42620874381",
"C27264478506",
"C82758563554",
"C44588350706",
"C86153980315",
"C86782182478",
"C42077527679",
"C80606430852",
"C21377880382",
"C63590496985",
"C25745398205",
"C63136978056",
"C21983806615",
"C42016184953",
"C27458903931",
"C40590975531",
"C44987731257",
"C63554143992",
"C46468609277",
"C23483139052",
"C67759764676",
"C65286550984",
"C44772583755",
"C65037735356",
"C23065540340",
"C82330428460",
"C40136408323",
"C65896968515",
"C80093965053",
"C23029064125",
"C46291260090",
"C40915419435",
"C69234709266",
"C61064626771",
"C84216893653",
"C69869670458",
"C44755242724",
"C63706872923",
"C61047783388",
"C80663278468",
"C46232390797",
"C44561702210",
"C61404525849",
"C80813608253"
);
List<String> list2 = Lists.newArrayList(
"C69407733440",
"C84866578627",
"C46698893183",
"C46631353044",
"C23481086913",
"C88894523461",
"C48515444163",
"C82315862441",
"C48171513017",
"C29756635462",
"C69808468000",
"C88227376749",
"C63539142496",
"C42818195903",
"C40531240168",
"C42286214889",
"C82929694510",
"C63995701102",
"C44772182853",
"C84653836282",
"C42871512014",
"C42248888826",
"C82929004110",
"C44120948533",
"C61630741271",
"C23698728789",
"C86510728306",
"C80876734677",
"C46426468297",
"C40725164803",
"C48131292456",
"C65208404269",
"C82758563554",
"C27264263390",
"C27264478506",
"C42620874381",
"C25745398205",
"C86153980315",
"C63590496985",
"C21377880382",
"C80606430852",
"C44588350706",
"C42077527679",
"C86782182478",
"C40590975531",
"C27458903931",
"C42016184953",
"C21983806615",
"C63136978056",
"C44772583755",
"C46468609277",
"C65286550984",
"C63554143992",
"C67759764676",
"C44987731257",
"C23483139052",
"C23065540340",
"C65037735356",
"C80093965053",
"C65896968515",
"C82330428460",
"C40136408323",
"C23029064125",
"C63706872923",
"C40915419435",
"C44755242724",
"C69869670458",
"C84216893653",
"C61064626771",
"C69234709266",
"C46291260090",
"C80813608253",
"C61404525849",
"C61047783388",
"C44561702210",
"C46232390797",
"C80663278468");
Sets.SetView<String> difference = Sets.intersection(Sets.newHashSet(list1), Sets.newHashSet(list2));
System.out.println(difference);
}
2024-11-08 17:36:24 +08:00
/**
* 查询分账信息
* @throws BaseAdaPayException
*/
@Test
public void queryCreateConfirmReverse() throws BaseAdaPayException {
List<String> paymentIdList = getPaymentIdList(); // 查询分账信息
List<String> unSplitList = Lists.newArrayList(); // 未分帐
List<String> splitList = Lists.newArrayList(); // 已分帐
BigDecimal total = BigDecimal.ZERO;
List<String> selfList = Lists.newArrayList();
Map<String, BigDecimal> 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<QueryPaymentConfirmDetailResponse.PaymentConfirmInfo> confirms = response.getPaymentConfirms();
if (CollectionUtils.isEmpty(confirms)) {
unSplitList.add(paymentId);
} else {
splitList.add(paymentId);
for (QueryPaymentConfirmDetailResponse.PaymentConfirmInfo confirm : confirms) {
if (queryConfirmReverseStatus(confirm)) {
System.out.println("支付确认id:" + confirm.getId() + "撤销了。。。");
continue;
}
JSONObject jsonObject = JSON.parseObject(confirm.getDescription());
String adapayMemberId = jsonObject.getString("adapayMemberId");
BigDecimal confirmedAmt = new BigDecimal(confirm.getConfirmedAmt());
total = total.add(confirmedAmt);
// 放map
map.merge(adapayMemberId, confirmedAmt, 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("=================自己:" + JSON.toJSONString(selfList) + ", 数量:" + selfList.size());
}
/**
* 查询支付撤销状态
* @param confirm
* @return
* @throws BaseAdaPayException
*/
private boolean queryConfirmReverseStatus(QueryPaymentConfirmDetailResponse.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<String> 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<QueryPaymentConfirmDetailResponse.PaymentConfirmInfo> confirms = response.getPaymentConfirms();
System.out.println("支付id:" + paymentId + ", 确认信息:" + JSON.toJSONString(confirms));
if (CollectionUtils.isNotEmpty(confirms)) {
for (QueryPaymentConfirmDetailResponse.PaymentConfirmInfo confirm : confirms) {
adapayService.createConfirmReverse(confirm.getId(), wechatAppId1);
}
}
}
}
}
/**
* 查询退款信息
*/
@Test
public void queryRefundTest() {
List<String> list = getPaymentIdList(); // 查询退款信息
for (String paymentId : list) {
Map<String, Object> refundParams = Maps.newHashMap();
refundParams.put("payment_id", paymentId);
try {
Map<String, Object> 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<String> list = getPaymentIdList(); // 查询支付撤销信息
for (String paymentId : list) {
try {
Map<String, Object> reverse = Maps.newHashMap();
reverse.put("payment_id", paymentId);
reverse.put("app_id", wechatAppId2);
Map<String, Object> 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 = "002212024102215301510694702317666226176";
BigDecimal refundAmount = new BigDecimal("15.42");
String memberId = "42833137";
String orderCode = "C21960272918";
// 延迟分账未确认调撤销调撤销接口退款
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);
}
}