Przeglądaj źródła

微信回调验签测试

skyline 2 lat temu
rodzic
commit
6bc4c10449

+ 2 - 2
miniapp/src/main/java/com/kym/miniapp/controller/InvoiceController.java

@@ -81,8 +81,8 @@ public class InvoiceController {
      * @return
      */
     @PostMapping("/notify")
-    R<?> invoiceNotify(HttpServletRequest request) {
-        wxPayService.invoiceNotify(request);
+    R<?> invoiceNotify(HttpServletRequest request,@RequestBody String body) {
+        wxPayService.invoiceNotify(request,body);
         return R.success();
     }
 

+ 3 - 3
service/src/main/java/com/kym/service/wechat/WxPayService.java

@@ -41,13 +41,13 @@ public interface WxPayService {
     //================================================================发票=====================================================================
     TitleUrl titleUrl(Invoice invoice);
 
-    void titleWriteNotice(HttpServletRequest request);
+    void titleWriteNotice(Object[] notifyRes);
 
     InvoiceBaseInfo baseInformation();
 
     void fapiaoApplication(String invoiceId);
 
-    void wxInvoiceNotify(HttpServletRequest request);
+    void wxInvoiceNotify(Object[] notifyRes);
 
-    void invoiceNotify(HttpServletRequest request);
+    void invoiceNotify(HttpServletRequest request, String body);
 }

+ 46 - 9
service/src/main/java/com/kym/service/wechat/impl/WxPayServiceImpl.java

@@ -215,6 +215,43 @@ public class WxPayServiceImpl implements WxPayService {
         return new Object[]{requestParam, parser, no};
     }
 
+    @SneakyThrows
+    Object[] handleWxNotify2(HttpServletRequest request,String body) {
+        var no = RandomUtil.randomInt(1000, 9999);
+        var signature = request.getHeader("Wechatpay-Signature");
+        var serial = request.getHeader("Wechatpay-Serial");
+        var nonce = request.getHeader("Wechatpay-Nonce");
+        var timestamp = request.getHeader("Wechatpay-Timestamp");
+        var signatureType = request.getHeader("Wechatpay-Signature-Type");
+
+        LOGGER.info("微信支付回调{}:\n Request参数:\n signature:{},serial:{},nonce:{},timestamp:{},signatureType:{}", no, signature, serial, nonce, timestamp, signatureType);
+
+        ServletInputStream inputStream = request.getInputStream();
+        ByteArrayOutputStream result = new ByteArrayOutputStream();
+        byte[] buffer = new byte[1024];
+        for (int lenght; (lenght = inputStream.read(buffer)) != -1; ) {
+            result.write(buffer, 0, lenght);
+        }
+
+
+        LOGGER.info("微信支付回调{}:\nBody数据:\n{}", no, result);
+
+        // 构造 RequestParam
+        RequestParam requestParam = new RequestParam.Builder()
+                .serialNumber(serial)
+                .nonce(nonce) // 随机数
+                .signature(signature)
+                .timestamp(timestamp)
+                .body(body)
+                .build();
+        LOGGER.info("微信支付回调{}:构造 RequestParam完毕", no);
+
+        // 如果已经初始化了 RSAAutoCertificateConfig,可直接使用
+        // 初始化 NotificationParser
+        NotificationParser parser = new NotificationParser((NotificationConfig) config);
+        return new Object[]{requestParam, parser, no};
+    }
+
     /**
      * JSAPI支付下单
      */
@@ -661,11 +698,11 @@ public class WxPayServiceImpl implements WxPayService {
      * 处理发票抬头填写完成通知
      * event_type为FAPIAO.USER_APPLIED
      *
-     * @param request
+     * @param notifyRes
+     * @param body
      */
     @Override
-    public void titleWriteNotice(HttpServletRequest request) {
-        var notifyRes = handleWxNotify(request);
+    public void titleWriteNotice(Object[] notifyRes) {
         try {
 //            TitleWriteNotification titleWriteNotification = ((NotificationParser) notifyRes[1]).parse((RequestParam) notifyRes[0], TitleWriteNotification.class);
             Map<String,Object> titleWriteNotification = ((NotificationParser) notifyRes[1]).parse((RequestParam) notifyRes[0], Map.class);
@@ -819,8 +856,7 @@ public class WxPayServiceImpl implements WxPayService {
      * @return
      */
     @Override
-    public void wxInvoiceNotify(HttpServletRequest request) {
-        var notifyRes = handleWxNotify(request);
+    public void wxInvoiceNotify(Object[] notifyRes) {
         try {
             InvoiceNotification invoiceNotification = ((NotificationParser) notifyRes[1]).parse((RequestParam) notifyRes[0], InvoiceNotification.class);
             LOGGER.info("微信开具发票结果通知回调{}:验签解密完毕,数据:\n{}", notifyRes[2], invoiceNotification);
@@ -842,19 +878,20 @@ public class WxPayServiceImpl implements WxPayService {
      * 接收电子发票相关回调通知
      *
      * @param request
+     * @param body
      */
     @Override
-    public void invoiceNotify(HttpServletRequest request) {
-        var notifyRes = handleWxNotify(request);
+    public void invoiceNotify(HttpServletRequest request, String body) {
+        var notifyRes = handleWxNotify2(request,body);
         var requestParam = (RequestParam) notifyRes[0];
         Notification notification = JSONObject.parseObject(requestParam.getBody(), Notification.class);
         switch (notification.getEventType()) {
             // 发票抬头填写完成
             case "FAPIAO.USER_APPLIED":
-                titleWriteNotice(request);
+                titleWriteNotice(notifyRes);
                 // 发票开具结果
             case "FAPIAO.ISSUED":
-                wxInvoiceNotify(request);
+                wxInvoiceNotify(notifyRes);
             default:
                 throw new IllegalStateException("Unexpected value: " + notification.getEventType());
         }