Пример #1
0
        /// <summary>
        /// Authorizes an application installation, generating an access token for the given shop.
        /// </summary>
        /// <param name="code">The authorization code generated by Bizweb OAuth, which should be a parameter named 'code' on the request querystring.</param>
        /// <param name="myApiUrl">The store's *.bizwebvietnam.net URL, which should be a paramter named 'shop' on the request querystring.</param>
        /// <param name="apiKey">Your app's public API key.</param>
        /// <param name="apiSecretKey">Your app's secret key.</param>
        /// <returns>The <see cref="OAuthResult"/> that's contain access token.</returns>
        public static async Task <OAuthResult> AuthorizeWithResultAsync(string code, string myApiUrl, string apiKey,
                                                                        string apiSecretKey)
        {
            var authState = new BizwebAuthorizationState
            {
                ApiUrl = myApiUrl
            };

            //Build request body
            var content = new JsonContent(new
            {
                client_id     = apiKey,
                client_secret = apiSecretKey,
                code
            });

            using (var reqMsg = RequestEngine.CreateRequest(authState, "oauth/access_token", HttpMethod.Post, content))
            {
                var response = await RequestEngine.ExecuteRequestAsync(reqMsg, new DefaultRequestExecutionPolicy());

                var accessToken = response.Value <string>("access_token");

                IEnumerable <string> scopes = null;
                var scope = response.Value <string>("scope");
                if (scope != null)
                {
                    scope  = scope.Replace(" ", ","); // only for bizweb
                    scopes = scope.Split(',');
                }

                return(new OAuthResult(accessToken, scopes));
            }
        }
Пример #2
0
        private static BizwebRequestMessage CreateRequestMessage(BizwebAuthorizationState authState, string path,
                                                                 HttpMethod method, string rootElement = null, object payload = null)
        {
            JsonContent content = null;

            if (payload == null)
            {
                return(RequestEngine.CreateRequest(authState, path, method, rootElement: rootElement));
            }

            if (method != HttpMethod.Get && method != HttpMethod.Delete)
            {
                content = new JsonContent(payload);
            }
            else if (payload is Parameterizable)
            {
                path = RequestEngine.CreateUriPathAndQuery(path, ((Parameterizable)payload).ToParameters());
            }
            else
            {
                //foreach (var propertyInfo in payload.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
                //{
                //    req.AddParameter(propertyInfo.Name, propertyInfo.GetValue(payload));
                //}

                var token       = JToken.FromObject(payload);
                var queryParams = token.Select(s =>
                {
                    var i = (JProperty)s;
                    return(new KeyValuePair <string, object>(i.Name, i.Value));
                });

                path = RequestEngine.CreateUriPathAndQuery(path, queryParams);
            }

            return(RequestEngine.CreateRequest(authState, path, method, content, rootElement));
        }
Пример #3
0
 public PriceRuleService(BizwebAuthorizationState authState) : base(authState)
 {
 }
Пример #4
0
 public StoreService(BizwebAuthorizationState authState) : base(authState)
 {
 }
Пример #5
0
 public ScriptTagService(BizwebAuthorizationState authState) : base(authState)
 {
 }
Пример #6
0
 /// <summary>
 /// Creates a new instance of BizwebService.
 /// </summary>
 /// <param name="authState">The object contain Bizweb authorization data.</param>
 protected BaseServiceWithSimpleCRUD(BizwebAuthorizationState authState) : base(authState)
 {
     ApiClassPath         = typeof(T).Name.ToSnakeCase();
     ApiClassPathInPlural = ApiClassPath + "s";
 }
Пример #7
0
 public EventService(BizwebAuthorizationState authState) : base(authState)
 {
 }
 /// <summary>
 /// Creates a new instance of BizwebService.
 /// </summary>
 /// <param name="authState">The object contain Bizweb authorization data.</param>
 /// <param name="apiClassPath">The API name or path, e.g. "product".</param>
 /// <param name="apiClassPathInPlural">The API name or path in plural , e.g. "products".</param>
 protected BaseServiceHavePublishedOption(BizwebAuthorizationState authState, string apiClassPath,
                                          string apiClassPathInPlural) : base(authState, apiClassPath, apiClassPathInPlural)
 {
 }
Пример #9
0
 public WebhookService(BizwebAuthorizationState authState) : base(authState)
 {
 }
