Lua
Lua
Ecwid - Create Order
See more Ecwid Examples
Create a new order in an Ecwid store. This can be useful for storefronts with a custom checkout process or manually creating orders for sales made earlier.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
-- Create and send the following HTTP request:
-- Load the access token previously obtained in Ecwid Get Access Token
local jsonToken = chilkat.JsonObject.new()
jsonToken:load_file("qa_data/tokens/ecwid.json")
local sbUrl = chilkat.StringBuilder.new()
sbUrl:append("https://app.ecwid.com/api/v3/4870020/orders?token=")
sbUrl:append(jsonToken:string_of("access_token"))
-- POST /api/v3/4870020/orders?token=1234567890qwqeertt HTTP/1.1
-- Host: app.ecwid.com
-- Content-Type: application/json;charset=utf-8
-- Cache-Control: no-cache
--
-- {
-- "subtotal": 30,
-- "total": 40,
-- "email": "example@example.com",
-- "paymentMethod": "Phone order",
-- "tax": 0,
-- "paymentStatus": "PAID",
-- "customerTaxExempt": false,
-- "customerTaxId": "",
-- "customerTaxIdValid": false,
-- "reversedTaxApplied": false,
-- "fulfillmentStatus": "AWAITING_PROCESSING",
-- "createDate": "2015-09-20 19:59:43 +0000",
-- "items": [
-- {
-- "price": 15,
-- "weight": 0.32,
-- "sku": "00004",
-- "quantity": 2,
-- "name": "Cherry"
-- }
-- ],
-- "billingPerson": {
-- "name": "Eugene K",
-- "companyName": "Hedgehog and Bucket",
-- "street": "My Street",
-- "city": "San Diego",
-- "countryCode": "US",
-- "postalCode": "90002",
-- "stateOrProvinceCode": "CA",
-- "phone": "123141321"
-- },
-- "shippingPerson": {
-- "name": "Eugene K",
-- "companyName": "Hedgehog and Bucket",
-- "street": "My Street",
-- "city": "San Diego",
-- "countryCode": "US",
-- "postalCode": "90002",
-- "stateOrProvinceCode": "CA",
-- "phone": "123141321"
-- },
-- "shippingOption": {
-- "shippingMethodName": "Fast Delivery",
-- "shippingRate": 10,
-- "isPickup": false,
-- "fulfilmentType": "DELIVERY"
-- },
-- "hidden": false,
-- "privateAdminNotes": "Must be delivered till Sunday.",
-- "acceptMarketing": false,
-- "disableAllCustomerNotifications": true,
-- "externalFulfillment": true,
-- "externalOrderId": "2",
-- "pricesIncludeTax": false
-- }
local http = chilkat.Http.new()
http:set_request_header("Cache-Control", "no-cache")
local json = chilkat.JsonObject.new()
json:update_int("subtotal", 30)
json:update_int("total", 40)
json:update_string("email", "example@example.com")
json:update_string("paymentMethod", "Phone order")
json:update_int("tax", 0)
json:update_string("paymentStatus", "PAID")
json:update_bool("customerTaxExempt", false)
json:update_string("customerTaxId", "")
json:update_bool("customerTaxIdValid", false)
json:update_bool("reversedTaxApplied", false)
json:update_string("fulfillmentStatus", "AWAITING_PROCESSING")
json:update_string("createDate", "2015-09-20 19:59:43 +0000")
json:update_int("items[0].price", 15)
json:update_number("items[0].weight", "0.32")
json:update_string("items[0].sku", "00004")
json:update_int("items[0].quantity", 2)
json:update_string("items[0].name", "Cherry")
json:update_string("billingPerson.name", "Eugene K")
json:update_string("billingPerson.companyName", "Hedgehog and Bucket")
json:update_string("billingPerson.street", "My Street")
json:update_string("billingPerson.city", "San Diego")
json:update_string("billingPerson.countryCode", "US")
json:update_string("billingPerson.postalCode", "90002")
json:update_string("billingPerson.stateOrProvinceCode", "CA")
json:update_string("billingPerson.phone", "123141321")
json:update_string("shippingPerson.name", "Eugene K")
json:update_string("shippingPerson.companyName", "Hedgehog and Bucket")
json:update_string("shippingPerson.street", "My Street")
json:update_string("shippingPerson.city", "San Diego")
json:update_string("shippingPerson.countryCode", "US")
json:update_string("shippingPerson.postalCode", "90002")
json:update_string("shippingPerson.stateOrProvinceCode", "CA")
json:update_string("shippingPerson.phone", "123141321")
json:update_string("shippingOption.shippingMethodName", "Fast Delivery")
json:update_int("shippingOption.shippingRate", 10)
json:update_bool("shippingOption.isPickup", false)
json:update_string("shippingOption.fulfilmentType", "DELIVERY")
json:update_bool("hidden", false)
json:update_string("privateAdminNotes", "Must be delivered till Sunday.")
json:update_bool("acceptMarketing", false)
json:update_bool("disableAllCustomerNotifications", true)
json:update_bool("externalFulfillment", true)
json:update_string("externalOrderId", "2")
json:update_bool("pricesIncludeTax", false)
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)
local sbResponseBody = chilkat.StringBuilder.new()
resp:get_body_sb(sbResponseBody)
local jResp = chilkat.JsonObject.new()
jResp:load_sb(sbResponseBody)
jResp.emit_compact = false
print("Response Body:")
print(jResp:emit())
-- Sample JSON response:
-- (Sample code for parsing the JSON response is shown below)
-- {
-- "id": 20,
-- "orderid": "XJ12H"
-- }
-- Sample code for parsing the JSON response...
-- Use the following online tool to generate parsing code from sample JSON:
-- Generate Parsing Code from JSON
local id = jResp:int_of("id")
local orderid = jResp:string_of("orderid")