Skip to content

Commit 7daeff2

Browse files
committed
微信支付接口抽取部分常量
1 parent 630a4d5 commit 7daeff2

File tree

4 files changed

+157
-65
lines changed

4 files changed

+157
-65
lines changed

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxPayDownloadBillRequest.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.binarywang.wxpay.bean.request;
22

3+
import com.github.binarywang.wxpay.constant.WxPayConstants.BillType;
34
import com.github.binarywang.wxpay.exception.WxPayException;
45
import com.thoughtworks.xstream.annotations.XStreamAlias;
56
import me.chanjar.weixin.common.annotation.Required;
@@ -17,7 +18,8 @@
1718
*/
1819
@XStreamAlias("xml")
1920
public class WxPayDownloadBillRequest extends WxPayBaseRequest {
20-
private static final String[] BILL_TYPE = new String[]{"ALL", "REFUND", "SUCCESS"};
21+
private static final String[] BILL_TYPES = new String[]{BillType.ALL, BillType.SUCCESS, BillType.REFUND, BillType.RECHARGE_REFUND};
22+
private static final String TAR_TYPE_GZIP = "GZIP";
2123

2224
/**
2325
* <pre>
@@ -130,13 +132,13 @@ public void setTarType(String tarType) {
130132

131133
@Override
132134
protected void checkConstraints() throws WxPayException {
133-
if (StringUtils.isNotBlank(this.getTarType()) && !"GZIP".equals(this.getTarType())) {
135+
if (StringUtils.isNotBlank(this.getTarType()) && !TAR_TYPE_GZIP.equals(this.getTarType())) {
134136
throw new WxPayException("tar_type值如果存在,只能为GZIP");
135137
}
136138

137-
if (!ArrayUtils.contains(BILL_TYPE, this.getBillType())) {
138-
throw new WxPayException(String.format("bill_tpye目前必须为%s其中之一,实际值:%s",
139-
Arrays.toString(BILL_TYPE), this.getBillType()));
139+
if (!ArrayUtils.contains(BILL_TYPES, this.getBillType())) {
140+
throw new WxPayException(String.format("bill_type目前必须为%s其中之一,实际值:%s",
141+
Arrays.toString(BILL_TYPES), this.getBillType()));
140142
}
141143
}
142144
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.github.binarywang.wxpay.constant;
2+
3+
/**
4+
* <pre>
5+
* 微信支付常量类
6+
* Created by Binary Wang on 2017-8-24.
7+
* </pre>
8+
*
9+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
10+
*/
11+
public class WxPayConstants {
12+
/**
13+
* 校验用户姓名选项,企业付款时使用
14+
*/
15+
public static class CheckNameOption {
16+
/**
17+
* 不校验真实姓名
18+
*/
19+
public static final String NO_CHECK = "NO_CHECK";
20+
21+
/**
22+
* 强校验真实姓名
23+
*/
24+
public static final String FORCE_CHECK = "FORCE_CHECK";
25+
}
26+
27+
/**
28+
* 订单类型
29+
*/
30+
public static class BillType {
31+
/**
32+
* 查询红包时使用:通过商户订单号获取红包信息
33+
*/
34+
public static final String MCHT = "MCHT";
35+
36+
//以下为下载对账单时的账单类型
37+
/**
38+
* 返回当日所有订单信息,默认值
39+
*/
40+
public static final String ALL = "ALL";
41+
/**
42+
* 返回当日成功支付的订单
43+
*/
44+
public static final String SUCCESS = "SUCCESS";
45+
/**
46+
* 返回当日退款订单
47+
*/
48+
public static final String REFUND = "REFUND";
49+
/**
50+
* 返回当日充值退款订单(相比其他对账单多一栏“返还手续费”)
51+
*/
52+
public static final String RECHARGE_REFUND = "RECHARGE_REFUND";
53+
}
54+
55+
/**
56+
* 交易类型
57+
*/
58+
public static class TradeType {
59+
/**
60+
* 原生扫码支付
61+
*/
62+
public static final String NATIVE = "NATIVE";
63+
64+
/**
65+
* App支付
66+
*/
67+
public static final String APP = "APP";
68+
69+
/**
70+
* 公众号支付
71+
*/
72+
public static final String JSAPI = "JSAPI";
73+
74+
/**
75+
* 刷卡支付,刷卡支付有单独的支付接口,不调用统一下单接口
76+
*/
77+
public static final String MICROPAY = "MICROPAY";
78+
}
79+
80+
/**
81+
* 签名类型
82+
*/
83+
public static class SignType {
84+
public static final String HMAC_SHA256 = "HMAC-SHA256";
85+
public static final String MD5 = "MD5";
86+
}
87+
}

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

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import com.github.binarywang.wxpay.bean.request.*;
66
import com.github.binarywang.wxpay.bean.result.*;
77
import com.github.binarywang.wxpay.config.WxPayConfig;
8+
import com.github.binarywang.wxpay.constant.WxPayConstants.BillType;
9+
import com.github.binarywang.wxpay.constant.WxPayConstants.SignType;
10+
import com.github.binarywang.wxpay.constant.WxPayConstants.TradeType;
811
import com.github.binarywang.wxpay.exception.WxPayException;
912
import com.github.binarywang.wxpay.service.WxPayService;
1013
import com.github.binarywang.wxpay.util.SignUtils;
@@ -129,7 +132,7 @@ public WxPaySendRedpackResult sendRedpack(WxPaySendRedpackRequest request) throw
129132
public WxPayRedpackQueryResult queryRedpack(String mchBillNo) throws WxPayException {
130133
WxPayRedpackQueryRequest request = new WxPayRedpackQueryRequest();
131134
request.setMchBillNo(mchBillNo);
132-
request.setBillType("MCHT");
135+
request.setBillType(BillType.MCHT);
133136
request.checkAndSign(this.getConfig());
134137

135138
String url = this.getPayBaseUrl() + "/mmpaymkttransfers/gethbinfo";
@@ -199,35 +202,36 @@ public Map<String, String> getPayInfo(WxPayUnifiedOrderRequest request) throws W
199202
Map<String, String> payInfo = new HashMap<>();
200203
String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
201204
String nonceStr = String.valueOf(System.currentTimeMillis());
202-
if ("NATIVE".equals(request.getTradeType())) {
205+
if (TradeType.NATIVE.equals(request.getTradeType())) {
203206
payInfo.put("codeUrl", unifiedOrderResult.getCodeURL());
204-
} else if ("APP".equals(request.getTradeType())) {
207+
} else if (TradeType.APP.equals(request.getTradeType())) {
205208
// APP支付绑定的是微信开放平台上的账号,APPID为开放平台上绑定APP后发放的参数
206209
String appId = getConfig().getAppId();
207210
Map<String, String> configMap = new HashMap<>();
208211
// 此map用于参与调起sdk支付的二次签名,格式全小写,timestamp只能是10位,格式固定,切勿修改
209-
String partnerid = getConfig().getMchId();
212+
String partnerId = getConfig().getMchId();
210213
configMap.put("prepayid", prepayId);
211-
configMap.put("partnerid", partnerid);
212-
configMap.put("package", "Sign=WXPay");
214+
configMap.put("partnerid", partnerId);
215+
String packageValue = "Sign=WXPay";
216+
configMap.put("package", packageValue);
213217
configMap.put("timestamp", timestamp);
214218
configMap.put("noncestr", nonceStr);
215219
configMap.put("appid", appId);
216220
// 此map用于客户端与微信服务器交互
217221
payInfo.put("sign", SignUtils.createSign(configMap, this.getConfig().getMchKey()));
218222
payInfo.put("prepayId", prepayId);
219-
payInfo.put("partnerId", partnerid);
223+
payInfo.put("partnerId", partnerId);
220224
payInfo.put("appId", appId);
221-
payInfo.put("packageValue", "Sign=WXPay");
225+
payInfo.put("packageValue", packageValue);
222226
payInfo.put("timeStamp", timestamp);
223227
payInfo.put("nonceStr", nonceStr);
224-
} else if ("JSAPI".equals(request.getTradeType())) {
228+
} else if (TradeType.JSAPI.equals(request.getTradeType())) {
225229
payInfo.put("appId", unifiedOrderResult.getAppid());
226230
// 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
227231
payInfo.put("timeStamp", timestamp);
228232
payInfo.put("nonceStr", nonceStr);
229233
payInfo.put("package", "prepay_id=" + prepayId);
230-
payInfo.put("signType", "MD5");
234+
payInfo.put("signType", SignType.MD5);
231235
payInfo.put("paySign", SignUtils.createSign(payInfo, this.getConfig().getMchKey()));
232236
}
233237

@@ -278,7 +282,6 @@ public String createScanPayQrcodeMode1(String productId) {
278282
String sign = SignUtils.createSign(params, this.getConfig().getMchKey());
279283
params.put("sign", sign);
280284

281-
282285
for (String key : params.keySet()) {
283286
codeUrl.append(key + "=" + params.get(key) + "&");
284287
}
@@ -327,8 +330,7 @@ public WxPayBillResult downloadBill(String billDate, String billType, String tar
327330
result.checkResult(this);
328331
return null;
329332
} else {
330-
WxPayBillResult wxPayBillResult = billInformationDeal(responseContent);
331-
return wxPayBillResult;
333+
return billInformationDeal(responseContent);
332334
}
333335
}
334336

@@ -346,10 +348,8 @@ private WxPayBillResult billInformationDeal(String responseContent) {
346348
* 交易时间:2017-04-06 01:00:02 公众账号ID: 商户号: 子商户号:0 设备号:WEB 微信订单号: 商户订单号:2017040519091071873216 用户标识: 交易类型:NATIVE
347349
* 交易状态:REFUND 付款银行:CFT 货币种类:CNY 总金额:0.00 企业红包金额:0.00 微信退款单号: 商户退款单号:20170406010000933 退款金额:0.01 企业红包退款金额:0.00
348350
* 退款类型:ORIGINAL 退款状态:SUCCESS 商品名称: 商户数据包: 手续费:0.00000 费率 :0.60%
351+
* 参考以上格式进行取值
349352
*/
350-
351-
// 参考以上格式进行取值
352-
353353
List<WxPayBillBaseResult> wxPayBillBaseResultLst = new LinkedList<>();
354354
String newStr = listStr.replaceAll(",", " "); // 去空格
355355
String[] tempStr = newStr.split("`"); // 数据分组
@@ -387,12 +387,11 @@ private WxPayBillResult billInformationDeal(String responseContent) {
387387
k += t.length;
388388
}
389389
wxPayBillResult.setWxPayBillBaseResultLst(wxPayBillBaseResultLst);
390+
390391
/*
391392
* 总交易单数,总交易额,总退款金额,总代金券或立减优惠退款金额,手续费总金额 `2,`0.02,`0.0,`0.0,`0
393+
* 参考以上格式进行取值
392394
*/
393-
394-
// 参考以上格式进行取值
395-
396395
String totalStr = objStr.replaceAll(",", " ");
397396
String[] totalTempStr = totalStr.split("`");
398397
wxPayBillResult.setTotalRecord(totalTempStr[1]);

weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/WxPayServiceAbstractImplTest.java

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
import com.github.binarywang.wxpay.bean.coupon.*;
55
import com.github.binarywang.wxpay.bean.request.*;
66
import com.github.binarywang.wxpay.bean.result.*;
7+
import com.github.binarywang.wxpay.constant.WxPayConstants;
8+
import com.github.binarywang.wxpay.constant.WxPayConstants.BillType;
9+
import com.github.binarywang.wxpay.constant.WxPayConstants.SignType;
10+
import com.github.binarywang.wxpay.constant.WxPayConstants.TradeType;
711
import com.github.binarywang.wxpay.exception.WxPayException;
812
import com.github.binarywang.wxpay.service.WxPayService;
913
import com.github.binarywang.wxpay.testbase.ApiTestModule;
@@ -33,23 +37,58 @@ public class WxPayServiceAbstractImplTest {
3337
@Inject
3438
private WxPayService payService;
3539

40+
/**
41+
* Test method for {@link WxPayService#unifiedOrder(WxPayUnifiedOrderRequest)}.
42+
*/
43+
@Test
44+
public void testUnifiedOrder() throws WxPayException {
45+
WxPayUnifiedOrderResult result = this.payService
46+
.unifiedOrder(WxPayUnifiedOrderRequest.newBuilder()
47+
.body("我去")
48+
.totalFee(1)
49+
.spbillCreateIp("11.1.11.1")
50+
.notifyURL("111111")
51+
.tradeType(TradeType.JSAPI)
52+
.openid(((XmlWxPayConfig) this.payService.getConfig()).getOpenid())
53+
.outTradeNo("1111112")
54+
.build());
55+
this.logger.info(result.toString());
56+
}
57+
3658
@Test
3759
public void testGetPayInfo() throws Exception {
3860
Map<String, String> payInfo = this.payService.getPayInfo(WxPayUnifiedOrderRequest.newBuilder()
3961
.body("我去")
4062
.totalFee(1)
41-
.spbillCreateIp("111111")
63+
.spbillCreateIp("1.11.1.11")
4264
.notifyURL("111111")
43-
.tradeType("JSAPI")
44-
.outTradeNo("111111")
65+
.tradeType(TradeType.JSAPI)
66+
.outTradeNo("1111113")
4567
.openid(((XmlWxPayConfig) this.payService.getConfig()).getOpenid())
4668
.build());
4769
this.logger.info(payInfo.toString());
4870
}
4971

72+
/**
73+
* Test method for {@link WxPayService#queryOrder(String, String)} .
74+
*/
75+
@Test
76+
public void testQueryOrder() throws WxPayException {
77+
this.logger.info(this.payService.queryOrder("11212121", null).toString());
78+
this.logger.info(this.payService.queryOrder(null, "11111").toString());
79+
}
80+
81+
/**
82+
* Test method for {@link WxPayService#closeOrder(String)} .
83+
*/
84+
@Test
85+
public void testCloseOrder() throws WxPayException {
86+
this.logger.info(this.payService.closeOrder("11212121").toString());
87+
}
88+
5089
@Test
5190
public void testDownloadBill() throws Exception {
52-
WxPayBillResult wxPayBillResult = this.payService.downloadBill("20170101", "ALL", "GZIP", "1111111");
91+
WxPayBillResult wxPayBillResult = this.payService.downloadBill("20170101", BillType.ALL, "GZIP", "1111111");
5392
//前一天没有账单记录返回null
5493
assertNotNull(wxPayBillResult);
5594
//必填字段为空时,抛出异常
@@ -60,7 +99,7 @@ public void testDownloadBill() throws Exception {
6099
public void testReport() throws Exception {
61100
WxPayReportRequest request = new WxPayReportRequest();
62101
request.setInterfaceUrl("hahahah");
63-
request.setSignType("HMAC-SHA256");//貌似接口未校验此字段
102+
request.setSignType(SignType.HMAC_SHA256);//貌似接口未校验此字段
64103
request.setExecuteTime(1000);
65104
request.setReturnCode("aaa");
66105
request.setResultCode("aaa");
@@ -130,41 +169,6 @@ public void testQueryRedpack() throws Exception {
130169
this.logger.info(redpackResult.toString());
131170
}
132171

133-
/**
134-
* Test method for {@link WxPayService#unifiedOrder(WxPayUnifiedOrderRequest)}.
135-
*/
136-
@Test
137-
public void testUnifiedOrder() throws WxPayException {
138-
WxPayUnifiedOrderResult result = this.payService
139-
.unifiedOrder(WxPayUnifiedOrderRequest.newBuilder()
140-
.body("我去")
141-
.totalFee(1)
142-
.spbillCreateIp("11.1.11.1")
143-
.notifyURL("111111")
144-
.tradeType("JSAPI")
145-
.openid(((XmlWxPayConfig) this.payService.getConfig()).getOpenid())
146-
.outTradeNo("111111")
147-
.build());
148-
this.logger.info(result.toString());
149-
}
150-
151-
/**
152-
* Test method for {@link WxPayService#queryOrder(String, String)} .
153-
*/
154-
@Test
155-
public void testQueryOrder() throws WxPayException {
156-
this.logger.info(this.payService.queryOrder("11212121", null).toString());
157-
this.logger.info(this.payService.queryOrder(null, "11111").toString());
158-
}
159-
160-
/**
161-
* Test method for {@link WxPayService#closeOrder(String)} .
162-
*/
163-
@Test
164-
public void testCloseOrder() throws WxPayException {
165-
this.logger.info(this.payService.closeOrder("11212121").toString());
166-
}
167-
168172
/**
169173
* Test method for {@link WxPayService#entPay(WxEntPayRequest)}.
170174
*/
@@ -175,7 +179,7 @@ public void testEntPay() throws WxPayException {
175179
.openid("ojOQA0y9o-Eb6Aep7uVTdbkJqrP4")
176180
.amount(1)
177181
.spbillCreateIp("10.10.10.10")
178-
.checkName("NO_CHECK")
182+
.checkName(WxPayConstants.CheckNameOption.NO_CHECK)
179183
.description("描述信息")
180184
.build();
181185

0 commit comments

Comments
 (0)