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;
|
}
|
}
|