admin
2024-10-17 b30fb8afd3cd6228bda9b182dc412bb3c8daf69c
src/main/java/com/yeshi/buwan/controller/WXController.java
@@ -1,7 +1,8 @@
package com.yeshi.buwan.controller;
import com.yeshi.buwan.service.inter.vip.VIPService;
import com.yeshi.buwan.service.inter.order.OrderService;
import com.yeshi.buwan.util.StringUtil;
import com.yeshi.buwan.util.log.LoggerUtil;
import com.yeshi.buwan.util.user.VipUtil;
import com.yeshi.buwan.util.vip.VIPOrderUtil;
import net.sf.json.JSONObject;
@@ -20,8 +21,6 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.security.GeneralSecurityException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
@@ -34,7 +33,7 @@
    Logger logger = LoggerFactory.getLogger(WXController.class);
    @Resource
    private VIPService vipService;
    private OrderService orderService;
    private static String decryptToString(String apiV3Key, String associatedData, String nonce, String ciphertext)
            throws GeneralSecurityException, IOException {
@@ -64,9 +63,10 @@
        WXAPPInfo wxappInfo = VipUtil.getWXAPP();
        //验证证书序列号
        String mchSerialNo = request.getHeader("Wechatpay-Serial");
        if (!mchSerialNo.equalsIgnoreCase(wxappInfo.getMchSerialNo())) {
            throw new Exception("证书序列号不一致");
        }
        LoggerUtil.getTestLogger().info("微信支付mchSerialNo:{}",mchSerialNo);
//        if (!mchSerialNo.equalsIgnoreCase(wxappInfo.getMchSerialNo())) {
//            throw new Exception("证书序列号不一致");
//        }
        String timeStamp = request.getHeader("Wechatpay-Timestamp");
        String nonce = request.getHeader("Wechatpay-Nonce");
@@ -77,7 +77,7 @@
            if (request.getInputStream() != null) {
                String entity = IOUtils.toString(request.getInputStream(), "UTF-8");
                data = entity;
                logger.error("微信支付回调entity:" + entity);
                LoggerUtil.getTestLogger().info("微信支付回调entity:{}",entity);
            }
        } catch (IOException e) {
            e.printStackTrace();
@@ -97,6 +97,7 @@
                String ciphertext = resource.optString("ciphertext");
                String r = decryptToString(wxappInfo.getApiV3Key(), resource.optString("associated_data"), resource.optString("nonce"), ciphertext);
                //解密格式如下 {"mchid":"1520950211","appid":"wxa99686bb65a9f466","out_trade_no":"buwan-vip-8","transaction_id":"4200000796202101259681241680","trade_type":"MWEB","trade_state":"SUCCESS","trade_state_desc":"支付成功","bank_type":"OTHERS","attach":"","success_time":"2021-01-25T16:18:33+08:00","payer":{"openid":"oq7R20lxhKF8qSnkszxFJHViyKEY"},"amount":{"total":10,"payer_total":10,"currency":"CNY","payer_currency":"CNY"}}
                LoggerUtil.getTestLogger().info("数据解码:{}",r);
                JSONObject decript = JSONObject.fromObject(r);
                String outTradeNo = decript.optString("out_trade_no");
                String appId = decript.optString("appid");
@@ -105,7 +106,7 @@
                //支付成功
                if (tradeState.equalsIgnoreCase("SUCCESS")) {
                    String id = VIPOrderUtil.getIdFromOutOrderNo(outTradeNo);
                    vipService.checkOrderPayState(id);
                    orderService.checkOrderPayState(id);
                }
                break;
        }
@@ -127,6 +128,7 @@
            data.put("message", "处理成功");
            response.sendError(200, data.toString());
        } catch (Exception e) {
            logger.error("微信支付回调处理出错:",e);
            e.printStackTrace();
            JSONObject data = new JSONObject();
            data.put("code", "FAIL");