Ejemplo n.º 1
0
        /// <summary>
        /// Makes the get request to shopify API with specified settings and URL.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="shopifySettings">The shopify settings.</param>
        /// <returns>JSON response</returns>
        public static string MakeGet(string url, ShopifySettings shopifySettings)
        {
            try
            {
                WaitLimit();

                var client = GetClient(shopifySettings);

                var request  = new RestRequest(url, Method.GET);
                var response = client.Execute(request);

                var statusCode = response.StatusCode;

                var res = CheckHeader(response);

                return(res == RequestState.Repeat ? MakeGet(url, shopifySettings) : response.Content);
            }
            catch (Exception e)
            {
                LogHelper.LogEvent(new LogEventRequest(_logger)
                {
                    Exception = e,
                    LogLevel  = LogLevel.Error,
                    Method    = MethodBase.GetCurrentMethod(),
                    Values    = new List <object>()
                    {
                        url, shopifySettings
                    }
                });

                return(string.Empty);
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            var settings = new ShopifySettings("vlad-p.myshopify.com", "a14623576b0b4fc774468131b4552a8f");
            var serv     = new ShopifyProductService(settings);
            //var webhookServ = new ShopifyWebhookService(settings);

            //var hooks = webhookServ.GetAll();

            var prods = serv.GetAll();

            //var evListServ = new EventListService();
            //var productListServ = new ProductListService();

            //var res = evListServ.Search(new EventListSearchRequest()
            //{
            //    SearchType = 2,
            //    Skip = 0,
            //    Top = 10,
            //    Title = "gfdfgdfg3434tdsf"
            //});

            //var prodLists = productListServ.Search(new ProductListSearchRequest
            //{
            //    Skip = 0,
            //    Top = 10
            //});

            //var gfdg = 2;
        }
Ejemplo n.º 3
0
 public static ShopifyApiClientCredentials CreateShopifyApiCredentials(this ShopifySettings shopify)
 {
     return(new ShopifyApiClientCredentials
     {
         ShopName = shopify.ShopName,
         ApiKey = shopify.ApiKey,
         ApiPassword = shopify.Password
     });
 }
Ejemplo n.º 4
0
 public static void SetShopifySettings(this StoreBlob storeBlob, ShopifySettings settings)
 {
     if (settings is null)
     {
         storeBlob.AdditionalData.Remove(StoreBlobKey);
     }
     else
     {
         storeBlob.AdditionalData.AddOrReplace(StoreBlobKey, new Serializer(null).ToString(settings));
     }
 }
Ejemplo n.º 5
0
 private static RestClient GetClient(ShopifySettings shopifySettings)
 {
     if (shopifySettings.AuthenticationType == AuthenticationType.AccessToken)
     {
         var client = new RestClient($"https://{shopifySettings.HostName}");
         client.AddDefaultHeader("X-Shopify-Access-Token", shopifySettings.AccessToken);
         return(client);
     }
     else
     {
         return(new RestClient($"https://{shopifySettings.HostName}")
         {
             Authenticator = new HttpBasicAuthenticator(shopifySettings.ApiKey, shopifySettings.Password)
         });
     }
 }
Ejemplo n.º 6
0
        public static string Get(string url, ShopifySettings shopifySettings)
        {
            try
            {
                var client = GetClient(shopifySettings);

                var request  = new RestRequest(url, Method.GET);
                var response = client.Execute(request);

                return(response.Content);
            }
            catch (Exception e)
            {
                return(string.Empty);
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Makes the PUT request to shopify API with specified settings, data to update and URL.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="data">The data.</param>
        /// <param name="shopifySettings">The shopify settings.</param>
        /// <returns>JSON response</returns>
        public static string MakePut(string url, object data, ShopifySettings shopifySettings)
        {
            try
            {
                WaitLimit();

                var client = GetClient(shopifySettings);

                var request = new RestRequest(url, Method.PUT);
                request.RequestFormat  = DataFormat.Json;
                request.JsonSerializer = new RestSharpJsonNetSerializer();
                request.AddBody(data);
                var response = client.Execute(request);

                var res = CheckHeader(response);

                if (res == RequestState.Repeat)
                {
                    return(MakePut(url, data, shopifySettings));
                }

                return(response.Content);
            }
            catch (Exception e)
            {
                LogHelper.LogEvent(new LogEventRequest(_logger)
                {
                    Exception = e,
                    LogLevel  = LogLevel.Error,
                    Method    = MethodBase.GetCurrentMethod(),
                    Values    = new List <object>()
                    {
                        url, data, shopifySettings
                    }
                });

                return(string.Empty);
            }
        }
Ejemplo n.º 8
0
 public ShopifyProductImageService(ShopifySettings settings) : base(settings, "/admin/products/images.json")
 {
     Settings = settings;
 }
Ejemplo n.º 9
0
 public ShopifyVariantService(ShopifySettings settings) : base(settings, "/admin/variants.json")
 {
     Settings = settings;
 }
Ejemplo n.º 10
0
        public async Task <IActionResult> EditShopify(string storeId,
                                                      ShopifySettings vm, string command = "")
        {
            switch (command)
            {
            case "ShopifySaveCredentials":
            {
                var shopify    = vm;
                var validCreds = shopify != null && shopify?.CredentialsPopulated() == true;
                if (!validCreds)
                {
                    TempData[WellKnownTempData.ErrorMessage] = "Please provide valid Shopify credentials";
                    return(View(vm));
                }
                var apiClient = new ShopifyApiClient(_clientFactory, shopify.CreateShopifyApiCredentials());
                try
                {
                    await apiClient.OrdersCount();
                }
                catch (ShopifyApiException err)
                {
                    TempData[WellKnownTempData.ErrorMessage] = err.Message;
                    return(View(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(vm));
                }

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

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

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

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

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

            return(RedirectToAction(nameof(EditShopify), new { storeId = CurrentStore.Id }));
        }
        public async Task <IActionResult> EditShopifyIntegration(string storeId,
                                                                 ShopifySettings 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 = 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(vm));
                }
            }

            switch (command)
            {
            case "ShopifySaveCredentials":
            {
                var shopify    = vm;
                var validCreds = shopify != null && shopify?.CredentialsPopulated() == true;
                if (!validCreds)
                {
                    TempData[WellKnownTempData.ErrorMessage] = "Please provide valid Shopify credentials";
                    return(View(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(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(vm));
                }

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

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

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

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

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

            return(RedirectToAction(nameof(EditShopifyIntegration), new { storeId = CurrentStore.Id }));
        }
Ejemplo n.º 12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ShopifyOrderService"/> class.
 /// </summary>
 /// <param name="settings">The settings.</param>
 /// <param name="url">The URL.</param>
 public ShopifyOrderService(ShopifySettings settings) : base(settings, "/admin/orders.json")
 {
     Settings = settings;
 }
Ejemplo n.º 13
0
 public ShopifyPageService(ShopifySettings settings) : base(settings, "/admin/pages.json")
 {
     Settings = settings;
 }
Ejemplo n.º 14
0
 public ShopifyAuthorizationService(ShopifySettings settings, string url = "") : base(settings, url)
 {
 }
Ejemplo n.º 15
0
 protected ShopifyBaseService(ShopifySettings settings, string url)
 {
     Url      = url;
     Settings = settings;
 }
Ejemplo n.º 16
0
 public ShopifyCustomerService(ShopifySettings settings) : base(settings, "admin/customers.json")
 {
     Settings = settings;
 }
Ejemplo n.º 17
0
 public ShopifyFulfillmentService(ShopifySettings settings) : base(settings, "/admin/orders/fulfillments.json")
 {
     Settings = settings;
 }
Ejemplo n.º 18
0
 public ShopifyWebhookService(ShopifySettings settings) : base(settings, "/admin/webhooks.json")
 {
     Settings = settings;
 }
Ejemplo n.º 19
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ShopifyAddressService"/> class.
 /// </summary>
 /// <param name="settings">The settings.</param>
 public ShopifyAddressService(ShopifySettings settings) : base(settings, "/admin/customers/addresses.json")
 {
     Settings = settings;
 }
 public ShopifyTransactionService(ShopifySettings settings) : base(settings, "/admin/orders/transactions.json")
 {
     Settings = settings;
 }
 private ShopifyApiClient CreateShopifyApiClient(ShopifySettings shopify)
 {
     return(new ShopifyApiClient(_httpClientFactory, shopify.CreateShopifyApiCredentials()));
 }
Ejemplo n.º 22
0
 public ShopifyAssetService(ShopifySettings settings) : base(settings, "/admin/themes/assets.json")
 {
     Settings = settings;
 }
Ejemplo n.º 23
0
 public ShopifyProductService(ShopifySettings settings) : base(settings, "admin/products.json")
 {
     Settings = settings;
 }
 public ShopifyRecurringChargeService(ShopifySettings settings, string url = "/admin/recurring_application_charges") : base(settings, url)
 {
 }