星空网 > 软件开发 > ASP.net

微信JSApi支付~集成到MVC环境后的最后一个坑(网上没有这种解决方案)

大叔第一人

之前写了关于微信的坑《微信JSApi支付~坑和如何填坑》,今天将微信的jsapi支付封装到了MVC环境里,当然也出现了一些新的坑,如支付参数应该是Json对象而不是Json字符串,这样也会应付引起“get_brand_wcpay_request:fail_invalid appid”这个大家都知道的异常,呵呵,解决方案网上说是“授权目录“,事实上,还有一种原因,那就是你的WeixinJSBridge.invoke方法里的参数应该是Json对象而不是字符串,这才是最重要的。

代码我们需要写成下面的格式

    //调用微信JS api 支付    function jsApiCall() {      var price = 1.0;      var no = '@Request.QueryString["orderNumber"]';      var action = '@Url.Action("Get")';      var openID = '@openID';      $.get(action, { price: price, orderNumber: no, openID: openID }, function (data) {        WeixinJSBridge.invoke('getBrandWCPayRequest', JSON.parse(data), function (res) {          WeixinJSBridge.log(res.err_msg);          alert(res.err_code + res.err_desc + res.err_msg);        });      });    }

对于我们封装成MVC后,由于MVC的路由将扩展名去除了,所以我们的授权目录也发生了变化,如Order/Pay这个页面,在收取时应该是http://域名/Order/Pay/,而之前的http://域名/Order/这个级别就不被认可了,这点也要注意一下。

微信JSApi支付~集成到MVC环境后的最后一个坑(网上没有这种解决方案)

还有一点要注册,如果你添加了测试授权目录,那必须要添加测试用的白名单,否则你的微信也测试不了。

大叔封装的MVC版的微信JSAPI支付

  /// <summary>  /// 构建支付处理类  /// </summary>  public class JsApiImplement  {    public static string wxJsApiParam { get; set; } //H5调起JS API参数    /// <summary>    /// 返回当前微信客户端的OpenId,每个客户端在每个公众号里的OpenId是唯一的    /// </summary>    /// <returns></returns>    public static string GetOpenId()    {      JsApiPay jsApiPay = new JsApiPay(System.Web.HttpContext.Current);      jsApiPay.GetOpenidAndAccessToken();      Log.Debug("GetOpenId", "openid : " + jsApiPay.openid);      return jsApiPay.openid;    }    /// <summary>    /// JsApi返回微信支付的连接参数,这个方法需要前台UI页面调用,通常可以使用AJAX进行调用它    /// </summary>    /// <param name="total_fee">订单金额</param>    /// <param name="orderId">业务的订单编号</param>    /// <returns></returns>    public static string Send(int total_fee, string orderId, string openId)    {      try      {        //调用【网页授权获取用户信息】接口获取用户的openid和access_token        //jsApiPay.GetOpenidAndAccessToken();        JsApiPay jsApiPay = new JsApiPay(System.Web.HttpContext.Current);        jsApiPay.openid = openId;        Log.Debug("Send", "openid : " + jsApiPay.openid);        //若传递了相关参数,则调统一下单接口,获得后续相关接口的入口参数,微信的价格是分        jsApiPay.total_fee = total_fee;        WxPayData unifiedOrderResult = jsApiPay.GetUnifiedOrderResult(orderId);        wxJsApiParam = jsApiPay.GetJsApiParameters();//获取H5调起JS API参数                  Log.Debug("Send", "wxJsApiParam : " + wxJsApiParam);      }      catch (Exception ex)      {        Log.Error("Error", ex.Message);      }      return wxJsApiParam;    }    /// <summary>    /// JsApi微信回调    /// </summary>    public static void Notify(Action<NotifyModel> action)    {      var context = System.Web.HttpContext.Current;      ResultNotify resultNotify = new ResultNotify(context);      resultNotify.ProcessNotify(action);    }  }

对于使用者来说,也很简单,在页面上拿OpenId,之后把OpenId传到后台方法,拿到微信支付的参数对象(JSON对象),之后完成支付

<script type="text/javascript">  //调用微信JS api 支付  function jsApiCall() {    $.get("/weixin/get", new { money: 1, order: '001', openId: 'test' }, function (data) {      WeixinJSBridge.invoke('getBrandWCPayRequest', JSON.parse(data), function (res) {        WeixinJSBridge.log(res.err_msg);        alert(res.err_code + res.err_desc + res.err_msg);      });    });  }  function callpay() {    if (typeof (WeixinJSBridge) == "undefined") {      if (document.addEventListener) {        document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);      }      else if (document.attachEvent) {        document.attachEvent('WeixinJSBridgeReady', jsApiCall);        document.attachEvent('onWeixinJSBridgeReady', jsApiCall);      }    }    else {      jsApiCall();    }  }</script><body>  <a href="javascript:void(0)" onclick="callpay();return false;">立即支付</a></body>

希望大家一起来找各种坑,然后把坑填上,分享给大家!

微信JSApi支付~坑和如何填坑




原标题:微信JSApi支付~集成到MVC环境后的最后一个坑(网上没有这种解决方案)

关键词:JS

JS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

tiktok安卓可以用吗:https://www.goluckyvip.com/tag/82284.html
tiktok,怎么下载:https://www.goluckyvip.com/tag/82285.html
tiktok怎么转换成中文:https://www.goluckyvip.com/tag/82286.html
ios怎么下tiktok:https://www.goluckyvip.com/tag/82287.html
tiktok怎么更换别的国家:https://www.goluckyvip.com/tag/82288.html
tiktok安卓加速器:https://www.goluckyvip.com/tag/82289.html
回乡创业,不知道干什么,能否推荐几个农村老家创业的好项目?:https://www.vstour.cn/a/365177.html
恐怖游轮2002 恐怖游轮2022:https://www.vstour.cn/a/365178.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流