public async Task <bool> InstallAsync(string shopUrl, string accessCode) { ShopService shopService = null; string accessToken = null; try { accessToken = await AuthorizationService.Authorize(accessCode, shopUrl, _shopifyConfig.ApiKey, _shopifyConfig.SecretKey); _logger.LogInformation($"accessToken: {accessToken}"); if (string.IsNullOrEmpty(accessToken)) { return(false); } shopService = new ShopService(shopUrl, accessToken); Shop shop = await shopService.GetAsync(); string scriptUrl = _shopifyConfig.ScriptUrl; var scriptService = new ScriptTagService(shopUrl, accessToken); var existingScripts = await scriptService.ListAsync(); foreach (var s in existingScripts) { if (s.Id.HasValue) { await scriptService.DeleteAsync(s.Id.Value); } } ScriptTag newScriptTag = await scriptService.CreateAsync(new ScriptTag { Src = scriptUrl, Event = _shopifyConfig.Event }); return(true); } catch (Exception ex) { _logger.LogError(ex, ""); await TryUninstallAsync(shopService, accessToken); return(false); } }
public async Task <ActionResult> Callback([FromQuery] string shop, [FromQuery] string hmac, [FromQuery] string code, [FromQuery] string state) { //Now retrieve the access token var apiKey = _config["Shopify_API_Key"]; var apiSecret = _config["Shopify_Secret_Key"]; var callbackUrl = _config["CallbackUrl"]; string accessToken = await AuthorizationService.Authorize(code, shop, apiKey, apiSecret); //Add a script tag ScriptTagService svc = new ScriptTagService(shop, accessToken); var tag = new ShopifySharp.ScriptTag(); tag.DisplayScope = "online_store"; tag.Event = "onload"; tag.Src = $"{callbackUrl}/js/Rda.Shopify.js"; var createdTag = await svc.CreateAsync(tag); return(Ok()); }
public async System.Threading.Tasks.Task <ActionResult> auth() { DB list = new DB(); string code = Request.QueryString["code"]; string myShopifyUrl = Request.QueryString["shop"]; /*TRY-Catch*/ try { string accessToken = await AuthorizationService.Authorize(code, myShopifyUrl, API, Secret); Session["token"] = accessToken; var qs = Request.QueryString.ToKvps(); if (AuthorizationService.IsAuthenticRequest(qs, Secret)) { Session["shop"] = myShopifyUrl; if (db.ShopLinks.Where(w => w.Shop == myShopifyUrl).Count() > 0) { list.shopLinks = db.ShopLinks.ToList(); if (db.ShopLinks.Where(w => w.Shop == myShopifyUrl).FirstOrDefault().IsPremium == true) { Session["premium"] = true; } list.users = db.Users.ToList(); } else { ShopLink NewShop = new ShopLink(); NewShop.Shop = myShopifyUrl; NewShop.Token = accessToken; NewShop.InstallDate = DateTime.Now; NewShop.IsPremium = false; NewShop.SendLimit = 500; db.ShopLinks.Add(NewShop); db.SaveChanges(); list.shopLinks = db.ShopLinks.ToList(); list.users = db.Users.ToList(); var service = new ScriptTagService(myShopifyUrl, accessToken); var tag = new ScriptTag() { Event = "onload", Src = "https://www.leaderpush.com/public/scripts/main.js", }; tag = await service.CreateAsync(tag); } } else { //Request is not authentic and should not be acted on. return(Content("Error, Please Try Again...")); } return(View(list)); } catch { return(RedirectToAction("UrlBreak", "Home")); } }