【微信支付】微信JSAPI支付流程 (5)

微信下单接口,关键代码(服务层)

 @Resource(name = WxParament.H5_WX_PAY)
 private WXPay wxH5Pay;

/* 微信统一下单 */
   private Map<String, String> wxUnifiedOrder(String orderNo, String orderFee, String requestIp, String openid) throws RuntimeException {
      Map<String, String> data = new HashMap<String, String>();
      data.put("nonce_str", WXPayUtil.generateNonceStr());
      data.put("body", "我来下单啦");
      data.put("out_trade_no", orderNo);
      data.put("sign_type", "MD5");
      data.put("total_fee", orderFee);
      data.put("spbill_create_ip", requestIp);
      data.put("openid", openid);
      data.put("notify_url",ymlParament.getWx_callback_url());
      data.put("trade_type", "JSAPI"); // 此处指定为JSAPI支付
      Map<String, String> wxOrderResult = WxPay.unifiedorder(data,wxH5Pay);
     if("FAIL".equals(wxOrderResult.get("return_code"))){
       throw new RuntimeException(wxOrderResult.get("return_msg"));
     }
        /* IsNull自定义,主要判断非空 */
      if (IsNull.isNull(wxOrderResult.get("prepay_id"))) {
         throw new RuntimeException("微信支付下单成功后,返回的prepay_id为空");
      }
      return wxOrderResult;

   }

 @Override
 public Map<String, String> insertWxH5Pay(String orderNo,String orderFee, String requestIp, String openid) {
  try {
   /* 微信下单 */
   Map<String, String> wxOrderResult = wxUnifiedOrder(orderNo, orderFee, requestIp, openid);
   Map<String, String> chooseWXPay = new HashMap<>();
            /* 这里我遇到一个大坑,这里的key值是要驼峰式写法,而APP支付却不用!!! */
   chooseWXPay.put("appId", ymlParament.getH_app_id());
   chooseWXPay.put("timeStamp", WxUtils.create_timestamp());
   chooseWXPay.put("nonceStr", wxOrderResult.get("nonce_str"));
   chooseWXPay.put("package", "prepay_id=" + wxOrderResult.get("prepay_id"));
   chooseWXPay.put("signType", "MD5");
   String signature = WXPayUtil.generateSignature(chooseWXPay, ymlParament.getH_key());
   chooseWXPay.put("paySign", signature);
   return chooseWXPay;
  } catch (Exception e) {
   e.printStackTrace();
   throw new RuntimeException(e.getMessage());
  }
 }

controller层(略)

微信支付成功回调

关键代码

   @Resource(name = WxParament.H5_WX_PAY)
   private WXPay wxH5Pay;

   @ApiOperation("微信支付回调")
   @PostMapping("callback")
   public String callback(HttpServletRequest request) throws Exception {
    try {
     // 1、获取参数
     Map<String, String> params = getMapByRequest(request);
     log.info("微信回调回来啦!!!!" + params);
     // 2、校验
           checkCallbackWxPay(params);
     //业务逻辑
     return wxPaySuccess();
    } catch (Exception e) {
     e.printStackTrace();
     return wxPayFail(e.getMessage());
    }
   }
   
/**
    * 获取微信过来的请求参数
    * @param request
    * @return
    * @throws Exception
    */
   private static Map<String, String> getMapByRequest(HttpServletRequest request) throws Exception{
    InputStream inStream = request.getInputStream();
    ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len = 0;
    while ((len = inStream.read(buffer)) != -1) {
     outSteam.write(buffer, 0, len);
    }
    Map<String, String> ret= WXPayUtil.xmlToMap(new String(outSteam.toByteArray(), "utf-8"));
    outSteam.close();
    inStream.close();
    return ret;
   } 
   
   /*校验 */
   private void checkCallbackWxPay(Map<String, String> params)
     throws Exception {
    if ("FAIL".equals(params.get("result_code"))) {
     throw new Exception("微信支付失败");
    }
    if (!checkWxCallbackSing(params, wxH5Pay)) {
     throw new Exception("微信支付回调签名认证失败");
    }
    //校验金额
       //判断订单是否重复
       //....业务逻辑
   }
   
/**
    * 检查微信回调签名 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_7
    * 
    * @param wp 传入:@Autowired WXPay
    */
   private static boolean checkWxCallbackSing(Map<String, String> notifyMap, WXPay wp) throws Exception {
    return wp.isPayResultNotifySignatureValid(notifyMap);
   }

   /**
    * 微信支付返回参数结果封装
    */
   private static String wxPaySuccess() throws Exception {
    Map<String, String> succResult = new HashMap<String, String>();
    succResult.put("return_code", "SUCCESS");
    succResult.put("return_msg", "OK");
    return WXPayUtil.mapToXml(succResult);
   }
   /**
    * @param mess 错误消息提示
    * @return 微信返回错我的提示
    * @throws Exception
    */
   private static String wxPayFail(String mess) throws Exception {
    Map<String, String> succResult = new HashMap<String, String>();
    succResult.put("return_code", "FAIL");
    succResult.put("return_msg", IsNull.isNull(mess)?"自定义异常错误!":mess);
    return WXPayUtil.mapToXml(succResult);
   }
补充

如果不想验证签名,还有一种方式判断是否支付成功,就是调用微信查询订单接口查看是否支付成功

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zgwgyy.html