微信支付对接通知

 /**
     * 支付成功结果通知
     */
    @ApiOperation(value = "支付成功结果通知")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "成功")})
    @PostMapping("/playResult")
    public AjaxResult playResult(HttpServletRequest request)
    {
        try {
            String wechatSignature = request.getHeader("Wechatpay-Signature");
            String timestamp = request.getHeader("Wechatpay-Timestamp");
            String nonce = request.getHeader("Wechatpay-Nonce");
            String serialNumber = request.getHeader("Wechatpay-Serial");

//            System.out.println("支付成功结果通知");
//            System.out.println(wechatSignature);
//            System.out.println(timestamp);
//            System.out.println(nonce);
//            System.out.println(serialNumber);



            String requestBody = IOUtils.toString(request.getInputStream(), StandardCharsets.UTF_8);
            System.out.println(requestBody);

            // 构造 RequestParam
            RequestParam requestParam = new RequestParam.Builder()
                    .serialNumber(serialNumber)
                    .nonce(nonce)
                    .signature(wechatSignature)
                    .timestamp(timestamp)
                    .body(requestBody)
                    .build();

            File file = new File(WeiXinConfig.privateKeyPath);
            if (!file.exists()){
                return error("找不到证书");
            }


            // 如果已经初始化了 RSAAutoCertificateConfig,可直接使用
            // 没有的话,则构造一个
            NotificationConfig config = new RSAAutoCertificateConfig.Builder()
                    .merchantId(WeiXinConfig.merchantId)
                    .privateKeyFromPath(WeiXinConfig.privateKeyPath)
                    .merchantSerialNumber(WeiXinConfig.merchantSerialNumber)
                    .apiV3Key(WeiXinConfig.apiV3Key)
                    .build();

            // 初始化 NotificationParser
            NotificationParser parser = new NotificationParser(config);

            try {
                // 以支付通知回调为例,验签、解密并转换成 Transaction
                Transaction transaction = parser.parse(requestParam, Transaction.class);
                if (transaction != null){
                    return dealWeixinOrder(transaction.getOutTradeNo());
                }else {
                    return AjaxResult.error(482,"sign verification failed");
                }
            } catch (ValidationException e) {
                // 签名验证失败,返回 401 UNAUTHORIZED 状态码
                logger.error("sign verification failed", e);
                return AjaxResult.error(401,"sign verification failed");
            }
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error(483,e.getLocalizedMessage());
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值