/** * 支付成功结果通知 */ @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()); } }
微信支付对接通知
最新推荐文章于 2024-10-03 23:45:00 发布