code review:钱包实现

This commit is contained in:
YunaiV
2023-08-28 18:54:40 +08:00
parent 9077f14bae
commit 6132443d26
21 changed files with 220 additions and 138 deletions

View File

@ -9,14 +9,17 @@ import cn.iocoder.yudao.framework.pay.core.client.impl.AbstractPayClient;
import java.util.Map;
// TODO @jason其它模块主要是无法 pay client 初始化存在问题,所以我感觉,是不是可以搞个 PayClientInitializer 接口。这样PayClientFactory 去 get 这个支付模式对应的 PayClientInitializer通过它来创建。具体注入的地方可以在 PayChannel init 方法那;
/**
* 代理支付 Client 的抽象类。用于支付 Client 由其它模块实现, 例如钱包支付
* 代理支付 Client 的抽象类。
*
* 用于支付 Client 由其它模块实现,例如钱包支付
*
* @author jason
*/
public abstract class DelegatePayClient<Config extends PayClientConfig> extends AbstractPayClient<PayClientConfig> {
public abstract class DelegatePayClient<Config extends PayClientConfig> extends AbstractPayClient<PayClientConfig> {
private final DelegatePayClient<Config> delegate;
private final DelegatePayClient<Config> delegate;
public DelegatePayClient(Long channelId, String channelCode, PayClientConfig config) {
super(channelId, channelCode, config);

View File

@ -168,7 +168,7 @@ public abstract class AbstractAlipayClientTest extends BaseMockitoUnitTest {
@Test
@DisplayName("支付宝 Client 统一退款:抛出业务异常")
public void testUnifiedRefund_throwServiceException() throws AlipayApiException {
// mock
// mock 方法
when(defaultAlipayClient.execute(argThat((ArgumentMatcher<AlipayTradeRefundRequest>) request -> true)))
.thenThrow(ServiceExceptionUtil.exception(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR));
// 准备请求参数
@ -182,7 +182,7 @@ public abstract class AbstractAlipayClientTest extends BaseMockitoUnitTest {
@Test
@DisplayName("支付宝 Client 统一退款:抛出系统异常")
public void testUnifiedRefund_throwPayException() throws AlipayApiException {
// mock
// mock 方法
when(defaultAlipayClient.execute(argThat((ArgumentMatcher<AlipayTradeRefundRequest>) request -> true)))
.thenThrow(new RuntimeException("系统异常"));
// 准备请求参数

View File

@ -70,7 +70,7 @@ public class AlipayPcPayClientTest extends AbstractAlipayClientTest {
@Test
@DisplayName("支付宝 PC 网站支付Form Display Mode 下单成功")
public void testUnifiedOrder_formSuccess() throws AlipayApiException {
// mock
// mock 方法
String notifyUrl = randomURL();
AlipayTradePagePayResponse response = randomPojo(AlipayTradePagePayResponse.class, o -> o.setSubCode(""));
when(defaultAlipayClient.pageExecute(argThat((ArgumentMatcher<AlipayTradePagePayRequest>) request -> true),
@ -99,7 +99,7 @@ public class AlipayPcPayClientTest extends AbstractAlipayClientTest {
@Test
@DisplayName("支付宝 PC 网站支付:渠道返回失败")
public void testUnifiedOrder_channelFailed() throws AlipayApiException {
// mock
// mock 方法
String subCode = randomString();
String subMsg = randomString();
AlipayTradePagePayResponse response = randomPojo(AlipayTradePagePayResponse.class, o -> {

View File

@ -39,9 +39,9 @@ public class AlipayQrPayClientTest extends AbstractAlipayClientTest {
}
@Test
@DisplayName("支付宝扫描支付下单成功")
public void test_unified_order_success() throws AlipayApiException {
// 准备返回对象
@DisplayName("支付宝扫描支付下单成功")
public void testUnifiedOrder_success() throws AlipayApiException {
// mock 方法
String notifyUrl = randomURL();
String qrCode = randomString();
Integer price = randomInteger();
@ -49,7 +49,6 @@ public class AlipayQrPayClientTest extends AbstractAlipayClientTest {
o.setQrCode(qrCode);
o.setSubCode("");
});
// mock
when(defaultAlipayClient.execute(argThat((ArgumentMatcher<AlipayTradePrecreateRequest>) request -> {
assertEquals(notifyUrl, request.getNotifyUrl());
return true;
@ -58,18 +57,25 @@ public class AlipayQrPayClientTest extends AbstractAlipayClientTest {
String outTradeNo = randomString();
PayOrderUnifiedReqDTO reqDTO = buildOrderUnifiedReqDTO(notifyUrl, outTradeNo, price);
// 调用
PayOrderRespDTO resp = client.unifiedOrder(reqDTO);
// 断言
assertEquals(WAITING.getStatus(), resp.getStatus());
assertEquals(PayOrderDisplayModeEnum.QR_CODE.getMode(), resp.getDisplayMode());
assertEquals(outTradeNo, resp.getOutTradeNo());
assertEquals(qrCode, resp.getDisplayContent());
assertNull(resp.getChannelOrderNo());
assertNull(resp.getChannelUserId());
assertNull(resp.getSuccessTime());
assertEquals(PayOrderDisplayModeEnum.QR_CODE.getMode(), resp.getDisplayMode());
assertEquals(response.getQrCode(), resp.getDisplayContent());
assertSame(response, resp.getRawData());
assertNull(resp.getChannelErrorCode());
assertNull(resp.getChannelErrorMsg());
}
@Test
@DisplayName("支付宝扫描支付,渠道返回失败")
public void test_unified_order_channel_failed() throws AlipayApiException {
@DisplayName("支付宝扫描支付渠道返回失败")
public void testUnifiedOrder_channelFailed() throws AlipayApiException {
// mock 方法
String notifyUrl = randomURL();
String subCode = randomString();
String subMsg = randomString();
@ -87,47 +93,55 @@ public class AlipayQrPayClientTest extends AbstractAlipayClientTest {
String outTradeNo = randomString();
PayOrderUnifiedReqDTO reqDTO = buildOrderUnifiedReqDTO(notifyUrl, outTradeNo, price);
// 调用
PayOrderRespDTO resp = client.unifiedOrder(reqDTO);
// 断言
assertEquals(CLOSED.getStatus(), resp.getStatus());
assertEquals(outTradeNo, resp.getOutTradeNo());
assertNull(resp.getChannelOrderNo());
assertNull(resp.getChannelUserId());
assertNull(resp.getSuccessTime());
assertNull(resp.getDisplayMode());
assertNull(resp.getDisplayContent());
assertSame(response, resp.getRawData());
assertEquals(subCode, resp.getChannelErrorCode());
assertEquals(subMsg, resp.getChannelErrorMsg());
assertSame(response, resp.getRawData());
}
@Test
@DisplayName("支付宝扫描支付, 抛出系统异常")
public void test_unified_order_throw_pay_exception() throws AlipayApiException {
// 准备请求参数
public void testUnifiedOrder_throwPayException() throws AlipayApiException {
// mock 方法
String outTradeNo = randomString();
String notifyUrl = randomURL();
Integer price = randomInteger();
// mock
when(defaultAlipayClient.execute(argThat((ArgumentMatcher<AlipayTradePrecreateRequest>) request -> {
assertEquals(notifyUrl, request.getNotifyUrl());
return true;
}))).thenThrow(new RuntimeException("系统异常"));
// 准备请求参数
PayOrderUnifiedReqDTO reqDTO = buildOrderUnifiedReqDTO(notifyUrl, outTradeNo,price);
// 断言
// 调用,并断言
assertThrows(PayException.class, () -> client.unifiedOrder(reqDTO));
}
@Test
@DisplayName("支付宝 Client 统一下单,抛出业务异常")
public void test_unified_order_throw_service_exception() throws AlipayApiException {
// 准备请求参数
@DisplayName("支付宝 Client 统一下单抛出业务异常")
public void testUnifiedOrder_throwServiceException() throws AlipayApiException {
// mock 方法
String outTradeNo = randomString();
String notifyUrl = randomURL();
Integer price = randomInteger();
// mock
when(defaultAlipayClient.execute(argThat((ArgumentMatcher<AlipayTradePrecreateRequest>) request -> {
assertEquals(notifyUrl, request.getNotifyUrl());
return true;
}))).thenThrow(ServiceExceptionUtil.exception(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR));
// 准备请求参数
PayOrderUnifiedReqDTO reqDTO = buildOrderUnifiedReqDTO(notifyUrl, outTradeNo, price);
// 断言
// 调用,并断言
assertThrows(ServiceException.class, () -> client.unifiedOrder(reqDTO));
}
}

View File

@ -42,9 +42,9 @@ public class AlipayWapPayClientTest extends AbstractAlipayClientTest {
}
@Test
@DisplayName("支付宝 H5 支付下单成功")
public void test_unified_order_success() throws AlipayApiException {
// 准备响应对象
@DisplayName("支付宝 H5 支付下单成功")
public void testUnifiedOrder_success() throws AlipayApiException {
// mock 方法
String h5Body = randomString();
Integer price = randomInteger();
AlipayTradeWapPayResponse response = randomPojo(AlipayTradeWapPayResponse.class, o -> {
@ -52,7 +52,6 @@ public class AlipayWapPayClientTest extends AbstractAlipayClientTest {
o.setBody(h5Body);
});
String notifyUrl = randomURL();
// mock
when(defaultAlipayClient.pageExecute(argThat((ArgumentMatcher<AlipayTradeWapPayRequest>) request -> {
assertInstanceOf(AlipayTradeWapPayModel.class, request.getBizModel());
AlipayTradeWapPayModel bizModel = (AlipayTradeWapPayModel) request.getBizModel();
@ -60,37 +59,53 @@ public class AlipayWapPayClientTest extends AbstractAlipayClientTest {
assertEquals(notifyUrl, request.getNotifyUrl());
return true;
}), eq(Method.GET.name()))).thenReturn(response);
// 准备请求参数
String outTradeNo = randomString();
PayOrderUnifiedReqDTO reqDTO = buildOrderUnifiedReqDTO(notifyUrl, outTradeNo, price);
// 调用
PayOrderRespDTO resp = client.unifiedOrder(reqDTO);
// 断言
assertEquals(WAITING.getStatus(), resp.getStatus());
assertEquals(PayOrderDisplayModeEnum.URL.getMode(), resp.getDisplayMode());
assertEquals(outTradeNo, resp.getOutTradeNo());
assertEquals(h5Body, resp.getDisplayContent());
assertNull(resp.getChannelOrderNo());
assertNull(resp.getChannelUserId());
assertNull(resp.getSuccessTime());
assertEquals(PayOrderDisplayModeEnum.URL.getMode(), resp.getDisplayMode());
assertEquals(response.getBody(), resp.getDisplayContent());
assertSame(response, resp.getRawData());
assertNull(resp.getChannelErrorCode());
assertNull(resp.getChannelErrorMsg());
}
@Test
@DisplayName("支付宝 H5 支付,渠道返回失败")
@DisplayName("支付宝 H5 支付渠道返回失败")
public void test_unified_order_channel_failed() throws AlipayApiException {
// 准备响应对象
// mock 方法
String subCode = randomString();
String subMsg = randomString();
AlipayTradeWapPayResponse response = randomPojo(AlipayTradeWapPayResponse.class, o -> {
o.setSubCode(subCode);
o.setSubMsg(subMsg);
});
// mock
when(defaultAlipayClient.pageExecute(argThat((ArgumentMatcher<AlipayTradeWapPayRequest>) request -> true),
eq(Method.GET.name()))).thenReturn(response);
PayOrderUnifiedReqDTO reqDTO = buildOrderUnifiedReqDTO(randomURL(), randomString(), randomInteger());
String outTradeNo = randomString();
PayOrderUnifiedReqDTO reqDTO = buildOrderUnifiedReqDTO(randomURL(), outTradeNo, randomInteger());
// 调用
PayOrderRespDTO resp = client.unifiedOrder(reqDTO);
// 断言
assertEquals(CLOSED.getStatus(), resp.getStatus());
assertEquals(outTradeNo, resp.getOutTradeNo());
assertNull(resp.getChannelOrderNo());
assertNull(resp.getChannelUserId());
assertNull(resp.getSuccessTime());
assertNull(resp.getDisplayMode());
assertNull(resp.getDisplayContent());
assertSame(response, resp.getRawData());
assertEquals(subCode, resp.getChannelErrorCode());
assertEquals(subMsg, resp.getChannelErrorMsg());
assertSame(response, resp.getRawData());
}
}