Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

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 Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Http,
  Chilkat.StringBuilder,
  Chilkat.HttpResponse,
  Chilkat.JsonObject;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  jsonToken: TJsonObject;
  sbUrl: TStringBuilder;
  http: THttp;
  json: TJsonObject;
  url: string;
  resp: THttpResponse;
  sbResponseBody: TStringBuilder;
  jResp: TJsonObject;
  id: Integer;
  orderid: string;

begin
  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 := TJsonObject.Create;
  jsonToken.LoadFile('qa_data/tokens/ecwid.json');

  sbUrl := TStringBuilder.Create;
  sbUrl.Append('https://app.ecwid.com/api/v3/4870020/orders?token=');
  sbUrl.Append(jsonToken.StringOf('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 := THttp.Create;
  http.SetRequestHeader('Cache-Control','no-cache');

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

  url := sbUrl.GetAsString();
  resp := THttpResponse.Create;
  success := http.HttpJson('POST',url,json,'application/json',resp);
  if (success = False) then
    begin
      WriteLn(http.LastErrorText);
      Exit;
    end;

  WriteLn('Response status code = ' + resp.StatusCode);

  sbResponseBody := TStringBuilder.Create;
  resp.GetBodySb(sbResponseBody);
  jResp := TJsonObject.Create;
  jResp.LoadSb(sbResponseBody);
  jResp.EmitCompact := False;

  WriteLn('Response Body:');
  WriteLn(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

  id := jResp.IntOf('id');
  orderid := jResp.StringOf('orderid');


  jsonToken.Free;
  sbUrl.Free;
  http.Free;
  json.Free;
  resp.Free;
  sbResponseBody.Free;
  jResp.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.