Skip to content

Commit 8619430

Browse files
committed
添加构造支付参数的重载方法,准备替换原有方法
1 parent b7e8f21 commit 8619430

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpPayService.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,18 @@ WxUnifiedOrderResult unifiedOrder(WxUnifiedOrderRequest request)
6565
* 详见http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115&token=&lang=zh_CN
6666
*
6767
* @param parameters the required or optional parameters
68+
* @deprecated use me.chanjar.weixin.mp.api.WxMpPayService.getPayInfo(WxUnifiedOrderRequest) instead.
6869
*/
70+
@Deprecated
6971
Map<String, String> getPayInfo(Map<String, String> parameters) throws WxErrorException;
7072

73+
/**
74+
* 该接口调用“统一下单”接口,并拼装发起支付请求需要的参数
75+
* 详见http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115&token=&lang=zh_CN
76+
*
77+
*/
78+
Map<String, String> getPayInfo(WxUnifiedOrderRequest request) throws WxErrorException;
79+
7180
/**
7281
* 该接口调用“统一下单”接口,并拼装NATIVE发起支付请求需要的参数
7382
* 详见http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115&token=&lang=zh_CN
@@ -79,7 +88,7 @@ WxUnifiedOrderResult unifiedOrder(WxUnifiedOrderRequest request)
7988
* @param body 商品描述
8089
* @param ip 发起支付的客户端IP
8190
* @param notifyUrl 通知地址
82-
* @deprecated Use me.chanjar.weixin.mp.api.WxMpPayService.getPayInfo(Map<String, String>) instead
91+
* @deprecated Use me.chanjar.weixin.mp.api.WxMpPayService.getPayInfo(WxUnifiedOrderRequest) instead
8392
*/
8493
@Deprecated
8594
Map<String, String> getNativePayInfo(String productId, String outTradeNo, double amt, String body, String ip, String notifyUrl) throws WxErrorException;
@@ -95,7 +104,7 @@ WxUnifiedOrderResult unifiedOrder(WxUnifiedOrderRequest request)
95104
* @param body 商品描述
96105
* @param ip 发起支付的客户端IP
97106
* @param notifyUrl 通知地址
98-
* @deprecated Use me.chanjar.weixin.mp.api.WxMpPayService.getPayInfo(Map<String, String>) instead
107+
* @deprecated Use me.chanjar.weixin.mp.api.WxMpPayService.getPayInfo(WxUnifiedOrderRequest) instead
99108
*/
100109
@Deprecated
101110
Map<String, String> getJsapiPayInfo(String openId, String outTradeNo, double amt, String body, String ip, String notifyUrl) throws WxErrorException;

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpPayServiceImpl.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.TreeMap;
1010

1111
import org.apache.commons.codec.digest.DigestUtils;
12+
import org.apache.commons.lang3.StringUtils;
1213
import org.joor.Reflect;
1314
import org.slf4j.Logger;
1415
import org.slf4j.LoggerFactory;
@@ -168,6 +169,7 @@ public Map<String, String> getNativePayInfo(String productId,
168169
}
169170

170171
@Override
172+
@Deprecated
171173
public Map<String, String> getPayInfo(Map<String, String> parameters)
172174
throws WxErrorException {
173175
WxMpPrepayIdResult wxMpPrepayIdResult = getPrepayId(parameters);
@@ -477,7 +479,7 @@ private void checkParameters(WxUnifiedOrderRequest request) {
477479

478480
if (!TRADE_TYPES.contains(request.getTradeType())) {
479481
throw new IllegalArgumentException(
480-
"trade_type目前必须为" + TRADE_TYPES + "其中之一");
482+
"trade_type目前必须为" + TRADE_TYPES + "其中之一");
481483

482484
}
483485

@@ -491,4 +493,39 @@ private void checkParameters(WxUnifiedOrderRequest request) {
491493
}
492494
}
493495

496+
@Override
497+
public Map<String, String> getPayInfo(WxUnifiedOrderRequest request) throws WxErrorException {
498+
WxUnifiedOrderResult unifiedOrderResult = this.unifiedOrder(request);
499+
500+
if (!"SUCCESS".equalsIgnoreCase(unifiedOrderResult.getReturnCode())
501+
|| !"SUCCESS".equalsIgnoreCase(unifiedOrderResult.getResultCode())) {
502+
throw new WxErrorException(WxError.newBuilder().setErrorCode(-1)
503+
.setErrorMsg("return_code:" + unifiedOrderResult.getReturnCode() + ";return_msg:"
504+
+ unifiedOrderResult.getReturnMsg() + ";result_code:" + unifiedOrderResult.getResultCode() + ";err_code"
505+
+ unifiedOrderResult.getErrCode() + ";err_code_des" + unifiedOrderResult.getErrCodeDes())
506+
.build());
507+
}
508+
509+
String prepayId = unifiedOrderResult.getPrepayId();
510+
if (StringUtils.isBlank(prepayId)) {
511+
throw new RuntimeException(String.format("Failed to get prepay id due to error code '%s'(%s).",
512+
unifiedOrderResult.getErrCode(), unifiedOrderResult.getErrCodeDes()));
513+
}
514+
515+
Map<String, String> payInfo = new HashMap<>();
516+
payInfo.put("appId", this.wxMpService.getWxMpConfigStorage().getAppId());
517+
// 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
518+
payInfo.put("timeStamp", String.valueOf(System.currentTimeMillis() / 1000));
519+
payInfo.put("nonceStr", System.currentTimeMillis() + "");
520+
payInfo.put("package", "prepay_id=" + prepayId);
521+
payInfo.put("signType", "MD5");
522+
if ("NATIVE".equals(request.getTradeType())) {
523+
payInfo.put("codeUrl", unifiedOrderResult.getCodeURL());
524+
}
525+
526+
String finalSign = this.createSign(payInfo, this.wxMpService.getWxMpConfigStorage().getPartnerKey());
527+
payInfo.put("paySign", finalSign);
528+
return payInfo;
529+
}
530+
494531
}

0 commit comments

Comments
 (0)