Sample code for 30+ languages & platforms
Zig

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

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // 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
    const json_token = try chilkat.JsonObject.init();
    defer json_token.deinit();
    json_token.loadFile("qa_data/tokens/ecwid.json") catch {};

    const sb_url = try chilkat.StringBuilder.init();
    defer sb_url.deinit();
    sb_url.append("https://app.ecwid.com/api/v3/4870020/orders?token=") catch {};
    sb_url.append(try json_token.stringOf(alloc, "access_token")) catch {};

    // 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
    //     }

    const http = try chilkat.Http.init();
    defer http.deinit();
    http.setRequestHeader("Cache-Control", "no-cache");

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

    const url = try sb_url.getAsString(alloc);
    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpJson("POST", url, json, "application/json", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Response status code = {d}\n", .{resp.getStatusCode()});

    const sb_response_body = try chilkat.StringBuilder.init();
    defer sb_response_body.deinit();
    resp.getBodySb(sb_response_body) catch {};
    const j_resp = try chilkat.JsonObject.init();
    defer j_resp.deinit();
    j_resp.loadSb(sb_response_body) catch {};
    j_resp.setEmitCompact(false);

    std.debug.print("Response Body:\n", .{});
    std.debug.print("{s}\n", .{try j_resp.emit(alloc)});

    // 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

    _ = j_resp.intOf("id");
    _ = try j_resp.stringOf(alloc, "orderid");
}