|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+package com.ruoyi.web.modules.order.service;
|
|
|
2
|
+
|
|
|
3
|
+import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
4
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
5
|
+import static org.mockito.ArgumentMatchers.eq;
|
|
|
6
|
+import static org.mockito.Mockito.never;
|
|
|
7
|
+import static org.mockito.Mockito.verify;
|
|
|
8
|
+import static org.mockito.Mockito.when;
|
|
|
9
|
+import java.math.BigDecimal;
|
|
|
10
|
+import java.util.Calendar;
|
|
|
11
|
+import java.util.Collections;
|
|
|
12
|
+import java.util.Date;
|
|
|
13
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
14
|
+import org.junit.jupiter.api.Test;
|
|
|
15
|
+import org.junit.jupiter.api.extension.ExtendWith;
|
|
|
16
|
+import org.mockito.InjectMocks;
|
|
|
17
|
+import org.mockito.Mock;
|
|
|
18
|
+import org.mockito.junit.jupiter.MockitoExtension;
|
|
|
19
|
+import org.springframework.test.util.ReflectionTestUtils;
|
|
|
20
|
+import com.ruoyi.web.modules.account.facade.IMemberFacade;
|
|
|
21
|
+import com.ruoyi.web.modules.order.constant.OrderConstants;
|
|
|
22
|
+import com.ruoyi.web.modules.order.domain.BizOrder;
|
|
|
23
|
+import com.ruoyi.web.modules.order.domain.BizOrderItem;
|
|
|
24
|
+import com.ruoyi.web.modules.order.mapper.BizOrderItemMapper;
|
|
|
25
|
+import com.ruoyi.web.modules.order.mapper.BizOrderMapper;
|
|
|
26
|
+import com.ruoyi.web.modules.order.service.impl.OrderAppServiceImpl;
|
|
|
27
|
+import com.ruoyi.web.modules.order.service.impl.OrderPaySuccessServiceImpl;
|
|
|
28
|
+import com.ruoyi.web.modules.order.support.CheckoutSupport;
|
|
|
29
|
+import com.ruoyi.web.modules.pay.config.WeChatPayProperties;
|
|
|
30
|
+import com.ruoyi.web.modules.pay.domain.BizPayRecord;
|
|
|
31
|
+import com.ruoyi.web.modules.pay.mapper.BizPayRecordMapper;
|
|
|
32
|
+import com.ruoyi.web.modules.pay.service.impl.WeChatPayServiceImpl;
|
|
|
33
|
+import com.ruoyi.web.modules.pay.vo.WeChatPayParamsVO;
|
|
|
34
|
+
|
|
|
35
|
+/**
|
|
|
36
|
+ * 立即购买(BUY_NOW)· 支付 Mock 闭环
|
|
|
37
|
+ */
|
|
|
38
|
+@ExtendWith(MockitoExtension.class)
|
|
|
39
|
+class BuyNowPayFlowTest
|
|
|
40
|
+{
|
|
|
41
|
+ private static final Long MEMBER_ID = 200L;
|
|
|
42
|
+
|
|
|
43
|
+ private static final Long ORDER_ID = 900L;
|
|
|
44
|
+
|
|
|
45
|
+ @Mock
|
|
|
46
|
+ private IMemberFacade memberFacade;
|
|
|
47
|
+
|
|
|
48
|
+ @Mock
|
|
|
49
|
+ private BizOrderMapper orderMapper;
|
|
|
50
|
+
|
|
|
51
|
+ @Mock
|
|
|
52
|
+ private BizOrderItemMapper orderItemMapper;
|
|
|
53
|
+
|
|
|
54
|
+ @Mock
|
|
|
55
|
+ private com.ruoyi.web.modules.goods.mapper.BizGoodsMapper goodsMapper;
|
|
|
56
|
+
|
|
|
57
|
+ @Mock
|
|
|
58
|
+ private com.ruoyi.web.modules.cart.facade.ICartFacade cartFacade;
|
|
|
59
|
+
|
|
|
60
|
+ @Mock
|
|
|
61
|
+ private CheckoutSupport checkoutSupport;
|
|
|
62
|
+
|
|
|
63
|
+ @Mock
|
|
|
64
|
+ private BizPayRecordMapper payRecordMapper;
|
|
|
65
|
+
|
|
|
66
|
+ @Mock
|
|
|
67
|
+ private WeChatPayProperties payProperties;
|
|
|
68
|
+
|
|
|
69
|
+ @InjectMocks
|
|
|
70
|
+ private WeChatPayServiceImpl weChatPayService;
|
|
|
71
|
+
|
|
|
72
|
+ @InjectMocks
|
|
|
73
|
+ private OrderPaySuccessServiceImpl orderPaySuccessService;
|
|
|
74
|
+
|
|
|
75
|
+ @InjectMocks
|
|
|
76
|
+ private OrderAppServiceImpl orderAppService;
|
|
|
77
|
+
|
|
|
78
|
+ private BizOrder order;
|
|
|
79
|
+
|
|
|
80
|
+ @BeforeEach
|
|
|
81
|
+ void setUp()
|
|
|
82
|
+ {
|
|
|
83
|
+ order = pendingOrder();
|
|
|
84
|
+ ReflectionTestUtils.setField(weChatPayService, "checkoutSupport", checkoutSupport);
|
|
|
85
|
+ ReflectionTestUtils.setField(orderPaySuccessService, "checkoutSupport", checkoutSupport);
|
|
|
86
|
+ ReflectionTestUtils.setField(orderAppService, "weChatPayService", weChatPayService);
|
|
|
87
|
+ ReflectionTestUtils.setField(orderAppService, "orderPaySuccessService", orderPaySuccessService);
|
|
|
88
|
+ }
|
|
|
89
|
+
|
|
|
90
|
+ @Test
|
|
|
91
|
+ void buyNow_mockPay_completesWithoutOpenidAndSkipsCart()
|
|
|
92
|
+ {
|
|
|
93
|
+ when(memberFacade.isMemberEnabled(MEMBER_ID)).thenReturn(true);
|
|
|
94
|
+ when(memberFacade.getWxOpenid(MEMBER_ID)).thenReturn(null);
|
|
|
95
|
+ when(orderMapper.selectByIdAndMember(ORDER_ID, MEMBER_ID)).thenReturn(order);
|
|
|
96
|
+ when(payProperties.isRealPayEnabled()).thenReturn(false);
|
|
|
97
|
+ when(payRecordMapper.selectLatestByOrderId(ORDER_ID)).thenReturn(null);
|
|
|
98
|
+ BizPayRecord record = new BizPayRecord();
|
|
|
99
|
+ record.setPayId(1L);
|
|
|
100
|
+ record.setOrderId(ORDER_ID);
|
|
|
101
|
+ record.setPayStatus("0");
|
|
|
102
|
+ when(payRecordMapper.selectByOutTradeNo(order.getOrderNo())).thenReturn(record);
|
|
|
103
|
+ when(payRecordMapper.selectByWxTransactionId(any())).thenReturn(null);
|
|
|
104
|
+ when(orderMapper.selectById(ORDER_ID)).thenReturn(order);
|
|
|
105
|
+ BizOrderItem item = new BizOrderItem();
|
|
|
106
|
+ item.setGoodsId(1L);
|
|
|
107
|
+ item.setQuantity(2);
|
|
|
108
|
+ item.setCartItemId(null);
|
|
|
109
|
+ when(orderItemMapper.selectByOrderId(ORDER_ID)).thenReturn(Collections.singletonList(item));
|
|
|
110
|
+ when(goodsMapper.deductStock(1L, 2)).thenReturn(1);
|
|
|
111
|
+ when(orderMapper.updateForPay(any(BizOrder.class))).thenReturn(1);
|
|
|
112
|
+
|
|
|
113
|
+ WeChatPayParamsVO result = orderAppService.createPay(MEMBER_ID, ORDER_ID);
|
|
|
114
|
+
|
|
|
115
|
+ assertTrue(result.isMock());
|
|
|
116
|
+ verify(payRecordMapper).insert(any(BizPayRecord.class));
|
|
|
117
|
+ verify(orderMapper).updateForPay(any(BizOrder.class));
|
|
|
118
|
+ verify(cartFacade, never()).removeItems(any(), any());
|
|
|
119
|
+ }
|
|
|
120
|
+
|
|
|
121
|
+ private BizOrder pendingOrder()
|
|
|
122
|
+ {
|
|
|
123
|
+ BizOrder row = new BizOrder();
|
|
|
124
|
+ row.setOrderId(ORDER_ID);
|
|
|
125
|
+ row.setMemberId(MEMBER_ID);
|
|
|
126
|
+ row.setShopId(101L);
|
|
|
127
|
+ row.setOrderStatus(OrderConstants.STATUS_PENDING_PAY);
|
|
|
128
|
+ row.setPayAmount(new BigDecimal("25.00"));
|
|
|
129
|
+ row.setOrderNo("O20260526000001");
|
|
|
130
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
131
|
+ calendar.add(Calendar.HOUR, 1);
|
|
|
132
|
+ row.setPayExpireTime(calendar.getTime());
|
|
|
133
|
+ return row;
|
|
|
134
|
+ }
|
|
|
135
|
+}
|