微信支付的回调函数实现验签以及解密

当我使用的微信的依赖版本大于0.4.2的时候,就可以使用一下方法进行验签和解密

现在我是用的版本是0.4.8

<dependency>
    <groupId>com.github.wechatpay-apiv3</groupId>
    <artifactId>wechatpay-apache-httpclient</artifactId>
    <version>0.4.8</version>
</dependency>

以下是我写的回调函数,也就是回调接口

@PostMapping("/wx-pay-call-back.json")
    public String wxPayCallBack(HttpServletRequest request, HttpServletResponse response) throws ValidationException, ParseException, GeneralSecurityException {
        String readData = HttpUtils.readData(request);
        NotificationRequest notificationRequest = new NotificationRequest.Builder()
                .withSerialNumber(request.getHeader(WechatPayHttpHeaders.WECHAT_PAY_SERIAL))
                .withNonce(request.getHeader(WechatPayHttpHeaders.WECHAT_PAY_NONCE))
                .withTimestamp(request.getHeader(WechatPayHttpHeaders.WECHAT_PAY_TIMESTAMP))
                .withSignature(request.getHeader(WechatPayHttpHeaders.WECHAT_PAY_SIGNATURE))
                .withBody(readData)
                .build();
        NotificationHandler handler = new NotificationHandler(
                verifier, config.getApiV3Key().getBytes(StandardCharsets.UTF_8));
        // 验签和解析请求体
        Notification notification = handler.parse(notificationRequest);

       log.info("====================验签和解析请求体=================>>{}",notification.toString());
        AesUtil util = new AesUtil(config.getApiV3Key().getBytes(StandardCharsets.UTF_8));
        Notification.Resource resource = notification.getResource();
        String ciphertext = resource.getCiphertext();
        log.info("===密文==={}",ciphertext);
        String associatedData = resource.getAssociatedData();
        String nonce = resource.getNonce();
        String plainText = util.decryptToString(associatedData.getBytes(StandardCharsets.UTF_8),nonce.getBytes(StandardCharsets.UTF_8),ciphertext);
        log.info("===明文==={}",plainText);
        response.setStatus(200);
        Map<String,String> result = new HashMap<>();
        result.put("code","SUCCESS");
        result.put("message","成功");
        return JSONObject.toJSONString(result);
    }

工具类

//处理微信回diao xin xi
public class HttpUtils {

    /**
     * 将通知参数转化为字符串
     * @param request
     * @return
     */
    public static String readData(HttpServletRequest request) {
        BufferedReader br = null;
        try {
            StringBuilder result = new StringBuilder();
            br = request.getReader();
            for (String line; (line = br.readLine()) != null; ) {
                if (result.length() > 0) {
                    result.append("\n");
                }
                result.append(line);
            }
            return result.toString();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

 

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喜欢编程的夏先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值