Pārlūkot izejas kodu

:art: #2985【企业微信】增加会话存档常量类型支持

0katekate0 2 gadi atpakaļ
vecāks
revīzija
9e190fef36

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

@@ -176,6 +176,61 @@ public class WxCpConsts {
   }
 
   /**
+   * 会话存档媒体类型
+   * https://developer.work.weixin.qq.com/document/path/91774
+   */
+  @UtilityClass
+  public static class MsgAuditMediaType {
+
+    /**
+     * 图片
+     */
+    public static final String IMAGE = "image";
+
+    /**
+     * 语音
+     */
+    public static final String VOICE = "voice";
+
+    /**
+     * 视频
+     */
+    public static final String VIDEO = "video";
+
+    /**
+     * 表情
+     */
+    public static final String EMOTION = "emotion";
+
+    /**
+     * 文件
+     */
+    public static final String FILE = "file";
+
+    /**
+     * 音频存档消息
+     */
+    public static final String MEETING_VOICE_CALL = "meeting_voice_call";
+
+    /**
+     * 音频共享文档消息
+     */
+    public static final String VOIP_DOC_SHARE = "voip_doc_share";
+
+    @UtilityClass
+    public static class MsgAuditSuffix {
+
+      public static final String JPG = ".jpg";
+      public static final String PNG = ".png";
+      public static final String GIF = ".gif";
+      public static final String MP4 = ".mp4";
+      public static final String AMR = ".amr";
+
+    }
+
+  }
+
+  /**
    * 家校通讯录变更事件CHANGE_TYPE
    */
   @UtilityClass

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

