| 1234567891011121314151617181920212223242526272829303132333435363738394041 |
- package com.ruoyi.app.flashdelivery.payment;
- import com.alibaba.fastjson2.JSONObject;
- import com.alibaba.fastjson2.JSON;
- import com.alibaba.fastjson2.JSONArray;
- import java.math.BigDecimal;
- import com.ruoyi.app.utils.linepay.LinePayResponse;
- import com.ruoyi.system.domain.flash.FlashDeliveryLinePayment;
- /** 只把匹配原始交易绑定的网关证据交给资金事实服务;无效 JSON/小数金额均不作到账。 */
- final class FlashLinePayEvidence {
- static JSONObject captured(LinePayResponse response,FlashDeliveryLinePayment payment,boolean confirmed){
- if(response==null || !response.isSuccess() || response.rawBody()==null)return null;
- try {
- Object info=JSON.parseObject(response.rawBody()).get("info");JSONObject tx;
- if(confirmed){if(!(info instanceof JSONObject))return null;tx=(JSONObject)info;}
- 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;}
- String id=tx.getString("transactionId");
- if(id==null || id.isBlank() || payment.getTransactionId()!=null && !payment.getTransactionId().equals(id)
- || !payment.getLineOrderId().equals(tx.getString("orderId"))
- || tx.getString("currency")!=null && !payment.getCurrency().equals(tx.getString("currency")) || amount(tx)<=0)return null;
- return tx;
- }catch(RuntimeException malformed){return null;}
- }
- static int amount(JSONObject transaction){
- try {
- JSONArray list=transaction.getJSONArray("payInfo");if(list==null || list.isEmpty())return -1;long total=0;
- 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);}
- return total<=Integer.MAX_VALUE?(int)total:-1;
- }catch(RuntimeException invalid){return -1;}
- }
- static String refundedId(JSONObject transaction,int expectedAmount){
- try {
- JSONArray list=transaction.getJSONArray("refundList");if(list==null || list.isEmpty())return null;long total=0;String tx=null;
- 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));}
- return total==expectedAmount?tx:null;
- }catch(RuntimeException invalid){return null;}
- }
- static boolean hasRefund(JSONObject transaction){
- Object raw=transaction.get("refundList");
- return raw!=null && (!(raw instanceof JSONArray) || !((JSONArray)raw).isEmpty());
- }
- }
|