/// <summary> /// Creates a new order that can be used to test the Fulfillment API. /// </summary> /// <returns>The new product.</returns> public static async Task<ShopifyOrder> CreateOrder() { var order = OrderCreation.GenerateOrder(); var service = new ShopifyOrderService(Utils.MyShopifyUrl, Utils.AccessToken); return await service.CreateAsync(order); }
/// <summary> /// Creates a new order that can be used to test the Transaction API. /// </summary> /// <returns>The new product.</returns> public static async Task <ShopifyOrder> CreateOrder() { var order = Test_Data.OrderCreation.GenerateOrder(); var service = new ShopifyOrderService(Utils.MyShopifyUrl, Utils.AccessToken); return(await service.CreateAsync(order)); }
internal static IEnumerable <ShopifyOrder> GetShopifyOpenOrders(ShopifyOrderService shopifyOrderService) { var listOrder = default(Task <IEnumerable <ShopifyOrder> >); var orders = new List <ShopifyOrder>(); var page = 1; do { ShopifyCall.ExecuteCall(delegate() { listOrder = shopifyOrderService.ListAsync(new ShopifyOrderFilterOptions { Limit = 250, Status = ShopifyOrderStatus.Open, Page = page }); listOrder.Wait(); }); orders.AddRange(listOrder.Result); page++; }while (listOrder.Result.Count() == 250); return(orders); }
public static async Task <long> GetOrderId() { var service = new ShopifyOrderService(Utils.MyShopifyUrl, Utils.AccessToken); var orders = await service.ListAsync(new Filters.ShopifyOrderFilter() { Limit = 1, Fields = "id" }); return(orders.First().Id.Value); }
public static async Task<long> GetOrderId() { var service = new ShopifyOrderService(Utils.MyShopifyUrl, Utils.AccessToken); var orders = await service.ListAsync(new Filters.ShopifyOrderFilter() { Limit = 1, Fields = "id" }); return orders.First().Id.Value; }
public static IShopOrderService CreateOrderService(PlatformType type) { IShopOrderService orderService = null; switch (type) { case PlatformType.Shopify: orderService = new ShopifyOrderService(new ShopifyClient()); break; case PlatformType.XShoppy: orderService = new XShoppyOrderService(new XShoppyClient()); break; default: break; } return(orderService); }
/// <summary> /// Deletes a product that was used to test the Transaction API. /// </summary> public static async Task DeleteOrder(long id) { var service = new ShopifyOrderService(Utils.MyShopifyUrl, Utils.AccessToken); await service.DeleteAsync(id); }
public static void UpdateShopifyProduct(UpdateShopifyProductModel item) { using (var db = AliShopEntities.New()) { var shopifyProductRules = RulesCreator.NewRules <AliShopifyProductRules>(db); var shopifyPriceRules = RulesCreator.NewRules <AliShopifyPriceRules>(db); var parameterRules = RulesCreator.NewRules <AliParameterRules>(db); var shopifyProduct = shopifyProductRules.GetById( item.AliShopifyProductId, "AliProduct.AliProductVariant", "AliProduct.AliProductImage", "AliProduct.AliProductLink", "AliProduct.AliProductOption", "AliProduct.AliProductSpecific", "AliProduct.AliStore" ).First(); foreach (var image in shopifyProduct.AliProduct.AliProductImage) { if (!image.Url.EndsWith("_640x640.jpg")) { image.Url += "_640x640.jpg"; } } var title = shopifyProduct.AliProduct.Title; var invalidKeywords = ShopifyHelper.GetShopifyTags(parameterRules.GetByName("product_title_invalid_keywords").Value); foreach (var keyword in invalidKeywords) { title = Regex.Replace(title, Regex.Escape(keyword), "", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase).Trim(); } title = Regex.Replace(title, "\\s{1,}", " ", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase); title = new TitleCollection(title).Short(100).Edited; Print.PrintStatus(item.RefName, "BeforeRun", title); var calculationPrices = shopifyPriceRules.GetFixedCalculationPrices(shopifyProduct.AliProductId); var stockSafetyMargin = int.Parse(parameterRules.GetByName("stock_safety_margin").Value); var handleFriendlyName = ShopifyHelper.ShopifyHandleFriendlyName(shopifyProduct.AliProductId, title); var shopifyProductService = new ShopifyProductService(AppSettings.ShopifyMyShopifyUrl, AppSettings.ShopifyApiKey); var shopifyOrderService = new ShopifyOrderService(AppSettings.ShopifyMyShopifyUrl, AppSettings.ShopifyApiKey); var getShopifyProduct = default(ShopifyProduct); var tryStatusOK = Program.Try(item.RefName, title, 3, () => getShopifyProduct = ShopifyMethod.GetProductOnShopify(shopifyProductService, shopifyProduct.HandleFriendlyName ?? handleFriendlyName)); Print.PrintStatus(item.RefName, tryStatusOK ? "GetProduct" : "FailGetProduct", title, ConsoleColor.Blue); if (tryStatusOK) { var changeShopifyProduct = ShopifyHelper.ChangeShopifyProduct(shopifyProduct, calculationPrices, getShopifyProduct, stockSafetyMargin, handleFriendlyName, title); tryStatusOK = Program.Try(item.RefName, title, 3, () => getShopifyProduct = ShopifyMethod.SaveProductOnShopify(shopifyProductService, changeShopifyProduct)); Print.PrintStatus(item.RefName, tryStatusOK ? "SaveProduct" : "FailSaveProduct", title, ConsoleColor.Blue); } if (getShopifyProduct != null && tryStatusOK) { tryStatusOK = Program.Try(item.RefName, title, 3, () => getShopifyProduct = ShopifyMethod.UpdateVariantsOnShopify(shopifyProductService, shopifyProduct, getShopifyProduct)); Print.PrintStatus(item.RefName, tryStatusOK ? "UpdateVariant" : "FailUpdateVariant", title, ConsoleColor.Blue); } var hasProductOnShopify = getShopifyProduct != null; if (hasProductOnShopify && (!tryStatusOK || !shopifyProduct.AliProduct.Enabled && getShopifyProduct.PublishedAt != null)) { var unPublishTryStatusOK = Program.Try(item.RefName, title, 3, () => getShopifyProduct = ShopifyMethod.UnpublishProductOnShopify(shopifyProductService, getShopifyProduct.Id.Value)); Print.PrintStatus(item.RefName, unPublishTryStatusOK ? "Unpublish" : "FailUnpublish", title, ConsoleColor.Blue); } shopifyProduct = shopifyProductRules.GetById(item.AliShopifyProductId).Where(i => i.RowVersion == shopifyProduct.RowVersion).FirstOrDefault(); if (hasProductOnShopify) { if (shopifyProduct != null) { shopifyProduct.AvgCompareAtPrice = (decimal?)getShopifyProduct.Variants.Average(i => i.CompareAtPrice); shopifyProduct.AvgPrice = (decimal?)getShopifyProduct.Variants.Average(i => i.Price); shopifyProduct.ExistsOnShopify = true; shopifyProduct.HandleFriendlyName = getShopifyProduct.Handle; shopifyProduct.LastUpdate = DateTime.UtcNow; shopifyProduct.Published = getShopifyProduct.PublishedAt != null; shopifyProduct.RequiredUpdateOnShopify = !tryStatusOK; shopifyProduct.ShopifyProductId = getShopifyProduct.Id; db.SaveChanges(); } } else { if (shopifyProduct != null) { shopifyProduct.ExistsOnShopify = false; shopifyProduct.LastUpdate = DateTime.UtcNow; shopifyProduct.RequiredUpdateOnShopify = true; db.SaveChanges(); } Print.PrintStatus(item.RefName, "DoesNotHaveProductOnShopify", title, ConsoleColor.Blue); } Print.PrintStatus(item.RefName, shopifyProduct == null ? "NotUpdateOnDatabase" : "UpdateOnDatabase", title, ConsoleColor.Blue); Print.PrintStatus(item.RefName, "RunComplete", title, ConsoleColor.Green); } }
public ShopifyRoute() : base("/api/v1/shopify") { Post["/authorize", true] = async(parameters, ct) => { var model = this.BindAndValidate <AuthorizeRequest>(); if (!ModelValidationResult.IsValid) { return(Response.AsJsonError("Request did not pass validation.", HttpStatusCode.NotAcceptable, ModelValidationResult.FormattedErrors)); } var getUser = await Database.Users.Entities.GetAsync <User>(SessionToken.UserId); if (!getUser.IsSuccess) { return(Response.AsJsonError("Could not find user in database.", HttpStatusCode.NotFound)); } // Complete the OAuth process and integrate the user var user = getUser.Content; string accessToken; try { accessToken = await ShopifyAuthorizationService.Authorize(model.Code, model.ShopUrl, Config.ShopifyApiKey, Config.ShopifySecretKey); } catch (ShopifyException e) when(e.JsonError.ContainsIgnoreCase("authorization code was not found or was already used")) { return(Response.AsJsonError("Integration failed: the authorization code was not found or was already used.", HttpStatusCode.BadRequest)); } var shop = await new ShopifyShopService(model.ShopUrl, accessToken).GetAsync(); user.ShopifyAccessToken = accessToken; user.ShopifyUrl = model.ShopUrl; user.ShopId = shop.Id; user.ShopName = shop.Name; user.Permissions = Config.ShopifyPermissions; // Create an App_Uninstalled webhook if we're not running on localhost if (!Regex.IsMatch(Request.Url.HostName, "localhost", RegexOptions.IgnoreCase)) { var service = new ShopifyWebhookService(user.ShopifyUrl, user.ShopifyAccessToken); var url = new UriBuilder(Request.Url.ToString()) { Scheme = Uri.UriSchemeHttps, // All Shopify webhooks must be https Path = "/api/v1/webhooks/app_uninstalled", Query = $"shop_id={shop.Id.Value}", }; try { await service.CreateAsync(new ShopifyWebhook() { Address = url.ToString(), Topic = "app/uninstalled", }); } catch (ShopifyException ex) when(ex.Errors.Any(e => e.Key.EqualsIgnoreCase("address") && e.Value.Any(innerError => innerError.ContainsIgnoreCase("for this topic has already been taken")))) { // Webhook already exists. } } // Update the user var update = await Database.Users.Entities.PutAsync(user); if (!update.IsSuccess) { throw new Exception($"Failed to save user's integration. {(int)update.StatusCode} {update.Reason}"); } return(Response.WithSessionToken(user)); }; Post["/activate_charge", true] = async(parameters, ct) => { long chargeId = parameters.charge_id; var getUser = await Database.Users.Entities.GetAsync <User>(this.SessionToken.UserId); if (!getUser.IsSuccess) { throw new Exception("User not found."); } // Activate the charge if its status is accepted. var user = getUser.Content; var service = new ShopifyRecurringChargeService(user.ShopifyUrl, user.ShopifyAccessToken); var charge = await service.GetAsync(chargeId); if (charge.Status != "accepted") { throw new Exception($"Charge #${charge.Id.Value} has not been accepted."); } // Update the user var update = await Database.Users.Entities.PutAsync(user); if (!update.IsSuccess) { throw new Exception($"Failed to save user's new charge. {update.StatusCode} {update.Reason}"); } return(Response.WithSessionToken(user)); }; Post["/create_authorization_url"] = (parameters) => { var model = this.Bind <CreateAuthorizationUrlRequest>(); var url = ShopifyAuthorizationService.BuildAuthorizationUrl(Config.ShopifyPermissions, model.Url, Config.ShopifyApiKey, model.RedirectUrl); return(Response.AsJson(new { url = url })); }; Get["/orders", true] = async(parameters, ct) => { int? limit = Request.Query.limit; int? page = Request.Query.page; string status = Request.Query.status ?? "any"; var service = new ShopifyOrderService(SessionToken.ShopifyUrl, SessionToken.ShopifyAccessToken); var orders = await service.ListAsync(new ShopifySharp.Filters.ShopifyOrderFilter() { Limit = limit, Page = page, Status = "any", }); return(Response.AsJson(orders)); }; Post["/orders", true] = async(parameters, ct) => { var model = this.BindAndValidate <CreateOrderRequest>(); if (!ModelValidationResult.IsValid) { return(Response.AsJsonError("Request did not pass validation.", HttpStatusCode.NotAcceptable, ModelValidationResult.FormattedErrors)); } var service = new ShopifyOrderService(SessionToken.ShopifyUrl, SessionToken.ShopifyAccessToken); var order = await service.CreateAsync(new ShopifyOrder() { CreatedAt = DateTime.UtcNow, BillingAddress = new ShopifyAddress() { Address1 = model.Street, City = model.City, Province = model.State, Zip = model.Zip, Name = model.Name, CountryCode = "US", Default = true, }, LineItems = new List <ShopifyLineItem>() { new ShopifyLineItem() { Name = model.LineItem, Title = model.LineItem, Quantity = model.Quantity, Price = 5, }, }, FinancialStatus = "authorized", Email = model.Email, }); return(Response.AsJson(order).WithStatusCode(HttpStatusCode.Created)); }; Post["/orders/{id:long}/{verb}", true] = async(parameters, ct) => { string verb = parameters.verb; long id = parameters.id; if (!verb.EqualsIgnoreCase("open") && !verb.EqualsIgnoreCase("close")) { return(Negotiate.WithStatusCode(HttpStatusCode.MethodNotAllowed)); } if (!ModelValidationResult.IsValid) { return(Response.AsJsonError("Request did not pass validation.", HttpStatusCode.NotAcceptable, ModelValidationResult.FormattedErrors)); } var service = new ShopifyOrderService(SessionToken.ShopifyUrl, SessionToken.ShopifyAccessToken); if (verb.EqualsIgnoreCase("open")) { await service.OpenAsync(id); } else { await service.CloseAsync(id); } // Refresh the order and return it to the client var order = await service.GetAsync(id); return(Response.AsJson(order)); }; Delete["/orders/{id:long}", true] = async(parameters, ct) => { long id = parameters.id; var service = new ShopifyOrderService(SessionToken.ShopifyUrl, SessionToken.ShopifyAccessToken); await service.DeleteAsync(id); return(Negotiate.WithStatusCode(200)); }; }
/// <summary> /// Deletes a product that was used to test the Fulfillment API. /// </summary> public static async Task DeleteOrder(long id) { var service = new ShopifyOrderService(Utils.MyShopifyUrl, Utils.AccessToken); await service.DeleteAsync(id); }