Skip to content

Commit 7b3a3a4

Browse files
committed
Merge branch 'develop'
2 parents 293ef25 + 5e7d1eb commit 7b3a3a4

File tree

12 files changed

+151
-58
lines changed

12 files changed

+151
-58
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ weixin-java-tools
1717
<dependency>
1818
<groupId>me.chanjar</groupId>
1919
<artifactId>weixin-java-mp</artifactId>
20-
<version>1.3.0</version>
20+
<version>1.3.1</version>
2121
</dependency>
2222
```
2323

@@ -27,7 +27,7 @@ weixin-java-tools
2727
<dependency>
2828
<groupId>me.chanjar</groupId>
2929
<artifactId>weixin-java-cp</artifactId>
30-
<version>1.3.0</version>
30+
<version>1.3.1</version>
3131
</dependency>
3232
```
3333

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<modelVersion>4.0.0</modelVersion>
66
<groupId>me.chanjar</groupId>
77
<artifactId>weixin-java-parent</artifactId>
8-
<version>1.3.0</version>
8+
<version>1.3.1</version>
99
<packaging>pom</packaging>
1010
<name>WeiXin Java Tools - Parent</name>
1111
<description>微信公众号、企业号上级POM</description>

weixin-java-common/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>me.chanjar</groupId>
88
<artifactId>weixin-java-parent</artifactId>
9-
<version>1.3.0</version>
9+
<version>1.3.1</version>
1010
</parent>
1111

1212
<artifactId>weixin-java-common</artifactId>

weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/MediaUploadRequestExecutor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.apache.http.client.methods.CloseableHttpResponse;
1212
import org.apache.http.client.methods.HttpPost;
1313
import org.apache.http.entity.ContentType;
14+
import org.apache.http.entity.mime.HttpMultipartMode;
1415
import org.apache.http.entity.mime.MultipartEntityBuilder;
1516

1617
import me.chanjar.weixin.common.bean.result.WxError;
@@ -35,6 +36,7 @@ public WxMediaUploadResult execute(CloseableHttpClient httpclient, HttpHost http
3536
HttpEntity entity = MultipartEntityBuilder
3637
.create()
3738
.addBinaryBody("media", file)
39+
.setMode(HttpMultipartMode.RFC6532)
3840
.build();
3941
httpPost.setEntity(entity);
4042
httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString());

weixin-java-cp/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>me.chanjar</groupId>
88
<artifactId>weixin-java-parent</artifactId>
9-
<version>1.3.0</version>
9+
<version>1.3.1</version>
1010
</parent>
1111

1212
<artifactId>weixin-java-cp</artifactId>

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) thro
555555
throw new RuntimeException("微信服务端异常,超出重试次数");
556556
}
557557

558-
protected <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
558+
protected synchronized <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
559559
if (uri.indexOf("access_token=") != -1) {
560560
throw new IllegalArgumentException("uri参数中不允许有access_token: " + uri);
561561
}

