AdapayServiceTest.java 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package com.izouma.nineth.service;
  2. import com.alibaba.excel.EasyExcel;
  3. import com.alibaba.fastjson.JSON;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.huifu.adapay.core.exception.BaseAdaPayException;
  6. import com.huifu.adapay.model.Refund;
  7. import com.izouma.nineth.ApplicationTests;
  8. import com.izouma.nineth.domain.AdaTrade;
  9. import com.izouma.nineth.repo.GiftOrderRepo;
  10. import com.izouma.nineth.repo.OrderRepo;
  11. import com.izouma.nineth.utils.SnowflakeIdWorker;
  12. import org.junit.jupiter.api.Test;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import java.util.HashMap;
  15. import java.util.List;
  16. import java.util.Map;
  17. import java.util.concurrent.ExecutionException;
  18. import java.util.concurrent.ForkJoinPool;
  19. import java.util.stream.Collectors;
  20. import org.springframework.boot.test.context.SpringBootTest;
  21. import org.junit.jupiter.api.extension.ExtendWith;
  22. import org.springframework.test.context.junit.jupiter.SpringExtension;
  23. @ExtendWith(SpringExtension.class)
  24. @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class AdapayServiceTest {
  25. @Autowired
  26. private OrderRepo orderRepo;
  27. @Autowired
  28. private GiftOrderRepo giftOrderRepo;
  29. @Autowired
  30. private SnowflakeIdWorker snowflakeIdWorker;
  31. private final String appId = "app_0e8d3acb-3d95-4ebb-8445-e470c378a787";
  32. @Test
  33. public void queryRefund() throws ExecutionException, InterruptedException {
  34. ForkJoinPool customThreadPool = new ForkJoinPool(100);
  35. customThreadPool.submit(() -> {
  36. List<AdaTrade> list = EasyExcel.read("/Users/drew/Downloads/merTransDetail_0284905900625472_20220212_20220212_1644980532.xlsx")
  37. .head(AdaTrade.class).sheet().doReadSync();
  38. list = list.parallelStream().filter(adaTrade -> {
  39. return adaTrade.get交易金额().equals("19.80")
  40. && orderRepo.findByTransactionId(adaTrade.get支付流水号()) == null
  41. && giftOrderRepo.findByTransactionId(adaTrade.get支付流水号()) == null;
  42. }).collect(Collectors.toList());
  43. EasyExcel.write("/Users/drew/Downloads/1.xlsx", AdaTrade.class).sheet("模板").doWrite(list);
  44. System.out.println(list.size());
  45. }).get();
  46. }
  47. @Test
  48. public void refund() {
  49. List<AdaTrade> list = EasyExcel.read("/Users/drew/Downloads/1.xlsx")
  50. .head(AdaTrade.class).sheet().doReadSync();
  51. list.parallelStream().forEach(adaTrade -> {
  52. Map<String, Object> refundParams = new HashMap<>();
  53. refundParams.put("refund_amt", adaTrade.get交易金额());
  54. refundParams.put("refund_order_no", snowflakeIdWorker.nextId() + "");
  55. try {
  56. Map<String, Object> response = Refund.create(adaTrade.get支付流水号(), refundParams);
  57. } catch (BaseAdaPayException e) {
  58. e.printStackTrace();
  59. }
  60. });
  61. }
  62. @Test
  63. public void verifyRefund() throws BaseAdaPayException {
  64. List<AdaTrade> list = EasyExcel.read("/Users/drew/Downloads/1.xlsx")
  65. .head(AdaTrade.class).sheet().doReadSync();
  66. list = list.parallelStream().filter(adaTrade -> {
  67. boolean success = false;
  68. try {
  69. Map<String, Object> refundParams = new HashMap<>(2);
  70. refundParams.put("payment_id", adaTrade.get支付流水号());
  71. Map<String, Object> refund = Refund.query(refundParams);
  72. JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(refund));
  73. if ("S".equals(jsonObject.getJSONArray("refunds")
  74. .getJSONObject(jsonObject.getJSONArray("refunds").size() - 1).getString("trans_status"))) {
  75. success = true;
  76. }
  77. } catch (Exception e) {
  78. }
  79. return !success;
  80. }).collect(Collectors.toList());
  81. EasyExcel.write("/Users/drew/Downloads/2.xlsx", AdaTrade.class).sheet("模板").doWrite(list);
  82. }
  83. }