Sample code for 30+ languages & platforms
Dart

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

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  // 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
  final jsonToken = CkJsonObject();
  jsonToken.loadFile('qa_data/tokens/ecwid.json');

  final sbUrl = CkStringBuilder();
  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  
  //     }

  final http = CkHttp();
  http.setRequestHeader('Cache-Control', 'no-cache');

  final json = CkJsonObject();
  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);

  final url = sbUrl.getAsString();
  final resp = CkHttpResponse();
  try {
    http.httpJson('POST', url, json, 'application/json', resp);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  print('Response status code = ${resp.statusCode}');

  final sbResponseBody = CkStringBuilder();
  resp.getBodySb(sbResponseBody);
  final jResp = CkJsonObject();
  jResp.loadSb(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

  final id = jResp.intOf('id');
  final orderid = jResp.stringOf('orderid');
}