Example #1
0
        public async Task <bool> CreateWebhook(string domain, string token)
        {
            bool isSuccess = false;

            try
            {
                var serviceWebhook = new WebhookService(domain, token);
                var hook           = new Webhook()
                {
                    Address   = ApplicationEngine.Address_ChargeResult_UnInstall,
                    CreatedAt = DateTime.Now,
                    Format    = "json",
                    Topic     = "app/uninstalled",
                };
                hook = await serviceWebhook.CreateAsync(hook);

                var shopUpdateWebhook = new Webhook()
                {
                    Address   = ApplicationEngine.Url_Path + "/api/services/updateshopdetails",
                    CreatedAt = DateTime.Now,
                    Format    = "json",
                    Topic     = "shop/update",
                };
                shopUpdateWebhook = await serviceWebhook.CreateAsync(shopUpdateWebhook);

                isSuccess = true;
            }
            catch (ShopifyException e)
            {
                Log.Error("Error on creating webhook", e);
                throw e;
            }
            return(isSuccess);
        }
Example #2
0
        private async Task CreateAppUninstalledWebhook(ShopifyStore shopifyStore)
        {
            if (shopifyStore == null)
            {
                throw new NullReferenceException();
            }

            var shopifyWebhookService = new WebhookService(shopifyStore.Shop, shopifyStore.AccessToken);

            try
            {
                await shopifyWebhookService.CreateAsync(new Webhook
                {
                    Address   = GetWebhookAddress(ShopifyWebhookTopic.AppUninstalled, shopifyStore.Id),
                    CreatedAt = DateTime.Now,
                    Format    = ShopifyConstants.Format,
                    Topic     = ShopifyWebhookTopic.AppUninstalled
                });
            }
            catch (ShopifyException e)
            {
                string message =
                    $"Couldn't create {ShopifyWebhookTopic.AppUninstalled} webhook for {shopifyStore.Shop}. Reason: " +
                    string.Join(", ", e.Errors);
                _logger.Error(message);
            }
        }
Example #3
0
        /// <summary>
        /// Creates the webhook asynchronously.
        /// </summary>
        /// <param name="myShopifyDomain">The myshopify URL.</param>
        /// <param name="shopifyAccessToken">The shopify access token.</param>
        /// <param name="webhook">Valid webhook object.</param>
        /// <returns></returns>
        public async Task <ShopifyWebhookObject> CreateWebhookAsync(string myShopifyDomain, string shopifyAccessToken, ShopifyWebhookObject webhook)
        {
            _CheckmyShopifyDomain(myShopifyDomain);
            _CheckShopAccessToken(shopifyAccessToken);
            if (webhook == null)
            {
                _Logger.LogError($"Web hook object cannot be null for shop {myShopifyDomain}");
                throw new Exception("Web hook object cannot be null");
            }
            _Logger.LogInformation($"Sending request to create webhook for '{myShopifyDomain}' on topic '{webhook.Topic}'");
            WebhookService service = new WebhookService(myShopifyDomain, shopifyAccessToken);
            var            data    = await service.CreateAsync(new Webhook()
            {
                Address             = webhook.Address,
                CreatedAt           = webhook.CreatedAt,
                Fields              = webhook.Fields,
                Format              = webhook.Format,
                MetafieldNamespaces = webhook.MetafieldNamespaces,
                Topic     = webhook.Topic,
                UpdatedAt = webhook.UpdatedAt
            });

            if (data == null)
            {
                _Logger.LogError("Failed creating webhook. Server response was NULL.");
                throw new Exception("Failed creating webhook.Server responded NULL.");
            }
            else
            {
                var ret = new ShopifyWebhookObject()
                {
                    Address             = data.Address,
                    CreatedAt           = data.CreatedAt,
                    Fields              = data.Fields,
                    Format              = data.Format,
                    MetafieldNamespaces = data.MetafieldNamespaces,
                    Topic     = data.Topic,
                    UpdatedAt = data.UpdatedAt,
                    Id        = data.Id
                };
                _Logger.LogInformation($"Done creating webhook for '{myShopifyDomain}' on topic '{webhook.Topic}' where id = '{ret.Id}'");
                return(ret);
            }
        }
