admin
2022-01-28 cd7767932dddeaf6d9c73a83d4a9b38f0341b77f
src/main/java/com/yeshi/buwan/controller/WXController.java
@@ -1,6 +1,11 @@
package com.yeshi.buwan.controller;
import com.yeshi.buwan.service.inter.order.OrderService;
import com.yeshi.buwan.service.inter.vip.VIPService;
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;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
@@ -8,8 +13,8 @@
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.yeshi.utils.entity.wx.WXAPPInfo;
import org.yeshi.utils.wx.WXPayV3Util;
import javax.annotation.Resource;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.GCMParameterSpec;
@@ -17,6 +22,8 @@
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;
@@ -27,6 +34,9 @@
@RequestMapping("wx")
public class WXController {
    Logger logger = LoggerFactory.getLogger(WXController.class);
    @Resource
    private OrderService orderService;
    private static String decryptToString(String apiV3Key, String associatedData, String nonce, String ciphertext)
            throws GeneralSecurityException, IOException {
@@ -53,12 +63,13 @@
     * @throws Exception
     */
    private void process(HttpServletRequest request) throws Exception {
        WXAPPInfo wxappInfo = new WXAPPInfo();
        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");
@@ -69,7 +80,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();
@@ -89,6 +100,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");
@@ -96,15 +108,11 @@
                String tradeState = decript.optString("trade_state");
                //支付成功
                if (tradeState.equalsIgnoreCase("SUCCESS")) {
                    boolean isPaySuccess = WXPayV3Util.isPaySuccess(outTradeNo, wxappInfo);
                    if (isPaySuccess) {
                        //TODO 支付成功
                    }
                    String id = VIPOrderUtil.getIdFromOutOrderNo(outTradeNo);
                    orderService.checkOrderPayState(id);
                }
                break;
        }
    }
@@ -123,6 +131,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");