收藏
回答

微信支付回调验证签名问题:Processing WechatPay notification?

报错:Processing WechatPay notification,signature verification failed,signType[WECHATPAY2-SHA256-RSA2048] 

原因:HTTP 请求体 body。切记使用原始报文,不要用 JSON 对象序列化后的字符串,避免验签的 body 和原文不一致。(body顺序乱了不行,HttpServletRequest只能被读取一次)
处理HttpServletRequest工具类:
import com.wechat.pay.java.core.notification.RequestParam;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

public class HttpServletRequestUtils {
    
    public static String getRequestBody(HttpServletRequest request) {
        StringBuilder sb = new StringBuilder();
        try (ServletInputStream inputStream = request.getInputStream();
             BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
        ) {
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        return sb.toString();
    }

    public static RequestParam createRequestParam(HttpServletRequest request){
        String body = getRequestBody(request);
        String timestamp = request.getHeader("Wechatpay-Timestamp");
        String nonce = request.getHeader("Wechatpay-Nonce");
        String signature = request.getHeader("Wechatpay-Signature");
        String singType = request.getHeader("Wechatpay-Signature-Type");
        String number = request.getHeader("Wechatpay-Serial");
        return new RequestParam.Builder()
                .nonce(nonce)
                .signature(signature)
                .signType(singType)
                .timestamp(timestamp)
                .serialNumber(number)
                .body(body)
                .build();
    }
}


回答关注问题邀请回答
收藏
登录 后发表内容