Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加 WxPayService.createOrder 返回交易类型对应实体类方法 #1458

Merged
merged 2 commits into from
Mar 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -103,6 +108,55 @@ public static class TradeType {
* 刷卡支付有单独的支付接口,不调用统一下单接口
*/
public static final String MICROPAY = "MICROPAY";

@SuppressWarnings("unused")
public abstract static class Specific<R> {

public abstract String getType();

private Specific() {
}

public static Specific<WxPayNativeOrderResult> NATIVE =
new Specific<WxPayNativeOrderResult>() {
@Override
public String getType() {
return TradeType.NATIVE;
}
};

public static Specific<WxPayAppOrderResult> APP =
new Specific<WxPayAppOrderResult>() {
@Override
public String getType() {
return TradeType.APP;
}
};

public static Specific<WxPayMpOrderResult> JSAPI =
new Specific<WxPayMpOrderResult>() {
@Override
public String getType() {
return TradeType.JSAPI;
}
};

public static Specific<WxPayMwebOrderResult> MWEB =
new Specific<WxPayMwebOrderResult>() {
@Override
public String getType() {
return TradeType.MWEB;
}
};

public static Specific<WxPayMicropayResult> MICROPAY =
new Specific<WxPayMicropayResult>() {
@Override
public String getType() {
return TradeType.MICROPAY;
}
};
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -166,6 +167,17 @@ public interface WxPayService {
*/
<T> 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> T createOrder(WxPayConstants.TradeType.Specific<T> specificTradeType, WxPayUnifiedOrderRequest request) throws WxPayException;

/**
* 统一下单(详见https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_1)
* 在发起微信支付前,需要调用统一下单接口,获取"预支付交易会话标识"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,15 @@ public <T> T createOrder(WxPayUnifiedOrderRequest request) throws WxPayException

}

@Override
public <T> T createOrder(TradeType.Specific<T> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down