Sample code for 30+ languages & platforms
Swift

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 Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    // 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
    let jsonToken = CkoJsonObject()!
    jsonToken.loadFile(path: "qa_data/tokens/ecwid.json")

    let sbUrl = CkoStringBuilder()!
    sbUrl.append(value: "https://app.ecwid.com/api/v3/4870020/orders?token=")
    sbUrl.append(value: 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  
    //     }

    let http = CkoHttp()!
    http.setRequestHeader(name: "Cache-Control", value: "no-cache")

    let json = CkoJsonObject()!
    json.updateInt(jsonPath: "subtotal", value: 30)
    json.updateInt(jsonPath: "total", value: 40)
    json.updateString(jsonPath: "email", value: "example@example.com")
    json.updateString(jsonPath: "paymentMethod", value: "Phone order")
    json.updateInt(jsonPath: "tax", value: 0)
    json.updateString(jsonPath: "paymentStatus", value: "PAID")
    json.updateBool(jsonPath: "customerTaxExempt", value: false)
    json.updateString(jsonPath: "customerTaxId", value: "")
    json.updateBool(jsonPath: "customerTaxIdValid", value: false)
    json.updateBool(jsonPath: "reversedTaxApplied", value: false)
    json.updateString(jsonPath: "fulfillmentStatus", value: "AWAITING_PROCESSING")
    json.updateString(jsonPath: "createDate", value: "2015-09-20 19:59:43 +0000")
    json.updateInt(jsonPath: "items[0].price", value: 15)
    json.updateNumber(jsonPath: "items[0].weight", numericStr: "0.32")
    json.updateString(jsonPath: "items[0].sku", value: "00004")
    json.updateInt(jsonPath: "items[0].quantity", value: 2)
    json.updateString(jsonPath: "items[0].name", value: "Cherry")
    json.updateString(jsonPath: "billingPerson.name", value: "Eugene K")
    json.updateString(jsonPath: "billingPerson.companyName", value: "Hedgehog and Bucket")
    json.updateString(jsonPath: "billingPerson.street", value: "My Street")
    json.updateString(jsonPath: "billingPerson.city", value: "San Diego")
    json.updateString(jsonPath: "billingPerson.countryCode", value: "US")
    json.updateString(jsonPath: "billingPerson.postalCode", value: "90002")
    json.updateString(jsonPath: "billingPerson.stateOrProvinceCode", value: "CA")
    json.updateString(jsonPath: "billingPerson.phone", value: "123141321")
    json.updateString(jsonPath: "shippingPerson.name", value: "Eugene K")
    json.updateString(jsonPath: "shippingPerson.companyName", value: "Hedgehog and Bucket")
    json.updateString(jsonPath: "shippingPerson.street", value: "My Street")
    json.updateString(jsonPath: "shippingPerson.city", value: "San Diego")
    json.updateString(jsonPath: "shippingPerson.countryCode", value: "US")
    json.updateString(jsonPath: "shippingPerson.postalCode", value: "90002")
    json.updateString(jsonPath: "shippingPerson.stateOrProvinceCode", value: "CA")
    json.updateString(jsonPath: "shippingPerson.phone", value: "123141321")
    json.updateString(jsonPath: "shippingOption.shippingMethodName", value: "Fast Delivery")
    json.updateInt(jsonPath: "shippingOption.shippingRate", value: 10)
    json.updateBool(jsonPath: "shippingOption.isPickup", value: false)
    json.updateString(jsonPath: "shippingOption.fulfilmentType", value: "DELIVERY")
    json.updateBool(jsonPath: "hidden", value: false)
    json.updateString(jsonPath: "privateAdminNotes", value: "Must be delivered till Sunday.")
    json.updateBool(jsonPath: "acceptMarketing", value: false)
    json.updateBool(jsonPath: "disableAllCustomerNotifications", value: true)
    json.updateBool(jsonPath: "externalFulfillment", value: true)
    json.updateString(jsonPath: "externalOrderId", value: "2")
    json.updateBool(jsonPath: "pricesIncludeTax", value: false)

    var url: String? = sbUrl.getAsString()
    let resp = CkoHttpResponse()!
    success = http.httpJson(verb: "POST", url: url, json: json, contentType: "application/json", response: resp)
    if success == false {
        print("\(http.lastErrorText!)")
        return
    }

    print("Response status code = \(resp.statusCode.intValue)")

    let sbResponseBody = CkoStringBuilder()!
    resp.getBodySb(sb: sbResponseBody)
    let jResp = CkoJsonObject()!
    jResp.loadSb(sb: sbResponseBody)
    jResp.emitCompact = 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

    var id: Int = jResp.int(of: "id").intValue
    var orderid: String? = jResp.string(of: "orderid")

}