From a0882025073d51c27735fad3d54c839744f0f98e Mon Sep 17 00:00:00 2001 From: Binary Wang Date: Thu, 21 Sep 2017 15:15:35 +0800 Subject: [PATCH] =?UTF-8?q?pay=E6=A8=A1=E5=9D=97=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0createOrder=E7=94=A8=E4=BB=A5=E6=9B=BF?= =?UTF-8?q?=E6=8D=A2getPayInfo=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wxpay/service/WxPayService.java | 16 ++- .../impl/WxPayServiceAbstractImpl.java | 8 +- .../impl/WxPayServiceAbstractImplTest.java | 105 +++++++++++++++--- 3 files changed, 110 insertions(+), 19 deletions(-) diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/WxPayService.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/WxPayService.java index ae058e14..33cd56c6 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/WxPayService.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/WxPayService.java @@ -19,7 +19,7 @@ import java.util.Map; * Created by Binary Wang on 2016/7/28. * * - * @author binarywang (https://github.com/binarywang) + * @author Binary Wang */ public interface WxPayService { @@ -56,6 +56,15 @@ public interface WxPayService { */ WxPayOrderCloseResult closeOrder(String outTradeNo) throws WxPayException; + /** + * 调用统一下单接口,并组装生成支付所需参数对象 + * + * @param request 统一下单请求参数 + * @param 请使用{@link com.github.binarywang.wxpay.bean.order}包下的类 + * @return 返回 {@link com.github.binarywang.wxpay.bean.order}包下的类对象 + */ + 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 请求对象,注意一些参数如appid、mchid等不用设置,方法内会自动从配置对象中获取到(前提是对应配置中已经设置) + * @deprecated 建议使用 {@link com.github.binarywang.wxpay.service.WxPayService#createOrder(WxPayUnifiedOrderRequest)} */ + @Deprecated Map 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 * + * * @param beginDate 开始时间 * @param endDate 结束时间 * @param offset 位移 diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceAbstractImpl.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceAbstractImpl.java index ae855d5b..0685c6c9 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceAbstractImpl.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceAbstractImpl.java @@ -204,6 +204,7 @@ public abstract class WxPayServiceAbstractImpl implements WxPayService { return result; } + @Override public 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) diff --git a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/WxPayServiceAbstractImplTest.java b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/WxPayServiceAbstractImplTest.java index afea13d8..d54942b4 100644 --- a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/WxPayServiceAbstractImplTest.java +++ b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/WxPayServiceAbstractImplTest.java @@ -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 Binary Wang */ @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 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 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() + } + }