weixin-java-mp/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>me.chanjar</groupId>
88
<artifactId>weixin-java-parent</artifactId>
9-
<version>1.3.0</version>
9+
<version>1.3.1</version>
1010
</parent>
1111
<artifactId>weixin-java-mp</artifactId>
1212
<name>WeiXin Java Tools - MP</name>

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

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -671,33 +671,57 @@ public interface WxMpService {
671671
*/
672672
void setMaxRetryTimes(int maxRetryTimes);
673673

674-
/**
675-
* 统一下单(详见http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1)
676-
* 在发起微信支付前,需要调用统一下单接口,获取"预支付交易会话标识"
677-
* @param openId 支付人openId
678-
* @param outTradeNo 商户端对应订单号
679-
* @param amt 金额(单位元)
680-
* @param body 商品描述
681-
* @param tradeType 交易类型 JSAPI,NATIVE,APP,WAP
682-
* @param ip 发起支付的客户端IP
683-
* @param notifyUrl 通知地址
684-
* @return
685-
*/
686-
WxMpPrepayIdResult getPrepayId(String openId, String outTradeNo, double amt, String body, String tradeType, String ip, String notifyUrl);
674+
/**
675+
* 统一下单(详见http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1)
676+
* 在发起微信支付前,需要调用统一下单接口,获取"预支付交易会话标识"
677+
* @param openId 支付人openId
678+
* @param outTradeNo 商户端对应订单号
679+
* @param amt 金额(单位元)
680+
* @param body 商品描述
681+
* @param tradeType 交易类型 JSAPI,NATIVE,APP,WAP
682+
* @param ip 发起支付的客户端IP
683+
* @param notifyUrl 通知地址
684+
* @return
685+
* @deprecated Use me.chanjar.weixin.mp.api.WxMpService.getPrepayId(Map<String, String>) instead
686+
*/
687+
@Deprecated
688+
WxMpPrepayIdResult getPrepayId(String openId, String outTradeNo, double amt, String body, String tradeType, String ip, String notifyUrl);
687689

688-
/**
689-
* 该接口调用“统一下单”接口,并拼装JSSDK发起支付请求需要的参数
690-
* 详见http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E5.8F.91.E8.B5.B7.E4.B8.80.E4.B8.AA.E5.BE.AE.E4.BF.A1.E6.94.AF.E4.BB.98.E8.AF.B7.E6.B1.82
691-
* @param openId 支付人openId
692-
* @param outTradeNo 商户端对应订单号
693-
* @param amt 金额(单位元)
694-
* @param body 商品描述
695-
* @param tradeType 交易类型 JSAPI,NATIVE,APP,WAP
696-
* @param ip 发起支付的客户端IP
697-
* @param notifyUrl 通知地址
698-
* @return
699-
*/
700-
Map<String, String> getJSSDKPayInfo(String openId, String outTradeNo, double amt, String body, String tradeType, String ip, String notifyUrl);
690+
/**
691+
* 统一下单(详见http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1)
692+
* 在发起微信支付前,需要调用统一下单接口,获取"预支付交易会话标识"
693+
*
694+
* @param parameters
695+
* All required/optional parameters for weixin payment
696+
* @return
697+
* @throws IllegalArgumentException
698+
*/
699+
WxMpPrepayIdResult getPrepayId(Map<String, String> parameters);
700+
701+
/**
702+
* 该接口调用“统一下单”接口,并拼装JSSDK发起支付请求需要的参数
703+
* 详见http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E5.8F.91.E8.B5.B7.E4.B8.80.E4.B8.AA.E5.BE.AE.E4.BF.A1.E6.94.AF.E4.BB.98.E8.AF.B7.E6.B1.82
704+
* @param parameters
705+
* the required or optional parameters
706+
* @return
707+
*/
708+
Map<String, String> getJSSDKPayInfo(Map<String, String> parameters);
709+
710+
/**
711+
* 该接口调用“统一下单”接口,并拼装JSSDK发起支付请求需要的参数
712+
* 详见http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E5.8F.91.E8.B5.B7.E4.B8.80.E4.B8.AA.E5.BE.AE.E4.BF.A1.E6.94.AF.E4.BB.98.E8.AF.B7.E6.B1.82
713+
* @param openId 支付人openId
714+
* @param outTradeNo 商户端对应订单号
715+
* @param amt 金额(单位元)
716+
* @param body 商品描述
717+
* @param tradeType 交易类型 JSAPI,NATIVE,APP,WAP
718+
* @param ip 发起支付的客户端IP
719+
* @param notifyUrl 通知地址
720+
* @return
721+
* @deprecated Use me.chanjar.weixin.mp.api.WxMpService.getJSSDKPayInfo(Map<String, String>) instead
722+
*/
723+
@Deprecated
724+
Map<String, String> getJSSDKPayInfo(String openId, String outTradeNo, double amt, String body, String tradeType, String ip, String notifyUrl);
701725

702726
/**
703727
* 该接口提供所有微信支付订单的查询,当支付通知处理异常戒丢失的情冴,商户可以通过该接口查询订单支付状态。

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

Lines changed: 57 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.HashMap;
1212
import java.util.List;
1313
import java.util.Map;
14+
import java.util.Map.Entry;
1415
import java.util.SortedMap;
1516
import java.util.TreeMap;
1617
import java.util.UUID;
@@ -634,8 +635,9 @@ public List<WxMpUserSummary> getUserSummary(Date beginDate, Date endDate) throws
634635
param.addProperty("end_date", SIMPLE_DATE_FORMAT.format(endDate));
635636
String responseContent = post(url, param.toString());
636637
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
637-
return WxMpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement.getAsJsonObject().get("list"), new TypeToken<List<WxMpUserSummary>>() {
638-
}.getType());
638+
return WxMpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement.getAsJsonObject().get("list"),
639+
new TypeToken<List<WxMpUserSummary>>() {
640+
}.getType());
639641
}
640642

641643
@Override
@@ -695,7 +697,7 @@ public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) thro
695697
throw new RuntimeException("微信服务端异常,超出重试次数");
696698
}
697699

698-
protected <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
700+
protected synchronized <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
699701
if (uri.indexOf("access_token=") != -1) {
700702
throw new IllegalArgumentException("uri参数中不允许有access_token: " + uri);
701703
}
@@ -776,43 +778,45 @@ public void setMaxRetryTimes(int maxRetryTimes) {
776778

777779
@Override
778780
public WxMpPrepayIdResult getPrepayId(String openId, String outTradeNo, double amt, String body, String tradeType, String ip, String callbackUrl) {
779-
String nonce_str = System.currentTimeMillis() + "";
780-
781-
SortedMap<String, String> packageParams = new TreeMap<String, String>();
781+
Map<String, String> packageParams = new HashMap<String, String>();
782782
packageParams.put("appid", wxMpConfigStorage.getAppId());
783783
packageParams.put("mch_id", wxMpConfigStorage.getPartnerId());
784-
packageParams.put("nonce_str", nonce_str);
785784
packageParams.put("body", body);
786785
packageParams.put("out_trade_no", outTradeNo);
787-
788786
packageParams.put("total_fee", (int) (amt * 100) + "");
789787
packageParams.put("spbill_create_ip", ip);
790788
packageParams.put("notify_url", callbackUrl);
791789
packageParams.put("trade_type", tradeType);
792790
packageParams.put("openid", openId);
793791

792+
return getPrepayId(packageParams);
793+
}
794+
795+
public WxMpPrepayIdResult getPrepayId(final Map<String, String> parameters) {
796+
String nonce_str = System.currentTimeMillis() + "";
797+
798+
final SortedMap<String, String> packageParams = new TreeMap<String, String>(parameters);
799+
packageParams.put("appid", wxMpConfigStorage.getAppId());
800+
packageParams.put("mch_id", wxMpConfigStorage.getPartnerId());
801+
packageParams.put("nonce_str", nonce_str);
802+
checkParameters(packageParams);
803+
794804
String sign = WxCryptUtil.createSign(packageParams, wxMpConfigStorage.getPartnerKey());
795-
String xml = "<xml>" +
796-
"<appid>" + wxMpConfigStorage.getAppId() + "</appid>" +
797-
"<mch_id>" + wxMpConfigStorage.getPartnerId() + "</mch_id>" +
798-
"<nonce_str>" + nonce_str + "</nonce_str>" +
799-
"<sign>" + sign + "</sign>" +
800-
"<body><![CDATA[" + body + "]]></body>" +
801-
"<out_trade_no>" + outTradeNo + "</out_trade_no>" +
802-
"<total_fee>" + packageParams.get("total_fee") + "</total_fee>" +
803-
"<spbill_create_ip>" + ip + "</spbill_create_ip>" +
804-
"<notify_url>" + callbackUrl + "</notify_url>" +
805-
"<trade_type>" + tradeType + "</trade_type>" +
806-
"<openid>" + openId + "</openid>" +
807-
"</xml>";
805+
packageParams.put("sign", sign);
806+
807+
StringBuilder request = new StringBuilder("<xml>");
808+
for (Entry<String, String> para : packageParams.entrySet()) {
809+
request.append(String.format("<%s>%s</%s>", para.getKey(), para.getValue(), para.getKey()));
810+
}
811+
request.append("</xml>");
808812

809813
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/pay/unifiedorder");
810814
if (httpProxy != null) {
811815
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
812816
httpPost.setConfig(config);
813817
}
814818

815-
StringEntity entity = new StringEntity(xml, Consts.UTF_8);
819+
StringEntity entity = new StringEntity(request.toString(), Consts.UTF_8);
816820
httpPost.setEntity(entity);
817821
try {
818822
CloseableHttpResponse response = getHttpclient().execute(httpPost);
@@ -827,9 +831,39 @@ public WxMpPrepayIdResult getPrepayId(String openId, String outTradeNo, double a
827831
return new WxMpPrepayIdResult();
828832
}
829833

834+
final String[] REQUIRED_ORDER_PARAMETERS = new String[] { "appid", "mch_id", "body", "out_trade_no", "total_fee", "spbill_create_ip", "notify_url",
835+
"trade_type", };
836+
837+
private void checkParameters(Map<String, String> parameters) {
838+
for (String para : REQUIRED_ORDER_PARAMETERS) {
839+
if (!parameters.containsKey(para))
840+
throw new IllegalArgumentException("Reqiured argument '" + para + "' is missing.");
841+
}
842+
if ("JSAPI".equals(parameters.get("trade_type")) && !parameters.containsKey("openid"))
843+
throw new IllegalArgumentException("Reqiured argument 'openid' is missing when trade_type is 'JSAPI'.");
844+
if ("NATIVE".equals(parameters.get("trade_type")) && !parameters.containsKey("product_id"))
845+
throw new IllegalArgumentException("Reqiured argument 'product_id' is missing when trade_type is 'NATIVE'.");
846+
}
847+
830848
@Override
831849
public Map<String, String> getJSSDKPayInfo(String openId, String outTradeNo, double amt, String body, String tradeType, String ip, String callbackUrl) {
832-
WxMpPrepayIdResult wxMpPrepayIdResult = getPrepayId(openId, outTradeNo, amt, body, tradeType, ip, callbackUrl);
850+
Map<String, String> packageParams = new HashMap<String, String>();
851+
packageParams.put("appid", wxMpConfigStorage.getAppId());
852+
packageParams.put("mch_id", wxMpConfigStorage.getPartnerId());
853+
packageParams.put("body", body);
854+
packageParams.put("out_trade_no", outTradeNo);
855+
packageParams.put("total_fee", (int) (amt * 100) + "");
856+
packageParams.put("spbill_create_ip", ip);
857+
packageParams.put("notify_url", callbackUrl);
858+
packageParams.put("trade_type", tradeType);
859+
packageParams.put("openid", openId);
860+
861+
return getJSSDKPayInfo(packageParams);
862+
}
863+
864+
@Override
865+
public Map<String, String> getJSSDKPayInfo(Map<String, String> parameters) {
866+
WxMpPrepayIdResult wxMpPrepayIdResult = getPrepayId(parameters);
833867
String prepayId = wxMpPrepayIdResult.getPrepay_id();
834868
if (prepayId == null || prepayId.equals("")) {
835869
throw new RuntimeException("get prepayid error");

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/result/WxMpPrepayIdResult.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ public class WxMpPrepayIdResult implements Serializable {
2222
private String result_code;
2323
private String prepay_id;
2424
private String trade_type;
25+
private String err_code;
26+
private String err_code_des;
27+
private String code_url;
2528

2629
public String getReturn_code() {
2730
return return_code;
@@ -94,4 +97,28 @@ public String getTrade_type() {
9497
public void setTrade_type(String trade_type) {
9598
this.trade_type = trade_type;
9699
}
100+
101+
public String getErr_code() {
102+
return err_code;
103+
}
104+
105+
public void setErr_code(String err_code) {
106+
this.err_code = err_code;
107+
}
108+
109+
public String getErr_code_des() {
110+
return err_code_des;
111+
}
112+
113+
public void setErr_code_des(String err_code_des) {
114+
this.err_code_des = err_code_des;
115+
}
116+
117+
public String getCode_url() {
118+
return code_url;
119+
}
120+
121+
public void setCode_url(String code_url) {
122+
this.code_url = code_url;
123+
}
97124
}

0 commit comments

Comments
 (0)