liujie
2026-08-21 a7c8a8f6055e1a03414f2227d12ff2e87ec52d41
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
package com.tourguide.modules.order.service;
 
import com.alibaba.fastjson.JSONObject;
import com.tourguide.common.exception.BusinessException;
import com.tourguide.common.huifu.biz.DouGongPayUtil;
import com.tourguide.modules.order.dto.MiniPaymentResponse;
import com.tourguide.modules.order.payment.OrderPaymentPreparation;
import com.tourguide.modules.user.entity.MiniUser;
import com.tourguide.modules.user.service.MiniUserService;
import org.junit.jupiter.api.Test;
 
import java.math.BigDecimal;
 
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.when;
 
class HuifuMiniPaymentServiceTest {
    @Test
    void shouldConvertHuifuPayInfoToMiniProgramParameters() {
        MiniUserService miniUserService = mock(MiniUserService.class);
        TourOrderPaymentService orderPaymentService = mock(TourOrderPaymentService.class);
        DouGongPayUtil payUtil = mock(DouGongPayUtil.class);
        HuifuMiniPaymentService service = new HuifuMiniPaymentService(
                miniUserService, orderPaymentService, payUtil);
        MiniUser user = new MiniUser();
        user.setOpenid("openid-1");
        when(miniUserService.findEnabledById(7L)).thenReturn(user);
        OrderPaymentPreparation preparation = new OrderPaymentPreparation(99L, "DG001", "20260803",
                new BigDecimal("36.00"), "青城山景区-拼团快讲");
        when(orderPaymentService.prepare(7L, 99L)).thenReturn(preparation);
 
        JSONObject payInfo = new JSONObject();
        payInfo.put("timeStamp", "1785744000");
        payInfo.put("nonceStr", "nonce-1");
        payInfo.put("package", "prepay_id=wx123");
        payInfo.put("signType", "RSA");
        payInfo.put("paySign", "pay-sign-1");
        JSONObject data = new JSONObject();
        data.put("pay_info", payInfo.toJSONString());
        JSONObject response = new JSONObject();
        response.put("resp_code", "00000100");
        response.put("data", data);
        when(payUtil.wxPayOrder(preparation.getAmount(), "openid-1", preparation.getGoodsDescription(),
                preparation.getReqSeqId(), preparation.getReqDate())).thenReturn(response);
 
        MiniPaymentResponse result = service.create(7L, 99L);
 
        assertThat(result.getProvider()).isEqualTo("wxpay");
        assertThat(result.getTimeStamp()).isEqualTo("1785744000");
        assertThat(result.getNonceStr()).isEqualTo("nonce-1");
        assertThat(result.getPackageValue()).isEqualTo("prepay_id=wx123");
        assertThat(result.getPaySign()).isEqualTo("pay-sign-1");
    }
 
 
    @Test
    void shouldExposeMissingHuifuConfiguration() {
        MiniUserService miniUserService = mock(MiniUserService.class);
        TourOrderPaymentService orderPaymentService = mock(TourOrderPaymentService.class);
        DouGongPayUtil payUtil = mock(DouGongPayUtil.class);
        HuifuMiniPaymentService service = new HuifuMiniPaymentService(
                miniUserService, orderPaymentService, payUtil);
        MiniUser user = new MiniUser();
        user.setOpenid("openid-1");
        when(miniUserService.findEnabledById(7L)).thenReturn(user);
        OrderPaymentPreparation preparation = new OrderPaymentPreparation(99L, "DG001", "20260803",
                new BigDecimal("36.00"), "青城山景区-拼团快讲");
        when(orderPaymentService.prepare(7L, 99L)).thenReturn(preparation);
        when(payUtil.wxPayOrder(preparation.getAmount(), "openid-1", preparation.getGoodsDescription(),
                preparation.getReqSeqId(), preparation.getReqDate()))
                .thenThrow(new IllegalStateException("汇付支付配置缺少 HUIFU_CALLBACK_PATH"));
 
        assertThatThrownBy(() -> service.create(7L, 99L))
                .isInstanceOf(BusinessException.class)
                .hasMessage("汇付支付配置缺少 HUIFU_CALLBACK_PATH");
    }
}