public static List <string> GetProductSizes(string productType, string color) { List <string> ProductSizes = new List <string>(); string URL = "https://api.scalablepress.com/v2/products/" + productType + "/availability"; string Method = "GET"; try { string result = ScalablePressAPICall(URL, Method, string.Empty); var sizesByColor = JsonConvert.DeserializeObject <Dictionary <string, Dictionary <string, int> > >(result); if (sizesByColor.ContainsKey(color)) { var colorSizes = sizesByColor[color]; foreach (var size in colorSizes) { if (size.Value > 0) { ProductSizes.Add(size.Key); } } } } catch (Exception ex) { LoggingUtil.InsertError(ex); } return(ProductSizes); }
public static bool PlaceOrder(string OrderID) { bool completed = false; try { string OrderToken = ""; List <SqlParameter> op = new List <SqlParameter>(); op.Add(new SqlParameter("@OrderID", OrderID)); DataTable dtToken = DB.Get("OrderTokenSelect", op.ToArray()); if (dtToken != null && dtToken.Rows.Count > 0) { OrderToken = dtToken.Rows[0][0].ToString(); } else { return(completed); } string URL = "https://api.scalablepress.com/v2/order"; string Method = "POST"; string Body = "orderToken=" + OrderToken; string result = ScalablePressAPICall(URL, Method, Body); Order orderObject = JsonConvert.DeserializeObject <Order>(result); if (orderObject.Events.Where(x => x.Name.Equals("order", StringComparison.OrdinalIgnoreCase)).Any()) { completed = true; List <SqlParameter> p = new List <SqlParameter>(); p.Add(new SqlParameter("@OrderID", OrderID)); p.Add(new SqlParameter("@SPOrderID", orderObject.OrderID)); int RowsAffected = DB.SetWithRowsAffected("OrderIDSet", p.ToArray()); } else { throw new Exception("Order " + OrderToken + " was not completed successfully"); } } catch (Exception ex) { ex.Data.Add("OrderID", OrderID); LoggingUtil.InsertError(ex); } return(completed); }
public static Status UpdateOrderStatus(string OrderID) { Status OrderStatus = Status.Error; try { Order orderObject = GetOrder(OrderID); Event eventObject = GetOrderShipping(orderObject); if (eventObject != null && eventObject.Shipping != null) { List <SqlParameter> p = new List <SqlParameter>(); p.Add(new SqlParameter("@OrderID", OrderID)); p.Add(new SqlParameter("@ShippingDate", eventObject.CreatedAt)); p.Add(new SqlParameter("@Tracking", eventObject.Shipping.Tracking)); p.Add(new SqlParameter("@Service", eventObject.Shipping.Service)); int RowsAffected = DB.SetWithRowsAffected("OrderShippingInsert", p.ToArray()); if (RowsAffected <= 0) { OrderStatus = Status.Error; } else { OrderStatus = Status.Shipped; } } else { OrderStatus = GetOrderStatus(orderObject); List <SqlParameter> p = new List <SqlParameter>(); p.Add(new SqlParameter("@OrderID", OrderID)); p.Add(new SqlParameter("@StatusID", (int)OrderStatus)); int RowsAffected = DB.SetWithRowsAffected("OrderStatusUpdate", p.ToArray()); } } catch (Exception ex) { LoggingUtil.InsertError(ex); } if (OrderStatus == Status.Shipped) { BasicUtils.SendUpdate(OrderID); } return(OrderStatus); }
public static string GetDesignID(string VariantID) { List <SqlParameter> vp = new List <SqlParameter>(); vp.Add(new SqlParameter("@VariantID", VariantID)); DataTable dtProduct = DB.Get("ProductDesignSelect", vp.ToArray()); string designID = string.Empty; if (dtProduct != null && dtProduct.Rows.Count > 0) { DataRow drProduct = dtProduct.Rows[0]; if (!string.IsNullOrWhiteSpace(drProduct["DesignID"].ToString())) { designID = drProduct["DesignID"].ToString(); } else { string URL = "https://api.scalablepress.com/v2/design"; string Method = "POST"; /*var bodyCapsule = new { name = designName, type = "dtg", sides = new { front = new { artwork = artworkLoc, dimensions = new { width = 14 }, position = new { horizontal = "C", offset = new { top = 1.5 } } } } }; * * string Body = JsonConvert.SerializeObject(bodyCapsule).ToString();*/ string Body = "name=" + drProduct["Name"].ToString() + "&type=dtg&sides[front][artwork]=" + drProduct["Design"].ToString() //+ "&sides[front][proof]=" + drProduct["Proof"].ToString() + "&sides[front][dimensions][width]=" + drProduct["Width"].ToString() + "&sides[front][position][horizontal]=C&sides[front][position][offset][top]=" + drProduct["InchesFromTop"].ToString(); try { string result = ScalablePressAPICall(URL, Method, Body); Design designObject = JsonConvert.DeserializeObject <Design>(result); designID = designObject.DesignID; vp.Add(new SqlParameter("@DesignID", designID)); DB.Set("ProductDesignIDPut", vp.ToArray()); } catch (Exception ex) { LoggingUtil.InsertError(ex); } } } return(designID); }
public static Event GetOrderShipping(Order orderObject) { Event eventObject = null; try { eventObject = orderObject.Events.Where(x => x.Name.Equals("shipped", StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); } catch (Exception ex) { LoggingUtil.InsertError(ex); } return(eventObject); }
public static string ScalablePressAPICall(string URL, string Method, string Body) { try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL); request.Method = Method; request.ContentType = "application/x-www-form-urlencoded"; request.Accept = "*/*"; CredentialCache mycache = new CredentialCache(); mycache.Add(new Uri(URL), "Basic", new NetworkCredential("", APIKEY)); request.Credentials = mycache; request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(":" + APIKEY))); if (!string.IsNullOrWhiteSpace(Body)) { byte[] myShinyMetalAss = System.Text.ASCIIEncoding.Default.GetBytes(Body); request.ContentLength = myShinyMetalAss.Length; using (Stream postStream = request.GetRequestStream()) postStream.Write(myShinyMetalAss, 0, myShinyMetalAss.Length); } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream responseStream = response.GetResponseStream(); var reader = new StreamReader(responseStream); string result = reader.ReadToEnd(); reader.Close(); responseStream.Close(); return(result); } catch (WebException ex) { LoggingUtil.InsertError(ex); StreamReader sr = new StreamReader(ex.Response.GetResponseStream()); string body = sr.ReadToEnd(); sr.Close(); throw new Exception(body); } }
public static Status GetOrderStatus(Order orderObject) { Status OrderStatus = Status.Error; try { Event eventObject = orderObject.Events.OrderByDescending(x => x.CreatedAt).FirstOrDefault(); if (eventObject != null) { switch (eventObject.Name.ToUpper()) { case "QUOTE": OrderStatus = Status.Pending; break; case "ORDER": case "PAID": case "UNPAID": case "HOLD": OrderStatus = Status.Paid; break; case "PRINTING": OrderStatus = Status.Printing; break; case "SHIPPED": OrderStatus = Status.Shipped; break; case "CANCELLED": OrderStatus = Status.Canceled; break; } } } catch (Exception ex) { LoggingUtil.InsertError(ex); } return(OrderStatus); }
public static bool IsCartAlive(Guid CacheKey) { bool exist = false; try { DataTable dtCart = DB.GetWithQuery("SELECT TOP 1 1 FROM Cart WITH(NOLOCK) WHERE CacheID = '" + CacheKey + "'"); if (dtCart != null && dtCart.Rows.Count > 0) { exist = true; } } catch (Exception ex) { ex.Data.Add("CacheKey", CacheKey); LoggingUtil.InsertError(ex); } return(exist); }
public static bool ApplyDiscountToCart(int DiscountID, Guid CartKey) { bool added = false; try { List <SqlParameter> p = new List <SqlParameter>(); p.Add(new SqlParameter("@CartKey", CartKey)); p.Add(new SqlParameter("@DiscountID", DiscountID)); int iAffected = DB.SetWithRowsAffected("DiscountCartApply", p.ToArray()); added = iAffected > 0; } catch (Exception ex) { ex.Data.Add("DiscountID", DiscountID); ex.Data.Add("CartKey", CartKey); LoggingUtil.InsertError(ex); } return(added); }
public static bool IsValidDiscount(string DiscountCode, out int DiscountID) { bool valid = false; DiscountID = 0; try { Discount d = new Discount(); using (SqlConnection conn = DB.GetConnection()) d = conn.Query <Discount>("DiscountSelect", new { DiscountCode = DiscountCode }, commandType: CommandType.StoredProcedure).FirstOrDefault(); if (d != null) { if (d.DiscountUsage != null && d.DiscountLimit != null && d.DiscountUsage >= d.DiscountLimit) { return(false); } if (d.ExpirationDate != null && d.ExpirationDate < DateTime.UtcNow) { return(false); } DiscountID = d.DiscountID; valid = true; } } catch (Exception ex) { ex.Data.Add("DiscountCode", DiscountCode); LoggingUtil.InsertError(ex); } return(valid); }
public static decimal GetShippingQuote(Guid CacheID) { string BulkQuoteURL = "https://api.scalablepress.com/v2/quote/bulk"; string Method = "POST"; string Body = string.Empty; decimal ShipTotal = 0M; try { List <SqlParameter> p = new List <SqlParameter>(); p.Add(new SqlParameter("@CacheID", CacheID)); DataTable dtCartAddress = DB.Get("CartAddressSelect", p.ToArray()); DataTable dtCart = DB.Get("CartDetailedSelect", p.ToArray()); DataRow drAddress = dtCartAddress.Rows[0]; int i = 0; foreach (DataRow drCart in dtCart.Rows) { if (i > 0) { Body += "&"; } string DesignID = drCart["DesignID"].ToString(); if (string.IsNullOrWhiteSpace(DesignID)) { DesignID = GetDesignID(drCart["VariationID"].ToString()); } Body += "items[" + i + "][type]=dtg"; Body += "&items[" + i + "][designId]=" + DesignID; Body += "&items[" + i + "][address][name]=" + drAddress["ShipFirstName"].ToString() + " " + drAddress["ShipLastName"].ToString(); Body += "&items[" + i + "][address][address1]=" + drAddress["ShipAddress1"].ToString(); if (!string.IsNullOrWhiteSpace(drAddress["ShipAddress2"].ToString())) { Body += "&items[" + i + "][address][address2]=" + drAddress["ShipAddress2"].ToString(); } Body += "&items[" + i + "][address][city]=" + drAddress["ShipCity"].ToString(); Body += "&items[" + i + "][address][state]=" + drAddress["ShipState"].ToString(); Body += "&items[" + i + "][address][country]=" + drAddress["ShipCountryCode"].ToString(); Body += "&items[" + i + "][address][zip]=" + drAddress["ShipZip"].ToString(); Body += "&items[" + i + "][products][0][id]=" + drCart["Type"].ToString(); Body += "&items[" + i + "][products][0][color]=" + drCart["Color"].ToString(); Body += "&items[" + i + "][products][0][quantity]=" + drCart["Quantity"].ToString(); Body += "&items[" + i + "][products][0][size]=" + drCart["Size"].ToString(); i++; } string result = ScalablePressAPICall(BulkQuoteURL, Method, Body); Quote quoteObject = JsonConvert.DeserializeObject <Quote>(result); string OrderToken = quoteObject.OrderToken; p.Add(new SqlParameter("@OrderToken", OrderToken)); int RowsAffected = DB.SetWithRowsAffected("CartOrderTokenPost", p.ToArray()); if (RowsAffected <= 0) { throw new Exception("Order token not saved"); } ShipTotal = quoteObject.Shipping; } catch (Exception ex) { ex.Data.Add("CacheID", CacheID); LoggingUtil.InsertError(ex); } return(ShipTotal); }
public static string ProcessSale(string firstName, string lastName, string billAddress1, string billAddress2, string billCity, string billState, string billCountry, string billZip, List <SaleProduct> CartItems, string CardNo, string cvv2, int expYear, int expMonth, decimal shippingCharge) { string PaymentID = ""; decimal totalPrice = 0M; Dictionary <string, string> payPalConfig = new Dictionary <string, string>(); payPalConfig.Add("mode", mode); try { string AccessToken = GetPayPalAccessToken(); APIContext AC = new APIContext(AccessToken); AC.Config = payPalConfig; Payee pe = new Payee(); pe.merchant_id = "Q4A2XY37JY7VW"; PayPal.Api.Address billingAddress = new PayPal.Api.Address(); billingAddress.city = billCity; billingAddress.line1 = billAddress1; if (!string.IsNullOrWhiteSpace(billAddress2)) { billingAddress.line2 = billAddress2; } billingAddress.state = billState; billingAddress.country_code = billCountry; billingAddress.postal_code = billZip; CreditCard cc = new CreditCard(); cc.billing_address = billingAddress; cc.number = CardNo; cc.cvv2 = cvv2; cc.type = CCUtils.CreditCardType(CardNo).ToLower(); cc.first_name = firstName; cc.last_name = lastName; cc.expire_month = expMonth; cc.expire_year = expYear; FundingInstrument fi = new FundingInstrument(); fi.credit_card = cc; Payer py = new Payer(); py.payment_method = "credit_card"; py.funding_instruments = new List <FundingInstrument>() { fi }; Transaction t = new Transaction(); t.amount = new Amount(); foreach (SaleProduct item in CartItems) { totalPrice = Math.Round(totalPrice + (item.UnitPrice * item.Quantity), 2, MidpointRounding.AwayFromZero); } t.amount.currency = "USD"; t.amount.details = new Details(); t.amount.details.subtotal = totalPrice.ToString("0.00"); t.amount.details.tax = "0.00"; t.amount.details.shipping = shippingCharge.ToString("0.00"); t.amount.total = Math.Round(totalPrice + shippingCharge, 2, MidpointRounding.AwayFromZero).ToString("0.00"); t.description = "Chimaera Conspiracy Store Purchase"; Payment p = new Payment(); p.intent = "sale"; p.transactions = new List <Transaction>() { t }; p.payer = py; Payment pResp = p.Create(AC); if (pResp.state.Equals("approved", StringComparison.OrdinalIgnoreCase)) { PaymentID = pResp.id; } } catch (PayPal.PayPalException ppex) { LoggingUtil.InsertError(ppex); } catch (WebException ex) { LoggingUtil.InsertError(ex); WebResponse wr = ex.Response; if (wr != null) { StreamReader sr = new StreamReader(wr.GetResponseStream()); string ss = sr.ReadToEnd(); throw new Exception(ss); } } catch (Exception ex) { LoggingUtil.InsertError(ex); } return(PaymentID); }