Example #1
0
        public async Task <IActionResult> PayButton(bool enableStore)
        {
            var blob = CurrentStore.GetStoreBlob();

            blob.AnyoneCanInvoice = enableStore;
            if (CurrentStore.SetStoreBlob(blob))
            {
                await _Repo.UpdateStore(CurrentStore);

                TempData[WellKnownTempData.SuccessMessage] = "Store successfully updated";
            }

            return(RedirectToAction(nameof(PayButton), new
            {
                storeId = CurrentStore.Id
            }));
        }
        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 }));
        }