Skip to content

Commit 5231f8c

Browse files
committed
优化代码
1 parent 05a680c commit 5231f8c

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceAbstractImpl.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -213,14 +213,13 @@ public <T> T createOrder(WxPayUnifiedOrderRequest request) throws WxPayException
213213

214214
String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
215215
String nonceStr = String.valueOf(System.currentTimeMillis());
216-
Object payResult = null;
217216
switch (request.getTradeType()) {
218217
case TradeType.NATIVE: {
219-
payResult = WxPayNativeOrderResult.builder()
218+
return (T) WxPayNativeOrderResult.builder()
220219
.codeUrl(unifiedOrderResult.getCodeURL())
221220
.build();
222-
break;
223221
}
222+
224223
case TradeType.APP: {
225224
// APP支付绑定的是微信开放平台上的账号,APPID为开放平台上绑定APP后发放的参数
226225
String appId = this.getConfig().getAppId();
@@ -235,7 +234,7 @@ public <T> T createOrder(WxPayUnifiedOrderRequest request) throws WxPayException
235234
configMap.put("noncestr", nonceStr);
236235
configMap.put("appid", appId);
237236

238-
payResult = WxPayAppOrderResult.builder()
237+
return (T) WxPayAppOrderResult.builder()
239238
.sign(SignUtils.createSign(configMap, null, this.getConfig().getMchKey(), false))
240239
.prepayId(prepayId)
241240
.partnerId(partnerId)
@@ -244,26 +243,33 @@ public <T> T createOrder(WxPayUnifiedOrderRequest request) throws WxPayException
244243
.timeStamp(timestamp)
245244
.nonceStr(nonceStr)
246245
.build();
247-
break;
248246
}
247+
249248
case TradeType.JSAPI: {
250-
payResult = WxPayMpOrderResult.builder()
249+
String signType = SignType.MD5;
250+
WxPayMpOrderResult payResult = WxPayMpOrderResult.builder()
251251
.appId(unifiedOrderResult.getAppid())
252252
.timeStamp(timestamp)
253253
.nonceStr(nonceStr)
254254
.packageValue("prepay_id=" + prepayId)
255-
.signType(SignType.MD5)
255+
.signType(signType)
256256
.build();
257-
((WxPayMpOrderResult) payResult)
258-
.setPaySign(SignUtils.createSign(payResult, null, this.getConfig().getMchKey(), false));
259-
break;
257+
258+
payResult.setPaySign(
259+
SignUtils.createSign(
260+
payResult,
261+
signType,
262+
this.getConfig().getMchKey(),
263+
false)
264+
);
265+
return (T) payResult;
260266
}
267+
261268
default: {
262269
throw new WxPayException("该交易类型暂不支持");
263270
}
264271
}
265272

266-
return (T) payResult;
267273
}
268274

269275
@Override

0 commit comments

Comments
 (0)