//
        // GET: /Shopify/finalize
        public ActionResult Finalize()
        {
            string appGuid      = Request.Params["AppGuid"];
            string shopName     = Request.Params["ShopName"];
            string apiKey       = Request.Params["ApiKey"];
            string sharedSecret = Request.Params["SharedSecret"];

            var authorizer = new ShopifyAPIAuthorizer(shopName, apiKey, sharedSecret);

            // get the following variables from the Query String of the request
            string error = Request.QueryString["error"];
            string code  = Request.Params["code"];
            string shop  = Request.QueryString["shop"];

            // check for an error first
            if (!String.IsNullOrEmpty(error))
            {
                this.TempData["Error"] = error;
                return(RedirectToAction("Login"));
            }

            // make sure we have the code
            if (string.IsNullOrWhiteSpace(code) || string.IsNullOrWhiteSpace(shop))
            {
                return(RedirectToAction("Index", "Home"));
            }

            // get the authorization state
            ShopifyAuthorizationState authState = authorizer.AuthorizeClient(code);

            if (authState != null && authState.AccessToken != null)
            {
                using (ShopifyAppsContext context = new ShopifyAppsContext())
                {
                    var app = context.ShopifyApps.Find(new Guid(appGuid));
                    if (app != null)
                    {
                        app.ShopName     = shopName;
                        app.ApiKey       = apiKey;
                        app.SharedSecret = sharedSecret;
                        app.AccessToken  = authState.AccessToken;
                        context.SaveChanges();
                    }
                }
            }

            return(RedirectToAction("Index"));
        }
        public System.Web.Mvc.EmptyResult Destroy()
        {
            var appId = Request.Params["id"];

            using (ShopifyAppsContext context = new ShopifyAppsContext())
            {
                var app = context.ShopifyApps.Where(a => a.Owner == User.Identity.Name && a.ApplicationGuid == new Guid(appId)).FirstOrDefault();
                if (app != null)
                {
                    context.ShopifyApps.Remove(app);
                    context.SaveChanges();
                }
            }

            return(new EmptyResult());
        }
        public JsonResult Create()
        {
            var name = Request.Params["Name"].Trim();

            ShopifyApp app;

            using (ShopifyAppsContext context = new ShopifyAppsContext())
            {
                app = context.ShopifyApps.Create();
                app.ApplicationName = name;
                app.Owner           = User.Identity.Name;
                context.ShopifyApps.Add(app);
                context.SaveChanges();
            }

            var root = Request.Url.GetLeftPart(UriPartial.Authority);

            app.InitializeUrls(root);
            return(Json(app));
        }