Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Shippo Create an Order
See more Shippo Examples
Demonstrates how to create an order with all the information about your order by sending a POST request to the orders endpoint.Chilkat Pascal (Lazarus/Delphi) Downloads
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;
http: THttp;
json: TJsonObject;
resp: THttpResponse;
sbResponseBody: TStringBuilder;
jResp: TJsonObject;
respStatusCode: Integer;
begin
success := False;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http := THttp.Create;
// Implements the following CURL command:
// curl https://api.goshippo.com/orders/ \
// -H "Authorization: ShippoToken <API_Token>" \
// -H "Content-Type: application/json" \
// -d '{
// "to_address": {
// "city": "San Francisco",
// "company": "Shippo",
// "country": "US",
// "email": "shippotle@goshippo.com",
// "name": "Mr Hippo",
// "phone": "15553419393",
// "state": "CA",
// "street1": "215 Clayton St.",
// "zip": "94117"
// },
// "line_items": [
// {
// "quantity": 1,
// "sku": "HM-123",
// "title": "Hippo Magazines",
// "total_price": "12.10",
// "currency": "USD",
// "weight": "0.40",
// "weight_unit": "lb"
// }
// ],
// "placed_at": "2016-09-23T01:28:12Z",
// "order_number": "#1068",
// "order_status": "PAID",
// "shipping_cost": "12.83",
// "shipping_cost_currency": "USD",
// "shipping_method": "USPS First Class Package",
// "subtotal_price": "12.10",
// "total_price": "24.93",
// "total_tax": "0.00",
// "currency": "USD",
// "weight": "0.40",
// "weight_unit": "lb"
// }'
// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "to_address": {
// "city": "San Francisco",
// "company": "Shippo",
// "country": "US",
// "email": "shippotle@goshippo.com",
// "name": "Mr Hippo",
// "phone": "15553419393",
// "state": "CA",
// "street1": "215 Clayton St.",
// "zip": "94117"
// },
// "line_items": [
// {
// "quantity": 1,
// "sku": "HM-123",
// "title": "Hippo Magazines",
// "total_price": "12.10",
// "currency": "USD",
// "weight": "0.40",
// "weight_unit": "lb"
// }
// ],
// "placed_at": "2016-09-23T01:28:12Z",
// "order_number": "#1068",
// "order_status": "PAID",
// "shipping_cost": "12.83",
// "shipping_cost_currency": "USD",
// "shipping_method": "USPS First Class Package",
// "subtotal_price": "12.10",
// "total_price": "24.93",
// "total_tax": "0.00",
// "currency": "USD",
// "weight": "0.40",
// "weight_unit": "lb"
// }
json := TJsonObject.Create;
json.UpdateString('to_address.city','San Francisco');
json.UpdateString('to_address.company','Shippo');
json.UpdateString('to_address.country','US');
json.UpdateString('to_address.email','shippotle@goshippo.com');
json.UpdateString('to_address.name','Mr Hippo');
json.UpdateString('to_address.phone','15553419393');
json.UpdateString('to_address.state','CA');
json.UpdateString('to_address.street1','215 Clayton St.');
json.UpdateString('to_address.zip','94117');
json.UpdateInt('line_items[0].quantity',1);
json.UpdateString('line_items[0].sku','HM-123');
json.UpdateString('line_items[0].title','Hippo Magazines');
json.UpdateString('line_items[0].total_price','12.10');
json.UpdateString('line_items[0].currency','USD');
json.UpdateString('line_items[0].weight','0.40');
json.UpdateString('line_items[0].weight_unit','lb');
json.UpdateString('placed_at','2016-09-23T01:28:12Z');
json.UpdateString('order_number','#1068');
json.UpdateString('order_status','PAID');
json.UpdateString('shipping_cost','12.83');
json.UpdateString('shipping_cost_currency','USD');
json.UpdateString('shipping_method','USPS First Class Package');
json.UpdateString('subtotal_price','12.10');
json.UpdateString('total_price','24.93');
json.UpdateString('total_tax','0.00');
json.UpdateString('currency','USD');
json.UpdateString('weight','0.40');
json.UpdateString('weight_unit','lb');
http.SetRequestHeader('Authorization','ShippoToken <API_Token>');
http.SetRequestHeader('Content-Type','application/json');
resp := THttpResponse.Create;
success := http.HttpJson('POST','https://api.goshippo.com/orders/',json,'application/json',resp);
if (success = False) then
begin
WriteLn(http.LastErrorText);
Exit;
end;
sbResponseBody := TStringBuilder.Create;
resp.GetBodySb(sbResponseBody);
jResp := TJsonObject.Create;
jResp.LoadSb(sbResponseBody);
jResp.EmitCompact := False;
WriteLn('Response Body:');
WriteLn(jResp.Emit());
respStatusCode := resp.StatusCode;
WriteLn('Response Status Code = ' + respStatusCode);
if (respStatusCode >= 400) then
begin
WriteLn('Response Header:');
WriteLn(resp.Header);
WriteLn('Failed.');
Exit;
end;
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {}
// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
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.