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