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. * Created by Binary Wang on 2016/7/28.
* </pre> * </pre>
* *
* @author binarywang (https://github.com/binarywang) * @author <a href="https://github.com/binarywang">Binary Wang</a>
*/ */
public interface WxPayService { public interface WxPayService {
@ -56,6 +56,15 @@ public interface WxPayService {
*/ */
WxPayOrderCloseResult closeOrder(String outTradeNo) throws WxPayException; 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) * 统一下单(详见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 * 详见https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_5
* *
* @param request 请求对象注意一些参数如appidmchid等不用设置方法内会自动从配置对象中获取到前提是对应配置中已经设置 * @param request 请求对象注意一些参数如appidmchid等不用设置方法内会自动从配置对象中获取到前提是对应配置中已经设置
* @deprecated 建议使用 {@link com.github.binarywang.wxpay.service.WxPayService#createOrder(WxPayUnifiedOrderRequest)}
*/ */
@Deprecated
Map<String, String> getPayInfo(WxPayUnifiedOrderRequest request) throws WxPayException; Map<String, String> getPayInfo(WxPayUnifiedOrderRequest request) throws WxPayException;
/** /**
@ -117,7 +128,7 @@ public interface WxPayService {
/** /**
* @see WxPayService#parseOrderNotifyResult(String) * @see WxPayService#parseOrderNotifyResult(String)
* @deprecated use WxPayService#parseOrderNotifyResult(String) instead * @deprecated use {@link WxPayService#parseOrderNotifyResult(String)} instead
*/ */
@Deprecated @Deprecated
WxPayOrderNotifyResult getOrderNotifyResult(String xmlData) throws WxPayException; 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 * 文档地址https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_17&index=10
* </pre> * </pre>
*
* @param beginDate 开始时间 * @param beginDate 开始时间
* @param endDate 结束时间 * @param endDate 结束时间
* @param offset 位移 * @param offset 位移

View File

@ -204,6 +204,7 @@ public abstract class WxPayServiceAbstractImpl implements WxPayService {
return result; return result;
} }
@Override
public <T> T createOrder(WxPayUnifiedOrderRequest request) throws WxPayException { public <T> T createOrder(WxPayUnifiedOrderRequest request) throws WxPayException {
WxPayUnifiedOrderResult unifiedOrderResult = this.unifiedOrder(request); WxPayUnifiedOrderResult unifiedOrderResult = this.unifiedOrder(request);
String prepayId = unifiedOrderResult.getPrepayId(); String prepayId = unifiedOrderResult.getPrepayId();
@ -217,7 +218,8 @@ public abstract class WxPayServiceAbstractImpl implements WxPayService {
Object payResult = null; Object payResult = null;
switch (request.getTradeType()) { switch (request.getTradeType()) {
case TradeType.NATIVE: { case TradeType.NATIVE: {
payResult = WxPayNativeOrderResult.newBuilder().codeUrl(unifiedOrderResult.getCodeURL()) payResult = WxPayNativeOrderResult.builder()
.codeUrl(unifiedOrderResult.getCodeURL())
.build(); .build();
break; break;
} }
@ -235,7 +237,7 @@ public abstract class WxPayServiceAbstractImpl implements WxPayService {
configMap.put("noncestr", nonceStr); configMap.put("noncestr", nonceStr);
configMap.put("appid", appId); configMap.put("appid", appId);
payResult = WxPayAppOrderResult.newBuilder() payResult = WxPayAppOrderResult.builder()
.sign(SignUtils.createSign(configMap, this.getConfig().getMchKey(), null)) .sign(SignUtils.createSign(configMap, this.getConfig().getMchKey(), null))
.prepayId(prepayId) .prepayId(prepayId)
.partnerId(partnerId) .partnerId(partnerId)
@ -247,7 +249,7 @@ public abstract class WxPayServiceAbstractImpl implements WxPayService {
break; break;
} }
case TradeType.JSAPI: { case TradeType.JSAPI: {
payResult = WxPayMpOrderResult.newBuilder() payResult = WxPayMpOrderResult.builder()
.appId(unifiedOrderResult.getAppid()) .appId(unifiedOrderResult.getAppid())
.timeStamp(timestamp) .timeStamp(timestamp)
.nonceStr(nonceStr) .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.utils.qrcode.QrcodeUtils;
import com.github.binarywang.wxpay.bean.coupon.*; 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.request.*;
import com.github.binarywang.wxpay.bean.result.*; import com.github.binarywang.wxpay.bean.result.*;
import com.github.binarywang.wxpay.constant.WxPayConstants; import com.github.binarywang.wxpay.constant.WxPayConstants;
@ -29,7 +32,7 @@ import static org.testng.Assert.*;
* 测试支付相关接口 * 测试支付相关接口
* Created by Binary Wang on 2016/7/28. * 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 @Test
@Guice(modules = ApiTestModule.class) @Guice(modules = ApiTestModule.class)
@ -58,17 +61,85 @@ public class WxPayServiceAbstractImplTest {
this.logger.warn(this.payService.getWxApiData().toString()); 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 @Test
public void testGetPayInfo() throws Exception { public void testGetPayInfo() throws Exception {
Map<String, String> payInfo = this.payService.getPayInfo(WxPayUnifiedOrderRequest.newBuilder() Map<String, String> payInfo = this.payService
.body("我去") .getPayInfo(WxPayUnifiedOrderRequest.newBuilder()
.totalFee(1) .body("我去")
.spbillCreateIp("1.11.1.11") .totalFee(1)
.notifyURL("111111") .spbillCreateIp("1.11.1.11")
.tradeType(TradeType.JSAPI) .notifyURL("111111")
.outTradeNo("1111113") .tradeType(TradeType.JSAPI)
.openid(((XmlWxPayConfig) this.payService.getConfig()).getOpenid()) .outTradeNo("1111113")
.build()); .openid(((XmlWxPayConfig) this.payService.getConfig()).getOpenid())
.build());
this.logger.info(payInfo.toString()); this.logger.info(payInfo.toString());
} }
@ -237,10 +308,6 @@ public class WxPayServiceAbstractImplTest {
assertEquals(QrcodeUtils.decodeQrcode(qrcodeFilePath.toFile()), qrcodeContent); assertEquals(QrcodeUtils.decodeQrcode(qrcodeFilePath.toFile()), qrcodeContent);
} }
@Test
public void testGetOrderNotifyResult() throws Exception {
}
@Test @Test
public void testMicropay() throws Exception { public void testMicropay() throws Exception {
WxPayMicropayResult result = this.payService.micropay(WxPayMicropayRequest.newBuilder() WxPayMicropayResult result = this.payService.micropay(WxPayMicropayRequest.newBuilder()
@ -347,4 +414,14 @@ public class WxPayServiceAbstractImplTest {
this.payService.queryComment(beginDate, endDate, 0, null); 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()
}
} }