Пример #10
0
 public ProductService(BizwebAuthorizationState authState) : base(authState)
 {
 }
Пример #11
0
 public FulfillmentService(BizwebAuthorizationState authState) : base(authState)
 {
 }
Пример #12
0
 public RedirectService(BizwebAuthorizationState authState) : base(authState)
 {
 }
Пример #13
0
 /// <summary>
 /// Creates a new instance of the service.
 /// </summary>
 /// <param name="authState">The object contain Bizweb authorization data.</param>
 public ArticleService(BizwebAuthorizationState authState) : base(authState)
 {
 }
Пример #14
0
 /// <summary>
 /// Creates a new instance of <see cref="SimpleBizwebService" />.
 /// </summary>
 /// <param name="authState">The object contain Bizweb authorization data.</param>
 public SimpleBizwebService(BizwebAuthorizationState authState) : base(authState)
 {
 }
Пример #15
0
 public CustomCollectionService(BizwebAuthorizationState authState) : base(authState)
 {
 }
Пример #16
0
 public ProductVariantService(BizwebAuthorizationState authState) : base(authState, "variant", "variants")
 {
 }
Пример #17
0
 public SmartCollectionService(BizwebAuthorizationState authState) : base(authState)
 {
 }
 /// <summary>
 /// Creates a new instance of BizwebService.
 /// </summary>
 /// <param name="authState">The object contain Bizweb authorization data.</param>
 protected BaseServiceHavePublishedOption(BizwebAuthorizationState authState) : base(authState)
 {
 }
Пример #19
0
 /// <summary>
 /// Creates a new instance of BizwebService.
 /// </summary>
 /// <param name="authState">The object contain Bizweb authorization data.</param>
 protected BaseService(BizwebAuthorizationState authState)
 {
     _AuthState = authState;
 }
