七 SpringCloud - 微信支付( 四 )

4.3 请求方法/*** @author : huayu* @date: 3/11/2022* @param: [request, response]* @return : void* @description : 接收用户支付成功后,微信异步回调支付结果*/@RequestMapping("/wxpay/notifyresult")public void receiveWechatPayNotifyResult(HttpServletRequest request, HttpServletResponse response){//支付完成后,微信会把相关支付结果及用户信息通过数据流的形式发送给商户,商户需要接收处理,并按文档规范返回应答 。try(InputStream inputStream = request.getInputStream()){//解析回调数据流BufferedReader brf = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));//定义可变字符对象,保存返回的xml格式支付结果StringBuilder wxpayNotifyXmlResult = new StringBuilder();//逐行解析String readline = null;while ((readline = brf.readLine()) != null){wxpayNotifyXmlResult.append(readline);}log.info("------ 3.微信支付结果异步回调内容:{} ------",wxpayNotifyXmlResult);//调用业务接口,解析异步回调的支付结果,并获取通知微信官方的结果String respWechatXmlResult = wechatPayService.getNotifyWechatXmlResult(wxpayNotifyXmlResult.toString());//同步给微信官方响应结果PrintWriter out = response.getWriter();out.write(respWechatXmlResult);out.flush();out.close();log.info("------ 4、微信支付异步回调处理后,响应结果:{} ------",respWechatXmlResult);}catch (Exception e){log.info("------ 微信支付结果异步回调处理异常,异常信息:{} ------", e.getMessage());}}4.4 请求测试(微信官方回调)

七 SpringCloud - 微信支付

文章插图
5、查看用户订单状态
  1. 获取参数,放进map集合中并按key值,字典排序 。
  2. 通过参数生成签名(生成的签名也放进map集合) 。
  3. 将map集合转成xml字符串 。
  4. 获取订单状态查询地址,xml参数字符串作为参数发送请求 。
  5. 返回订单状态信息 。
5.0 参数列表
七 SpringCloud - 微信支付

文章插图
5.1 接口/*** @author : huayu* @date: 3/11/2022* @param: [orderNo]* @return : java.lang.String* @description : 通过订单好查询订单状态*/Map<String, String> getOrderStatus(String orderNo) throws Exception;5.2 实现类@Overridepublic Map<String, String> getOrderStatus(String orderNo) throws Exception {log.info("++++++获取查询订单状态的参数 ++++++");//参数集合TreeMap<String, String> paramsMap = new TreeMap<>();//公众账号IDappidparamsMap.put("appid",wechatPayConfig.getAppId());//商户号 mch_idparamsMap.put("mch_id",wechatPayConfig.getMchId());//商户订单号 out_trade_noparamsMap.put("out_trade_no",orderNo);//随机字符串 nonce_strparamsMap.put("nonce_str",WechatPayUtil.generateNonceStr());//签名 signparamsMap.put("sign",WechatPayUtil.generateSignature(paramsMap,wechatPayConfig.getMchKey()));//将map集合的参数转换成xml格式的字符串String paramsMapToXml = WechatPayUtil.generateMapToXml(paramsMap);log.info("++++++查询订单状态的参数:{} ++++++",paramsMapToXml);//发送post请求,获取订单状态 xml格式字符串String orderStatusXmlResult = HttpClient4Util.getResponse4PostByString(wechatPayConfig.getViewOrderStatusUri(), paramsMapToXml, WechatPayConstant.WECHAT_PAY_ENCODING_UTF8);log.info("++++++发送post请求,获取订单状态 xml格式字符串 orderStatusXmlResult:++++++\n{}",orderStatusXmlResult);//将xml格式结果字符串转化成 map 集合Map<String, String> orderStatusXmlResultToMap = WechatPayUtil.generateXmlToMap(orderStatusXmlResult);//判断返回结果//交易成功判断条件: return_code、result_code和trade_state都为SUCCESSif(WechatPayConstant.WECHAT_PAY_VIEW_ORDER_STATUS_SUCCESS.equals(orderStatusXmlResultToMap.get("return_code"))&& WechatPayConstant.WECHAT_PAY_VIEW_ORDER_STATUS_SUCCESS.equals(orderStatusXmlResultToMap.get("result_code"))&& WechatPayConstant.WECHAT_PAY_VIEW_ORDER_STATUS_SUCCESS.equals(orderStatusXmlResultToMap.get("trade_state"))){//返回订单信息return orderStatusXmlResultToMap;}return null;}

经验总结扩展阅读