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

优化服务商模式下下单代码,去掉部分重复代码 #3070

Merged
merged 9 commits into from
Jul 3, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,34 @@ public static class SubOrders implements Serializable {
*/
@SerializedName(value = "out_trade_no")
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>
* 字段名:订单金额
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,32 @@ public static class SubOrders implements Serializable {
*/
@SerializedName(value = "out_trade_no")
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>
* 字段名:商品描述
Expand All @@ -264,6 +290,21 @@ public static class SubOrders implements Serializable {
*/
@SerializedName(value = "settle_info")
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
Expand All @@ -283,6 +324,21 @@ public static class CombinePayerInfo implements Serializable {
*/
@SerializedName(value = "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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
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.v3.util.SignUtils;
import com.google.gson.annotations.SerializedName;
Expand Down Expand Up @@ -133,32 +132,4 @@ public <T> T getPayInfo(TradeTypeEnum tradeType, String appId, String mchId, Pri
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("不支持的支付类型");
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ public enum TradeTypeEnum {
/**
* 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("/v3/pay/transactions/jsapi", "/v3/combine-transactions/jsapi"),
JSAPI("/v3/pay/transactions/jsapi", "/v3/combine-transactions/jsapi", "/v3/pay/partner/transactions/jsapi"),
/**
* 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("/v3/pay/transactions/h5", "/v3/combine-transactions/h5");
H5("/v3/pay/transactions/h5", "/v3/combine-transactions/h5", "/v3/pay/partner/transactions/native");

/**
* 单独下单url
Expand All @@ -37,4 +37,9 @@ public enum TradeTypeEnum {
* 合并下单url
*/
private final String combineUrl;

/**
* 服务商下单
*/
private final String basePartnerUrl;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
import com.github.binarywang.wxpay.bean.notify.*;
import com.github.binarywang.wxpay.bean.request.*;
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.config.WxPayConfig;
import com.github.binarywang.wxpay.constant.WxPayConstants;
import com.github.binarywang.wxpay.exception.WxPayException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;

import java.io.File;
import java.io.InputStream;
import java.util.Date;
Expand Down Expand Up @@ -641,16 +639,17 @@ public interface WxPayService {
* @return 返回 {@link com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderV3Result}里的内部类或字段
* @throws WxPayException the wx pay exception
*/
<T> T createPartnerOrderV3(PartnerTradeTypeEnum tradeType, WxPayPartnerUnifiedOrderV3Request request) throws WxPayException;
<T> T createPartnerOrderV3(TradeTypeEnum tradeType, WxPayPartnerUnifiedOrderV3Request request) throws WxPayException;

/**
* 在发起微信支付前,需要调用统一下单接口,获取"预支付交易会话标识"
*
* @param tradeType the trade type
* @param request 请求对象,注意一些参数如spAppid、spMchid等不用设置,方法内会自动从配置对象中获取到(前提是对应配置中已经设置)
* @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;

/**
* 在发起微信支付前,需要调用统一下单接口,获取"预支付交易会话标识"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
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.bean.result.enums.PartnerTradeTypeEnum;
import com.github.binarywang.wxpay.bean.result.enums.TradeTypeEnum;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.config.WxPayConfigHolder;
Expand Down Expand Up @@ -712,15 +711,15 @@ public <T> T createOrderV3(TradeTypeEnum tradeType, WxPayUnifiedOrderV3Request r
}

@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);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

7% of developers fix this issue

THREAD_SAFETY_VIOLATION: Read/Write race. Non-private method BaseWxPayServiceImpl.createPartnerOrderV3(...) indirectly reads without synchronization from this.configMap. Potentially races with write in method BaseWxPayServiceImpl.addConfig(...).
Reporting because another access to the same memory occurs on a background thread, although this access may not.


ℹ️ Expand to see all @sonatype-lift commands

You can reply with the following commands. For example, reply with @sonatype-lift ignoreall to leave out all findings.

Command Usage
@sonatype-lift ignore Leave out the above finding from this PR
@sonatype-lift ignoreall Leave out all the existing findings from this PR
@sonatype-lift exclude <file|issue|path|tool> Exclude specified file|issue|path|tool from Lift findings by updating your config.toml file

Note: When talking to LiftBot, you need to refresh the page to see its response.
Click here to add LiftBot to another repo.

//获取应用ID
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
public WxPayUnifiedOrderV3Result unifiedPartnerOrderV3(PartnerTradeTypeEnum tradeType, WxPayPartnerUnifiedOrderV3Request request) throws WxPayException {
public WxPayUnifiedOrderV3Result unifiedPartnerOrderV3(TradeTypeEnum tradeType, WxPayPartnerUnifiedOrderV3Request request) throws WxPayException {
if (StringUtils.isBlank(request.getSpAppid())) {
request.setSpAppid(this.getConfig().getAppId());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

7% of developers fix this issue

THREAD_SAFETY_VIOLATION: Read/Write race. Non-private method BaseWxPayServiceImpl.unifiedPartnerOrderV3(...) indirectly reads without synchronization from this.configMap. Potentially races with write in method BaseWxPayServiceImpl.addConfig(...).
Reporting because another access to the same memory occurs on a background thread, although this access may not.


ℹ️ Expand to see all @sonatype-lift commands

You can reply with the following commands. For example, reply with @sonatype-lift ignoreall to leave out all findings.

Command Usage
@sonatype-lift ignore Leave out the above finding from this PR
@sonatype-lift ignoreall Leave out all the existing findings from this PR
@sonatype-lift exclude <file|issue|path|tool> Exclude specified file|issue|path|tool from Lift findings by updating your config.toml file

Note: When talking to LiftBot, you need to refresh the page to see its response.
Click here to add LiftBot to another repo.

}
Expand All @@ -741,7 +740,7 @@ public WxPayUnifiedOrderV3Result unifiedPartnerOrderV3(PartnerTradeTypeEnum trad
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));
return GSON.fromJson(response, WxPayUnifiedOrderV3Result.class);
}
Expand Down