SQL Server
SQL Server
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 SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
DECLARE @http int
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- 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"
-- }
DECLARE @json int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'to_address.city', 'San Francisco'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'to_address.company', 'Shippo'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'to_address.country', 'US'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'to_address.email', 'shippotle@goshippo.com'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'to_address.name', 'Mr Hippo'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'to_address.phone', '15553419393'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'to_address.state', 'CA'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'to_address.street1', '215 Clayton St.'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'to_address.zip', '94117'
EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'line_items[0].quantity', 1
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'line_items[0].sku', 'HM-123'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'line_items[0].title', 'Hippo Magazines'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'line_items[0].total_price', '12.10'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'line_items[0].currency', 'USD'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'line_items[0].weight', '0.40'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'line_items[0].weight_unit', 'lb'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placed_at', '2016-09-23T01:28:12Z'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'order_number', '#1068'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'order_status', 'PAID'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'shipping_cost', '12.83'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'shipping_cost_currency', 'USD'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'shipping_method', 'USPS First Class Package'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'subtotal_price', '12.10'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'total_price', '24.93'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'total_tax', '0.00'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'currency', 'USD'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'weight', '0.40'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'weight_unit', 'lb'
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Authorization', 'ShippoToken <API_Token>'
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Content-Type', 'application/json'
DECLARE @resp int
EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT
EXEC sp_OAMethod @http, 'HttpJson', @success OUT, 'POST', 'https://api.goshippo.com/orders/', @json, 'application/json', @resp
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @resp
RETURN
END
DECLARE @sbResponseBody int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbResponseBody OUT
EXEC sp_OAMethod @resp, 'GetBodySb', @success OUT, @sbResponseBody
DECLARE @jResp int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jResp OUT
EXEC sp_OAMethod @jResp, 'LoadSb', @success OUT, @sbResponseBody
EXEC sp_OASetProperty @jResp, 'EmitCompact', 0
PRINT 'Response Body:'
EXEC sp_OAMethod @jResp, 'Emit', @sTmp0 OUT
PRINT @sTmp0
DECLARE @respStatusCode int
EXEC sp_OAGetProperty @resp, 'StatusCode', @respStatusCode OUT
PRINT 'Response Status Code = ' + @respStatusCode
IF @respStatusCode >= 400
BEGIN
PRINT 'Response Header:'
EXEC sp_OAGetProperty @resp, 'Header', @sTmp0 OUT
PRINT @sTmp0
PRINT 'Failed.'
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @sbResponseBody
EXEC @hr = sp_OADestroy @jResp
RETURN
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
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @sbResponseBody
EXEC @hr = sp_OADestroy @jResp
END
GO