internal static FulfillmentNodes GetFulfillmentNodes(String token) { FulfillmentNodes fulfillmentNodes = new FulfillmentNodes(); using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); var response = client.GetAsync("https://merchant-api.jet.com/api/setup/fulfillmentNodes").Result.Content.ReadAsStringAsync().Result; fulfillmentNodes = (FulfillmentNodes)JsonConvert.DeserializeObject(response, typeof(FulfillmentNodes)); } return(fulfillmentNodes); }
internal static String OrderToXML(string publicDirectory, dynamic order, FulfillmentNodes fulfillmentNodes) { String fileType = ".xml"; String fileName = "jetOrder"; Random random = new Random(); String uniqueNum = String.Format("_{0}", random.Next(0, 100000)); String fileDir = publicDirectory + fileName + order.reference_order_id + uniqueNum + fileType; XmlTextWriter writer = new XmlTextWriter(fileDir, null); writer.WriteStartDocument(); writer.WriteComment("Jet order"); writer.WriteStartElement("orders"); // <orders> writer.WriteStartElement("order"); // <order> writer.WriteStartElement("fulfilment"); if (order.fulfillment_node == fulfillmentNodes.fulfillmentNode[0].fulfillmentNodeId) { writer.WriteString(fulfillmentNodes.fulfillmentNode[0].locationName); } else { writer.WriteString(fulfillmentNodes.fulfillmentNode[1].locationName); } writer.WriteEndElement(); writer.WriteStartElement("ponumber"); writer.WriteString(""); writer.WriteEndElement(); writer.WriteStartElement("customerid"); writer.WriteString(""); writer.WriteEndElement(); writer.WriteStartElement("number"); writer.WriteString(order.reference_order_id.Value); writer.WriteEndElement(); //tranid writer.WriteStartElement("tranid", ""); writer.WriteString(""); writer.WriteEndElement(); writer.WriteStartElement("source", ""); writer.WriteString("Jet"); writer.WriteEndElement(); writer.WriteStartElement("internalid", ""); writer.WriteString(order.merchant_order_id.Value); writer.WriteEndElement(); DateTime order_placed_date = DateTime.Parse(order.order_placed_date.Value.ToString()); writer.WriteStartElement("date", ""); writer.WriteString(order_placed_date.Date.ToShortDateString()); writer.WriteEndElement(); writer.WriteStartElement("time", ""); writer.WriteString(order_placed_date.TimeOfDay.ToString()); writer.WriteEndElement(); writer.WriteStartElement("salesrep", ""); writer.WriteString(""); writer.WriteEndElement(); writer.WriteStartElement("contact", " "); // <contact type="SHIP TO"> writer.WriteAttributeString("type", "SHIP TO"); writer.WriteStartElement("name", " "); writer.WriteString(order.shipping_to.recipient.name.Value); writer.WriteEndElement(); String emailAddress = ""; if (!String.IsNullOrWhiteSpace(order.hash_email.Value)) { emailAddress = order.hash_email.Value; } writer.WriteStartElement("email", " "); writer.WriteString(emailAddress); writer.WriteEndElement(); writer.WriteStartElement("phone", " "); writer.WriteString(order.buyer.phone_number.Value); writer.WriteEndElement(); writer.WriteStartElement("address1", " "); writer.WriteString(order.shipping_to.address.address1.Value); writer.WriteEndElement(); writer.WriteStartElement("address2", " "); writer.WriteString(order.shipping_to.address.address2.Value); writer.WriteEndElement(); writer.WriteStartElement("city", " "); writer.WriteString(order.shipping_to.address.city.Value); writer.WriteEndElement(); writer.WriteStartElement("state", " "); writer.WriteString(order.shipping_to.address.state.Value); writer.WriteEndElement(); writer.WriteStartElement("zip", " "); writer.WriteString(order.shipping_to.address.zip_code.Value); writer.WriteEndElement(); writer.WriteStartElement("country", " "); writer.WriteString("USA"); writer.WriteEndElement(); writer.WriteEndElement(); // </contact> writer.WriteStartElement("contact", " "); // <contact type="BILL TO"> writer.WriteAttributeString("type", "BILL TO"); writer.WriteStartElement("name", " "); writer.WriteString(order.buyer.name.Value); writer.WriteEndElement(); writer.WriteStartElement("email", " "); writer.WriteString(emailAddress); writer.WriteEndElement(); writer.WriteStartElement("phone", " "); writer.WriteString(order.buyer.phone_number.Value); writer.WriteEndElement(); writer.WriteStartElement("address1", " "); writer.WriteString(order.shipping_to.address.address1.Value); writer.WriteEndElement(); writer.WriteStartElement("address2", " "); writer.WriteString(order.shipping_to.address.address2.Value); writer.WriteEndElement(); writer.WriteStartElement("city", " "); writer.WriteString(order.shipping_to.address.city.Value); writer.WriteEndElement(); writer.WriteStartElement("state", " "); writer.WriteString(order.shipping_to.address.state.Value); writer.WriteEndElement(); writer.WriteStartElement("zip", " "); writer.WriteString(order.shipping_to.address.zip_code.Value); writer.WriteEndElement(); writer.WriteStartElement("country", " "); writer.WriteString("USA"); writer.WriteEndElement(); writer.WriteEndElement(); // </contact> writer.WriteStartElement("lines", " "); for (var i = 0; i < ((Newtonsoft.Json.Linq.JContainer)(order.order_items)).Count; i++) { writer.WriteStartElement("line", " "); writer.WriteAttributeString("id", order.order_items[i].order_item_id.Value); writer.WriteStartElement("sku", " "); writer.WriteString(order.order_items[i].merchant_sku.Value); writer.WriteEndElement(); writer.WriteStartElement("productname", " "); writer.WriteString(order.order_items[i].product_title.Value); writer.WriteEndElement(); writer.WriteStartElement("quantity", " "); writer.WriteString(order.order_items[i].request_order_quantity.Value.ToString()); writer.WriteEndElement(); writer.WriteStartElement("price", " "); writer.WriteString(order.order_items[i].item_price.base_price.Value.ToString()); writer.WriteEndElement(); writer.WriteEndElement(); // </line> } writer.WriteEndElement(); // </lines> writer.WriteStartElement("amounts", " "); // <amounts> Decimal basePrice = Convert.ToDecimal(order.order_totals.item_price.base_price.Value); Decimal shippingCost = Convert.ToDecimal(order.order_totals.item_price.item_shipping_cost.Value); Decimal tax = Convert.ToDecimal(order.order_totals.item_price.item_shipping_tax.Value); Decimal feeAdjustments = 0; if (!String.IsNullOrEmpty(order.order_totals.fee_adjustments)) { feeAdjustments = Convert.ToDecimal(order.order_totals.fee_adjustments.Value); } Decimal grandTotal = basePrice + shippingCost + tax - feeAdjustments; writer.WriteStartElement("subtotal", " "); writer.WriteString(basePrice.ToString()); writer.WriteEndElement(); writer.WriteStartElement("shipping", " "); writer.WriteString(shippingCost.ToString()); writer.WriteEndElement(); writer.WriteStartElement("handling", " "); writer.WriteString("0.00"); writer.WriteEndElement(); writer.WriteStartElement("tax", " "); writer.WriteString(tax.ToString()); writer.WriteEndElement(); writer.WriteStartElement("discount", " "); writer.WriteString(feeAdjustments.ToString()); writer.WriteEndElement(); writer.WriteStartElement("grandtotal", " "); writer.WriteString(grandTotal.ToString()); writer.WriteEndElement(); writer.WriteStartElement("payment-type", " "); writer.WriteString("Credit Card"); writer.WriteEndElement(); writer.WriteEndElement(); // </amounts> writer.WriteEndElement(); // </order> writer.WriteEndElement(); // </orders> writer.Flush(); writer.Close(); return(fileDir); }
static void Main(string[] args) { BearerToken tokenResponse = new BearerToken(); String line = String.Empty; Int32 value; Dictionary <int, string> menuList = new Dictionary <int, string>(); menuList.Add(1, "Check for open orders"); menuList.Add(2, "Acknowledge Orders"); menuList.Add(3, "Add Tracking Number"); menuList.Add(4, "Download Order XML, acknowledge order"); Console.WriteLine("-------------------------------------\r\r"); Console.WriteLine("\rPlease select:\r"); Console.WriteLine("-------------------------------------\r\r"); foreach (KeyValuePair <int, string> menuItem in menuList) { Console.WriteLine("{0}) {1} \r", menuItem.Key, menuItem.Value); } Console.WriteLine("9) Quit \r"); Console.WriteLine("\r\r"); if (args.Length > 0) { line = args[0]; Console.WriteLine(args[0]); } else { Console.Write(">"); line = Console.ReadLine(); } if (int.TryParse(line, out value)) { if (menuList.ContainsKey(value)) { /* Get token object, convert to custom object type Bearer Token */ String token = HelperClass.GetToken(user, password); tokenResponse = (BearerToken)JsonConvert.DeserializeObject(token, typeof(BearerToken)); Console.WriteLine("Token expires " + tokenResponse.ExpiresOn.ToString()); /* Check for authentication */ String isAuthenticated = HelperClass.AuthCheck(tokenResponse.Token); Console.WriteLine(isAuthenticated); } switch (value) { case 1: fulfillmentNodes = HelperClass.GetFulfillmentNodes(tokenResponse.Token); Console.WriteLine(String.Format("Retrieved {0} fulfillment nodes.", fulfillmentNodes.fulfillmentNode.Count())); OrderUrlList orderUrls = HelperClass.GetOpenOrders(tokenResponse.Token); Console.WriteLine(String.Format("There are {0} open orders", orderUrls.OrderUrls.Count)); foreach (string order_url in orderUrls.OrderUrls) { dynamic order = HelperClass.GetOrderDetail(tokenResponse.Token, order_url); String filename = HelperClass.OrderToXML(publicDirectory, order, fulfillmentNodes); } break; case 2: String order_id = String.Empty; if (args.Length == 2) { order_id = args[1]; Console.WriteLine(args[1]); } else { Console.WriteLine("Please enter order id: \r"); Console.WriteLine("\r\r"); Console.Write(">"); order_id = Console.ReadLine(); } dynamic orderAck = HelperClass.GetOrderDetail(tokenResponse.Token, "orders/withoutShipmentDetail/" + order_id); String ackResult = HelperClass.AcknowledgeOrder(orderAck, tokenResponse.Token); Console.WriteLine(ackResult); break; case 3: String trackingFilename = string.Empty; if (args.Length == 2) { order_id = args[1]; Console.WriteLine(args[1]); } else { Console.WriteLine("Please enter file name: \r"); Console.WriteLine("\r\r"); Console.Write(">"); trackingFilename = Console.ReadLine(); } String trackingResult = HelperClass.TrackingUpdate(tokenResponse.Token, trackingFilename, publicDirectory); Console.WriteLine(trackingResult); break; case 4: fulfillmentNodes = HelperClass.GetFulfillmentNodes(tokenResponse.Token); Console.WriteLine(String.Format("Retrieved {0} fulfillment nodes.", fulfillmentNodes.fulfillmentNode.Count())); OrderUrlList orderUrls2 = HelperClass.GetOpenOrders(tokenResponse.Token); Console.WriteLine(String.Format("There are {0} open orders", orderUrls2.OrderUrls.Count)); foreach (string order_url in orderUrls2.OrderUrls) { dynamic order = HelperClass.GetOrderDetail(tokenResponse.Token, order_url); Boolean successful = AddJetOrder(order); HelperClass.AcknowledgeOrder(order, tokenResponse.Token); } if (orderUrls2.OrderUrls.Count > 0) { MailMessage mail = new MailMessage(); mail.To = emailList; mail.From = emailFrom; mail.Subject = orderUrls2.OrderUrls.Count + " new order(s) on Jet.com"; mail.Body = orderUrls2.OrderUrls.Count + " new order(s) on Jet.com"; SmtpMail.SmtpServer = smtp; SmtpMail.Send(mail); } break; default: break; } } }