@@ -165,8 +165,10 @@ public class WxCpMsgAuditTest {
     /*
      * 图片,语音,视频,表情,文件,音频存档消息,音频共享文档消息调用  获取媒体消息
      */
-    List<String> mediaType = Arrays.asList("image", "voice", "video", "emotion", "file",
-      "meeting_voice_call", "voip_doc_share");
+    List<String> mediaType = Arrays.asList(WxCpConsts.MsgAuditMediaType.IMAGE,
+      WxCpConsts.MsgAuditMediaType.VOICE, WxCpConsts.MsgAuditMediaType.VIDEO,
+      WxCpConsts.MsgAuditMediaType.EMOTION, WxCpConsts.MsgAuditMediaType.FILE,
+      WxCpConsts.MsgAuditMediaType.MEETING_VOICE_CALL, WxCpConsts.MsgAuditMediaType.VOIP_DOC_SHARE);
 
     // 模拟多次拉取数据,根据seq拉取
     for (int i = 0; i < 3; i++) {
@@ -214,43 +216,43 @@ public class WxCpMsgAuditTest {
             // sdkFileId
             String sdkFileId = "";
             switch (msgType) {
-              case "image":
-                suffix = ".jpg";
+              case WxCpConsts.MsgAuditMediaType.IMAGE:
+                suffix = WxCpConsts.MsgAuditMediaType.MsgAuditSuffix.JPG;
                 md5Sum = decryptData.getImage().getMd5Sum();
                 sdkFileId = decryptData.getImage().getSdkFileId();
                 break;
-              case "voice":
-                suffix = ".amr";
+              case WxCpConsts.MsgAuditMediaType.VOICE:
+                suffix = WxCpConsts.MsgAuditMediaType.MsgAuditSuffix.AMR;
                 md5Sum = decryptData.getVoice().getMd5Sum();
                 sdkFileId = decryptData.getVoice().getSdkFileId();
                 break;
-              case "video":
-                suffix = ".mp4";
+              case WxCpConsts.MsgAuditMediaType.VIDEO:
+                suffix = WxCpConsts.MsgAuditMediaType.MsgAuditSuffix.MP4;
                 md5Sum = decryptData.getVideo().getMd5Sum();
                 sdkFileId = decryptData.getVideo().getSdkFileId();
                 break;
-              case "emotion":
+              case WxCpConsts.MsgAuditMediaType.EMOTION:
                 md5Sum = decryptData.getEmotion().getMd5Sum();
                 sdkFileId = decryptData.getEmotion().getSdkFileId();
                 int type = decryptData.getEmotion().getType();
                 switch (type) {
                   case 1:
-                    suffix = ".gif";
+                    suffix = WxCpConsts.MsgAuditMediaType.MsgAuditSuffix.GIF;
                     break;
                   case 2:
-                    suffix = ".png";
+                    suffix = WxCpConsts.MsgAuditMediaType.MsgAuditSuffix.PNG;
                     break;
                   default:
                     return;
                 }
                 break;
-              case "file":
+              case WxCpConsts.MsgAuditMediaType.FILE:
                 md5Sum = decryptData.getFile().getMd5Sum();
                 suffix = "." + decryptData.getFile().getFileExt();
                 sdkFileId = decryptData.getFile().getSdkFileId();
                 break;
               // 音频存档消息
-              case "meeting_voice_call":
+              case WxCpConsts.MsgAuditMediaType.MEETING_VOICE_CALL:
 
                 md5Sum = decryptData.getVoiceId();
                 sdkFileId = decryptData.getMeetingVoiceCall().getSdkFileId();
@@ -262,7 +264,7 @@ public class WxCpMsgAuditTest {
 
                 break;
               // 音频共享文档消息
-              case "voip_doc_share":
+              case WxCpConsts.MsgAuditMediaType.VOIP_DOC_SHARE:
 
                 md5Sum = decryptData.getVoipId();
                 WxCpFileItem docShare = decryptData.getVoipDocShare();

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

@@ -1,10 +1,10 @@
 package com.github.binarywang.wxpay.bean.notify;
 
-import com.google.gson.Gson;
 import lombok.AllArgsConstructor;
 import lombok.Builder;
 import lombok.Data;
 import lombok.NoArgsConstructor;
+import me.chanjar.weixin.common.util.json.WxGsonBuilder;
 
 /**
  * 微信支付订单和退款的异步通知,V3版本共用的响应类.
@@ -33,7 +33,7 @@ public class WxPayNotifyV3Response {
    */
   public static String success(String msg) {
     WxPayNotifyV3Response response = new WxPayNotifyV3Response(SUCCESS, msg);
-    return new Gson().toJson(response);
+    return WxGsonBuilder.create().toJson(response);
   }
 
   /**
@@ -44,7 +44,7 @@ public class WxPayNotifyV3Response {
    */
   public static String fail(String msg) {
     WxPayNotifyV3Response response = new WxPayNotifyV3Response(FAIL, msg);
-    return new Gson().toJson(response);
+    return WxGsonBuilder.create().toJson(response);
   }
 
 }

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

@@ -158,6 +158,13 @@ public abstract class BaseWxPayRequest implements Serializable {
   }
 
   /**
+   * 分转元
+   */
+  public static BigDecimal fen2Yuan(BigDecimal fen) {
+    return fen.divide(BigDecimal.valueOf(100)).setScale(2, BigDecimal.ROUND_HALF_UP);
+  }
+
+  /**
    * 检查请求参数内容,包括必填参数以及特殊约束.
    */
   private void checkFields() throws WxPayException {

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

@@ -779,7 +779,7 @@ public class BaseWxPayServiceImplTest {
    * @throws Exception the exception
    */
   @Test
-  public void testParseOrderNotifyV3Result(HttpServletRequest request, HttpServletResponse response) throws Exception {
+  public String testParseOrderNotifyV3Result(HttpServletRequest request, HttpServletResponse response) throws Exception {
 
     String timestamp = request.getHeader("Wechatpay-Timestamp");
     Optional.ofNullable(timestamp).orElseThrow(() -> new RuntimeException("时间戳不能为空"));
@@ -799,6 +799,8 @@ public class BaseWxPayServiceImplTest {
     final WxPayOrderNotifyV3Result wxPayOrderNotifyV3Result = this.payService.parseOrderNotifyV3Result(RequestUtils.readData(request),
       new SignatureHeader(timestamp, nonce, signature, serialNo));
     log.info(GSON.toJson(wxPayOrderNotifyV3Result));
+
+    return WxPayNotifyV3Response.success("成功");
   }
 
   /**
@@ -808,7 +810,7 @@ public class BaseWxPayServiceImplTest {
    * @throws Exception the exception
    */
   @Test
-  public void testParseRefundNotifyV3Result(HttpServletRequest request, HttpServletResponse response) throws Exception {
+  public String testParseRefundNotifyV3Result(HttpServletRequest request, HttpServletResponse response) throws Exception {
 
     String timestamp = request.getHeader("Wechatpay-Timestamp");
     Optional.ofNullable(timestamp).orElseThrow(() -> new RuntimeException("时间戳不能为空"));
@@ -827,6 +829,21 @@ public class BaseWxPayServiceImplTest {
     final WxPayRefundNotifyV3Result wxPayRefundNotifyV3Result = this.payService.parseRefundNotifyV3Result(RequestUtils.readData(request),
       new SignatureHeader(timestamp, nonce, signature, serialNo));
     log.info(GSON.toJson(wxPayRefundNotifyV3Result));
+
+    // 退款金额
+    final WxPayRefundNotifyV3Result.DecryptNotifyResult result = wxPayRefundNotifyV3Result.getResult();
+    final BigDecimal total = BaseWxPayRequest.fen2Yuan(BigDecimal.valueOf(result.getAmount().getTotal()));
+    final BigDecimal payerRefund = BaseWxPayRequest.fen2Yuan(BigDecimal.valueOf(result.getAmount().getPayerRefund()));
+
+    // 处理业务逻辑 ...
+
+    return WxPayNotifyV3Response.success("成功");
+  }
+
+  @Test
+  public void testWxPayNotifyV3Response() {
+    System.out.println(WxPayNotifyV3Response.success("success"));
+    System.out.println(WxPayNotifyV3Response.fail("fail"));
   }
 
   @Test

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

@@ -1,12 +1,14 @@
 <xml>
-  <appId>公众号appid</appId>
-  <mchId>微信商户平台ID</mchId>
   <!---
     以下为官网文档所提供样例参数,仅供部分接口测试使用
     <appId>wxd930ea5d5a258f4f</appId>
     <mchId>10000100</mchId>
     <mchKey>192006250b4c09247ec02edce69f6a2d</mchKey>
 -->
+
+  <appId>appid</appId>
+  <mchId>微信商户平台ID</mchId>
+
   <!-- v2版本 -->
   <mchKey>商户平台设置的API密钥</mchKey>
   <keyPath>商户平台的证书文件地址</keyPath>