public async Task <IActionResult> UpdateWebhooks([FromServices] IHttpClientFactory clientFactory,
                                                         IntegrationsViewModel vm, string command = "")
        {
            switch (command.ToLowerInvariant())
            {
            case "add":
                vm.Webhooks.Add(new WebhookSubscription());
                return(View("Integrations", vm));

            case string c when c.StartsWith("remove:", StringComparison.InvariantCultureIgnoreCase):
                var index = int.Parse(c.Substring(command.IndexOf(":", StringComparison.InvariantCultureIgnoreCase) + 1), CultureInfo.InvariantCulture);

                vm.Webhooks.RemoveAt(index);
                return(View("Integrations", vm));

            case "save":
                var blob = CurrentStore.GetStoreBlob();
                blob.Webhooks = vm.Webhooks.Where(subscription => subscription.Url != null).ToList();
                var store = CurrentStore;
                store.SetStoreBlob(blob);
                await _Repo.UpdateStore(store);

                TempData[WellKnownTempData.SuccessMessage] = "Webhooks saved";
                break;
            }
            return(RedirectToAction(nameof(Integrations), new { storeId = CurrentStore.Id }));
        }
        public async Task <IActionResult> Integrations([FromServices] StoreRepository storeRepository)
        {
            var blob = CurrentStore.GetStoreBlob();

            var vm = new IntegrationsViewModel {
                Shopify = blob.Shopify, EventPublicKey = blob.EventSigner.ToHex(), Webhooks = blob.Webhooks
            };

            return(View("Integrations", vm));
        }
예제 #3
0
        public IActionResult Integrations()
        {
            var blob = CurrentStore.GetStoreBlob();

            var vm = new IntegrationsViewModel {
                Shopify = blob.Shopify
            };

            return(View("Integrations", vm));
        }
        public async Task <IActionResult> UpdateShopify([FromServices] IHttpClientFactory clientFactory,
                                                        IntegrationsViewModel vm, string command = "", string exampleUrl = "")
        {
            if (!string.IsNullOrEmpty(exampleUrl))
            {
                try
                {
                    //https://{apikey}:{password}@{hostname}/admin/api/{version}/{resource}.json
                    var parsedUrl = new Uri(exampleUrl);
                    var userInfo  = parsedUrl.UserInfo.Split(":");
                    vm.Shopify = new ShopifySettings()
                    {
                        ApiKey   = userInfo[0],
                        Password = userInfo[1],
                        ShopName = parsedUrl.Host.Replace(".myshopify.com", "",
                                                          StringComparison.InvariantCultureIgnoreCase)
                    };
                    command = "ShopifySaveCredentials";
                }
                catch (Exception)
                {
                    TempData[WellKnownTempData.ErrorMessage] = "The provided Example Url was invalid.";
                    return(View("Integrations", vm));
                }
            }

            switch (command)
            {
            case "ShopifySaveCredentials":
            {
                var shopify    = vm.Shopify;
                var validCreds = shopify != null && shopify?.CredentialsPopulated() == true;
                if (!validCreds)
                {
                    TempData[WellKnownTempData.ErrorMessage] = "Please provide valid Shopify credentials";
                    return(View("Integrations", vm));
                }

                var apiClient = new ShopifyApiClient(clientFactory, shopify.CreateShopifyApiCredentials());
                try
                {
                    await apiClient.OrdersCount();
                }
                catch (ShopifyApiException)
                {
                    TempData[WellKnownTempData.ErrorMessage] =
                        "Shopify rejected provided credentials, please correct values and try again";
                    return(View("Integrations", vm));
                }

                var scopesGranted = await apiClient.CheckScopes();

                if (!scopesGranted.Contains("read_orders") || !scopesGranted.Contains("write_orders"))
                {
                    TempData[WellKnownTempData.ErrorMessage] =
                        "Please grant the private app permissions for read_orders, write_orders";
                    return(View("Integrations", vm));
                }

                // everything ready, proceed with saving Shopify integration credentials
                shopify.IntegratedAt = DateTimeOffset.Now;

                var blob = CurrentStore.GetStoreBlob();
                blob.Shopify = shopify;
                if (CurrentStore.SetStoreBlob(blob))
                {
                    await _Repo.UpdateStore(CurrentStore);
                }

                TempData[WellKnownTempData.SuccessMessage] = "Shopify integration successfully updated";
                break;
            }

            case "ShopifyClearCredentials":
            {
                var blob = CurrentStore.GetStoreBlob();
                blob.Shopify = null;
                if (CurrentStore.SetStoreBlob(blob))
                {
                    await _Repo.UpdateStore(CurrentStore);
                }

                TempData[WellKnownTempData.SuccessMessage] = "Shopify integration credentials cleared";
                break;
            }
            }

            return(RedirectToAction(nameof(Integrations), new { storeId = CurrentStore.Id }));
        }