zhanglin8526
2023-08-26 ee57cf6a52011ac167216cf2420149e783bbdfa9
ruoyi-modules/ruoyi-order/src/main/java/com/ruoyi/order/controller/miniapp/NotifyController.java
@@ -64,17 +64,6 @@
    @ApiOperation(value = "微信支付/退款通知")
    public String payNotify(@RequestBody String notifyData, HttpServletRequest request, HttpServletResponse response) throws WxPayException {
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()){
            String name = headerNames.nextElement();
            log.info("request headerNames:  {}", name);
        }
        Collection<String> headerNames1 = response.getHeaderNames();
        for (String name : headerNames1) {
            log.info("request headerNames:  {}", name);
        }
        // 获取请求头
        SignatureHeader signatureHeader = getSignatureHeader(request);
@@ -84,27 +73,33 @@
        String eventType = responseData.getEventType();
        int resultType = 1;
        String resultMessage = "";
        if(WxPayNotifyEventTypeEnum.TRANSACTION_SUCCESS.getCode().equals(eventType)){
            PartnerTransactionsNotifyResult notifyResult = wxService.getEcommerceService().parsePartnerNotifyResult(notifyData, signatureHeader);
            PartnerTransactionsResult result = notifyResult.getResult();
            // 支付通知回调
            if("SUCCESS".equals(result.getTradeState())){
                orderService.payBack(result);
            }
            resultMessage = GSON.toJson(notifyResult);
        } else if(WxPayNotifyEventTypeEnum.REFUND_SUCCESS.getCode().equals(eventType)
            || WxPayNotifyEventTypeEnum.REFUND_ABNORMAL.getCode().equals(eventType)
            || WxPayNotifyEventTypeEnum.REFUND_CLOSED.getCode().equals(eventType)){
        String resultMessage = notifyData;
            RefundNotifyResult result = wxService.getEcommerceService().parseRefundNotifyResult(notifyData, signatureHeader);
            orderService.orderRefundBack(result);
            resultType = 2;
            resultMessage = GSON.toJson(result);
        try {
            if (WxPayNotifyEventTypeEnum.TRANSACTION_SUCCESS.getCode().equals(eventType)) {
                PartnerTransactionsNotifyResult notifyResult = wxService.getEcommerceService().parsePartnerNotifyResult(notifyData, signatureHeader);
                PartnerTransactionsResult result = notifyResult.getResult();
                // 支付通知回调
                if ("SUCCESS".equals(result.getTradeState())) {
                    orderService.payBack(result);
                }
                resultMessage = GSON.toJson(notifyResult);
            } else if (WxPayNotifyEventTypeEnum.REFUND_SUCCESS.getCode().equals(eventType)
                    || WxPayNotifyEventTypeEnum.REFUND_ABNORMAL.getCode().equals(eventType)
                    || WxPayNotifyEventTypeEnum.REFUND_CLOSED.getCode().equals(eventType)) {
                RefundNotifyResult result = this.parseRefundNotifyResult(notifyData, signatureHeader);
                orderService.orderRefundBack(result);
                resultType = 2;
                resultMessage = GSON.toJson(result);
            }
        }catch (Exception e){
            e.printStackTrace();
            log.info("微信支付/退款通知异常:  {}", e.getMessage());
        }
        // 保存支付/退款回调信息
        backMessageService.saveBackMessage(resultType, resultMessage);
        return WxPayNotifyV3Response.success("成功");
    }
@@ -120,14 +115,19 @@
        ProfitSharingNotifyNewResult notifyResult = getProfitSharingNotifyData(notifyData, signatureHeader);
        ProfitSharingNotifyResult result = notifyResult.getResult();
        String eventType = "-1";
        String resultMessage = notifyData;
        try {
            eventType = notifyResult.getRawData().getEventType();
            if (WxPayNotifyEventTypeEnum.PROFIT_SHARING_SUCCESS.getCode().equals(eventType)) {
                // 分账通知回调
                orderService.profitSharingBack(result);
            }
        String eventType = notifyResult.getRawData().getEventType();
        if(WxPayNotifyEventTypeEnum.PROFIT_SHARING_SUCCESS.getCode().equals(eventType)){
            // 分账通知回调
            orderService.profitSharingBack(result);
            resultMessage = GSON.toJson(notifyResult);
        }catch (Exception e){
            log.info("微信分账通知通知异常:  {}", e.getMessage());
        }
        String resultMessage = GSON.toJson(notifyResult);
        // 保存记录分账回调信息
        backMessageService.saveBackMessage(3, resultMessage);
@@ -165,6 +165,24 @@
        }
    }
    private RefundNotifyResult parseRefundNotifyResult(String notifyData, SignatureHeader header) throws WxPayException {
        NotifyResponse response = GSON.fromJson(notifyData, NotifyResponse.class);
        NotifyResponse.Resource resource = response.getResource();
        String cipherText = resource.getCiphertext();
        String associatedData = resource.getAssociatedData();
        String nonce = resource.getNonce();
        String apiV3Key = this.wxService.getConfig().getApiV3Key();
        try {
            String result = AesUtils.decryptToString(associatedData, nonce, cipherText, apiV3Key);
            RefundNotifyResult notifyResult = GSON.fromJson(result, RefundNotifyResult.class);
            notifyResult.setRawData(response);
            return notifyResult;
        } catch (GeneralSecurityException | IOException e) {
            throw new WxPayException("解析报文异常!", e);
        }
    }
    private boolean verifyNotifySign(SignatureHeader header, String data) throws WxPayException {
        String beforeSign = String.format("%s\n%s\n%s\n",
                header.getTimeStamp(),