pay模块接口增加createOrder用以替换getPayInfo方法

This commit is contained in:
Binary Wang 2017-09-21 15:15:35 +08:00
parent d594656acc
commit a088202507
3 changed files with 110 additions and 19 deletions

View File

@ -19,7 +19,7 @@ import java.util.Map;
* Created by Binary Wang on 2016/7/28.
* </pre>
*
* @author binarywang (https://github.com/binarywang)
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
public interface WxPayService {
@ -56,6 +56,15 @@ public interface WxPayService {
*/
WxPayOrderCloseResult closeOrder(String outTradeNo) throws WxPayException;
/**
* 调用统一下单接口并组装生成支付所需参数对象
*
* @param request 统一下单请求参数
* @param <T> 请使用{@link com.github.binarywang.wxpay.bean.order}包下的类
* @return 返回 {@link com.github.binarywang.wxpay.bean.order}包下的类对象
*/
<T> T createOrder(WxPayUnifiedOrderRequest request) throws WxPayException;
/**
* 统一下单(详见https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_1)
* 在发起微信支付前需要调用统一下单接口获取"预支付交易会话标识"
@ -70,7 +79,9 @@ public interface WxPayService {
* 详见https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_5
*
* @param request 请求对象注意一些参数如appidmchid等不用设置方法内会自动从配置对象中获取到前提是对应配置中已经设置
* @deprecated 建议使用 {@link com.github.binarywang.wxpay.service.WxPayService#createOrder(WxPayUnifiedOrderRequest)}
*/
@Deprecated
Map<String, String> getPayInfo(WxPayUnifiedOrderRequest request) throws WxPayException;
/**
@ -117,7 +128,7 @@ public interface WxPayService {
/**
* @see WxPayService#parseOrderNotifyResult(String)
* @deprecated use WxPayService#parseOrderNotifyResult(String) instead
* @deprecated use {@link WxPayService#parseOrderNotifyResult(String)} instead
*/
@Deprecated
WxPayOrderNotifyResult getOrderNotifyResult(String xmlData) throws WxPayException;
@ -403,6 +414,7 @@ public interface WxPayService {
* 是否需要证书需要
* 文档地址https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_17&index=10
* </pre>
*
* @param beginDate 开始时间
* @param endDate 结束时间
* @param offset 位移

View File

@ -204,6 +204,7 @@ public abstract class WxPayServiceAbstractImpl implements WxPayService {
return result;
}
@Override
public <T> T createOrder(WxPayUnifiedOrderRequest request) throws WxPayException {
WxPayUnifiedOrderResult unifiedOrderResult = this.unifiedOrder(request);
String prepayId = unifiedOrderResult.getPrepayId();
@ -217,7 +218,8 @@ public abstract class WxPayServiceAbstractImpl implements WxPayService {
Object payResult = null;
switch (request.getTradeType()) {
case TradeType.NATIVE: {
payResult = WxPayNativeOrderResult.newBuilder().codeUrl(unifiedOrderResult.getCodeURL())
payResult = WxPayNativeOrderResult.builder()
.codeUrl(unifiedOrderResult.getCodeURL())
.build();
break;
}
@ -235,7 +237,7 @@ public abstract class WxPayServiceAbstractImpl implements WxPayService {
configMap.put("noncestr", nonceStr);
configMap.put("appid", appId);
payResult = WxPayAppOrderResult.newBuilder()
payResult = WxPayAppOrderResult.builder()
.sign(SignUtils.createSign(configMap, this.getConfig().getMchKey(), null))
.prepayId(prepayId)
.partnerId(partnerId)
@ -247,7 +249,7 @@ public abstract class WxPayServiceAbstractImpl implements WxPayService {
break;
}
case TradeType.JSAPI: {
payResult = WxPayMpOrderResult.newBuilder()
payResult = WxPayMpOrderResult.builder()
.appId(unifiedOrderResult.getAppid())
.timeStamp(timestamp)
.nonceStr(nonceStr)

View File

@ -2,6 +2,9 @@ package com.github.binarywang.wxpay.service.impl;
import com.github.binarywang.utils.qrcode.QrcodeUtils;
import com.github.binarywang.wxpay.bean.coupon.*;
import com.github.binarywang.wxpay.bean.order.WxPayAppOrderResult;
import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
import com.github.binarywang.wxpay.bean.order.WxPayNativeOrderResult;
import com.github.binarywang.wxpay.bean.request.*;
import com.github.binarywang.wxpay.bean.result.*;
import com.github.binarywang.wxpay.constant.WxPayConstants;
@ -29,7 +32,7 @@ import static org.testng.Assert.*;
* 测试支付相关接口
* Created by Binary Wang on 2016/7/28.
*
* @author binarywang (https://github.com/binarywang)
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
@Test
@Guice(modules = ApiTestModule.class)
@ -58,17 +61,85 @@ public class WxPayServiceAbstractImplTest {
this.logger.warn(this.payService.getWxApiData().toString());
}
@Test
public void testCreateOrder() throws Exception {
//see other tests with method name starting with 'testCreateOrder_'
}
@Test
public void testCreateOrder_jssdk() throws Exception {
WxPayMpOrderResult result = this.payService
.createOrder(WxPayUnifiedOrderRequest.newBuilder()
.body("我去")
.totalFee(1)
.spbillCreateIp("11.1.11.1")
.notifyURL("111111")
.tradeType(TradeType.JSAPI)
.openid(((XmlWxPayConfig) this.payService.getConfig()).getOpenid())
.outTradeNo("1111112")
.build());
this.logger.info(result.toString());
this.logger.warn(this.payService.getWxApiData().toString());
}
@Test
public void testCreateOrder_app() throws Exception {
WxPayAppOrderResult result = this.payService
.createOrder(WxPayUnifiedOrderRequest.newBuilder()
.body("我去")
.totalFee(1)
.spbillCreateIp("11.1.11.1")
.notifyURL("111111")
.tradeType(TradeType.APP)
.outTradeNo("1111112")
.build());
this.logger.info(result.toString());
this.logger.warn(this.payService.getWxApiData().toString());
}
@Test
public void testCreateOrder_native() throws Exception {
WxPayNativeOrderResult result = this.payService
.createOrder(WxPayUnifiedOrderRequest.newBuilder()
.body("我去")
.totalFee(1)
.spbillCreateIp("11.1.11.1")
.notifyURL("111111")
.tradeType(TradeType.NATIVE)
.outTradeNo("1111112")
.build());
this.logger.info(result.toString());
this.logger.warn(this.payService.getWxApiData().toString());
}
@Test
public void testCreateOrder_micropay() throws Exception {
//TODO 待完善
Object result = this.payService
.createOrder(WxPayUnifiedOrderRequest.newBuilder()
.body("我去")
.totalFee(1)
.spbillCreateIp("11.1.11.1")
.notifyURL("111111")
.tradeType(TradeType.MICROPAY)
.outTradeNo("1111112")
.build());
this.logger.info(result.toString());
this.logger.warn(this.payService.getWxApiData().toString());
}
@Test
public void testGetPayInfo() throws Exception {
Map<String, String> payInfo = this.payService.getPayInfo(WxPayUnifiedOrderRequest.newBuilder()
.body("我去")
.totalFee(1)
.spbillCreateIp("1.11.1.11")
.notifyURL("111111")
.tradeType(TradeType.JSAPI)
.outTradeNo("1111113")
.openid(((XmlWxPayConfig) this.payService.getConfig()).getOpenid())
.build());
Map<String, String> payInfo = this.payService
.getPayInfo(WxPayUnifiedOrderRequest.newBuilder()
.body("我去")
.totalFee(1)
.spbillCreateIp("1.11.1.11")
.notifyURL("111111")
.tradeType(TradeType.JSAPI)
.outTradeNo("1111113")
.openid(((XmlWxPayConfig) this.payService.getConfig()).getOpenid())
.build());
this.logger.info(payInfo.toString());
}
@ -237,10 +308,6 @@ public class WxPayServiceAbstractImplTest {
assertEquals(QrcodeUtils.decodeQrcode(qrcodeFilePath.toFile()), qrcodeContent);
}
@Test
public void testGetOrderNotifyResult() throws Exception {
}
@Test
public void testMicropay() throws Exception {
WxPayMicropayResult result = this.payService.micropay(WxPayMicropayRequest.newBuilder()
@ -347,4 +414,14 @@ public class WxPayServiceAbstractImplTest {
this.payService.queryComment(beginDate, endDate, 0, null);
}
@Test
public void testParseOrderNotifyResult() throws Exception {
// 请参考com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResultTest里的单元测试
}
@Test
public void testGetWxApiData() throws Exception {
//see test in testUnifiedOrder()
}
}