Skip to content

Commit e2120d6

Browse files
authored
🎨 #3909 【微信支付】修复企业付款请求对象在序列化/取签名参数时因 brandId 为空导致的 NPE
1 parent d06da07 commit e2120d6

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/entpay/EntPayRequest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,9 @@ protected void storeMap(Map<String, String> map) {
249249
map.put("desc", description);
250250
map.put("spbill_create_ip", spbillCreateIp);
251251
map.put("scene", scene);
252-
map.put("brand_id", brandId.toString());
252+
if (brandId != null) {
253+
map.put("brand_id", brandId.toString());
254+
}
253255
map.put("finder_template_id", finderTemplateId);
254256
}
255257
}

weixin-java-pay/src/test/java/com/github/binarywang/wxpay/bean/entpay/EntPayRequestTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
import org.testng.annotations.Test;
44

5+
import java.util.Map;
6+
7+
import static org.assertj.core.api.Assertions.assertThat;
8+
59
/**
610
* .
711
*
@@ -14,4 +18,32 @@ public class EntPayRequestTest {
1418
public void testToString() {
1519
System.out.println(EntPayRequest.newBuilder().mchId("123").build().toString());
1620
}
21+
22+
/**
23+
* 测试 brandId 为 null 时,getSignParams() 不抛出 NullPointerException.
24+
*/
25+
@Test
26+
public void testGetSignParamsWithNullBrandId() {
27+
EntPayRequest request = EntPayRequest.newBuilder()
28+
.mchId("123")
29+
.amount(100)
30+
.brandId(null)
31+
.build();
32+
Map<String, String> params = request.getSignParams();
33+
assertThat(params).doesNotContainKey("brand_id");
34+
}
35+
36+
/**
37+
* 测试 brandId 不为 null 时,getSignParams() 正确包含 brand_id.
38+
*/
39+
@Test
40+
public void testGetSignParamsWithNonNullBrandId() {
41+
EntPayRequest request = EntPayRequest.newBuilder()
42+
.mchId("123")
43+
.amount(100)
44+
.brandId(1234)
45+
.build();
46+
Map<String, String> params = request.getSignParams();
47+
assertThat(params).containsEntry("brand_id", "1234");
48+
}
1749
}

0 commit comments

Comments
 (0)