Skip to content

Commit a088202

Browse files
committed
pay模块接口增加createOrder用以替换getPayInfo方法
1 parent d594656 commit a088202

3 files changed

Lines changed: 110 additions & 19 deletions

File tree

‎weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/WxPayService.java‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* Created by Binary Wang on 2016/7/28.
2020
* </pre>
2121
*
22-
* @author binarywang (https://github.com/binarywang)
22+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
2323
*/
2424
public interface WxPayService {
2525

@@ -56,6 +56,15 @@ public interface WxPayService {
5656
*/
5757
WxPayOrderCloseResult closeOrder(String outTradeNo) throws WxPayException;
5858

59+
/**
60+
* 调用统一下单接口,并组装生成支付所需参数对象
61+
*
62+
* @param request 统一下单请求参数
63+
* @param <T> 请使用{@link com.github.binarywang.wxpay.bean.order}包下的类
64+
* @return 返回 {@link com.github.binarywang.wxpay.bean.order}包下的类对象
65+
*/
66+
<T> T createOrder(WxPayUnifiedOrderRequest request) throws WxPayException;
67+
5968
/**
6069
* 统一下单(详见https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_1)
6170
* 在发起微信支付前,需要调用统一下单接口,获取"预支付交易会话标识"
@@ -70,7 +79,9 @@ public interface WxPayService {
7079
* 详见https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_5
7180
*
7281
* @param request 请求对象,注意一些参数如appid、mchid等不用设置,方法内会自动从配置对象中获取到(前提是对应配置中已经设置)
82+
* @deprecated 建议使用 {@link com.github.binarywang.wxpay.service.WxPayService#createOrder(WxPayUnifiedOrderRequest)}
7383
*/
84+
@Deprecated
7485
Map<String, String> getPayInfo(WxPayUnifiedOrderRequest request) throws WxPayException;
7586

7687
/**
@@ -117,7 +128,7 @@ WxPayRefundQueryResult refundQuery(String transactionId, String outTradeNo, Stri
117128

118129
/**
119130
* @see WxPayService#parseOrderNotifyResult(String)
120-
* @deprecated use WxPayService#parseOrderNotifyResult(String) instead
131+
* @deprecated use {@link WxPayService#parseOrderNotifyResult(String)} instead
121132
*/
122133
@Deprecated
123134
WxPayOrderNotifyResult getOrderNotifyResult(String xmlData) throws WxPayException;
@@ -403,6 +414,7 @@ WxPayRefundQueryResult refundQuery(String transactionId, String outTradeNo, Stri
403414
* 是否需要证书:需要
404415
* 文档地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_17&index=10
405416
* </pre>
417+
*
406418
* @param beginDate 开始时间
407419
* @param endDate 结束时间
408420
* @param offset 位移

‎weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceAbstractImpl.java‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ public WxPayOrderCloseResult closeOrder(String outTradeNo) throws WxPayException
204204
return result;
205205
}
206206

207+
@Override
207208
public <T> T createOrder(WxPayUnifiedOrderRequest request) throws WxPayException {
208209
WxPayUnifiedOrderResult unifiedOrderResult = this.unifiedOrder(request);
209210
String prepayId = unifiedOrderResult.getPrepayId();
@@ -217,7 +218,8 @@ public <T> T createOrder(WxPayUnifiedOrderRequest request) throws WxPayException
217218
Object payResult = null;
218219
switch (request.getTradeType()) {
219220
case TradeType.NATIVE: {
220-
payResult = WxPayNativeOrderResult.newBuilder().codeUrl(unifiedOrderResult.getCodeURL())
221+
payResult = WxPayNativeOrderResult.builder()
222+
.codeUrl(unifiedOrderResult.getCodeURL())
221223
.build();
222224
break;
223225
}
@@ -235,7 +237,7 @@ public <T> T createOrder(WxPayUnifiedOrderRequest request) throws WxPayException
235237
configMap.put("noncestr", nonceStr);
236238
configMap.put("appid", appId);
237239

238-
payResult = WxPayAppOrderResult.newBuilder()
240+
payResult = WxPayAppOrderResult.builder()
239241
.sign(SignUtils.createSign(configMap, this.getConfig().getMchKey(), null))
240242
.prepayId(prepayId)
241243
.partnerId(partnerId)
@@ -247,7 +249,7 @@ public <T> T createOrder(WxPayUnifiedOrderRequest request) throws WxPayException
247249
break;
248250
}
249251
case TradeType.JSAPI: {
250-
payResult = WxPayMpOrderResult.newBuilder()
252+
payResult = WxPayMpOrderResult.builder()
251253
.appId(unifiedOrderResult.getAppid())
252254
.timeStamp(timestamp)
253255
.nonceStr(nonceStr)

‎weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/WxPayServiceAbstractImplTest.java‎

Lines changed: 91 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import com.github.binarywang.utils.qrcode.QrcodeUtils;
44
import com.github.binarywang.wxpay.bean.coupon.*;
5+
import com.github.binarywang.wxpay.bean.order.WxPayAppOrderResult;
6+
import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
7+
import com.github.binarywang.wxpay.bean.order.WxPayNativeOrderResult;
58
import com.github.binarywang.wxpay.bean.request.*;
69
import com.github.binarywang.wxpay.bean.result.*;
710
import com.github.binarywang.wxpay.constant.WxPayConstants;
@@ -29,7 +32,7 @@
2932
* 测试支付相关接口
3033
* Created by Binary Wang on 2016/7/28.
3134
*
32-
* @author binarywang (https://github.com/binarywang)
35+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
3336
*/
3437
@Test
3538
@Guice(modules = ApiTestModule.class)
@@ -58,17 +61,85 @@ public void testUnifiedOrder() throws WxPayException {
5861
this.logger.warn(this.payService.getWxApiData().toString());
5962
}
6063

64+
@Test
65+
public void testCreateOrder() throws Exception {
66+
//see other tests with method name starting with 'testCreateOrder_'
67+
}
68+
69+
@Test
70+
public void testCreateOrder_jssdk() throws Exception {
71+
WxPayMpOrderResult result = this.payService
72+
.createOrder(WxPayUnifiedOrderRequest.newBuilder()
73+
.body("我去")
74+
.totalFee(1)
75+
.spbillCreateIp("11.1.11.1")
76+
.notifyURL("111111")
77+
.tradeType(TradeType.JSAPI)
78+
.openid(((XmlWxPayConfig) this.payService.getConfig()).getOpenid())
79+
.outTradeNo("1111112")
80+
.build());
81+
this.logger.info(result.toString());
82+
this.logger.warn(this.payService.getWxApiData().toString());
83+
}
84+
85+
@Test
86+
public void testCreateOrder_app() throws Exception {
87+
WxPayAppOrderResult result = this.payService
88+
.createOrder(WxPayUnifiedOrderRequest.newBuilder()
89+
.body("我去")
90+
.totalFee(1)
91+
.spbillCreateIp("11.1.11.1")
92+
.notifyURL("111111")
93+
.tradeType(TradeType.APP)
94+
.outTradeNo("1111112")
95+
.build());
96+
this.logger.info(result.toString());
97+
this.logger.warn(this.payService.getWxApiData().toString());
98+
}
99+
100+
@Test
101+
public void testCreateOrder_native() throws Exception {
102+
WxPayNativeOrderResult result = this.payService
103+
.createOrder(WxPayUnifiedOrderRequest.newBuilder()
104+
.body("我去")
105+
.totalFee(1)
106+
.spbillCreateIp("11.1.11.1")
107+
.notifyURL("111111")
108+
.tradeType(TradeType.NATIVE)
109+
.outTradeNo("1111112")
110+
.build());
111+
this.logger.info(result.toString());
112+
this.logger.warn(this.payService.getWxApiData().toString());
113+
}
114+
115+
@Test
116+
public void testCreateOrder_micropay() throws Exception {
117+
//TODO 待完善
118+
Object result = this.payService
119+
.createOrder(WxPayUnifiedOrderRequest.newBuilder()
120+
.body("我去")
121+
.totalFee(1)
122+
.spbillCreateIp("11.1.11.1")
123+
.notifyURL("111111")
124+
.tradeType(TradeType.MICROPAY)
125+
.outTradeNo("1111112")
126+
.build());
127+
this.logger.info(result.toString());
128+
this.logger.warn(this.payService.getWxApiData().toString());
129+
}
130+
61131
@Test
62132
public void testGetPayInfo() throws Exception {
63-
Map<String, String> payInfo = this.payService.getPayInfo(WxPayUnifiedOrderRequest.newBuilder()
64-
.body("我去")
65-
.totalFee(1)
66-
.spbillCreateIp("1.11.1.11")
67-
.notifyURL("111111")
68-
.tradeType(TradeType.JSAPI)
69-
.outTradeNo("1111113")
70-
.openid(((XmlWxPayConfig) this.payService.getConfig()).getOpenid())
71-
.build());
133+
Map<String, String> payInfo = this.payService
134+
.getPayInfo(WxPayUnifiedOrderRequest.newBuilder()
135+
.body("我去")
136+
.totalFee(1)
137+
.spbillCreateIp("1.11.1.11")
138+
.notifyURL("111111")
139+
.tradeType(TradeType.JSAPI)
140+
.outTradeNo("1111113")
141+
.openid(((XmlWxPayConfig) this.payService.getConfig()).getOpenid())
142+
.build());
72143
this.logger.info(payInfo.toString());
73144
}
74145

@@ -237,10 +308,6 @@ public void testCreateScanPayQrcodeMode2() throws Exception {
237308
assertEquals(QrcodeUtils.decodeQrcode(qrcodeFilePath.toFile()), qrcodeContent);
238309
}
239310

240-
@Test
241-
public void testGetOrderNotifyResult() throws Exception {
242-
}
243-
244311
@Test
245312
public void testMicropay() throws Exception {
246313
WxPayMicropayResult result = this.payService.micropay(WxPayMicropayRequest.newBuilder()
@@ -347,4 +414,14 @@ public void testQueryComment() throws Exception {
347414
this.payService.queryComment(beginDate, endDate, 0, null);
348415
}
349416

417+
@Test
418+
public void testParseOrderNotifyResult() throws Exception {
419+
// 请参考com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResultTest里的单元测试
420+
}
421+
422+
@Test
423+
public void testGetWxApiData() throws Exception {
424+
//see test in testUnifiedOrder()
425+
}
426+
350427
}

0 commit comments

Comments
 (0)