Sample code for 30+ languages & platforms
Java

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

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        // 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
        JsonObject jsonToken = new JsonObject();
        jsonToken.loadFile("qa_data/tokens/ecwid.json");

        StringBuilder sbUrl = new StringBuilder();
        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 http = new Http();
        http.setRequestHeader("Cache-Control", "no-cache");

        JsonObject json = new JsonObject();
        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);

        String url = sbUrl.getAsString();
        HttpResponse resp = new HttpResponse();
        try {
            http.httpJson("POST", url, json, "application/json", resp);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("Response status code = " + resp.getStatusCode());

        StringBuilder sbResponseBody = new StringBuilder();
        resp.getBodySb(sbResponseBody);
        JsonObject jResp = new JsonObject();
        jResp.loadSb(sbResponseBody);
        jResp.setEmitCompact(false);

        System.out.println("Response Body:");
        System.out.println(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

        int id = jResp.intOf("id");
        String orderid = jResp.stringOf("orderid");
    }
}