mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-08-24 07:23:01 +08:00
🎨 #3070【微信支付】优化服务商模式下下单接口,去掉部分重复代码
This commit is contained in:
parent
c3b7d8c843
commit
382b4ce63a
@ -250,6 +250,34 @@ public class CombineNotifyResult implements Serializable {
|
|||||||
*/
|
*/
|
||||||
@SerializedName(value = "out_trade_no")
|
@SerializedName(value = "out_trade_no")
|
||||||
private String outTradeNo;
|
private String outTradeNo;
|
||||||
|
/**
|
||||||
|
* <pre>
|
||||||
|
* 字段名:子商户应用ID
|
||||||
|
* 变量名:sub_appid
|
||||||
|
* 是否必填:是
|
||||||
|
* 类型:string[1,32]
|
||||||
|
* 描述:
|
||||||
|
* 子商户申请的应用ID,全局唯一。请求基础下单接口时请注意APPID的应用属性,例如公众号场景下,
|
||||||
|
* 需使用应用属性为公众号的APPID 若sub_openid有传的情况下,
|
||||||
|
* sub_appid必填,且sub_appid需与sub_openid对应
|
||||||
|
* 示例值:wxd678efh567hg6999
|
||||||
|
* </pre>
|
||||||
|
*/
|
||||||
|
@SerializedName(value = "sub_appid")
|
||||||
|
private String subAppid;
|
||||||
|
/**
|
||||||
|
* <pre>
|
||||||
|
* 字段名:二级商户号
|
||||||
|
* 变量名:sub_mchid
|
||||||
|
* 是否必填:是
|
||||||
|
* 类型:string[1,32]
|
||||||
|
* 描述:
|
||||||
|
* 二级商户商户号,由微信支付生成并下发。服务商子商户的商户号,被合单方。直连商户不用传二级商户号。
|
||||||
|
* 示例值:1900000109
|
||||||
|
* </pre>
|
||||||
|
*/
|
||||||
|
@SerializedName(value = "sub_mchid")
|
||||||
|
private String subMchid;
|
||||||
/**
|
/**
|
||||||
* <pre>
|
* <pre>
|
||||||
* 字段名:订单金额
|
* 字段名:订单金额
|
||||||
|
@ -240,6 +240,32 @@ public class CombineTransactionsRequest implements Serializable {
|
|||||||
*/
|
*/
|
||||||
@SerializedName(value = "out_trade_no")
|
@SerializedName(value = "out_trade_no")
|
||||||
private String outTradeNo;
|
private String outTradeNo;
|
||||||
|
/**
|
||||||
|
* <pre>
|
||||||
|
* 字段名:订单优惠标记
|
||||||
|
* 变量名:goods_tag
|
||||||
|
* 是否必填:是
|
||||||
|
* 类型:string[1,32]
|
||||||
|
* 描述:
|
||||||
|
* 订单优惠标记,使用代金券或立减优惠功能时需要的参数,说明详见代金券或立减优惠
|
||||||
|
* 示例值:WXG
|
||||||
|
* </pre>
|
||||||
|
*/
|
||||||
|
@SerializedName(value = "goods_tag")
|
||||||
|
private String goodsTag;
|
||||||
|
/**
|
||||||
|
* <pre>
|
||||||
|
* 字段名:二级商户号
|
||||||
|
* 变量名:sub_mchid
|
||||||
|
* 是否必填:是
|
||||||
|
* 类型:string[1,32]
|
||||||
|
* 描述:
|
||||||
|
* 二级商户商户号,由微信支付生成并下发。服务商子商户的商户号,被合单方。直连商户不用传二级商户号。
|
||||||
|
* 示例值:1900000109
|
||||||
|
* </pre>
|
||||||
|
*/
|
||||||
|
@SerializedName(value = "sub_mchid")
|
||||||
|
private String subMchid;
|
||||||
/**
|
/**
|
||||||
* <pre>
|
* <pre>
|
||||||
* 字段名:商品描述
|
* 字段名:商品描述
|
||||||
@ -264,6 +290,21 @@ public class CombineTransactionsRequest implements Serializable {
|
|||||||
*/
|
*/
|
||||||
@SerializedName(value = "settle_info")
|
@SerializedName(value = "settle_info")
|
||||||
private SettleInfo settleInfo;
|
private SettleInfo settleInfo;
|
||||||
|
/**
|
||||||
|
* <pre>
|
||||||
|
* 字段名:子商户应用ID
|
||||||
|
* 变量名:sub_appid
|
||||||
|
* 是否必填:是
|
||||||
|
* 类型:string[1,32]
|
||||||
|
* 描述:
|
||||||
|
* 子商户申请的应用ID,全局唯一。请求基础下单接口时请注意APPID的应用属性,例如公众号场景下,
|
||||||
|
* 需使用应用属性为公众号的APPID 若sub_openid有传的情况下,
|
||||||
|
* sub_appid必填,且sub_appid需与sub_openid对应
|
||||||
|
* 示例值:wxd678efh567hg6999
|
||||||
|
* </pre>
|
||||||
|
*/
|
||||||
|
@SerializedName(value = "sub_appid")
|
||||||
|
private String subAppid;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@ -283,6 +324,21 @@ public class CombineTransactionsRequest implements Serializable {
|
|||||||
*/
|
*/
|
||||||
@SerializedName(value = "openid")
|
@SerializedName(value = "openid")
|
||||||
private String openid;
|
private String openid;
|
||||||
|
/**
|
||||||
|
* <pre>
|
||||||
|
* 字段名:子用户标识
|
||||||
|
* 变量名:sub_openid
|
||||||
|
* 是否必填:是
|
||||||
|
* 类型:string[1,128]
|
||||||
|
* 描述:
|
||||||
|
* 服务商模式下,使用某个子商户的Appid获取的对应用户Openid,
|
||||||
|
* 是用户在该子商户Appid下的唯一标识。openid和sub_openid可以选传其中之一,
|
||||||
|
* 如果选择传sub_openid,则必须传sub_appid。
|
||||||
|
* 示例值:oUpF8uMuAJO_M2pxb1Q9zNjWeS6o
|
||||||
|
* </pre>
|
||||||
|
*/
|
||||||
|
@SerializedName(value = "sub_openid")
|
||||||
|
private String subOpenid;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package com.github.binarywang.wxpay.bean.result;
|
package com.github.binarywang.wxpay.bean.result;
|
||||||
|
|
||||||
import com.github.binarywang.wxpay.bean.result.enums.PartnerTradeTypeEnum;
|
|
||||||
import com.github.binarywang.wxpay.bean.result.enums.TradeTypeEnum;
|
import com.github.binarywang.wxpay.bean.result.enums.TradeTypeEnum;
|
||||||
import com.github.binarywang.wxpay.v3.util.SignUtils;
|
import com.github.binarywang.wxpay.v3.util.SignUtils;
|
||||||
import com.google.gson.annotations.SerializedName;
|
import com.google.gson.annotations.SerializedName;
|
||||||
@ -133,32 +132,4 @@ public class WxPayUnifiedOrderV3Result implements Serializable {
|
|||||||
throw new WxRuntimeException("不支持的支付类型");
|
throw new WxRuntimeException("不支持的支付类型");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> T getPartnerPayInfo(PartnerTradeTypeEnum tradeType, String appId, String mchId, PrivateKey privateKey) {
|
|
||||||
String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
|
|
||||||
String nonceStr = SignUtils.genRandomStr();
|
|
||||||
switch (tradeType) {
|
|
||||||
case JSAPI:
|
|
||||||
JsapiResult jsapiResult = new JsapiResult();
|
|
||||||
jsapiResult.setAppId(appId).setTimeStamp(timestamp)
|
|
||||||
.setPackageValue("prepay_id=" + this.prepayId).setNonceStr(nonceStr)
|
|
||||||
//签名类型,默认为RSA,仅支持RSA。
|
|
||||||
.setSignType("RSA").setPaySign(SignUtils.sign(jsapiResult.getSignStr(), privateKey));
|
|
||||||
return (T) jsapiResult;
|
|
||||||
case H5:
|
|
||||||
return (T) this.h5Url;
|
|
||||||
case APP:
|
|
||||||
AppResult appResult = new AppResult();
|
|
||||||
appResult.setAppid(appId).setPrepayId(this.prepayId).setPartnerId(mchId)
|
|
||||||
.setNoncestr(nonceStr).setTimestamp(timestamp)
|
|
||||||
//暂填写固定值Sign=WXPay
|
|
||||||
.setPackageValue("Sign=WXPay")
|
|
||||||
.setSign(SignUtils.sign(appResult.getSignStr(), privateKey));
|
|
||||||
return (T) appResult;
|
|
||||||
case NATIVE:
|
|
||||||
return (T) this.codeUrl;
|
|
||||||
default:
|
|
||||||
throw new WxRuntimeException("不支持的支付类型");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,45 +0,0 @@
|
|||||||
package com.github.binarywang.wxpay.bean.result.enums;
|
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.Getter;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 支付方式
|
|
||||||
*
|
|
||||||
* @author thinsstar
|
|
||||||
*/
|
|
||||||
@Getter
|
|
||||||
@AllArgsConstructor
|
|
||||||
public enum PartnerTradeTypeEnum {
|
|
||||||
/**
|
|
||||||
* APP
|
|
||||||
*/
|
|
||||||
APP("/v3/pay/partner/transactions/app", "/v3/combine-transactions/app", TradeTypeEnum.APP),
|
|
||||||
/**
|
|
||||||
* JSAPI 或 小程序
|
|
||||||
*/
|
|
||||||
JSAPI("/v3/pay/partner/transactions/jsapi", "/v3/combine-transactions/jsapi", TradeTypeEnum.JSAPI),
|
|
||||||
/**
|
|
||||||
* NATIVE
|
|
||||||
*/
|
|
||||||
NATIVE("/v3/pay/partner/transactions/native", "/v3/combine-transactions/native", TradeTypeEnum.NATIVE),
|
|
||||||
/**
|
|
||||||
* H5
|
|
||||||
*/
|
|
||||||
H5("/v3/pay/partner/transactions/h5", "/v3/combine-transactions/h5", TradeTypeEnum.H5);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 单独下单url
|
|
||||||
*/
|
|
||||||
private final String partnerUrl;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 合并下单url
|
|
||||||
*/
|
|
||||||
private final String combineUrl;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 直连支付枚举
|
|
||||||
*/
|
|
||||||
private final TradeTypeEnum directConnTrade;
|
|
||||||
}
|
|
@ -14,19 +14,19 @@ public enum TradeTypeEnum {
|
|||||||
/**
|
/**
|
||||||
* APP
|
* APP
|
||||||
*/
|
*/
|
||||||
APP("/v3/pay/transactions/app", "/v3/combine-transactions/app"),
|
APP("/v3/pay/transactions/app", "/v3/combine-transactions/app", "/v3/pay/partner/transactions/app"),
|
||||||
/**
|
/**
|
||||||
* JSAPI 或 小程序
|
* JSAPI 或 小程序
|
||||||
*/
|
*/
|
||||||
JSAPI("/v3/pay/transactions/jsapi", "/v3/combine-transactions/jsapi"),
|
JSAPI("/v3/pay/transactions/jsapi", "/v3/combine-transactions/jsapi", "/v3/pay/partner/transactions/jsapi"),
|
||||||
/**
|
/**
|
||||||
* NATIVE
|
* NATIVE
|
||||||
*/
|
*/
|
||||||
NATIVE("/v3/pay/transactions/native", "/v3/combine-transactions/native"),
|
NATIVE("/v3/pay/transactions/native", "/v3/combine-transactions/native", "/v3/pay/partner/transactions/native"),
|
||||||
/**
|
/**
|
||||||
* H5
|
* H5
|
||||||
*/
|
*/
|
||||||
H5("/v3/pay/transactions/h5", "/v3/combine-transactions/h5");
|
H5("/v3/pay/transactions/h5", "/v3/combine-transactions/h5", "/v3/pay/partner/transactions/native");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 单独下单url
|
* 单独下单url
|
||||||
@ -37,4 +37,9 @@ public enum TradeTypeEnum {
|
|||||||
* 合并下单url
|
* 合并下单url
|
||||||
*/
|
*/
|
||||||
private final String combineUrl;
|
private final String combineUrl;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 服务商下单
|
||||||
|
*/
|
||||||
|
private final String basePartnerUrl;
|
||||||
}
|
}
|
||||||
|
@ -5,14 +5,12 @@ import com.github.binarywang.wxpay.bean.coupon.*;
|
|||||||
import com.github.binarywang.wxpay.bean.notify.*;
|
import com.github.binarywang.wxpay.bean.notify.*;
|
||||||
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.bean.result.enums.PartnerTradeTypeEnum;
|
|
||||||
import com.github.binarywang.wxpay.bean.result.enums.TradeTypeEnum;
|
import com.github.binarywang.wxpay.bean.result.enums.TradeTypeEnum;
|
||||||
import com.github.binarywang.wxpay.config.WxPayConfig;
|
import com.github.binarywang.wxpay.config.WxPayConfig;
|
||||||
import com.github.binarywang.wxpay.constant.WxPayConstants;
|
import com.github.binarywang.wxpay.constant.WxPayConstants;
|
||||||
import com.github.binarywang.wxpay.exception.WxPayException;
|
import com.github.binarywang.wxpay.exception.WxPayException;
|
||||||
import org.apache.http.client.methods.HttpPost;
|
import org.apache.http.client.methods.HttpPost;
|
||||||
import org.apache.http.client.methods.HttpRequestBase;
|
import org.apache.http.client.methods.HttpRequestBase;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
@ -641,7 +639,7 @@ public interface WxPayService {
|
|||||||
* @return 返回 {@link com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderV3Result}里的内部类或字段
|
* @return 返回 {@link com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderV3Result}里的内部类或字段
|
||||||
* @throws WxPayException the wx pay exception
|
* @throws WxPayException the wx pay exception
|
||||||
*/
|
*/
|
||||||
<T> T createPartnerOrderV3(PartnerTradeTypeEnum tradeType, WxPayPartnerUnifiedOrderV3Request request) throws WxPayException;
|
<T> T createPartnerOrderV3(TradeTypeEnum tradeType, WxPayPartnerUnifiedOrderV3Request request) throws WxPayException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 在发起微信支付前,需要调用统一下单接口,获取"预支付交易会话标识"
|
* 在发起微信支付前,需要调用统一下单接口,获取"预支付交易会话标识"
|
||||||
@ -649,8 +647,9 @@ public interface WxPayService {
|
|||||||
* @param tradeType the trade type
|
* @param tradeType the trade type
|
||||||
* @param request 请求对象,注意一些参数如spAppid、spMchid等不用设置,方法内会自动从配置对象中获取到(前提是对应配置中已经设置)
|
* @param request 请求对象,注意一些参数如spAppid、spMchid等不用设置,方法内会自动从配置对象中获取到(前提是对应配置中已经设置)
|
||||||
* @return the wx pay unified order result
|
* @return the wx pay unified order result
|
||||||
|
* @throws WxPayException the wx pay exception
|
||||||
*/
|
*/
|
||||||
WxPayUnifiedOrderV3Result unifiedPartnerOrderV3(PartnerTradeTypeEnum tradeType, WxPayPartnerUnifiedOrderV3Request request) throws WxPayException;
|
WxPayUnifiedOrderV3Result unifiedPartnerOrderV3(TradeTypeEnum tradeType, WxPayPartnerUnifiedOrderV3Request request) throws WxPayException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 在发起微信支付前,需要调用统一下单接口,获取"预支付交易会话标识"
|
* 在发起微信支付前,需要调用统一下单接口,获取"预支付交易会话标识"
|
||||||
|
@ -10,7 +10,6 @@ import com.github.binarywang.wxpay.bean.order.WxPayMwebOrderResult;
|
|||||||
import com.github.binarywang.wxpay.bean.order.WxPayNativeOrderResult;
|
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.bean.result.enums.PartnerTradeTypeEnum;
|
|
||||||
import com.github.binarywang.wxpay.bean.result.enums.TradeTypeEnum;
|
import com.github.binarywang.wxpay.bean.result.enums.TradeTypeEnum;
|
||||||
import com.github.binarywang.wxpay.config.WxPayConfig;
|
import com.github.binarywang.wxpay.config.WxPayConfig;
|
||||||
import com.github.binarywang.wxpay.config.WxPayConfigHolder;
|
import com.github.binarywang.wxpay.config.WxPayConfigHolder;
|
||||||
@ -712,15 +711,15 @@ public abstract class BaseWxPayServiceImpl implements WxPayService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> T createPartnerOrderV3(PartnerTradeTypeEnum tradeType, WxPayPartnerUnifiedOrderV3Request request) throws WxPayException {
|
public <T> T createPartnerOrderV3(TradeTypeEnum tradeType, WxPayPartnerUnifiedOrderV3Request request) throws WxPayException {
|
||||||
WxPayUnifiedOrderV3Result result = this.unifiedPartnerOrderV3(tradeType, request);
|
WxPayUnifiedOrderV3Result result = this.unifiedPartnerOrderV3(tradeType, request);
|
||||||
//获取应用ID
|
//获取应用ID
|
||||||
String appId = StringUtils.isBlank(request.getSubAppid()) ? request.getSpAppid() : request.getSubAppid();
|
String appId = StringUtils.isBlank(request.getSubAppid()) ? request.getSpAppid() : request.getSubAppid();
|
||||||
return result.getPayInfo(tradeType.getDirectConnTrade(), appId, request.getSubMchId(), this.getConfig().getPrivateKey());
|
return result.getPayInfo(tradeType, appId, request.getSubMchId(), this.getConfig().getPrivateKey());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public WxPayUnifiedOrderV3Result unifiedPartnerOrderV3(PartnerTradeTypeEnum tradeType, WxPayPartnerUnifiedOrderV3Request request) throws WxPayException {
|
public WxPayUnifiedOrderV3Result unifiedPartnerOrderV3(TradeTypeEnum tradeType, WxPayPartnerUnifiedOrderV3Request request) throws WxPayException {
|
||||||
if (StringUtils.isBlank(request.getSpAppid())) {
|
if (StringUtils.isBlank(request.getSpAppid())) {
|
||||||
request.setSpAppid(this.getConfig().getAppId());
|
request.setSpAppid(this.getConfig().getAppId());
|
||||||
}
|
}
|
||||||
@ -741,7 +740,7 @@ public abstract class BaseWxPayServiceImpl implements WxPayService {
|
|||||||
request.setSubMchId(this.getConfig().getSubMchId());
|
request.setSubMchId(this.getConfig().getSubMchId());
|
||||||
}
|
}
|
||||||
|
|
||||||
String url = this.getPayBaseUrl() + tradeType.getPartnerUrl();
|
String url = this.getPayBaseUrl() + tradeType.getBasePartnerUrl();
|
||||||
String response = this.postV3(url, GSON.toJson(request));
|
String response = this.postV3(url, GSON.toJson(request));
|
||||||
return GSON.fromJson(response, WxPayUnifiedOrderV3Result.class);
|
return GSON.fromJson(response, WxPayUnifiedOrderV3Result.class);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user