微信扫码支付回调处理

1.设置回调地址

String notify_url= "/notify/pay/supplier/wechat/rechargePayNotifyRes.htm";

2.接收回调信息并解析

    /**
     * 微信供应商充值的异步回调通知
     */
    @RequestMapping(value = "/notify/pay/supplier/wechat/rechargePayNotifyRes.htm")
    @ResponseBody
    @ApiIgnore
    public void supplierRechargeWXPayNotify(HttpServletRequest request,HttpServletResponse response) throws Exception {
        log.info("====== 开始接收微信支付回调(供应商充值)通知 ======");
        InputStream inputStream = request.getInputStream();
        //BufferedReader是包装设计模式,性能更搞
        BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
        StringBuffer sb = new StringBuffer();
        //1、将微信回调信息转为字符串
        String line;
        while ((line = in.readLine()) != null) {
            sb.append(line);
        }
        in.close();
        inputStream.close();
        //2、将xml格式字符串格式转为map集合
        Map<String, String> callbackMap = WXPayUtil.xmlToMap(sb.toString());
        //{transaction_id=4200000406201910302624423717, nonce_str=2I7gxX84Xs7i7Wg1qFdBuSLKYiAaPzq4, bank_type=CFT,
        // openid=oAmW11HX2ghkmdIVol2l6NEHhRHY, sign=8BA2949611A06D0F4C58992A2F357DAB, fee_type=CNY, mch_id=1557130741,
        // cash_fee=1, out_trade_no=P2019103031189489360957272065, appid=wx551f77763ebda8a2, total_fee=1, trade_type=NATIVE,
        // result_code=SUCCESS, time_end=20191030183017, is_subscribe=N, return_code=SUCCESS}
        log.info("接收到的微信支付回调结果为:{}", callbackMap);
         payOrderService.supplierRechargeWXPayNotify(callbackMap,response);
    }

3.处理具体相关业务并通知微信处理结果

    /**
   * 微信供应商充值的异步回调通知
   * //{transaction_id=4200000406201910302624423717, nonce_str=2I7gxX84Xs7i7Wg1qFdBuSLKYiAaPzq4, bank_type=CFT,
   * // openid=oAmW11HX2ghkmdIVol2l6NEHhRHY, sign=8BA2949611A06D0F4C58992A2F357DAB, fee_type=CNY, mch_id=1557130741,
   * // cash_fee=1, out_trade_no=P2019103031189489360957272065, appid=wx551f77763ebda8a2, total_fee=1, trade_type=NATIVE,
   * // result_code=SUCCESS, time_end=20191030183017, is_subscribe=N, return_code=SUCCESS}
   *
   * @param callbackMap
   * @param response
   * @return
   */
  @Override
  public void supplierRechargeWXPayNotify(Map<String, String> callbackMap, HttpServletResponse response) throws Exception {

      if ("SUCCESS".equals(callbackMap.get("result_code"))) {
          //获取商户订单号
          //商户系统内部订单号,要求32个字符内,只能是数字、大小写字母_-|* 且在同一个商户号下唯一
          //3、判断回调信息是否成功
          String outTradeNo = callbackMap.get("out_trade_no");
          System.out.println(outTradeNo);
          //6、数据库查找订单,如果存在则根据订单号更新该订单
          TbCharge entity = new TbCharge();
          entity.setChargeId(outTradeNo);
          TbCharge tbCharge = tbChargeMapper.selectOne(entity);
          //判断充值订单中的状态和付款类型
          if (tbCharge != null && tbCharge.getChargeStatus() == 0 && tbCharge.getType() == 1) {
              String totalFee = callbackMap.get("total_fee");
              double fen = Double.parseDouble(totalFee);
              double v = fen / 100;
              if (v != tbCharge.getAmount().doubleValue()) {
                  log.error("微信支付成功后的回调金额与充值金额不一致");
                  throw new RrkException("微信支付成功后的回调金额与充值金额不一致");
              } else {
                  try {
                      tbCharge.setRemark(callbackMap.get("openid"));
                      tbCharge.setUpdateTime(System.currentTimeMillis());
                      tbCharge.setPayTime(System.currentTimeMillis());
                      //修改支付状态,之前生成的订单支付状态是未支付,这里表面已经支付成功的订单
                      tbCharge.setChargeStatus(1);
                      //根据商户订单号更新订单
                      Integer integer = tbChargeMapper.updateById(tbCharge);
                      log.error("微信支付成功后的回调处理结果为integer={}", integer);
                      //7、通知微信订单处理成功
                      if (integer > 0) {
                          response.setContentType("text/xml");
                          response.getWriter().println("success");
                          return;
                      }
                  } catch (Exception e) {
                      log.error("更新供应商微信充值状态失败,原因是:" + e.getMessage());
                      throw new RrkException("更新供应商微信充值状态失败");
                  }
              }
          }
      }
      //7、通知微信订单处理失败
      response.setContentType("text/xml");
      response.getWriter().println("fail");
  }
发布了26 篇原创文章 · 获赞 1 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_42643690/article/details/102831603