Skip to content

Commit c065ad2

Browse files
authored
🎨 #2980【微信支付】增加部分回调相关的单元测试代码
1 parent 9d5e22f commit c065ad2

File tree

5 files changed

+140
-5
lines changed

5 files changed

+140
-5
lines changed

weixin-java-pay/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@
6868
<groupId>com.google.code.gson</groupId>
6969
<artifactId>gson</artifactId>
7070
</dependency>
71+
<dependency>
72+
<groupId>javax.servlet</groupId>
73+
<artifactId>javax.servlet-api</artifactId>
74+
</dependency>
7175
</dependencies>
7276

7377
<profiles>

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.math.BigDecimal;
2323
import java.util.HashMap;
2424
import java.util.Map;
25+
import java.util.Optional;
2526

2627
import static com.github.binarywang.wxpay.constant.WxPayConstants.SignType.ALL_SIGN_TYPES;
2728

@@ -149,6 +150,13 @@ public static Integer yuanToFen(String yuan) {
149150
return new BigDecimal(yuan).setScale(2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).intValue();
150151
}
151152

153+
/**
154+
* 元转分
155+
*/
156+
public static Integer yuan2Fen(BigDecimal yuan) {
157+
return yuan.multiply(BigDecimal.valueOf(100)).setScale(2, BigDecimal.ROUND_HALF_UP).intValue();
158+
}
159+
152160
/**
153161
* 检查请求参数内容,包括必填参数以及特殊约束.
154162
*/

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/constant/WxPayConstants.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ public class WxPayConstants {
2626
*/
2727
public static final Format QUERY_COMMENT_DATE_FORMAT = FastDateFormat.getInstance("yyyyMMddHHmmss");
2828

29+
/**
30+
* 币种类型.
31+
*/
32+
public static class CurrencyType {
33+
/**
34+
* 人民币.
35+
*/
36+
public static final String CNY = "CNY";
37+
38+
}
39+
2940
/**
3041
* 校验用户姓名选项,企业付款时使用.
3142
*/
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.github.binarywang.wxpay.util;
2+
3+
import javax.servlet.http.HttpServletRequest;
4+
import java.io.BufferedReader;
5+
import java.io.IOException;
6+
7+
/**
8+
* <pre>
9+
* 请求工具类.
10+
* Created by Wang_Wong on 2023-04-14.
11+
* </pre>
12+
*
13+
* @author <a href="https://github.com/0katekate0/">Wang_Wong</a>
14+
*/
15+
public class RequestUtils {
16+
17+
/**
18+
* 获取请求头数据,微信V3版本回调使用
19+
*
20+
* @param request
21+
* @return 字符串
22+
*/
23+
public static String readData(HttpServletRequest request) {
24+
BufferedReader br = null;
25+
StringBuilder result = new StringBuilder();
26+
try {
27+
br = request.getReader();
28+
for (String line; (line = br.readLine()) != null; ) {
29+
if (result.length() > 0) {
30+
result.append("\n");
31+
}
32+
result.append(line);
33+
}
34+
} catch (IOException e) {
35+
e.printStackTrace();
36+
} finally {
37+
if (br != null) {
38+
try {
39+
br.close();
40+
} catch (IOException e) {
41+
e.printStackTrace();
42+
}
43+
}
44+
}
45+
46+
return result.toString();
47+
}
48+
49+
}

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

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
import com.github.binarywang.utils.qrcode.QrcodeUtils;
44
import com.github.binarywang.wxpay.bean.coupon.*;
5-
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
6-
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResultTest;
5+
import com.github.binarywang.wxpay.bean.notify.*;
76
import com.github.binarywang.wxpay.bean.order.WxPayAppOrderResult;
87
import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
98
import com.github.binarywang.wxpay.bean.order.WxPayNativeOrderResult;
109
import com.github.binarywang.wxpay.bean.request.*;
1110
import com.github.binarywang.wxpay.bean.result.*;
1211
import com.github.binarywang.wxpay.bean.result.enums.TradeTypeEnum;
1312
import com.github.binarywang.wxpay.config.WxPayConfig;
13+
import com.github.binarywang.wxpay.constant.WxPayConstants;
1414
import com.github.binarywang.wxpay.constant.WxPayConstants.AccountType;
1515
import com.github.binarywang.wxpay.constant.WxPayConstants.BillType;
1616
import com.github.binarywang.wxpay.constant.WxPayConstants.SignType;
@@ -19,6 +19,7 @@
1919
import com.github.binarywang.wxpay.service.WxPayService;
2020
import com.github.binarywang.wxpay.testbase.ApiTestModule;
2121
import com.github.binarywang.wxpay.testbase.XmlWxPayConfig;
22+
import com.github.binarywang.wxpay.util.RequestUtils;
2223
import com.github.binarywang.wxpay.util.XmlConfig;
2324
import com.google.gson.Gson;
2425
import com.google.gson.GsonBuilder;
@@ -29,10 +30,14 @@
2930
import org.testng.annotations.Guice;
3031
import org.testng.annotations.Test;
3132

33+
import javax.servlet.http.HttpServletRequest;
34+
import javax.servlet.http.HttpServletResponse;
35+
import java.math.BigDecimal;
3236
import java.nio.file.Files;
3337
import java.nio.file.Path;
3438
import java.util.Calendar;
3539
import java.util.Date;
40+
import java.util.Optional;
3641

3742
import static com.github.binarywang.wxpay.constant.WxPayConstants.TarType;
3843
import static org.assertj.core.api.Assertions.assertThat;
@@ -728,9 +733,9 @@ public void testUnifiedOrderV3() throws WxPayException {
728733
//构建金额信息
729734
WxPayUnifiedOrderV3Request.Amount amount = new WxPayUnifiedOrderV3Request.Amount();
730735
//设置币种信息
731-
amount.setCurrency("CNY");
736+
amount.setCurrency(WxPayConstants.CurrencyType.CNY);
732737
//设置金额
733-
amount.setTotal(1);
738+
amount.setTotal(BaseWxPayRequest.yuan2Fen(BigDecimal.ONE));
734739
request.setAmount(amount);
735740

736741
WxPayUnifiedOrderV3Result.JsapiResult result = this.payService.createOrderV3(TradeTypeEnum.JSAPI, request);
@@ -767,6 +772,63 @@ public void testRefundV3() throws WxPayException {
767772
System.out.println(GSON.toJson(result));
768773
}
769774

775+
/**
776+
* 测试V3支付成功回调
777+
* https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_5.shtml
778+
*
779+
* @throws Exception the exception
780+
*/
781+
@Test
782+
public void testParseOrderNotifyV3Result(HttpServletRequest request, HttpServletResponse response) throws Exception {
783+
784+
String timestamp = request.getHeader("Wechatpay-Timestamp");
785+
Optional.ofNullable(timestamp).orElseThrow(() -> new RuntimeException("时间戳不能为空"));
786+
787+
String nonce = request.getHeader("Wechatpay-Nonce");
788+
Optional.ofNullable(nonce).orElseThrow(() -> new RuntimeException("nonce不能为空"));
789+
790+
String serialNo = request.getHeader("Wechatpay-Serial");
791+
Optional.ofNullable(serialNo).orElseThrow(() -> new RuntimeException("serialNo不能为空"));
792+
793+
String signature = request.getHeader("Wechatpay-Signature");
794+
Optional.ofNullable(signature).orElseThrow(() -> new RuntimeException("signature不能为空"));
795+
796+
log.info("请求头参数为:timestamp:{} nonce:{} serialNo:{} signature:{}", timestamp, nonce, serialNo, signature);
797+
798+
// V2版本请参考com.github.binarywang.wxpay.bean.notify.WxPayRefundNotifyResultTest里的单元测试
799+
final WxPayOrderNotifyV3Result wxPayOrderNotifyV3Result = this.payService.parseOrderNotifyV3Result(RequestUtils.readData(request),
800+
new SignatureHeader(timestamp, nonce, signature, serialNo));
801+
log.info(GSON.toJson(wxPayOrderNotifyV3Result));
802+
}
803+
804+
/**
805+
* 测试V3退款成功回调
806+
* https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_11.shtml
807+
*
808+
* @throws Exception the exception
809+
*/
810+
@Test
811+
public void testParseRefundNotifyV3Result(HttpServletRequest request, HttpServletResponse response) throws Exception {
812+
813+
String timestamp = request.getHeader("Wechatpay-Timestamp");
814+
Optional.ofNullable(timestamp).orElseThrow(() -> new RuntimeException("时间戳不能为空"));
815+
816+
String nonce = request.getHeader("Wechatpay-Nonce");
817+
Optional.ofNullable(nonce).orElseThrow(() -> new RuntimeException("nonce不能为空"));
818+
819+
String serialNo = request.getHeader("Wechatpay-Serial");
820+
Optional.ofNullable(serialNo).orElseThrow(() -> new RuntimeException("serialNo不能为空"));
821+
822+
String signature = request.getHeader("Wechatpay-Signature");
823+
Optional.ofNullable(signature).orElseThrow(() -> new RuntimeException("signature不能为空"));
824+
825+
log.info("支付请求头参数为:timestamp:{} nonce:{} serialNo:{} signature:{}", timestamp, nonce, serialNo, signature);
826+
827+
final WxPayRefundNotifyV3Result wxPayRefundNotifyV3Result = this.payService.parseRefundNotifyV3Result(RequestUtils.readData(request),
828+
new SignatureHeader(timestamp, nonce, signature, serialNo));
829+
log.info(GSON.toJson(wxPayRefundNotifyV3Result));
830+
}
831+
770832
@Test
771833
public void testRefundQueryV3() throws WxPayException {
772834
WxPayRefundQueryV3Request request = new WxPayRefundQueryV3Request();
@@ -778,10 +840,11 @@ public void testRefundQueryV3() throws WxPayException {
778840

779841
/**
780842
* 测试包含正向代理的测试
843+
*
781844
* @throws WxPayException
782845
*/
783846
@Test
784-
public void testQueryOrderV3WithProxy() {
847+
public void testQueryOrderV3WithProxy() {
785848
try {
786849
WxPayOrderQueryV3Request request = new WxPayOrderQueryV3Request();
787850
request.setOutTradeNo("n1ZvYqjAg3D3LUBa");

0 commit comments

Comments
 (0)