Esempio n. 1
0
        public ActionResult GenBuy(int id)
        {
            try
            {
                var openid      = Request.Cookies["openid"].Value;
                var accessToken = Senparc.Weixin.MP.Containers.AccessTokenContainer.TryGetAccessToken(Config.AppId, Config.AppSecret);
                var userinfo    = Senparc.Weixin.MP.AdvancedAPIs.UserApi.Info(accessToken, openid);
                var buy         = Config.Helper.CreateWhere <DbBuy>()
                                  .Where(o => o.ProductID == id && o.OpenID == openid && o.PayTime == null)
                                  .FirstOrDefault();
                if (buy == null)
                {
                    buy = new DbBuy()
                    {
                        OpenID     = openid,
                        ProductID  = id,
                        WeName     = userinfo.nickname,
                        ActualCost = Config.Helper.SingleById <DbProduct>(id).Cost
                    };
                    Config.Helper.Save(buy);
                }

                return(RedirectToAction("ForBuy", new { id = buy.Id }));
            }
            catch (Exception ex)
            {
                return(Content(ex.Message));
            }
        }
Esempio n. 2
0
        public ActionResult JsApi(string code, string state)
        {
            if (string.IsNullOrEmpty(code))
            {
                return(Content("您拒绝了授权!"));
            }

            if (!state.Contains("|"))
            {
                //这里的state其实是会暴露给客户端的,验证能力很弱,这里只是演示一下
                //实际上可以存任何想传递的数据,比如用户ID,并且需要结合例如下面的Session["OAuthAccessToken"]进行验证
                return(Content("验证失败!请从正规途径进入!1001"));
            }
            try
            {
                //通过,用code换取access_token
                var openIdResult = OAuthApi.GetAccessToken(TenPayV3Info.AppId, TenPayV3Info.AppSecret, code);
                if (openIdResult.errcode != ReturnCode.请求成功)
                {
                    return(Content("错误:" + openIdResult.errmsg));
                }
                //获取产品信息
                var   stateData = state.Split('|');
                int   buyid     = 0;
                DbBuy buy       = null;
                if (int.TryParse(stateData[0], out buyid))
                {
                    buy = Config.Helper.SingleOrDefaultById <DbBuy>(buyid);
                    if (buy == null || buy.OpenID != openIdResult.openid || buy.PayTime != null)
                    {
                        return(Content("商品信息不存在,或非法进入!1002"));
                    }
                    ViewData["product"] = buy;
                }
                string sp_billno = Request["order_no"];
                if (string.IsNullOrEmpty(sp_billno))
                {
                    //生成订单10位序列号,此处用时间和随机数生成,商户根据自己调整,保证唯一
                    sp_billno = string.Format("{0}{1}{2}", TenPayV3Info.MchId /*10位*/, DateTime.Now.ToString("yyyyMMddHHmmss"),
                                              TenPayV3Util.BuildRandomStr(6));
                }
                //else
                //{
                //    sp_billno = Request["order_no"];
                //}

                var timeStamp = TenPayV3Util.GetTimestamp();
                var nonceStr  = TenPayV3Util.GetNoncestr();

                var body        = buy == null ? "test" : buy.Product.ProductName;
                var price       = buy == null ? 100 : (int)buy.ActualCost * 100;
                var xmlDataInfo = new TenPayV3UnifiedorderRequestData(TenPayV3Info.AppId, TenPayV3Info.MchId, body, sp_billno, price, Request.UserHostAddress, TenPayV3Info.TenPayV3Notify, TenPayV3Type.JSAPI, openIdResult.openid, TenPayV3Info.Key, nonceStr);

                var result = TenPayV3.Unifiedorder(xmlDataInfo);//调用统一订单接口
                //JsSdkUiPackage jsPackage = new JsSdkUiPackage(TenPayV3Info.AppId, timeStamp, nonceStr,);
                var package = string.Format("prepay_id={0}", result.prepay_id);

                buy.OrderNo = sp_billno;
                Config.Helper.Save(buy);

                ViewData["appId"]     = TenPayV3Info.AppId;
                ViewData["timeStamp"] = timeStamp;
                ViewData["nonceStr"]  = nonceStr;
                ViewData["package"]   = package;
                ViewData["paySign"]   = TenPayV3.GetJsPaySign(TenPayV3Info.AppId, timeStamp, nonceStr, package, TenPayV3Info.Key);

                return(View(buy));
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
                msg += "<br>" + ex.StackTrace;
                msg += "<br>==Source==<br>" + ex.Source;

                if (ex.InnerException != null)
                {
                    msg += "<br>===InnerException===<br>" + ex.InnerException.Message;
                }
                return(Content(msg));
            }
        }