From 724a3302dd1ccbbb16f12aba551dc1b13df7c0f5 Mon Sep 17 00:00:00 2001 From: xJoeWoo Date: Fri, 20 Mar 2020 10:07:41 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20WxPayService#createOrd?= =?UTF-8?q?er=20=E8=BF=94=E5=9B=9E=E4=BA=A4=E6=98=93=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E5=AF=B9=E5=BA=94=E5=AE=9E=E4=BD=93=E7=B1=BB=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wxpay/constant/WxPayConstants.java | 51 +++++++++++++++++++ .../wxpay/service/WxPayService.java | 12 +++++ .../service/impl/BaseWxPayServiceImpl.java | 9 ++++ .../impl/BaseWxPayServiceImplTest.java | 18 +++++++ 4 files changed, 90 insertions(+) diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/constant/WxPayConstants.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/constant/WxPayConstants.java index 4f8480b6a2..267a31371c 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/constant/WxPayConstants.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/constant/WxPayConstants.java @@ -1,5 +1,10 @@ package com.github.binarywang.wxpay.constant; +import com.github.binarywang.wxpay.bean.order.WxPayAppOrderResult; +import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult; +import com.github.binarywang.wxpay.bean.order.WxPayMwebOrderResult; +import com.github.binarywang.wxpay.bean.order.WxPayNativeOrderResult; +import com.github.binarywang.wxpay.bean.result.WxPayMicropayResult; import com.google.common.collect.Lists; import org.apache.commons.lang3.time.FastDateFormat; @@ -103,6 +108,52 @@ public static class TradeType { * 刷卡支付有单独的支付接口,不调用统一下单接口 */ public static final String MICROPAY = "MICROPAY"; + + @SuppressWarnings("unused") + public abstract static class Specific { + + public abstract String getType(); + + public static Specific NATIVE = + new Specific() { + @Override + public String getType() { + return TradeType.NATIVE; + } + }; + + public static Specific APP = + new Specific() { + @Override + public String getType() { + return TradeType.APP; + } + }; + + public static Specific JSAPI = + new Specific() { + @Override + public String getType() { + return TradeType.JSAPI; + } + }; + + public static Specific MWEB = + new Specific() { + @Override + public String getType() { + return TradeType.MWEB; + } + }; + + public static Specific MICROPAY = + new Specific() { + @Override + public String getType() { + return TradeType.MICROPAY; + } + }; + } } /** 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 52f99af6c4..109ce34b98 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 @@ -8,6 +8,7 @@ import com.github.binarywang.wxpay.bean.request.*; import com.github.binarywang.wxpay.bean.result.*; import com.github.binarywang.wxpay.config.WxPayConfig; +import com.github.binarywang.wxpay.constant.WxPayConstants; import com.github.binarywang.wxpay.exception.WxPayException; import java.io.File; @@ -166,6 +167,17 @@ public interface WxPayService { */ T createOrder(WxPayUnifiedOrderRequest request) throws WxPayException; + /** + * 调用统一下单接口,并组装生成支付所需参数对象. + * + * @see WxPayService#createOrder(WxPayUnifiedOrderRequest) + * @param specificTradeType 将使用的交易方式,不能为 null + * @param request 统一下单请求参数,设定的 tradeType 及配置里的 tradeType 将被忽略,转而使用 specificTradeType + * @return 返回 {@link WxPayConstants.TradeType.Specific} 指定的类 + * @throws WxPayException the wx pay exception + */ + T createOrder(WxPayConstants.TradeType.Specific specificTradeType, WxPayUnifiedOrderRequest request) throws WxPayException; + /** * 统一下单(详见https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_1) * 在发起微信支付前,需要调用统一下单接口,获取"预支付交易会话标识" diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImpl.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImpl.java index 61dcee09ba..abf9a41e0e 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImpl.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImpl.java @@ -351,6 +351,15 @@ public T createOrder(WxPayUnifiedOrderRequest request) throws WxPayException } + @Override + public T createOrder(TradeType.Specific specificTradeType, WxPayUnifiedOrderRequest request) throws WxPayException { + if (specificTradeType == null) { + throw new IllegalArgumentException("specificTradeType 不能为 null"); + } + request.setTradeType(specificTradeType.getType()); + return createOrder(request); + } + @Override public WxPayUnifiedOrderResult unifiedOrder(WxPayUnifiedOrderRequest request) throws WxPayException { request.checkAndSign(this.getConfig()); diff --git a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImplTest.java b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImplTest.java index de7b1e586e..dfb4ffb8c0 100644 --- a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImplTest.java +++ b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImplTest.java @@ -144,6 +144,24 @@ public void testCreateOrder_native() throws Exception { log.warn(this.payService.getWxApiData().toString()); } + @Test + public void testCreateOrderSpecific() throws Exception { + // Won't compile + // WxPayMpOrderResult result = payService.createOrder(TradeType.Specific.APP, new WxPayUnifiedOrderRequest()); + payService.createOrder( + TradeType.Specific.JSAPI, + WxPayUnifiedOrderRequest.newBuilder() + .body("我去") + .totalFee(1) + .productId("aaa") + .spbillCreateIp("11.1.11.1") + .notifyUrl("111111") + .outTradeNo("111111290") + .build() + ) + .getAppId(); + } + /** * Test get pay info. * From c9db16b8bd43d9e6098815702afffc28e24e1eba Mon Sep 17 00:00:00 2001 From: xJoeWoo Date: Fri, 20 Mar 2020 13:11:27 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=A4=E6=98=93?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E6=9E=84=E9=80=A0=E5=87=BD=E6=95=B0=E4=B8=BA?= =?UTF-8?q?=20private?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/github/binarywang/wxpay/constant/WxPayConstants.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/constant/WxPayConstants.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/constant/WxPayConstants.java index 267a31371c..fbc499fedd 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/constant/WxPayConstants.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/constant/WxPayConstants.java @@ -114,6 +114,9 @@ public abstract static class Specific { public abstract String getType(); + private Specific() { + } + public static Specific NATIVE = new Specific() { @Override