Пример #20
0
        public static List <QT.Entities.Product> GetProductFromBizweb(string shopname, string accesstoken, QT.Entities.Company company)
        {
            List <QT.Entities.Product> ListProducts = new List <QT.Entities.Product>();
            var authorizeState = new BizwebAuthorizationState()
            {
                ShopName = shopname, AccessToken = accesstoken
            };
            var client = new BizwebAPIClient(authorizeState);
            ProductBizwebInfo listproductBizweb = new ProductBizwebInfo()
            {
                products = new List <Product>()
            };
            bool check = true;
            int  page  = 1;

            while (check)
            {
                Object obj = null;
                try
                {
                    //Mỗi lần lấy về 50 product của bizweb
                    string querry = "admin/products.json?page=" + page.ToString() + "&limit=250";
                    //string querry = "/admin/products.json";
                    obj = client.Get(querry);
                    //Thread.Sleep(1000);
                }
                catch (Exception ex)
                {
                    check = false;
                    Log.ErrorFormat("BIZWEB : Get/admin/products.json FAILS. ERROR : {0}", ex);
                }
                if (obj != null)
                {
                    ProductBizwebInfo listproduct = JsonConvert.DeserializeObject <ProductBizwebInfo>(obj.ToString());
                    if (listproduct.products.Count > 0)
                    {
                        listproductBizweb.products.AddRange(listproduct.products);
                        page++;
                    }
                    else
                    {
                        check = false;
                    }
                }
                else
                {
                    check = false;
                }
            }
            if (listproductBizweb.products.Count > 0)
            {
                #region Chuyển từ ListProducts of Bizweb sang ListProductWebsosanh
                for (int i = 0; i < listproductBizweb.products.Count; i++)
                {
                    //Check số lượng phiên bản variant
                    int variant = listproductBizweb.products[i].variants.Count;
                    //TH variant = 1 (1 phiên bản)
                    if (variant == 1)
                    {
                        #region Product
                        var tmpProduct = new QT.Entities.Product();
                        tmpProduct.Domain = company.Domain;
                        string parameter = "?";
                        if (!string.IsNullOrEmpty(listproductBizweb.products[i].alias) && listproductBizweb.products[i].alias.Contains("?"))
                        {
                            parameter = "&";
                        }
                        tmpProduct.DetailUrl = company.Website + "products/" + listproductBizweb.products[i].alias + parameter + "utm=websosanh";

                        tmpProduct.ID = Common.GetIDProduct(tmpProduct.DetailUrl);

                        if (listproductBizweb.products[i].variants[0].sku != null)
                        {
                            tmpProduct.MerchantSku = listproductBizweb.products[i].variants[0].sku.ToString();
                        }

                        if (listproductBizweb.products[i].variants[0].inventory_policy == "continue" || listproductBizweb.products[i].variants[0].inventory_management == "")
                        {
                            tmpProduct.Status = Common.ProductStatus.Available;
                        }
                        else if (listproductBizweb.products[i].variants[0].inventory_policy == "deny")
                        {
                            var inventory_quantity = Common.Obj2Int(listproductBizweb.products[i].variants[0].inventory_quantity);
                            if (inventory_quantity > 0)
                            {
                                tmpProduct.Status = Common.ProductStatus.Available;
                            }
                            else
                            {
                                tmpProduct.Status = Common.ProductStatus.Clear;
                            }
                        }
                        else
                        {
                            tmpProduct.Status = Common.ProductStatus.LienHe;
                        }
                        tmpProduct.Instock     = QT.Entities.Common.GetProductInstockFormStatus(tmpProduct.Status);
                        tmpProduct.Manufacture = listproductBizweb.products[i].vendor;

                        tmpProduct.Name     = listproductBizweb.products[i].name;
                        tmpProduct.HashName = Common.GetHashNameProduct(company.Domain, tmpProduct.Name.Trim());

                        tmpProduct.ProductContent = listproductBizweb.products[i].meta_description;

                        CultureInfo cultureInfo = CultureInfo.GetCultureInfo("vi-VN");
                        int         originPrice = 0;
                        int         price       = 0;
                        string      compare_at_price;
                        string      price_temp;
                        if ((listproductBizweb.products[i].variants[0].compare_at_price == null || listproductBizweb.products[i].variants[0].compare_at_price == 0) && listproductBizweb.products[i].variants[0].price == 0)
                        {
                            Log.Error("BIZWEB : Product price equal = 0" + "Product: ID " + tmpProduct.ID + " - " + tmpProduct.Name);
                            continue;
                        }
                        else
                        {
                            compare_at_price = listproductBizweb.products[i].variants[0].compare_at_price.ToString();
                            compare_at_price = (compare_at_price.Contains('.')) ? compare_at_price.Remove(compare_at_price.IndexOf('.')) : compare_at_price;
                            price_temp       = listproductBizweb.products[i].variants[0].price.ToString();
                            price_temp       = (price_temp.Contains('.')) ? price_temp.Remove(price_temp.IndexOf('.')) : price_temp;
                        }
                        try
                        {
                            originPrice = (int)Decimal.Parse(compare_at_price, cultureInfo);
                        }
                        catch (FormatException)
                        {
                        }
                        catch (Exception exception)
                        {
                            Log.Error(string.Format("BIZWEB : Parse discounted_price Error. {0}", tmpProduct.DetailUrl), exception);
                        }
                        try
                        {
                            price = (int)Decimal.Parse(price_temp, cultureInfo);
                        }
                        catch (FormatException ex)
                        {
                            Log.Error("BIZWEB : Parse discounted_price Error. " + listproductBizweb.products[i].variants[0].price + "\r\n" + ex);
                        }
                        catch (Exception exception)
                        {
                            Log.Error(string.Format("BIZWEB : Parse discounted_price Error. {0}", tmpProduct.DetailUrl), exception);
                        }

                        if (price <= 0)
                        {
                            Log.Error("BIZWEB : Product price equal = 0" + "Product: ID " + tmpProduct.ID + " - " + tmpProduct.Name);
                            continue;
                        }
                        if (originPrice > price)
                        {
                            tmpProduct.OriginPrice = originPrice;
                            tmpProduct.Price       = price;
                        }
                        else
                        {
                            tmpProduct.OriginPrice = tmpProduct.Price = price;
                        }
                        //Categories
                        tmpProduct.Categories = new List <string>();
                        tmpProduct.Categories.Add(company.Domain);
                        tmpProduct.Categories.Add(listproductBizweb.products[i].product_type);
                        tmpProduct.IDCategories = Common.GetIDClassification(Common.ConvertToString(tmpProduct.Categories, " -> "));

                        // list Url Images
                        if (listproductBizweb.products[i].images != null && listproductBizweb.products[i].images.Count != 0)
                        {
                            tmpProduct.ImageUrls = new List <string>()
                            {
                                listproductBizweb.products[i].images[0].src
                            };
                        }
                        else
                        {
                            tmpProduct.ImageUrls = new List <string>();
                        }
                        if (listproductBizweb.products[i].variants[0] != null)
                        {
                            tmpProduct.VATStatus = listproductBizweb.products[i].variants[0].taxable == false ? Common.VATStatus.NotVAT : Common.VATStatus.HaveVAT;
                        }
                        else
                        {
                            tmpProduct.VATStatus = Common.VATStatus.UndefinedVAT;
                        }
                        ListProducts.Add(tmpProduct);
                        #endregion
                    }
                    // TH có nhiều phiên bản
                    else if (variant > 1)
                    {
                        //check image nếu có ảnh thì = true, ko có = false
                        bool checkImage = false;
                        Dictionary <int, string> dicImage = new Dictionary <int, string>();
                        if (listproductBizweb.products[i].images != null && listproductBizweb.products[i].images.Count != 0)
                        {
                            checkImage = true;
                            for (int k = 0; k < listproductBizweb.products[i].images.Count; k++)
                            {
                                dicImage.Add(listproductBizweb.products[i].images[k].id, listproductBizweb.products[i].images[k].src);
                            }
                        }
                        else
                        {
                            checkImage = false;
                        }

                        for (int j = 0; j < variant; j++)
                        {
                            #region Product
                            var tmpProduct = new QT.Entities.Product();
                            tmpProduct.Domain = company.Domain;
                            string parameter = "?";
                            if (!string.IsNullOrEmpty(listproductBizweb.products[i].alias) && listproductBizweb.products[i].alias.Contains("?"))
                            {
                                parameter = "&";
                            }
                            tmpProduct.DetailUrl = company.Website + "products/" + listproductBizweb.products[i].alias + "#" + (j + 1).ToString() + parameter + "utm=websosanh";
                            tmpProduct.ID        = Common.GetIDProduct(tmpProduct.DetailUrl);

                            if (listproductBizweb.products[i].variants[j].sku != null)
                            {
                                tmpProduct.MerchantSku = listproductBizweb.products[i].variants[j].sku.ToString();
                            }

                            if (listproductBizweb.products[i].variants[j].inventory_policy == "continue" || listproductBizweb.products[i].variants[j].inventory_management == "")
                            {
                                tmpProduct.Status = Common.ProductStatus.Available;
                            }
                            else if (listproductBizweb.products[i].variants[j].inventory_policy == "deny")
                            {
                                int inventory_quantity = Common.Obj2Int(listproductBizweb.products[i].variants[j].inventory_quantity);
                                if (inventory_quantity > 0)
                                {
                                    tmpProduct.Status = Common.ProductStatus.Available;
                                }
                                else
                                {
                                    tmpProduct.Status = Common.ProductStatus.Clear;
                                }
                            }
                            else
                            {
                                tmpProduct.Status = Common.ProductStatus.LienHe;
                            }
                            tmpProduct.Instock     = QT.Entities.Common.GetProductInstockFormStatus(tmpProduct.Status);
                            tmpProduct.Manufacture = listproductBizweb.products[i].vendor;
                            string nameproduct = listproductBizweb.products[i].name;
                            if (listproductBizweb.products[i].variants[j].option1 != null)
                            {
                                nameproduct = nameproduct + " " + listproductBizweb.products[i].options[0].name + " " + listproductBizweb.products[i].variants[j].option1;
                            }
                            if (listproductBizweb.products[i].variants[j].option2 != null)
                            {
                                nameproduct = nameproduct + " " + listproductBizweb.products[i].variants[j].option2;
                            }
                            if (listproductBizweb.products[i].variants[j].option3 != null)
                            {
                                nameproduct = nameproduct + " " + listproductBizweb.products[i].variants[j].option3;
                            }
                            tmpProduct.Name     = nameproduct;
                            tmpProduct.HashName = Common.GetHashNameProduct(company.Domain, tmpProduct.Name.Trim());

                            tmpProduct.ProductContent = listproductBizweb.products[i].meta_description;

                            CultureInfo cultureInfo = CultureInfo.GetCultureInfo("vi-VN");
                            int         originPrice = 0;
                            int         price       = 0;
                            try
                            {
                                originPrice = (int)Decimal.Parse(listproductBizweb.products[i].variants[0].compare_at_price.ToString(), cultureInfo);
                            }
                            catch (FormatException)
                            {
                            }
                            try
                            {
                                price = (int)Decimal.Parse(listproductBizweb.products[i].variants[0].price.ToString(), cultureInfo);
                            }
                            catch (FormatException ex)
                            {
                                Log.Error("BIZWEB : Parse discounted_price Error. " + listproductBizweb.products[i].variants[0].price + "\r\n" + ex);
                                continue;
                            }
                            if (price <= 0)
                            {
                                Log.Error("BIZWEB : Product price equal = 0" + "Product: ID " + tmpProduct.ID + " - " + tmpProduct.Name);
                                continue;
                            }
                            if (originPrice > price)
                            {
                                tmpProduct.OriginPrice = originPrice;
                                tmpProduct.Price       = price;
                            }
                            else
                            {
                                tmpProduct.OriginPrice = tmpProduct.Price = price;
                            }

                            //Categories
                            tmpProduct.Categories = new List <string>();
                            tmpProduct.Categories.Add(company.Domain);
                            tmpProduct.Categories.Add(listproductBizweb.products[i].product_type);
                            tmpProduct.IDCategories = Common.GetIDClassification(Common.ConvertToString(tmpProduct.Categories, " -> "));

                            // list Url Images
                            if (checkImage)
                            {
                                int image_id = Common.Obj2Int(listproductBizweb.products[i].variants[j].image_id);
                                //nếu != 0 thì tìm values trong dicImage tương ứng
                                if (image_id != 0)
                                {
                                    string imageurl = dicImage.Where(kvp => kvp.Key == image_id).Select(kvp => kvp.Value).FirstOrDefault();
                                    if (!string.IsNullOrEmpty(imageurl))
                                    {
                                        tmpProduct.ImageUrls = new List <string>()
                                        {
                                            imageurl
                                        }
                                    }
                                    ;
                                    else
                                    {
                                        tmpProduct.ImageUrls = new List <string>()
                                        {
                                            listproductBizweb.products[i].images[0].src
                                        }
                                    };
                                }
                                // nếu = 0 (null) thì lấy ảnh mặc định images[0].src
                                else
                                {
                                    tmpProduct.ImageUrls = new List <string>()
                                    {
                                        listproductBizweb.products[i].images[0].src
                                    }
                                };
                            }
                            else
                            {
                                tmpProduct.ImageUrls = new List <string>();
                            }
                            if (listproductBizweb.products[i].variants[j] != null)
                            {
                                tmpProduct.VATStatus = listproductBizweb.products[i].variants[j].taxable == false ? Common.VATStatus.NotVAT : Common.VATStatus.HaveVAT;
                            }
                            else
                            {
                                tmpProduct.VATStatus = Common.VATStatus.UndefinedVAT;
                            }
                            ListProducts.Add(tmpProduct);
                            #endregion
                        }
                    }
                    else if (variant < 1)
                    {
                        Log.ErrorFormat("Error : Variant of {0}  = 0", listproductBizweb.products[i].name);
                    }
                    #endregion
                }
            }
            return(ListProducts);
        }
Пример #21
0
 public MetaFieldService(BizwebAuthorizationState authState) : base(authState, "metafield", "metafields")
 {
 }
Пример #22
0
 public TransactionService(BizwebAuthorizationState authState) : base(authState)
 {
 }
Пример #23
0
 public BlogService(BizwebAuthorizationState authState) : base(authState)
 {
 }
Пример #24
0
 /// <summary>
 /// Creates a new instance of BizwebService.
 /// </summary>
 /// <param name="authState">The object contain Bizweb authorization data.</param>
 /// <param name="apiClassPath">The API name or path, e.g. "product".</param>
 /// <param name="apiClassPathInPlural">The API name or path in plural , e.g. "products".</param>
 protected BaseServiceWithSimpleCRUD(BizwebAuthorizationState authState, string apiClassPath,
                                     string apiClassPathInPlural) : base(authState)
 {
     ApiClassPath         = apiClassPath;
     ApiClassPathInPlural = apiClassPathInPlural;
 }
Пример #25
0
 public OrderService(BizwebAuthorizationState authState) : base(authState)
 {
 }