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
85
86
87
88
89
package com.tourguide.modules.order.payment;
 
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.junit.jupiter.api.Test;
 
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
 
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
 
class HuifuPaymentRefundGatewayTest {
    @Test
    void shouldSubmitRefundUsingOriginalPaymentAttempt() throws Exception {
        DouGongRefundBiz refundBiz = mock(DouGongRefundBiz.class);
        TourOrderPaymentAttemptMapper attemptMapper = mock(TourOrderPaymentAttemptMapper.class);
        HuifuPaymentRefundGateway gateway = new HuifuPaymentRefundGateway(refundBiz, attemptMapper);
        TourOrder order = order();
        TourOrderPaymentAttempt attempt = attempt();
        when(attemptMapper.selectByReqSeqIdForUpdate(order.getPaymentOrderNo())).thenReturn(attempt);
        Map<String, Object> response = new HashMap<>();
        response.put("resp_code", "00000100");
        when(refundBiz.refund(order.getRefundOrderNo(), new BigDecimal("36.00"),
                "20260803", order.getPaymentOrderNo())).thenReturn(response);
 
        String refundOrderNo = gateway.refund(order, new BigDecimal("36.00"));
 
        assertThat(refundOrderNo).isEqualTo(order.getRefundOrderNo());
        verify(refundBiz).refund(order.getRefundOrderNo(), new BigDecimal("36.00"),
                "20260803", order.getPaymentOrderNo());
    }
 
    @Test
    void shouldRejectFailedHuifuResponse() throws Exception {
        DouGongRefundBiz refundBiz = mock(DouGongRefundBiz.class);
        TourOrderPaymentAttemptMapper attemptMapper = mock(TourOrderPaymentAttemptMapper.class);
        HuifuPaymentRefundGateway gateway = new HuifuPaymentRefundGateway(refundBiz, attemptMapper);
        TourOrder order = order();
        when(attemptMapper.selectByReqSeqIdForUpdate(order.getPaymentOrderNo())).thenReturn(attempt());
        Map<String, Object> response = new HashMap<>();
        response.put("resp_code", "10000100");
        response.put("resp_desc", "refund failed");
        when(refundBiz.refund(order.getRefundOrderNo(), new BigDecimal("36.00"),
                "20260803", order.getPaymentOrderNo())).thenReturn(response);
 
        assertThatThrownBy(() -> gateway.refund(order, new BigDecimal("36.00")))
                .isInstanceOf(IllegalStateException.class)
                .hasMessage("refund failed");
    }
 
    @Test
    void shouldReadNestedHuifuResponseCode() throws Exception {
        DouGongRefundBiz refundBiz = mock(DouGongRefundBiz.class);
        TourOrderPaymentAttemptMapper attemptMapper = mock(TourOrderPaymentAttemptMapper.class);
        HuifuPaymentRefundGateway gateway = new HuifuPaymentRefundGateway(refundBiz, attemptMapper);
        TourOrder order = order();
        when(attemptMapper.selectByReqSeqIdForUpdate(order.getPaymentOrderNo())).thenReturn(attempt());
        Map<String, Object> nested = new HashMap<>();
        nested.put("resp_code", "00000100");
        Map<String, Object> response = new HashMap<>();
        response.put("data", nested);
        when(refundBiz.refund(order.getRefundOrderNo(), new BigDecimal("36.00"),
                "20260803", order.getPaymentOrderNo())).thenReturn(response);
 
        assertThat(gateway.refund(order, new BigDecimal("36.00")))
                .isEqualTo(order.getRefundOrderNo());
    }
 
    private TourOrder order() {
        TourOrder order = new TourOrder();
        order.setPaymentOrderNo("PY202608031700000000000000000001");
        order.setRefundOrderNo("RF202608031710000000000000000001");
        return order;
    }
 
    private TourOrderPaymentAttempt attempt() {
        TourOrderPaymentAttempt attempt = new TourOrderPaymentAttempt();
        attempt.setReqSeqId("PY202608031700000000000000000001");
        attempt.setReqDate("20260803");
        return attempt;
    }
}