The example project uses the properties from an AuctionWorx Invoice object to create a ShipStationOrder object that is serialized and sent to the ShipStation API.
The classes in this example use only a subset of the properties exposed by the ShipStation API. See the ShipStation Developer documentation for more properties and their types.
public class ShipStationOrder { public AdvancedOptions advancedOptions { get; set; } public string amountPaid { get; set; } public BillTo billTo { get; set; } public string customerEmail { get; set; } public string customerId { get; set; } public string customerNotes { get; set; } public string customerUsername { get; set; } public string internalNotes { get; set; } public List<Item> items { get; set; } public string orderDate { get; set; } public string orderKey { get; set; } public string orderNumber { get; set; } public string orderStatus { get; set; } public string paymentDate { get; set; } public string paymentMethod { get; set; } public string requestedShippingService { get; set; } public string shipByDate { get; set; } public ShipTo shipTo { get; set; } public string shippingAmount { get; set; } public string taxAmount { get; set; } }
The ShipStationOrder class has four nested properties.
public class AdvancedOptions { public string customField1 { get; set; } public string customField2 { get; set; } public string customField3 { get; set; } }
public class BillTo { public string city { get; set; } public string company { get; set; } public string country { get; set; } public string name { get; set; } public string phone { get; set; } public string postalCode { get; set; } public bool residential { get; set; } public string state { get; set; } public string street1 { get; set; } public string street2 { get; set; } public string street3 { get; set; } }
public class Item { public string lineItemKey { get; set; } public string name { get; set; } public string quantity { get; set; } public string sku { get; set; } public string unitPrice { get; set; } }
public class ShipTo { public string city { get; set; } public string company { get; set; } public string country { get; set; } public string name { get; set; } public string phone { get; set; } public string postalCode { get; set; } public bool residential { get; set; } public string state { get; set; } public string street1 { get; set; } public string street2 { get; set; } public string street3 { get; set; } }
The CreateOrder() method (at the end of this topic) is part of ShipStationClient.cs and available for download here. The method takes an invoice ID and an optional boolean as parameters.
Line 3 uses the AuctionWorx GetInvoiceByID() method to get an AuctionWorx Invoice object.
Line 5 instantiates a ShipStationOrder object and Line 7 creates the first nested object, AdvancedOptions.
The customFieldx properties are unassigned strings that can store whatever information you need with the ShipStation Order.
Line 16 assigns the total value of the invoice to the ShipStationOrder object's amountPaid property.
The BillTo nested object is created on Line 18 and then filled with address information from the invoice.
Lines 33 to 37 fill more properties, including some from the AuctionWorx nested object, Payer.
The ShipStationOrder includes a collection of Item objects. To retrieve them, Line 41 uses the GetLineItemsByInvoice() method to return a Page of listing items. The foreach loop starting on Line 44 iterates through the returned line items, turns them into Item objects and adds each one to the Items collection on Line 56.
Line 63 affects the status of the order as recorded by ShipStation. If the paid parameter is true, the order is okay to ship and the status is set to the "awaiting_shipment" keyword. If the order has not been paid, the status is marked as "on_hold".
Notice the check for nulls when fetching values on Lines 64 and 79. ShipStation expects a string instead of a null in certain fields.
After instantiating a ShipTo object on Line 89, the code checks the invoice Type property before attempting to fill the ShipTo properties. ShipStation marks some shipping properties as required fields (for example, Country). If AuctionWorx hasn't yet created the shipping details, Line 112 applies the BillTo values as ShipTo values to ensure required fields are supplied.
Line 127 completes the creation of the objects by assigning a value for the taxAmount property.
See the topic Invoking the ShipStation API for a discussion of the remainder of the CreateOrder() function.
public async Task<bool> CreateOrder(int invoiceId, bool paid = false) { var invoice = AccountingClient.GetInvoiceByID (SystemActors.SystemUserName, invoiceId); ShipStationOrder sso = new ShipStationOrder(); AdvancedOptions advancedOptions = new AdvancedOptions(); sso.advancedOptions = advancedOptions; advancedOptions.customField1 = "Invoice status: " + invoice.Status; advancedOptions.customField2 = "Last Updated: " + (invoice.UpdatedDTTM).ToString ("yyyy-MM-ddTHH\\:mm\\:ss.fffffff"); advancedOptions.customField3 = "Partner: RainWorx"; sso.amountPaid = invoice.Total.ToString(); BillTo billto = new BillTo(); sso.billTo = billto; billto.city = invoice.BillingCity; billto.company = ""; billto.country = invoice.BillingCountry; billto.name = invoice.BillingFirstName + " " + invoice.BillingLastName; billto.phone = null; billto.postalCode = invoice.BillingZipPostal; billto.residential = false; billto.state = invoice.BillingStateRegion; billto.street1 = invoice.BillingStreet1; billto.street2 = invoice.BillingStreet2; billto.street3 = null; sso.customerEmail = invoice.Payer.Email; sso.customerId = invoice.Payer.ID.ToString(); sso.customerNotes = invoice.Comments; sso.customerUsername = invoice.Payer.UserName; sso.internalNotes = invoice.Comments; List<Item> Items = new List<Item>(); sso.items = Items; var pageOfLineItems = AccountingClient.GetLineItemsByInvoice (SystemActors.SystemUserName, invoice.ID, 0, 0, RainWorx.FrameWorx.Strings.Fields.DateStamp, false); foreach (var lineitem in pageOfLineItems.List) { if (lineitem.Type != LineItemTypes.Listing) { continue; } Item item = new Item(); item.lineItemKey = lineitem.ID.ToString(); item.name = lineitem.Description; item.quantity = lineitem.Quantity.ToString(); item.sku = lineitem.ListingID.ToString(); item.unitPrice = lineitem.TotalAmount.ToString(); Items.Add(item); } sso.orderDate = (invoice.CreatedDTTM).ToString ("yyyy-MM-ddTHH\\:mm\\:ss.fffffff"); sso.orderKey = invoice.ID.ToString(); sso.orderNumber = invoice.ID.ToString(); sso.orderStatus = paid ? "awaiting_shipment" : "on_hold"; if (invoice.PaidDTTM != null) { sso.paymentDate = ((DateTime)invoice.PaidDTTM).ToString ("yyyy-MM-ddTHH\\:mm\\:ss.fffffff"); } else { sso.paymentDate = string.Empty; } var paymentMethod = (from p in invoice.PaymentHistory orderby p.Timestamp descending select p.ProviderIdentifier).FirstOrDefault(); sso.paymentMethod = paymentMethod; if (invoice.ShippingOption != null) { sso.requestedShippingService = invoice.ShippingOption.Method.Name; } else { sso.requestedShippingService = string.Empty; } sso.shipByDate = null; ShipTo shipto = new ShipTo(); sso.shipTo = shipto; if (invoice.Type == InvoiceTypes.Shipping) { shipto.city = string.IsNullOrWhiteSpace(invoice.ShippingCity) ? "" : invoice.ShippingCity; shipto.company = ""; shipto.country = string.IsNullOrWhiteSpace(invoice.ShippingCountry) ? "NA" : invoice.ShippingCountry; shipto.name = invoice.ShippingFirstName + " " + invoice.ShippingLastName; shipto.phone = null; shipto.postalCode = string.IsNullOrWhiteSpace(invoice.ShippingZipPostal) ? "" : invoice.ShippingZipPostal; shipto.residential = false; shipto.state = string.IsNullOrWhiteSpace(invoice.ShippingStateRegion) ? "" : invoice.ShippingStateRegion; shipto.street1 = string.IsNullOrWhiteSpace(invoice.ShippingStreet1) ? "" : invoice.ShippingStreet1; shipto.street2 = string.IsNullOrWhiteSpace(invoice.ShippingStreet2) ? "" : invoice.ShippingStreet2; shipto.street3 = null; } else { shipto.city = billto.city; shipto.company = billto.company; shipto.country = billto.country; shipto.name = billto.name; shipto.phone = billto.phone; shipto.postalCode = billto.postalCode; shipto.residential = billto.residential; shipto.state = billto.state; shipto.street1 = billto.street1; shipto.street2 = billto.street2; shipto.street3 = billto.street3; } sso.shippingAmount = invoice.ShippingAmount.ToString(); sso.taxAmount = invoice.SalesTax.ToString(); AddBasicHeader(client); string jsonstring = JsonConvert.SerializeObject(sso); HttpContent content = new StringContent (jsonstring, Encoding.UTF8, "application/json"); msg = await client.PostAsync(ShipStationAPIAddress.AbsoluteUri + "orders/createorder", content); return msg.IsSuccessStatusCode; }
See Also: Invoking the ShipStation API
Copyright © 2002-2022. RainWorx Software. All rights reserved.