Example #1
0
        /// <summary>
        /// 支付
        /// </summary>
        /// <param name="id"></param>
        /// <param name="openId"></param>
        /// <param name="mark"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public ActionResult CheckOut(string code, string orderNo)
        {
            string openid = "";

            if (new AppSetting().IsDebug != null &&
                new AppSetting().IsDebug.ToLower() == "true")
            {
                openid = "123";
            }
            else
            {
                if (Request.Cookies[SystemConfig.WXOpenIDCookieKey] != null)
                {
                    openid = Request.Cookies[SystemConfig.WXOpenIDCookieKey].Value;
                }

                if (string.IsNullOrWhiteSpace(openid) && code == null)
                {
                    Response.Redirect(CommonHelper.GetRedirect("WxClass%2fClassList"));
                }
                try
                {
                    if (string.IsNullOrWhiteSpace(openid))
                    {
                        openid = GetOpenId(code).openid;


                        // 合法用户,允许访问
                        Response.Cookies[SystemConfig.WXOpenIDCookieKey].Value   = openid;
                        Response.Cookies[SystemConfig.WXOpenIDCookieKey].Path    = "/";
                        Response.Cookies[SystemConfig.WXOpenIDCookieKey].Expires = DateTime.Now.AddDays(1);
                    }
                }
                catch (Exception ex)
                {
                }
            }

            try
            {
                AppSetting  setting = new AppSetting();
                WxPayClient client  = new WxPayClient();

                OrderBC bc = new OrderBC();

                var order = bc.GetOrderByOrderNo(orderNo);

                string outTradeNumber = string.Format("{0}{1}", orderNo.ToString(), DateTime.Now.ToString("fff"));



                UnifiedOrderRequest req = new UnifiedOrderRequest();
                req.Body   = "万韬财税课程购买";        //商品描述-----------------------
                req.Attach = openid.ToString(); //附加信息,会原样返回,充值人员微信Openid

                req.GoodTag    = "Pay";
                req.TradeType  = "JSAPI";
                req.OpenId     = openid;
                req.OutTradeNo = outTradeNumber;   //---商户订单号----------------
                req.TotalFee   = 1;                //测试总金额
                //req.TotalFee = Convert.ToInt32(order.PayPrice * 100);//总金额
                req.NotifyUrl = setting.NotifyUrl; //异步通知地址-------------------------
                var resp = client.UnifiedOrder(req);

                WxPayData jsApiParam = new WxPayData();
                jsApiParam.SetValue("appId", resp.AppId);
                jsApiParam.SetValue("timeStamp", WxPayApi.GenerateTimeStamp());
                jsApiParam.SetValue("nonceStr", WxPayApi.GenerateNonceStr());
                jsApiParam.SetValue("package", "prepay_id=" + resp.PrepayId);
                jsApiParam.SetValue("signType", "MD5");
                jsApiParam.SetValue("paySign", jsApiParam.MakeSign());

                bc.UpdatePayInfo(outTradeNumber, jsApiParam.ToJson(), orderNo);

                //--给Viewbag赋值,供前台页面jsapi调用
                ViewBag.AppId     = (string)jsApiParam.GetValue("appId");
                ViewBag.Package   = (string)jsApiParam.GetValue("package");
                ViewBag.NonceStr  = (string)jsApiParam.GetValue("nonceStr");
                ViewBag.Paysign   = (string)jsApiParam.GetValue("paySign");
                ViewBag.TimeStamp = (string)jsApiParam.GetValue("timeStamp");
                ViewBag.OpenId    = openid;
                ViewBag.OrderNo   = orderNo;


                ViewBag.OpenId  = openid;
                ViewBag.OrderNo = orderNo;
            }
            catch (Exception ex)
            {
            }
            return(View());
        }