Sample code for 30+ languages & platforms
Unicode C

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 Unicode C Downloads

Unicode C
#include <C_CkJsonObjectW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkHttpW.h>
#include <C_CkHttpResponseW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkJsonObjectW jsonToken;
    HCkStringBuilderW sbUrl;
    HCkHttpW http;
    HCkJsonObjectW json;
    const wchar_t *url;
    HCkHttpResponseW resp;
    HCkStringBuilderW sbResponseBody;
    HCkJsonObjectW jResp;
    int id;
    const wchar_t *orderid;

    success = 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
    jsonToken = CkJsonObjectW_Create();
    CkJsonObjectW_LoadFile(jsonToken,L"qa_data/tokens/ecwid.json");

    sbUrl = CkStringBuilderW_Create();
    CkStringBuilderW_Append(sbUrl,L"https://app.ecwid.com/api/v3/4870020/orders?token=");
    CkStringBuilderW_Append(sbUrl,CkJsonObjectW_stringOf(jsonToken,L"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  
    //      }

    http = CkHttpW_Create();
    CkHttpW_SetRequestHeader(http,L"Cache-Control",L"no-cache");

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

    url = CkStringBuilderW_getAsString(sbUrl);
    resp = CkHttpResponseW_Create();
    success = CkHttpW_HttpJson(http,L"POST",url,json,L"application/json",resp);
    if (success == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkJsonObjectW_Dispose(jsonToken);
        CkStringBuilderW_Dispose(sbUrl);
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(json);
        CkHttpResponseW_Dispose(resp);
        return;
    }

    wprintf(L"Response status code = %d\n",CkHttpResponseW_getStatusCode(resp));

    sbResponseBody = CkStringBuilderW_Create();
    CkHttpResponseW_GetBodySb(resp,sbResponseBody);
    jResp = CkJsonObjectW_Create();
    CkJsonObjectW_LoadSb(jResp,sbResponseBody);
    CkJsonObjectW_putEmitCompact(jResp,FALSE);

    wprintf(L"Response Body:\n");
    wprintf(L"%s\n",CkJsonObjectW_emit(jResp));

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

    id = CkJsonObjectW_IntOf(jResp,L"id");
    orderid = CkJsonObjectW_stringOf(jResp,L"orderid");


    CkJsonObjectW_Dispose(jsonToken);
    CkStringBuilderW_Dispose(sbUrl);
    CkHttpW_Dispose(http);
    CkJsonObjectW_Dispose(json);
    CkHttpResponseW_Dispose(resp);
    CkStringBuilderW_Dispose(sbResponseBody);
    CkJsonObjectW_Dispose(jResp);

    }