<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>支付</title> <script src="~/Scripts/jquery-1.10.2.min.js"></script> <script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script> </head> <body> <div> 正在支付... </div> <input type="hidden" id="orderd" value="@ViewBag.ordernum" /> </body> </html> <script> var ordernum=$(‘#orderd‘).val(); $(function () { gopay(); }) //调用微信JS api 支付 function onBridgeReady() { var dataParam =@Html.Raw(ViewBag.wxJsApiParam); WeixinJSBridge.invoke( ‘getBrandWCPayRequest‘, dataParam,//,josn串 function (res) { if (res.err_msg == "get_brand_wcpay_request:ok") { tim_j(); } //WeixinJSBridge.log(res.err_msg); //alert(res.err_code + res.err_desc + res.err_msg); } ); } function gopay() { if (typeof WeixinJSBridge == "undefined"){ if( document.addEventListener ){ document.addEventListener(‘WeixinJSBridgeReady‘, onBridgeReady, false); }else if (document.attachEvent){ document.attachEvent(‘WeixinJSBridgeReady‘, onBridgeReady); document.attachEvent(‘onWeixinJSBridgeReady‘, onBridgeReady); } }else{ onBridgeReady(); } } function tim_j() { $.post(‘/RedPack/ListenOrder‘, { OrderNum: ordernum }, function (data) { if (data) { switch (data) { case "1": location.href = "PaySuccess?ordernum="+ordernum; break; case "2": { setTimeout(function () { tim_j(); }, 1000); break; } default: { alert(data); } } } }); } </script>
action
/// <summary> /// 调起微信支付 /// </summary> /// <returns></returns> public ActionResult Pay() { int p; if (!int.TryParse(Request["p"], out p)) return Redirect("Index"); Model.Product product = ProductService.LoadEntities(s => s.ProductId == p).FirstOrDefault(); if (product == null) return Redirect("Index"); if (product.ProductCode.Count(s => !s.IsSell) == 0) return Redirect("SellOut?p=" + p); Model.Order order = OrderService.LoadEntities(s => s.Product.ProductId == p && SSID == s.UserId).OrderByDescending(s => s.OrderId).FirstOrDefault(); string wxJsApiParam = null; if (order != null) { wxJsApiParam = Common.CacheContainer.GetData<string>(Model.Enums.CacheEnum.公众号支付参数.ToString() + order.OrderNum); if (!string.IsNullOrWhiteSpace(wxJsApiParam)) { ViewBag.wxJsApiParam = wxJsApiParam; ViewBag.ordernum = order.OrderNum; return View(); } } order = new Model.Order(); order.CreateTime = DateTime.Now; order.OrderNum = Common.StringHelper.GenerateId(); order.ProductId = p; order.UserId = SSID; order.Status = (int)Model.Enums.OrderStatus.未支付; order.Price = product.Price; order.RebackRedPack = GetRandom(product); order = OrderService.AddEntity(order); try { wxJsApiParam = Utility.WxPayHubba.GetWxJsApiParam(this, UUID, order.OrderNum, product.Price, "***订单"); Common.CacheContainer.SetData(Model.Enums.CacheEnum.公众号支付参数.ToString() + order.OrderNum, wxJsApiParam, 3599 * 24); ViewBag.wxJsApiParam = wxJsApiParam; ViewBag.ordernum = order.OrderNum; return View(); } catch (Exception ex) { Common.LogHelper.WriteLogFile("jsApiPay general wxjsapiparam error = " + ex.Message); } return View(); }
GetWxJsApiParam是封装好的微信支付方法