liujie
2026-09-14 82a8bc62d5b9d5d607f6952329952b17eda2f0a0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.tourguide.modules.order.payment;
 
import com.alibaba.fastjson.JSONObject;
import com.tourguide.common.huifu.biz.DouGongRefundBiz;
import com.tourguide.modules.order.entity.TourOrder;
import com.tourguide.modules.order.entity.TourOrderPaymentAttempt;
import com.tourguide.modules.order.mapper.TourOrderPaymentAttemptMapper;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
 
import java.math.BigDecimal;
import java.util.Map;
 
@Component
public class HuifuPaymentRefundGateway implements PaymentRefundGateway {
    private static final String SUCCESS_CODE = "00000100";
 
    private final DouGongRefundBiz refundBiz;
    private final TourOrderPaymentAttemptMapper paymentAttemptMapper;
 
    public HuifuPaymentRefundGateway(DouGongRefundBiz refundBiz,
                                     TourOrderPaymentAttemptMapper paymentAttemptMapper) {
        this.refundBiz = refundBiz;
        this.paymentAttemptMapper = paymentAttemptMapper;
    }
 
    @Override
    public String refund(TourOrder order, BigDecimal amount) {
        if (!StringUtils.hasText(order.getRefundOrderNo())) {
            throw new IllegalStateException("Refund request sequence is missing");
        }
        if (!StringUtils.hasText(order.getPaymentOrderNo())) {
            throw new IllegalStateException("Original payment sequence is missing");
        }
        TourOrderPaymentAttempt paymentAttempt = paymentAttemptMapper
                .selectByReqSeqIdForUpdate(order.getPaymentOrderNo());
        if (paymentAttempt == null || !StringUtils.hasText(paymentAttempt.getReqDate())) {
            throw new IllegalStateException("Original payment attempt is missing");
        }
        try {
            Map<String, Object> response = refundBiz.refund(order.getRefundOrderNo(), amount,
                    paymentAttempt.getReqDate(), paymentAttempt.getReqSeqId());
            String responseCode = responseValue(response, "resp_code", "code");
            if (!SUCCESS_CODE.equals(responseCode)) {
                String description = responseValue(response, "resp_desc", "msg");
                throw new IllegalStateException(StringUtils.hasText(description)
                        ? description : "Huifu refund request failed");
            }
            return order.getRefundOrderNo();
        } catch (RuntimeException exception) {
            throw exception;
        } catch (Exception exception) {
            throw new IllegalStateException("Huifu refund service unavailable", exception);
        }
    }
 
    private String stringValue(Object value) {
        return value == null ? null : String.valueOf(value);
    }
 
    private String responseValue(Map<String, Object> response, String... keys) {
        if (response == null) return null;
        for (String key : keys) {
            String value = stringValue(response.get(key));
            if (StringUtils.hasText(value)) return value;
        }
        for (String nestedKey : new String[]{"data", "resp_data"}) {
            Object nested = response.get(nestedKey);
            if (nested instanceof Map) {
                @SuppressWarnings("unchecked")
                Map<String, Object> nestedMap = (Map<String, Object>) nested;
                String value = responseValue(nestedMap, keys);
                if (StringUtils.hasText(value)) return value;
            } else if (nested instanceof String && StringUtils.hasText((String) nested)) {
                try {
                    String value = responseValue(JSONObject.parseObject((String) nested), keys);
                    if (StringUtils.hasText(value)) return value;
                } catch (RuntimeException ignored) {
                }
            }
        }
        return null;
    }
}