Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C) Add order to a ShippingEasy accountAdds an order to a ShippingEasy account (calls the ShippingEasy REST API).
#include <C_CkJsonObjectW.h> #include <C_CkStringBuilderW.h> #include <C_CkDateTimeW.h> #include <C_CkCrypt2W.h> #include <C_CkHttpW.h> #include <C_CkHttpResponseW.h> void ChilkatSample(void) { BOOL success; HCkJsonObjectW json; const wchar_t *storeApiKey; const wchar_t *apiKey; const wchar_t *apiSecret; HCkStringBuilderW sbSigStr; HCkDateTimeW dt; HCkCrypt2W crypt; HCkStringBuilderW sbSignature; HCkStringBuilderW sbUrl; HCkHttpW http; HCkHttpResponseW resp; // This requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. // First create the JSON for the order. // See https://shippingeasy.readme.io/v1.0/docs/creating-a-signature // for sample JSON. // You may use the online tool at https://tools.chilkat.io/Default.cshtml to // generate the following code by pasting sample JSON into the tool. json = CkJsonObjectW_Create(); CkJsonObjectW_UpdateString(json,L"order.external_order_identifier",L"ABC-100"); CkJsonObjectW_UpdateString(json,L"order.ordered_at",L"2014-01-16 14:37:56 -0600"); CkJsonObjectW_UpdateString(json,L"order.order_status",L"awaiting_shipment"); CkJsonObjectW_UpdateString(json,L"order.subtotal_including_tax",L"10.00"); CkJsonObjectW_UpdateString(json,L"order.total_including_tax",L"10.00"); CkJsonObjectW_UpdateString(json,L"order.total_excluding_tax",L"10.00"); CkJsonObjectW_UpdateString(json,L"order.discount_amount",L"0.00"); CkJsonObjectW_UpdateString(json,L"order.coupon_discount",L"1.00"); CkJsonObjectW_UpdateString(json,L"order.subtotal_including_tax",L"0.00"); CkJsonObjectW_UpdateString(json,L"order.subtotal_excluding_tax",L"0.00"); CkJsonObjectW_UpdateString(json,L"order.subtotal_excluding_tax",L"0.00"); CkJsonObjectW_UpdateString(json,L"order.subtotal_tax",L"0.00"); CkJsonObjectW_UpdateString(json,L"order.total_tax",L"0"); CkJsonObjectW_UpdateString(json,L"order.base_shipping_cost",L"0.00"); CkJsonObjectW_UpdateString(json,L"order.shipping_cost_including_tax",L"0.00"); CkJsonObjectW_UpdateString(json,L"order.shipping_cost_excluding_tax",L"0.00"); CkJsonObjectW_UpdateString(json,L"order.shipping_cost_tax",L"0.00"); CkJsonObjectW_UpdateString(json,L"order.base_handling_cost",L"0.00"); CkJsonObjectW_UpdateString(json,L"order.handling_cost_excluding_tax",L"0.00"); CkJsonObjectW_UpdateString(json,L"order.handling_cost_including_tax",L"0.00"); CkJsonObjectW_UpdateString(json,L"order.handling_cost_tax",L"0.00"); CkJsonObjectW_UpdateString(json,L"order.base_wrapping_cost",L"0.00"); CkJsonObjectW_UpdateString(json,L"order.wrapping_cost_excluding_tax",L"0.00"); CkJsonObjectW_UpdateString(json,L"order.wrapping_cost_including_tax",L"0.00"); CkJsonObjectW_UpdateString(json,L"order.wrapping_cost_tax",L"0.00"); CkJsonObjectW_UpdateString(json,L"order.notes",L"Please send promptly."); CkJsonObjectW_UpdateString(json,L"order.billing_company",L"Acme Inc."); CkJsonObjectW_UpdateString(json,L"order.billing_first_name",L"Fred"); CkJsonObjectW_UpdateString(json,L"order.billing_last_name",L"Jones"); CkJsonObjectW_UpdateString(json,L"order.billing_address",L"1234 Street"); CkJsonObjectW_UpdateString(json,L"order.billing_address2",L"Suite 100"); CkJsonObjectW_UpdateString(json,L"order.billing_city",L"Austin"); CkJsonObjectW_UpdateString(json,L"order.billing_state",L"TX"); CkJsonObjectW_UpdateString(json,L"order.billing_postal_code",L"78701"); CkJsonObjectW_UpdateString(json,L"order.billing_country",L"USA"); CkJsonObjectW_UpdateString(json,L"order.billing_phone_number",L"512-123-1234"); CkJsonObjectW_UpdateString(json,L"order.billing_email",L"test@test.com"); CkJsonObjectW_UpdateString(json,L"order.recipients[0].first_name",L"Colin"); CkJsonObjectW_UpdateString(json,L"order.recipients[0].last_name",L"Homenick"); CkJsonObjectW_UpdateString(json,L"order.recipients[0].company",L"Wintheiser-Hickle"); CkJsonObjectW_UpdateString(json,L"order.recipients[0].email",L"charles.crona@okeefe.org"); CkJsonObjectW_UpdateString(json,L"order.recipients[0].phone_number",L"637-481-6505"); CkJsonObjectW_UpdateString(json,L"order.recipients[0].residential",L"true"); CkJsonObjectW_UpdateString(json,L"order.recipients[0].address",L"21937 Adelbert Springs"); CkJsonObjectW_UpdateString(json,L"order.recipients[0].address2",L""); CkJsonObjectW_UpdateString(json,L"order.recipients[0].province",L""); CkJsonObjectW_UpdateString(json,L"order.recipients[0].state",L"CT"); CkJsonObjectW_UpdateString(json,L"order.recipients[0].city",L"Terryfurt"); CkJsonObjectW_UpdateString(json,L"order.recipients[0].postal_code",L"93322"); CkJsonObjectW_UpdateString(json,L"order.recipients[0].postal_code_plus_4",L"1234"); CkJsonObjectW_UpdateString(json,L"order.recipients[0].country",L"Andorra"); CkJsonObjectW_UpdateString(json,L"order.recipients[0].shipping_method",L"Ground"); CkJsonObjectW_UpdateString(json,L"order.recipients[0].base_cost",L"10.00"); CkJsonObjectW_UpdateString(json,L"order.recipients[0].cost_excluding_tax",L"10.00"); CkJsonObjectW_UpdateString(json,L"order.recipients[0].cost_tax",L"0.00"); CkJsonObjectW_UpdateString(json,L"order.recipients[0].base_handling_cost",L"0.00"); CkJsonObjectW_UpdateString(json,L"order.recipients[0].handling_cost_excluding_tax",L"0.00"); CkJsonObjectW_UpdateString(json,L"order.recipients[0].handling_cost_including_tax",L"0.00"); CkJsonObjectW_UpdateString(json,L"order.recipients[0].handling_cost_tax",L"0.00"); CkJsonObjectW_UpdateString(json,L"order.recipients[0].shipping_zone_id",L"123"); CkJsonObjectW_UpdateString(json,L"order.recipients[0].shipping_zone_name",L"XYZ"); CkJsonObjectW_UpdateString(json,L"order.recipients[0].items_total",L"1"); CkJsonObjectW_UpdateString(json,L"order.recipients[0].items_shipped",L"0"); CkJsonObjectW_UpdateString(json,L"order.recipients[0].line_items[0].item_name",L"Pencil Holder"); CkJsonObjectW_UpdateString(json,L"order.recipients[0].line_items[0].sku",L"9876543"); CkJsonObjectW_UpdateString(json,L"order.recipients[0].line_items[0].bin_picking_number",L"7"); CkJsonObjectW_UpdateString(json,L"order.recipients[0].line_items[0].unit_price",L"1.30"); CkJsonObjectW_UpdateString(json,L"order.recipients[0].line_items[0].total_excluding_tax",L"1.30"); CkJsonObjectW_UpdateString(json,L"order.recipients[0].line_items[0].weight_in_ounces",L"10"); CkJsonObjectW_UpdateString(json,L"order.recipients[0].line_items[0].product_options.pa_size",L"large"); CkJsonObjectW_UpdateString(json,L"order.recipients[0].line_items[0].product_options.Colour",L"Blue"); CkJsonObjectW_UpdateString(json,L"order.recipients[0].line_items[0].quantity",L"1"); // Replace these values with your actual apiKey and apiSecret. // (These are not valid values..) storeApiKey = L"569dc30d267f2c4d1f85f9920b2bf9f4"; apiKey = L"f9a7c8ebdfd34beaf260d9b0296c7059"; apiSecret = L"d3e9f8737d7196b1271a725f89e2156672bc1d900a25e6e0c1364e2f7a20f605"; // Our request will be sent to: // https://app.shippingeasy.com/api/stores/:store_api_key/orders // The path part of this request is: /api/stores/:store_api_key/orders // See https://shippingeasy.readme.io/v1.0/docs/authentication-signing-requests sbSigStr = CkStringBuilderW_Create(); CkStringBuilderW_Append(sbSigStr,L"POST&/api/stores/"); CkStringBuilderW_Append(sbSigStr,storeApiKey); CkStringBuilderW_Append(sbSigStr,L"/orders&"); CkStringBuilderW_Append(sbSigStr,L"api_key="); CkStringBuilderW_Append(sbSigStr,apiKey); CkStringBuilderW_Append(sbSigStr,L"&api_timestamp="); dt = CkDateTimeW_Create(); CkDateTimeW_SetFromCurrentSystemTime(dt); CkStringBuilderW_Append(sbSigStr,CkDateTimeW_getAsUnixTimeStr(dt,FALSE)); CkStringBuilderW_Append(sbSigStr,L"&"); CkJsonObjectW_putEmitCompact(json,TRUE); CkJsonObjectW_EmitSb(json,sbSigStr); // Calculate the HMAC-SHA256 of the API Signature String using the API Secret as the HMAC key. crypt = CkCrypt2W_Create(); CkCrypt2W_putMacAlgorithm(crypt,L"hmac"); CkCrypt2W_putEncodingMode(crypt,L"hex"); CkCrypt2W_SetMacKeyString(crypt,apiSecret); CkCrypt2W_putHashAlgorithm(crypt,L"sha256"); sbSignature = CkStringBuilderW_Create(); CkStringBuilderW_Append(sbSignature,CkCrypt2W_macStringENC(crypt,CkStringBuilderW_getAsString(sbSigStr))); CkStringBuilderW_ToLowercase(sbSignature); sbUrl = CkStringBuilderW_Create(); CkStringBuilderW_Append(sbUrl,L"https://app.shippingeasy.com/api/stores/"); CkStringBuilderW_Append(sbUrl,storeApiKey); CkStringBuilderW_Append(sbUrl,L"/orders?"); CkStringBuilderW_Append(sbUrl,L"api_key="); CkStringBuilderW_Append(sbUrl,apiKey); CkStringBuilderW_Append(sbUrl,L"&api_timestamp="); CkStringBuilderW_Append(sbUrl,CkDateTimeW_getAsUnixTimeStr(dt,FALSE)); CkStringBuilderW_Append(sbUrl,L"&api_signature="); CkStringBuilderW_Append(sbUrl,CkStringBuilderW_getAsString(sbSignature)); // Send a POST equivalent the following curl request: // curl -H "Content-Type: application/json" --data @body.json "https://app.shippingeasy.com/api/stores/27aa472e16faa83dd13b7758d31974ed/orders? // api_key=f9a7c8ebdfd34beaf260d9b0296c7059& // api_timestamp=1401803554& // api_signature=c65f43beed46e581939898a78acd10064cfa146845e97885ec02124d7ad648e4" http = CkHttpW_Create(); CkHttpW_putSessionLogFilename(http,L"qa_output/shippingEasy.txt"); resp = CkHttpW_PostJson2(http,CkStringBuilderW_getAsString(sbUrl),L"application/json",CkJsonObjectW_emit(json)); if (CkHttpW_getLastMethodSuccess(http) != TRUE) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkJsonObjectW_Dispose(json); CkStringBuilderW_Dispose(sbSigStr); CkDateTimeW_Dispose(dt); CkCrypt2W_Dispose(crypt); CkStringBuilderW_Dispose(sbSignature); CkStringBuilderW_Dispose(sbUrl); CkHttpW_Dispose(http); return; } wprintf(L"response status code = %d\n",CkHttpResponseW_getStatusCode(resp)); wprintf(L"response body:\n"); wprintf(L"%s\n",CkHttpResponseW_bodyStr(resp)); CkHttpResponseW_Dispose(resp); CkJsonObjectW_Dispose(json); CkStringBuilderW_Dispose(sbSigStr); CkDateTimeW_Dispose(dt); CkCrypt2W_Dispose(crypt); CkStringBuilderW_Dispose(sbSignature); CkStringBuilderW_Dispose(sbUrl); CkHttpW_Dispose(http); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.