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..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 @@ -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,55 @@ public static class TradeType { * 刷卡支付有单独的支付接口,不调用统一下单接口 */ public static final String MICROPAY = "MICROPAY"; + + @SuppressWarnings("unused") + public abstract static class Specific { + + public abstract String getType(); + + private Specific() { + } + + 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. *