Example #4
0
        private async Task CreateFulfillmentsCreateWebhook(ShopifyStore shopifyStore)
        {
            var shopifyWebhookService = new WebhookService(shopifyStore.Shop, shopifyStore.AccessToken);

            try
            {
                await shopifyWebhookService.CreateAsync(new Webhook
                {
                    Address   = GetWebhookAddress(ShopifyWebhookTopic.FulfillmentsCreate, shopifyStore.Id),
                    CreatedAt = DateTime.Now,
                    Format    = ShopifyConstants.Format,
                    Topic     = ShopifyWebhookTopic.FulfillmentsCreate
                });
            }
            catch (ShopifyException e)
            {
                string message =
                    $"Couldn't create {ShopifyWebhookTopic.FulfillmentsCreate} webhook for {shopifyStore.Shop}. Reason: " +
                    string.Join(", ", e.Errors);
                _logger.Error(message);
            }
        }
Example #5
0
 public async Task <bool> CreateWebHook(string shopifyurl, string token, string Address, string Topic)
 {
     try
     {
         var     service = new WebhookService(shopifyurl, token);
         Webhook hook    = new Webhook()
         {
             Address   = Address, // Address
             CreatedAt = DateTime.Now,
             //Fields = new List<string>() { "id", "updated_at","FirstName" },
             Format = "json",
             //MetafieldNamespaces = new List<string>() { "metafield1", "metafield2" },
             Topic = Topic, // Topic
         };
         hook = await service.CreateAsync(hook);
     }
     catch (Exception ex)
     {
         return(false);
     }
     return(true);
 }
Example #6
0
        /// <summary>
        /// Authorise method
        ///     Process returned authorisation sync from Shopify, get the updated accessToken
        ///     Check and add or update accessToken/install status if required
        /// </summary>
        /// <param name="shop">shop Url</param>
        /// <param name="code">authorisation code</param>
        /// <param name="state">authorisation state</param>
        /// <returns></returns>
        public async Task <ActionResult> Authorise(string shop, string code, string state)
        {
            //Get updating accessToken to Shopify Store
            string accessToken = await AuthorizationService.Authorize(code, shop, apiKey, secretKey);

            DbEngine DBConnection = new DbEngine();

            if (DBConnection.ExistingShop(shop))                                //Check if shop has installed the app
            {
                if (DBConnection.GetIntergerValues(shop, "AppInstalled") == 0)  //App previously uninstalled
                {
                    DBConnection.UpdateIntergerValues(shop, "AppInstalled", 1); //reset indicator
                    var service = new WebhookService(shop, accessToken);
                    var hook    = new Webhook()
                    {
                        Address   = appUrl + "Shopify/Uninstalled?shopUrl=" + shop,
                        CreatedAt = DateTime.Now,
                        Format    = "json",
                        Topic     = "app/uninstalled"
                    };

                    try
                    {
                        hook = await service.CreateAsync(hook);
                    }
                    catch (ShopifyException e)
                    {
                        throw e;
                    }
                }

                string currentToken = DBConnection.GetStringValues(shop, "ShopifyToken");
                if (currentToken != accessToken) //check and update Shopify Token
                {
                    DBConnection.UpdateStringValues(shop, "ShopifyToken", accessToken);
                }
            }
            else
            {//initiat a webhook to manage uninstalls
                DBConnection.InsertNewShop(shop, accessToken);
                var service = new WebhookService(shop, accessToken);
                var hook    = new Webhook()
                {
                    Address   = appUrl + "Shopify/Uninstalled?shopUrl=" + shop,
                    CreatedAt = DateTime.Now,
                    Format    = "json",
                    Topic     = "app/uninstalled"
                };

                try
                {
                    hook = await service.CreateAsync(hook);
                } catch (ShopifyException e)
                {
                    throw e;
                }
            }

            //Redirect to Home/Index with parameter
            return(RedirectToAction("Installed", "Home", new { shopUrl = shop }));
        }