doum
2025-09-15 82735e4d46ac7b9969facef2acc8f8e793b68f71
server/admin/src/main/java/com/doumee/api/business/PaymentCallback.java
@@ -1,12 +1,15 @@
package com.doumee.api.business;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.doumee.config.wx.TransferDetailEntityNew;
import com.doumee.config.wx.WxMiniConfig;
import com.doumee.core.constants.Constants;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.model.ApiResponse;
import com.doumee.service.business.OrdersService;
import com.doumee.service.business.WithdrawalOrdersService;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.RSAPublicKeyConfig;
@@ -16,6 +19,7 @@
import com.wechat.pay.java.service.partnerpayments.jsapi.model.Transaction;
import com.wechat.pay.java.service.refund.model.RefundNotification;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
@@ -46,6 +50,9 @@
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private WithdrawalOrdersService withdrawalOrdersService;
    /**
     * 【微信支付】异步通知
     *
@@ -53,6 +60,7 @@
     */
    @PostMapping("/web/wxPayNotify")
    public ApiResponse wxPay_notify(HttpServletRequest request) {
        log.error("微信支付回调结果开始===========" );
        try {
            ServletInputStream inputStream = request.getInputStream();
            StringBuffer stringBuffer = new StringBuffer();
@@ -77,9 +85,11 @@
                    .body(body)
                    .build();
            NotificationConfig config = new RSAAutoCertificateConfig.Builder()
            NotificationConfig config = new RSAPublicKeyConfig.Builder()
                    .merchantId(WxMiniConfig.wxProperties.getMchId())
                    .privateKeyFromPath(WxMiniConfig.wxProperties.getPrivateKeyPath())
                    .publicKeyFromPath(WxMiniConfig.wxProperties.getPubKeyPath())
                    .publicKeyId(WxMiniConfig.wxProperties.getPayPublicKeyId())
                    .merchantSerialNumber(WxMiniConfig.wxProperties.getSerialNumer())
                    .apiV3Key(WxMiniConfig.wxProperties.getApiV3Key())
                    .build();
@@ -89,6 +99,7 @@
            Transaction result = parser.parse(requestParam, Transaction.class);
            log.error("支付回调信息:{}"+ JSONObject.toJSONString(result));
            //自定义订单号
            String outTradeNo = result.getOutTradeNo();
            //微信订单号
@@ -105,11 +116,12 @@
            } else {
                // 支付失败
                switch (result.getAttach()) {
                    case "createGoodsOrder": {
                    case "createOrder": {
                        break;
                    }
                }
            }
            log.error("微信支付回调结果结束===========" );
            return ApiResponse.success("处理成功!");
        } catch (Exception e) {
            e.printStackTrace();
@@ -146,13 +158,20 @@
                    .body(body)
                    .build();
            NotificationConfig config = new RSAAutoCertificateConfig.Builder()
            NotificationConfig config = /*new RSAAutoCertificateConfig.Builder()
                    .merchantId(WxMiniConfig.wxProperties.getMchId())
                    .privateKeyFromPath(WxMiniConfig.wxProperties.getPrivateKeyPath())
                    .merchantSerialNumber(WxMiniConfig.wxProperties.getSerialNumer())
                    .apiV3Key(WxMiniConfig.wxProperties.getApiV3Key())
                    .build();*/
            new RSAPublicKeyConfig.Builder()
                    .merchantId(WxMiniConfig.wxProperties.getMchId())
                    .privateKeyFromPath(WxMiniConfig.wxProperties.getPrivateKeyPath())
                    .publicKeyFromPath(WxMiniConfig.wxProperties.getPubKeyPath())
                    .publicKeyId(WxMiniConfig.wxProperties.getPayPublicKeyId())
                    .merchantSerialNumber(WxMiniConfig.wxProperties.getSerialNumer())
                    .apiV3Key(WxMiniConfig.wxProperties.getApiV3Key())
                    .build();
            NotificationParser parser = new NotificationParser(config);
            RefundNotification result = parser.parse(requestParam, RefundNotification.class);
@@ -177,14 +196,30 @@
     * @return
     */
    @PostMapping(value = "/web/wechat/transferNotify")
    public ApiResponse wxPayCallback( HttpServletRequest request) {
    public ApiResponse transferNotify( HttpServletRequest request) {
        Map<String,String> errMap = new HashMap<>();
        try {
            log.info("微信商户零线转账 - 回调通知 /wxpay/callback");
            TransferDetailEntityNew entity = wxPaySuccessCallback(request);
            log.info("transfer ok.{}",entity);
            log.error("微信商户零线转账 - 回调通知 /wxpay/callback");
            Config config = new RSAPublicKeyConfig.Builder()
                .merchantId(WxMiniConfig.wxProperties.getSubMchId()) //微信支付的商户号
                .privateKeyFromPath(WxMiniConfig.wxProperties.getWechatPrivateKeyPath()) // 商户API证书私钥的存放路径
                .publicKeyFromPath(WxMiniConfig.wxProperties.getWechatPubKeyPath()) //微信支付公钥的存放路径
                .publicKeyId(WxMiniConfig.wxProperties.getWechatPayPublicKeyId()) //微信支付公钥ID
                .merchantSerialNumber(WxMiniConfig.wxProperties.getWechatSerialNumer()) //商户API证书序列号
                .apiV3Key(WxMiniConfig.wxProperties.getWechatApiV3Key()) //APIv3密钥
                .build();
            TransferDetailEntityNew entity = wxSuccessCallback(request,config);
            log.error("transfer ok.{}",entity);
            //回调成功后处理自己的业务
            if(entity != null){
                if((entity.getState().equals(Constants.SUCCESS)
                        || entity.getState().equals(Constants.CANCELLED)
                        || entity.getState().equals(Constants.FAIL))
                        && StringUtils.isNotBlank(entity.getOutBillNo())){
                    withdrawalOrdersService.transferSuccess(entity.getOutBillNo(),entity.getState().equals(Constants.SUCCESS)?true:false);
                }
            }
            return ApiResponse.success("处理成功");
        } catch (Exception e) {
@@ -195,7 +230,7 @@
        }
    }
    public TransferDetailEntityNew wxPaySuccessCallback(HttpServletRequest request) throws IOException {
    public TransferDetailEntityNew wxSuccessCallback(HttpServletRequest request,Config config) throws IOException {
        String requestBody = getBodyString(request, "UTF-8");
        //证书序列号(微信平台)   验签的“微信支付平台证书”所对应的平台证书序列号
        String wechatPaySerial = request.getHeader("Wechatpay-Serial");
@@ -214,14 +249,14 @@
                .body(requestBody)
                .build();
        // 2. 构建Config RSAPublicKeyConfig
        Config config = new RSAPublicKeyConfig.Builder()
                        .merchantId(WxMiniConfig.wxProperties.getMchId()) //微信支付的商户号
                        .privateKeyFromPath(WxMiniConfig.wxProperties.getPrivateKeyPath()) // 商户API证书私钥的存放路径
                        .publicKeyFromPath(WxMiniConfig.wxProperties.getWechatPubKeyPath()) //微信支付公钥的存放路径
                        .publicKeyId(WxMiniConfig.wxProperties.getWechatPayPublicKeyId()) //微信支付公钥ID
                        .merchantSerialNumber(WxMiniConfig.wxProperties.getSerialNumer()) //商户API证书序列号
                        .apiV3Key("") //APIv3密钥
                        .build();
//        Config config = new RSAPublicKeyConfig.Builder()
//                        .merchantId(WxMiniConfig.wxProperties.getMchId()) //微信支付的商户号
//                        .privateKeyFromPath(WxMiniConfig.wxProperties.getPrivateKeyPath()) // 商户API证书私钥的存放路径
//                        .publicKeyFromPath(WxMiniConfig.wxProperties.getWechatPubKeyPath()) //微信支付公钥的存放路径
//                        .publicKeyId(WxMiniConfig.wxProperties.getWechatPayPublicKeyId()) //微信支付公钥ID
//                        .merchantSerialNumber(WxMiniConfig.wxProperties.getSerialNumer()) //商户API证书序列号
//                        .apiV3Key("") //APIv3密钥
//                        .build();
        log.info("WxPayService.wxPaySuccessCallback request : wechatPaySerial is [{}]  , wechatSignature is [{}] , wechatTimestamp is [{}] , wechatpayNonce  is [{}] , requestBody is [{}]",wechatPaySerial,wechatSignature,wechatTimestamp,wechatpayNonce,requestBody);
        // 3. 初始化 NotificationParser
        NotificationParser parser = new NotificationParser((NotificationConfig) config);