支付完成后,第三方支付系统会主动通知支付结果,要实现主动通知,需要在请求支付系统下单时传入NotifyUrl,如果支付成功,使用消息队列通知学习中心添加选课记录到我的课程表。
查询Controller
@RequestMapping("/paynotify") public void paynotify(HttpServletRequest request, HttpServletResponse response) throws IOException, AlipayApiException { HashMap<String, String> params = new HashMap<>(); Map<String, String[]> requestParams = request.getParameterMap(); for (Iterator<String> iterator = requestParams.keySet().iterator(); iterator.hasNext();) { String name = iterator.next(); String[] values = requestParams.get(name); String valueStr = ""; for (int i = 0; i < values.length; i++) { valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ","; } params.put(name, valueStr); }
boolean signVerified = AlipaySignature.rsaCheckV1(params, ALIPAY_PUBLIC_KEY, AlipayConfig.CHARSET, AlipayConfig.SIGNTYPE); if (signVerified) { String outTradNo = new String(request.getParameter("out_trade_no").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8); String tradeNo = new String(request.getParameter("trade_no").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8); String tradeStatus = new String(request.getParameter("trade_status").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8); String totalAmount = new String(request.getParameter("total_amount").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8); if (tradeStatus.equals("TRADE_SUCCESS")) { PayStatusDTO payStatusDTO = new PayStatusDTO(); payStatusDTO.setOut_trade_no(outTradNo); payStatusDTO.setTrade_no(tradeNo); payStatusDTO.setTrade_status(tradeStatus); payStatusDTO.setApp_id(APP_ID); payStatusDTO.setTotal_amount(totalAmount); orderService.saveAlipayStatus(payStatusDTO); } response.getWriter().write("success"); } else { response.getWriter().write("fail"); } }
|