Lua
Lua
Add order to a ShippingEasy account
See more HTTP Misc Examples
Adds an order to a ShippingEasy account (calls the ShippingEasy REST API).Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
-- First create the JSON for the order.
-- See https://shippingeasy.readme.io/v1.0/docs/creating-a-signature
-- for sample JSON.
-- You may use the online tool at https://tools.chilkat.io/Default.cshtml to
-- generate the following code by pasting sample JSON into the tool.
local json = chilkat.JsonObject.new()
json:update_string("order.external_order_identifier", "ABC-100")
json:update_string("order.ordered_at", "2014-01-16 14:37:56 -0600")
json:update_string("order.order_status", "awaiting_shipment")
json:update_string("order.subtotal_including_tax", "10.00")
json:update_string("order.total_including_tax", "10.00")
json:update_string("order.total_excluding_tax", "10.00")
json:update_string("order.discount_amount", "0.00")
json:update_string("order.coupon_discount", "1.00")
json:update_string("order.subtotal_including_tax", "0.00")
json:update_string("order.subtotal_excluding_tax", "0.00")
json:update_string("order.subtotal_excluding_tax", "0.00")
json:update_string("order.subtotal_tax", "0.00")
json:update_string("order.total_tax", "0")
json:update_string("order.base_shipping_cost", "0.00")
json:update_string("order.shipping_cost_including_tax", "0.00")
json:update_string("order.shipping_cost_excluding_tax", "0.00")
json:update_string("order.shipping_cost_tax", "0.00")
json:update_string("order.base_handling_cost", "0.00")
json:update_string("order.handling_cost_excluding_tax", "0.00")
json:update_string("order.handling_cost_including_tax", "0.00")
json:update_string("order.handling_cost_tax", "0.00")
json:update_string("order.base_wrapping_cost", "0.00")
json:update_string("order.wrapping_cost_excluding_tax", "0.00")
json:update_string("order.wrapping_cost_including_tax", "0.00")
json:update_string("order.wrapping_cost_tax", "0.00")
json:update_string("order.notes", "Please send promptly.")
json:update_string("order.billing_company", "Acme Inc.")
json:update_string("order.billing_first_name", "Fred")
json:update_string("order.billing_last_name", "Jones")
json:update_string("order.billing_address", "1234 Street")
json:update_string("order.billing_address2", "Suite 100")
json:update_string("order.billing_city", "Austin")
json:update_string("order.billing_state", "TX")
json:update_string("order.billing_postal_code", "78701")
json:update_string("order.billing_country", "USA")
json:update_string("order.billing_phone_number", "512-123-1234")
json:update_string("order.billing_email", "test@test.com")
json:update_string("order.recipients[0].first_name", "Colin")
json:update_string("order.recipients[0].last_name", "Homenick")
json:update_string("order.recipients[0].company", "Wintheiser-Hickle")
json:update_string("order.recipients[0].email", "charles.crona@okeefe.org")
json:update_string("order.recipients[0].phone_number", "637-481-6505")
json:update_string("order.recipients[0].residential", "true")
json:update_string("order.recipients[0].address", "21937 Adelbert Springs")
json:update_string("order.recipients[0].address2", "")
json:update_string("order.recipients[0].province", "")
json:update_string("order.recipients[0].state", "CT")
json:update_string("order.recipients[0].city", "Terryfurt")
json:update_string("order.recipients[0].postal_code", "93322")
json:update_string("order.recipients[0].postal_code_plus_4", "1234")
json:update_string("order.recipients[0].country", "Andorra")
json:update_string("order.recipients[0].shipping_method", "Ground")
json:update_string("order.recipients[0].base_cost", "10.00")
json:update_string("order.recipients[0].cost_excluding_tax", "10.00")
json:update_string("order.recipients[0].cost_tax", "0.00")
json:update_string("order.recipients[0].base_handling_cost", "0.00")
json:update_string("order.recipients[0].handling_cost_excluding_tax", "0.00")
json:update_string("order.recipients[0].handling_cost_including_tax", "0.00")
json:update_string("order.recipients[0].handling_cost_tax", "0.00")
json:update_string("order.recipients[0].shipping_zone_id", "123")
json:update_string("order.recipients[0].shipping_zone_name", "XYZ")
json:update_string("order.recipients[0].items_total", "1")
json:update_string("order.recipients[0].items_shipped", "0")
json:update_string("order.recipients[0].line_items[0].item_name", "Pencil Holder")
json:update_string("order.recipients[0].line_items[0].sku", "9876543")
json:update_string("order.recipients[0].line_items[0].bin_picking_number", "7")
json:update_string("order.recipients[0].line_items[0].unit_price", "1.30")
json:update_string("order.recipients[0].line_items[0].total_excluding_tax", "1.30")
json:update_string("order.recipients[0].line_items[0].weight_in_ounces", "10")
json:update_string("order.recipients[0].line_items[0].product_options.pa_size", "large")
json:update_string("order.recipients[0].line_items[0].product_options.Colour", "Blue")
json:update_string("order.recipients[0].line_items[0].quantity", "1")
-- Replace these values with your actual apiKey and apiSecret.
-- (These are not valid values..)
local storeApiKey = "569dc30d267f2c4d1f85f9920b2bf9f4"
local apiKey = "f9a7c8ebdfd34beaf260d9b0296c7059"
local apiSecret = "d3e9f8737d7196b1271a725f89e2156672bc1d900a25e6e0c1364e2f7a20f605"
-- Our request will be sent to:
-- https://app.shippingeasy.com/api/stores/:store_api_key/orders
-- The path part of this request is: /api/stores/:store_api_key/orders
-- See https://shippingeasy.readme.io/v1.0/docs/authentication-signing-requests
local sbSigStr = chilkat.StringBuilder.new()
sbSigStr:append("POST&/api/stores/")
sbSigStr:append(storeApiKey)
sbSigStr:append("/orders&")
sbSigStr:append("api_key=")
sbSigStr:append(apiKey)
sbSigStr:append("&api_timestamp=")
local dt = chilkat.DateTime.new()
dt:set_from_current_system_time()
sbSigStr:append(dt:get_as_unix_time_str(false))
sbSigStr:append("&")
json.emit_compact = true
json:emit_sb(sbSigStr)
-- Calculate the HMAC-SHA256 of the API Signature String using the API Secret as the HMAC key.
local crypt = chilkat.Crypt2.new()
crypt.mac_algorithm = "hmac"
crypt.encoding_mode = "hex"
crypt:set_mac_key_string(apiSecret)
crypt.hash_algorithm = "sha256"
local sbSignature = chilkat.StringBuilder.new()
sbSignature:append(crypt:mac_string_enc(sbSigStr:get_as_string()))
sbSignature:to_lowercase()
local sbUrl = chilkat.StringBuilder.new()
sbUrl:append("https://app.shippingeasy.com/api/stores/")
sbUrl:append(storeApiKey)
sbUrl:append("/orders?")
sbUrl:append("api_key=")
sbUrl:append(apiKey)
sbUrl:append("&api_timestamp=")
sbUrl:append(dt:get_as_unix_time_str(false))
sbUrl:append("&api_signature=")
sbUrl:append(sbSignature:get_as_string())
-- Send a POST equivalent the following curl request:
-- curl -H "Content-Type: application/json" --data @body.json "https://app.shippingeasy.com/api/stores/27aa472e16faa83dd13b7758d31974ed/orders?
-- api_key=f9a7c8ebdfd34beaf260d9b0296c7059&
-- api_timestamp=1401803554&
-- api_signature=c65f43beed46e581939898a78acd10064cfa146845e97885ec02124d7ad648e4"
local http = chilkat.Http.new()
http.session_log_filename = "qa_output/shippingEasy.txt"
local url = sbUrl:get_as_string()
local resp = chilkat.HttpResponse.new()
local success = http:http_json("POST", url, json, "application/json", resp)
if not success then
print(http.last_error_text)
return
end
print("response status code = " .. resp.status_code)
print("response body:")
print(resp.body_str)