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
(C) Add order to a ShippingEasy accountAdds an order to a ShippingEasy account (calls the ShippingEasy REST API).
#include <C_CkJsonObject.h> #include <C_CkStringBuilder.h> #include <C_CkDateTime.h> #include <C_CkCrypt2.h> #include <C_CkHttp.h> #include <C_CkHttpResponse.h> void ChilkatSample(void) { BOOL success; HCkJsonObject json; const char *storeApiKey; const char *apiKey; const char *apiSecret; HCkStringBuilder sbSigStr; HCkDateTime dt; HCkCrypt2 crypt; HCkStringBuilder sbSignature; HCkStringBuilder sbUrl; HCkHttp http; HCkHttpResponse 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 = CkJsonObject_Create(); CkJsonObject_UpdateString(json,"order.external_order_identifier","ABC-100"); CkJsonObject_UpdateString(json,"order.ordered_at","2014-01-16 14:37:56 -0600"); CkJsonObject_UpdateString(json,"order.order_status","awaiting_shipment"); CkJsonObject_UpdateString(json,"order.subtotal_including_tax","10.00"); CkJsonObject_UpdateString(json,"order.total_including_tax","10.00"); CkJsonObject_UpdateString(json,"order.total_excluding_tax","10.00"); CkJsonObject_UpdateString(json,"order.discount_amount","0.00"); CkJsonObject_UpdateString(json,"order.coupon_discount","1.00"); CkJsonObject_UpdateString(json,"order.subtotal_including_tax","0.00"); CkJsonObject_UpdateString(json,"order.subtotal_excluding_tax","0.00"); CkJsonObject_UpdateString(json,"order.subtotal_excluding_tax","0.00"); CkJsonObject_UpdateString(json,"order.subtotal_tax","0.00"); CkJsonObject_UpdateString(json,"order.total_tax","0"); CkJsonObject_UpdateString(json,"order.base_shipping_cost","0.00"); CkJsonObject_UpdateString(json,"order.shipping_cost_including_tax","0.00"); CkJsonObject_UpdateString(json,"order.shipping_cost_excluding_tax","0.00"); CkJsonObject_UpdateString(json,"order.shipping_cost_tax","0.00"); CkJsonObject_UpdateString(json,"order.base_handling_cost","0.00"); CkJsonObject_UpdateString(json,"order.handling_cost_excluding_tax","0.00"); CkJsonObject_UpdateString(json,"order.handling_cost_including_tax","0.00"); CkJsonObject_UpdateString(json,"order.handling_cost_tax","0.00"); CkJsonObject_UpdateString(json,"order.base_wrapping_cost","0.00"); CkJsonObject_UpdateString(json,"order.wrapping_cost_excluding_tax","0.00"); CkJsonObject_UpdateString(json,"order.wrapping_cost_including_tax","0.00"); CkJsonObject_UpdateString(json,"order.wrapping_cost_tax","0.00"); CkJsonObject_UpdateString(json,"order.notes","Please send promptly."); CkJsonObject_UpdateString(json,"order.billing_company","Acme Inc."); CkJsonObject_UpdateString(json,"order.billing_first_name","Fred"); CkJsonObject_UpdateString(json,"order.billing_last_name","Jones"); CkJsonObject_UpdateString(json,"order.billing_address","1234 Street"); CkJsonObject_UpdateString(json,"order.billing_address2","Suite 100"); CkJsonObject_UpdateString(json,"order.billing_city","Austin"); CkJsonObject_UpdateString(json,"order.billing_state","TX"); CkJsonObject_UpdateString(json,"order.billing_postal_code","78701"); CkJsonObject_UpdateString(json,"order.billing_country","USA"); CkJsonObject_UpdateString(json,"order.billing_phone_number","512-123-1234"); CkJsonObject_UpdateString(json,"order.billing_email","test@test.com"); CkJsonObject_UpdateString(json,"order.recipients[0].first_name","Colin"); CkJsonObject_UpdateString(json,"order.recipients[0].last_name","Homenick"); CkJsonObject_UpdateString(json,"order.recipients[0].company","Wintheiser-Hickle"); CkJsonObject_UpdateString(json,"order.recipients[0].email","charles.crona@okeefe.org"); CkJsonObject_UpdateString(json,"order.recipients[0].phone_number","637-481-6505"); CkJsonObject_UpdateString(json,"order.recipients[0].residential","true"); CkJsonObject_UpdateString(json,"order.recipients[0].address","21937 Adelbert Springs"); CkJsonObject_UpdateString(json,"order.recipients[0].address2",""); CkJsonObject_UpdateString(json,"order.recipients[0].province",""); CkJsonObject_UpdateString(json,"order.recipients[0].state","CT"); CkJsonObject_UpdateString(json,"order.recipients[0].city","Terryfurt"); CkJsonObject_UpdateString(json,"order.recipients[0].postal_code","93322"); CkJsonObject_UpdateString(json,"order.recipients[0].postal_code_plus_4","1234"); CkJsonObject_UpdateString(json,"order.recipients[0].country","Andorra"); CkJsonObject_UpdateString(json,"order.recipients[0].shipping_method","Ground"); CkJsonObject_UpdateString(json,"order.recipients[0].base_cost","10.00"); CkJsonObject_UpdateString(json,"order.recipients[0].cost_excluding_tax","10.00"); CkJsonObject_UpdateString(json,"order.recipients[0].cost_tax","0.00"); CkJsonObject_UpdateString(json,"order.recipients[0].base_handling_cost","0.00"); CkJsonObject_UpdateString(json,"order.recipients[0].handling_cost_excluding_tax","0.00"); CkJsonObject_UpdateString(json,"order.recipients[0].handling_cost_including_tax","0.00"); CkJsonObject_UpdateString(json,"order.recipients[0].handling_cost_tax","0.00"); CkJsonObject_UpdateString(json,"order.recipients[0].shipping_zone_id","123"); CkJsonObject_UpdateString(json,"order.recipients[0].shipping_zone_name","XYZ"); CkJsonObject_UpdateString(json,"order.recipients[0].items_total","1"); CkJsonObject_UpdateString(json,"order.recipients[0].items_shipped","0"); CkJsonObject_UpdateString(json,"order.recipients[0].line_items[0].item_name","Pencil Holder"); CkJsonObject_UpdateString(json,"order.recipients[0].line_items[0].sku","9876543"); CkJsonObject_UpdateString(json,"order.recipients[0].line_items[0].bin_picking_number","7"); CkJsonObject_UpdateString(json,"order.recipients[0].line_items[0].unit_price","1.30"); CkJsonObject_UpdateString(json,"order.recipients[0].line_items[0].total_excluding_tax","1.30"); CkJsonObject_UpdateString(json,"order.recipients[0].line_items[0].weight_in_ounces","10"); CkJsonObject_UpdateString(json,"order.recipients[0].line_items[0].product_options.pa_size","large"); CkJsonObject_UpdateString(json,"order.recipients[0].line_items[0].product_options.Colour","Blue"); CkJsonObject_UpdateString(json,"order.recipients[0].line_items[0].quantity","1"); // Replace these values with your actual apiKey and apiSecret. // (These are not valid values..) storeApiKey = "569dc30d267f2c4d1f85f9920b2bf9f4"; apiKey = "f9a7c8ebdfd34beaf260d9b0296c7059"; apiSecret = "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 = CkStringBuilder_Create(); CkStringBuilder_Append(sbSigStr,"POST&/api/stores/"); CkStringBuilder_Append(sbSigStr,storeApiKey); CkStringBuilder_Append(sbSigStr,"/orders&"); CkStringBuilder_Append(sbSigStr,"api_key="); CkStringBuilder_Append(sbSigStr,apiKey); CkStringBuilder_Append(sbSigStr,"&api_timestamp="); dt = CkDateTime_Create(); CkDateTime_SetFromCurrentSystemTime(dt); CkStringBuilder_Append(sbSigStr,CkDateTime_getAsUnixTimeStr(dt,FALSE)); CkStringBuilder_Append(sbSigStr,"&"); CkJsonObject_putEmitCompact(json,TRUE); CkJsonObject_EmitSb(json,sbSigStr); // Calculate the HMAC-SHA256 of the API Signature String using the API Secret as the HMAC key. crypt = CkCrypt2_Create(); CkCrypt2_putMacAlgorithm(crypt,"hmac"); CkCrypt2_putEncodingMode(crypt,"hex"); CkCrypt2_SetMacKeyString(crypt,apiSecret); CkCrypt2_putHashAlgorithm(crypt,"sha256"); sbSignature = CkStringBuilder_Create(); CkStringBuilder_Append(sbSignature,CkCrypt2_macStringENC(crypt,CkStringBuilder_getAsString(sbSigStr))); CkStringBuilder_ToLowercase(sbSignature); sbUrl = CkStringBuilder_Create(); CkStringBuilder_Append(sbUrl,"https://app.shippingeasy.com/api/stores/"); CkStringBuilder_Append(sbUrl,storeApiKey); CkStringBuilder_Append(sbUrl,"/orders?"); CkStringBuilder_Append(sbUrl,"api_key="); CkStringBuilder_Append(sbUrl,apiKey); CkStringBuilder_Append(sbUrl,"&api_timestamp="); CkStringBuilder_Append(sbUrl,CkDateTime_getAsUnixTimeStr(dt,FALSE)); CkStringBuilder_Append(sbUrl,"&api_signature="); CkStringBuilder_Append(sbUrl,CkStringBuilder_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 = CkHttp_Create(); CkHttp_putSessionLogFilename(http,"qa_output/shippingEasy.txt"); resp = CkHttp_PostJson2(http,CkStringBuilder_getAsString(sbUrl),"application/json",CkJsonObject_emit(json)); if (CkHttp_getLastMethodSuccess(http) != TRUE) { printf("%s\n",CkHttp_lastErrorText(http)); CkJsonObject_Dispose(json); CkStringBuilder_Dispose(sbSigStr); CkDateTime_Dispose(dt); CkCrypt2_Dispose(crypt); CkStringBuilder_Dispose(sbSignature); CkStringBuilder_Dispose(sbUrl); CkHttp_Dispose(http); return; } printf("response status code = %d\n",CkHttpResponse_getStatusCode(resp)); printf("response body:\n"); printf("%s\n",CkHttpResponse_bodyStr(resp)); CkHttpResponse_Dispose(resp); CkJsonObject_Dispose(json); CkStringBuilder_Dispose(sbSigStr); CkDateTime_Dispose(dt); CkCrypt2_Dispose(crypt); CkStringBuilder_Dispose(sbSignature); CkStringBuilder_Dispose(sbUrl); CkHttp_Dispose(http); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.