Skip to content

Commit 9e190fe

Browse files
authored
🎨 #2985【企业微信】增加会话存档常量类型支持
1 parent e2e4d34 commit 9e190fe

File tree

6 files changed

+104
-21
lines changed

6 files changed

+104
-21
lines changed

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpConsts.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,61 @@ public static class MsgAuditChangeType {
175175

176176
}
177177

178+
/**
179+
* 会话存档媒体类型
180+
* https://developer.work.weixin.qq.com/document/path/91774
181+
*/
182+
@UtilityClass
183+
public static class MsgAuditMediaType {
184+
185+
/**
186+
* 图片
187+
*/
188+
public static final String IMAGE = "image";
189+
190+
/**
191+
* 语音
192+
*/
193+
public static final String VOICE = "voice";
194+
195+
/**
196+
* 视频
197+
*/
198+
public static final String VIDEO = "video";
199+
200+
/**
201+
* 表情
202+
*/
203+
public static final String EMOTION = "emotion";
204+
205+
/**
206+
* 文件
207+
*/
208+
public static final String FILE = "file";
209+
210+
/**
211+
* 音频存档消息
212+
*/
213+
public static final String MEETING_VOICE_CALL = "meeting_voice_call";
214+
215+
/**
216+
* 音频共享文档消息
217+
*/
218+
public static final String VOIP_DOC_SHARE = "voip_doc_share";
219+
220+
@UtilityClass
221+
public static class MsgAuditSuffix {
222+
223+
public static final String JPG = ".jpg";
224+
public static final String PNG = ".png";
225+
public static final String GIF = ".gif";
226+
public static final String MP4 = ".mp4";
227+
public static final String AMR = ".amr";
228+
229+
}
230+
231+
}
232+
178233
/**
179234
* 家校通讯录变更事件CHANGE_TYPE
180235
*/

weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpMsgAuditTest.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,10 @@ public void test() throws Exception {
165165
/*
166166
* 图片,语音,视频,表情,文件,音频存档消息,音频共享文档消息调用 获取媒体消息
167167
*/
168-
List<String> mediaType = Arrays.asList("image", "voice", "video", "emotion", "file",
169-
"meeting_voice_call", "voip_doc_share");
168+
List<String> mediaType = Arrays.asList(WxCpConsts.MsgAuditMediaType.IMAGE,
169+
WxCpConsts.MsgAuditMediaType.VOICE, WxCpConsts.MsgAuditMediaType.VIDEO,
170+
WxCpConsts.MsgAuditMediaType.EMOTION, WxCpConsts.MsgAuditMediaType.FILE,
171+
WxCpConsts.MsgAuditMediaType.MEETING_VOICE_CALL, WxCpConsts.MsgAuditMediaType.VOIP_DOC_SHARE);
170172

171173
// 模拟多次拉取数据,根据seq拉取
172174
for (int i = 0; i < 3; i++) {
@@ -214,43 +216,43 @@ public void test() throws Exception {
214216
// sdkFileId
215217
String sdkFileId = "";
216218
switch (msgType) {
217-
case "image":
218-
suffix = ".jpg";
219+
case WxCpConsts.MsgAuditMediaType.IMAGE:
220+
suffix = WxCpConsts.MsgAuditMediaType.MsgAuditSuffix.JPG;
219221
md5Sum = decryptData.getImage().getMd5Sum();
220222
sdkFileId = decryptData.getImage().getSdkFileId();
221223
break;
222-
case "voice":
223-
suffix = ".amr";
224+
case WxCpConsts.MsgAuditMediaType.VOICE:
225+
suffix = WxCpConsts.MsgAuditMediaType.MsgAuditSuffix.AMR;
224226
md5Sum = decryptData.getVoice().getMd5Sum();
225227
sdkFileId = decryptData.getVoice().getSdkFileId();
226228
break;
227-
case "video":
228-
suffix = ".mp4";
229+
case WxCpConsts.MsgAuditMediaType.VIDEO:
230+
suffix = WxCpConsts.MsgAuditMediaType.MsgAuditSuffix.MP4;
229231
md5Sum = decryptData.getVideo().getMd5Sum();
230232
sdkFileId = decryptData.getVideo().getSdkFileId();
231233
break;
232-
case "emotion":
234+
case WxCpConsts.MsgAuditMediaType.EMOTION:
233235
md5Sum = decryptData.getEmotion().getMd5Sum();
234236
sdkFileId = decryptData.getEmotion().getSdkFileId();
235237
int type = decryptData.getEmotion().getType();
236238
switch (type) {
237239
case 1:
238-
suffix = ".gif";
240+
suffix = WxCpConsts.MsgAuditMediaType.MsgAuditSuffix.GIF;
239241
break;
240242
case 2:
241-
suffix = ".png";
243+
suffix = WxCpConsts.MsgAuditMediaType.MsgAuditSuffix.PNG;
242244
break;
243245
default:
244246
return;
245247
}
246248
break;
247-
case "file":
249+
case WxCpConsts.MsgAuditMediaType.FILE:
248250
md5Sum = decryptData.getFile().getMd5Sum();
249251
suffix = "." + decryptData.getFile().getFileExt();
250252
sdkFileId = decryptData.getFile().getSdkFileId();
251253
break;
252254
// 音频存档消息
253-
case "meeting_voice_call":
255+
case WxCpConsts.MsgAuditMediaType.MEETING_VOICE_CALL:
254256

255257
md5Sum = decryptData.getVoiceId();
256258
sdkFileId = decryptData.getMeetingVoiceCall().getSdkFileId();
@@ -262,7 +264,7 @@ public void test() throws Exception {
262264

263265
break;
264266
// 音频共享文档消息
265-
case "voip_doc_share":
267+
case WxCpConsts.MsgAuditMediaType.VOIP_DOC_SHARE:
266268

267269
md5Sum = decryptData.getVoipId();
268270
WxCpFileItem docShare = decryptData.getVoipDocShare();

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/notify/WxPayNotifyV3Response.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.github.binarywang.wxpay.bean.notify;
22

3-
import com.google.gson.Gson;
43
import lombok.AllArgsConstructor;
54
import lombok.Builder;
65
import lombok.Data;
76
import lombok.NoArgsConstructor;
7+
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
88

99
/**
1010
* 微信支付订单和退款的异步通知,V3版本共用的响应类.
@@ -33,7 +33,7 @@ public class WxPayNotifyV3Response {
3333
*/
3434
public static String success(String msg) {
3535
WxPayNotifyV3Response response = new WxPayNotifyV3Response(SUCCESS, msg);
36-
return new Gson().toJson(response);
36+
return WxGsonBuilder.create().toJson(response);
3737
}
3838

3939
/**
@@ -44,7 +44,7 @@ public static String success(String msg) {
4444
*/
4545
public static String fail(String msg) {
4646
WxPayNotifyV3Response response = new WxPayNotifyV3Response(FAIL, msg);
47-
return new Gson().toJson(response);
47+
return WxGsonBuilder.create().toJson(response);
4848
}
4949

5050
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,13 @@ public static Integer yuan2Fen(BigDecimal yuan) {
157157
return yuan.multiply(BigDecimal.valueOf(100)).setScale(2, BigDecimal.ROUND_HALF_UP).intValue();
158158
}
159159

160+
/**
161+
* 分转元
162+
*/
163+
public static BigDecimal fen2Yuan(BigDecimal fen) {
164+
return fen.divide(BigDecimal.valueOf(100)).setScale(2, BigDecimal.ROUND_HALF_UP);
165+
}
166+
160167
/**
161168
* 检查请求参数内容,包括必填参数以及特殊约束.
162169
*/

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ public void testRefundV3() throws WxPayException {
779779
* @throws Exception the exception
780780
*/
781781
@Test
782-
public void testParseOrderNotifyV3Result(HttpServletRequest request, HttpServletResponse response) throws Exception {
782+
public String testParseOrderNotifyV3Result(HttpServletRequest request, HttpServletResponse response) throws Exception {
783783

784784
String timestamp = request.getHeader("Wechatpay-Timestamp");
785785
Optional.ofNullable(timestamp).orElseThrow(() -> new RuntimeException("时间戳不能为空"));
@@ -799,6 +799,8 @@ public void testParseOrderNotifyV3Result(HttpServletRequest request, HttpServlet
799799
final WxPayOrderNotifyV3Result wxPayOrderNotifyV3Result = this.payService.parseOrderNotifyV3Result(RequestUtils.readData(request),
800800
new SignatureHeader(timestamp, nonce, signature, serialNo));
801801
log.info(GSON.toJson(wxPayOrderNotifyV3Result));
802+
803+
return WxPayNotifyV3Response.success("成功");
802804
}
803805

804806
/**
@@ -808,7 +810,7 @@ public void testParseOrderNotifyV3Result(HttpServletRequest request, HttpServlet
808810
* @throws Exception the exception
809811
*/
810812
@Test
811-
public void testParseRefundNotifyV3Result(HttpServletRequest request, HttpServletResponse response) throws Exception {
813+
public String testParseRefundNotifyV3Result(HttpServletRequest request, HttpServletResponse response) throws Exception {
812814

813815
String timestamp = request.getHeader("Wechatpay-Timestamp");
814816
Optional.ofNullable(timestamp).orElseThrow(() -> new RuntimeException("时间戳不能为空"));
@@ -827,6 +829,21 @@ public void testParseRefundNotifyV3Result(HttpServletRequest request, HttpServle
827829
final WxPayRefundNotifyV3Result wxPayRefundNotifyV3Result = this.payService.parseRefundNotifyV3Result(RequestUtils.readData(request),
828830
new SignatureHeader(timestamp, nonce, signature, serialNo));
829831
log.info(GSON.toJson(wxPayRefundNotifyV3Result));
832+
833+
// 退款金额
834+
final WxPayRefundNotifyV3Result.DecryptNotifyResult result = wxPayRefundNotifyV3Result.getResult();
835+
final BigDecimal total = BaseWxPayRequest.fen2Yuan(BigDecimal.valueOf(result.getAmount().getTotal()));
836+
final BigDecimal payerRefund = BaseWxPayRequest.fen2Yuan(BigDecimal.valueOf(result.getAmount().getPayerRefund()));
837+
838+
// 处理业务逻辑 ...
839+
840+
return WxPayNotifyV3Response.success("成功");
841+
}
842+
843+
@Test
844+
public void testWxPayNotifyV3Response() {
845+
System.out.println(WxPayNotifyV3Response.success("success"));
846+
System.out.println(WxPayNotifyV3Response.fail("fail"));
830847
}
831848

832849
@Test

weixin-java-pay/src/test/resources/test-config.sample.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<xml>
2-
<appId>公众号appid</appId>
3-
<mchId>微信商户平台ID</mchId>
42
<!---
53
以下为官网文档所提供样例参数,仅供部分接口测试使用
64
<appId>wxd930ea5d5a258f4f</appId>
75
<mchId>10000100</mchId>
86
<mchKey>192006250b4c09247ec02edce69f6a2d</mchKey>
97
-->
8+
9+
<appId>appid</appId>
10+
<mchId>微信商户平台ID</mchId>
11+
1012
<!-- v2版本 -->
1113
<mchKey>商户平台设置的API密钥</mchKey>
1214
<keyPath>商户平台的证书文件地址</keyPath>

0 commit comments

Comments
 (0)