FlashLinePayEvidence.java 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package com.ruoyi.app.flashdelivery.payment;
  2. import com.alibaba.fastjson2.JSONObject;
  3. import com.alibaba.fastjson2.JSON;
  4. import com.alibaba.fastjson2.JSONArray;
  5. import java.math.BigDecimal;
  6. import com.ruoyi.app.utils.linepay.LinePayResponse;
  7. import com.ruoyi.system.domain.flash.FlashDeliveryLinePayment;
  8. /** 只把匹配原始交易绑定的网关证据交给资金事实服务;无效 JSON/小数金额均不作到账。 */
  9. final class FlashLinePayEvidence {
  10. static JSONObject captured(LinePayResponse response,FlashDeliveryLinePayment payment,boolean confirmed){
  11. if(response==null || !response.isSuccess() || response.rawBody()==null)return null;
  12. try {
  13. Object info=JSON.parseObject(response.rawBody()).get("info");JSONObject tx;
  14. if(confirmed){if(!(info instanceof JSONObject))return null;tx=(JSONObject)info;}
  15. else {if(!(info instanceof JSONArray) || ((JSONArray)info).size()!=1)return null;tx=((JSONArray)info).getJSONObject(0);if(tx==null || !"PAYMENT".equals(tx.getString("transactionType")))return null;}
  16. String id=tx.getString("transactionId");
  17. if(id==null || id.isBlank() || payment.getTransactionId()!=null && !payment.getTransactionId().equals(id)
  18. || !payment.getLineOrderId().equals(tx.getString("orderId"))
  19. || tx.getString("currency")!=null && !payment.getCurrency().equals(tx.getString("currency")) || amount(tx)<=0)return null;
  20. return tx;
  21. }catch(RuntimeException malformed){return null;}
  22. }
  23. static int amount(JSONObject transaction){
  24. try {
  25. JSONArray list=transaction.getJSONArray("payInfo");if(list==null || list.isEmpty())return -1;long total=0;
  26. for(int i=0;i<list.size();i++){BigDecimal raw=list.getJSONObject(i).getBigDecimal("amount");if(raw==null)return -1;long value=raw.longValueExact();if(value<=0)return -1;total=Math.addExact(total,value);}
  27. return total<=Integer.MAX_VALUE?(int)total:-1;
  28. }catch(RuntimeException invalid){return -1;}
  29. }
  30. static String refundedId(JSONObject transaction,int expectedAmount){
  31. try {
  32. JSONArray list=transaction.getJSONArray("refundList");if(list==null || list.isEmpty())return null;long total=0;String tx=null;
  33. for(int i=0;i<list.size();i++){JSONObject item=list.getJSONObject(i);long value=item.getBigDecimal("refundAmount").longValueExact();tx=item.getString("refundTransactionId");if(value==0 || value==Long.MIN_VALUE || tx==null || tx.isBlank())return null;total=Math.addExact(total,Math.abs(value));}
  34. return total==expectedAmount?tx:null;
  35. }catch(RuntimeException invalid){return null;}
  36. }
  37. static boolean hasRefund(JSONObject transaction){
  38. Object raw=transaction.get("refundList");
  39. return raw!=null && (!(raw instanceof JSONArray) || !((JSONArray)raw).isEmpty());
  40. }
  41. }