Unicode C
Unicode C
Amazon SP-API Get Specific Order
See more Amazon SP-API Examples
Get a specific Amazon Seller order.Chilkat Unicode C Downloads
#include <C_CkAuthAwsW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkRestW.h>
#include <C_CkStringBuilderW.h>
void ChilkatSample(void)
{
BOOL success;
const wchar_t *orderId;
HCkAuthAwsW authAws;
HCkJsonObjectW jsonLwaToken;
HCkRestW rest;
const wchar_t *lwa_token;
HCkStringBuilderW sbPath;
HCkJsonObjectW jsonRc;
HCkStringBuilderW sbRequest;
HCkStringBuilderW sbResponse;
const wchar_t *uri;
int statusCode;
HCkJsonObjectW jsonResp;
const wchar_t *restrictedDataToken;
HCkJsonObjectW json;
const wchar_t *strVal;
const wchar_t *AmazonOrderId;
const wchar_t *PurchaseDate;
const wchar_t *LastUpdateDate;
const wchar_t *OrderStatus;
const wchar_t *FulfillmentChannel;
const wchar_t *SalesChannel;
const wchar_t *ShipServiceLevel;
const wchar_t *CurrencyCode;
const wchar_t *Amount;
int NumberOfItemsShipped;
int NumberOfItemsUnshipped;
const wchar_t *PaymentMethod;
BOOL IsReplacementOrder;
const wchar_t *MarketplaceId;
const wchar_t *ShipmentServiceLevelCategory;
const wchar_t *OrderType;
const wchar_t *EarliestShipDate;
const wchar_t *LatestShipDate;
const wchar_t *EarliestDeliveryDate;
const wchar_t *LatestDeliveryDate;
BOOL IsBusinessOrder;
BOOL IsPrime;
BOOL IsGlobalExpressEnabled;
BOOL IsPremiumOrder;
BOOL IsSoldByAB;
BOOL IsIBA;
const wchar_t *Name;
const wchar_t *AddressLine1;
const wchar_t *City;
const wchar_t *StateOrRegion;
const wchar_t *PostalCode;
const wchar_t *CountryCode;
const wchar_t *Phone;
const wchar_t *AddressType;
const wchar_t *FulfillmentSupplySourceId;
BOOL IsISPU;
BOOL IsAccessPointOrder;
BOOL HasAutomatedShippingSettings;
const wchar_t *EasyShipShipmentStatus;
const wchar_t *ElectronicInvoiceStatus;
int i;
int count_i;
success = FALSE;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Gets information for this order ID
// The order ID is something like "902-1845936-5435065" and it is the AmazonOrderId returned in the JSON when getting the list of orders. For example:
// {
// "payload": {
// "CreatedBefore": "1.569521782042E9",
// "Orders": [
// {
// "AmazonOrderId": "902-1845936-5435065",
// "PurchaseDate": "1970-01-19T03:58:30Z",
// ...
// However, when using the sandbox, instead use the explicit keyword TEST_CASE_200
orderId = L"TEST_CASE_200";
authAws = CkAuthAwsW_Create();
CkAuthAwsW_putAccessKey(authAws,L"AWS_ACCESS_KEY");
CkAuthAwsW_putSecretKey(authAws,L"AWS_SECRET_KEY");
CkAuthAwsW_putServiceName(authAws,L"execute-api");
// Use the region that is correct for your needs.
CkAuthAwsW_putRegion(authAws,L"eu-west-1");
// First get a restricted data token for the given order ID.
// This requires an LWA access token which cannot be more than 1 hour old.
// See Fetch SP-API LWA Access Token
jsonLwaToken = CkJsonObjectW_Create();
success = CkJsonObjectW_LoadFile(jsonLwaToken,L"qa_data/tokens/sp_api_lwa_token.json");
if (success == FALSE) {
wprintf(L"Failed to load LWA access token.\n");
CkAuthAwsW_Dispose(authAws);
CkJsonObjectW_Dispose(jsonLwaToken);
return;
}
// Must use the non-sandbox domain for getting the RDT.
rest = CkRestW_Create();
success = CkRestW_Connect(rest,L"sellingpartnerapi-eu.amazon.com",443,TRUE,TRUE);
if (success == FALSE) {
wprintf(L"%s\n",CkRestW_lastErrorText(rest));
CkAuthAwsW_Dispose(authAws);
CkJsonObjectW_Dispose(jsonLwaToken);
CkRestW_Dispose(rest);
return;
}
success = CkRestW_SetAuthAws(rest,authAws);
// Add the x-amz-access-token request header.
lwa_token = CkJsonObjectW_stringOf(jsonLwaToken,L"access_token");
CkRestW_ClearAllHeaders(rest);
CkRestW_AddHeader(rest,L"x-amz-access-token",lwa_token);
// We're going to send a POST with the following JSON body:
// {
// "restrictedResources": [
// {
// "method": "GET",
// "path": "/orders/v0/orders/{orderId}",
// "dataElements": ["buyerInfo", "shippingAddress"]
// }
// ]
// }
sbPath = CkStringBuilderW_Create();
CkStringBuilderW_Append(sbPath,L"/orders/v0/orders/");
CkStringBuilderW_Append(sbPath,orderId);
jsonRc = CkJsonObjectW_Create();
CkJsonObjectW_UpdateString(jsonRc,L"restrictedResources[0].method",L"GET");
CkJsonObjectW_UpdateString(jsonRc,L"restrictedResources[0].path",CkStringBuilderW_getAsString(sbPath));
CkJsonObjectW_UpdateString(jsonRc,L"restrictedResources[0].dataElements[0]",L"buyerInfo");
CkJsonObjectW_UpdateString(jsonRc,L"restrictedResources[0].dataElements[1]",L"shippingAddress");
sbRequest = CkStringBuilderW_Create();
CkJsonObjectW_EmitSb(jsonRc,sbRequest);
sbResponse = CkStringBuilderW_Create();
uri = L"/tokens/2021-03-01/restrictedDataToken";
success = CkRestW_FullRequestSb(rest,L"POST",uri,sbRequest,sbResponse);
if (success == FALSE) {
wprintf(L"%s\n",CkRestW_lastErrorText(rest));
CkAuthAwsW_Dispose(authAws);
CkJsonObjectW_Dispose(jsonLwaToken);
CkRestW_Dispose(rest);
CkStringBuilderW_Dispose(sbPath);
CkJsonObjectW_Dispose(jsonRc);
CkStringBuilderW_Dispose(sbRequest);
CkStringBuilderW_Dispose(sbResponse);
return;
}
// Examine the response status.
statusCode = CkRestW_getResponseStatusCode(rest);
if (statusCode != 200) {
wprintf(L"Response status code: %d\n",statusCode);
wprintf(L"Response status text: %s\n",CkRestW_responseStatusText(rest));
wprintf(L"Response body: \n");
wprintf(L"%s\n",CkStringBuilderW_getAsString(sbResponse));
wprintf(L"Failed.\n");
CkAuthAwsW_Dispose(authAws);
CkJsonObjectW_Dispose(jsonLwaToken);
CkRestW_Dispose(rest);
CkStringBuilderW_Dispose(sbPath);
CkJsonObjectW_Dispose(jsonRc);
CkStringBuilderW_Dispose(sbRequest);
CkStringBuilderW_Dispose(sbResponse);
return;
}
// Get the restricted data token.
jsonResp = CkJsonObjectW_Create();
CkJsonObjectW_LoadSb(jsonResp,sbResponse);
restrictedDataToken = CkJsonObjectW_stringOf(jsonResp,L"restrictedDataToken");
wprintf(L"Restricted Data Token: %s\n",restrictedDataToken);
// ------------------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------------------
// Now that we have the RDT, we can use it to get information about the order.
//
// Yes, the SP-API is horribly tedious and painful. You must use an RDT specifically tailored to each request requiring an RDT,
// the RDT must not be over an hour old, and if you need to get a new RDT you must get it using an LWA token that itself is not
// more than an hour old. If the LWA is more than an hour old, you must get a new one. Ughhh!!!!!
// ------------------------------------------------------------------------------------------------------------
// Disconnect from the non-sandbox domain. This example will use the sandbox.
// (The RDT was obtained using the non-sandbox domain. For some reason, the sandbox domain does not work for getting the RDT.)
CkRestW_Disconnect(rest,100);
success = CkRestW_Connect(rest,L"sandbox.sellingpartnerapi-eu.amazon.com",443,TRUE,TRUE);
if (success == FALSE) {
wprintf(L"%s\n",CkRestW_lastErrorText(rest));
CkAuthAwsW_Dispose(authAws);
CkJsonObjectW_Dispose(jsonLwaToken);
CkRestW_Dispose(rest);
CkStringBuilderW_Dispose(sbPath);
CkJsonObjectW_Dispose(jsonRc);
CkStringBuilderW_Dispose(sbRequest);
CkStringBuilderW_Dispose(sbResponse);
CkJsonObjectW_Dispose(jsonResp);
return;
}
success = CkRestW_SetAuthAws(rest,authAws);
CkRestW_ClearAllHeaders(rest);
CkRestW_AddHeader(rest,L"x-amz-access-token",restrictedDataToken);
CkRestW_ClearAllQueryParams(rest);
CkRestW_AddQueryParam(rest,L"MarketplaceIds",L"ATVPDKIKX0DER");
CkRestW_ClearAllPathParams(rest);
CkRestW_AddPathParam(rest,L"{orderId}",orderId);
uri = L"/orders/v0/orders/{orderId}";
success = CkRestW_FullRequestNoBodySb(rest,L"GET",uri,sbResponse);
if (success == FALSE) {
wprintf(L"%s\n",CkRestW_lastErrorText(rest));
CkAuthAwsW_Dispose(authAws);
CkJsonObjectW_Dispose(jsonLwaToken);
CkRestW_Dispose(rest);
CkStringBuilderW_Dispose(sbPath);
CkJsonObjectW_Dispose(jsonRc);
CkStringBuilderW_Dispose(sbRequest);
CkStringBuilderW_Dispose(sbResponse);
CkJsonObjectW_Dispose(jsonResp);
return;
}
// Examine the response status.
statusCode = CkRestW_getResponseStatusCode(rest);
if (statusCode != 200) {
wprintf(L"Response status text: %s\n",CkRestW_responseStatusText(rest));
wprintf(L"Response body: \n");
wprintf(L"%s\n",CkStringBuilderW_getAsString(sbResponse));
wprintf(L"Failed.\n");
CkAuthAwsW_Dispose(authAws);
CkJsonObjectW_Dispose(jsonLwaToken);
CkRestW_Dispose(rest);
CkStringBuilderW_Dispose(sbPath);
CkJsonObjectW_Dispose(jsonRc);
CkStringBuilderW_Dispose(sbRequest);
CkStringBuilderW_Dispose(sbResponse);
CkJsonObjectW_Dispose(jsonResp);
return;
}
wprintf(L"%s\n",CkStringBuilderW_getAsString(sbResponse));
// If successful, gets a JSON response such as the following:
// {
// "payload": {
// "AmazonOrderId": "902-1845936-5435065",
// "PurchaseDate": "1970-01-19T03:58:30Z",
// "LastUpdateDate": "1970-01-19T03:58:32Z",
// "OrderStatus": "Unshipped",
// "FulfillmentChannel": "MFN",
// "SalesChannel": "Amazon.com",
// "ShipServiceLevel": "Std US D2D Dom",
// "OrderTotal": {
// "CurrencyCode": "USD",
// "Amount": "11.01"
// },
// "NumberOfItemsShipped": 0,
// "NumberOfItemsUnshipped": 1,
// "PaymentMethod": "Other",
// "PaymentMethodDetails": [
// "Standard"
// ],
// "IsReplacementOrder": false,
// "MarketplaceId": "ATVPDKIKX0DER",
// "ShipmentServiceLevelCategory": "Standard",
// "OrderType": "StandardOrder",
// "EarliestShipDate": "1970-01-19T03:59:27Z",
// "LatestShipDate": "1970-01-19T04:05:13Z",
// "EarliestDeliveryDate": "1970-01-19T04:06:39Z",
// "LatestDeliveryDate": "1970-01-19T04:15:17Z",
// "IsBusinessOrder": false,
// "IsPrime": false,
// "IsGlobalExpressEnabled": false,
// "IsPremiumOrder": false,
// "IsSoldByAB": false,
// "IsIBA": false,
// "DefaultShipFromLocationAddress": {
// "Name": "MFNIntegrationTestMerchant",
// "AddressLine1": "2201 WESTLAKE AVE",
// "City": "SEATTLE",
// "StateOrRegion": "WA",
// "PostalCode": "98121-2778",
// "CountryCode": "US",
// "Phone": "+1 480-386-0930 ext. 73824",
// "AddressType": "Commercial"
// },
// "FulfillmentInstruction": {
// "FulfillmentSupplySourceId": "sampleSupplySourceId"
// },
// "IsISPU": false,
// "IsAccessPointOrder": false,
// "AutomatedShippingSettings": {
// "HasAutomatedShippingSettings": false
// },
// "EasyShipShipmentStatus": "PendingPickUp",
// "ElectronicInvoiceStatus": "NotRequired"
// }
// }
// Use this online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
json = CkJsonObjectW_Create();
CkJsonObjectW_LoadSb(json,sbResponse);
AmazonOrderId = CkJsonObjectW_stringOf(json,L"payload.AmazonOrderId");
PurchaseDate = CkJsonObjectW_stringOf(json,L"payload.PurchaseDate");
LastUpdateDate = CkJsonObjectW_stringOf(json,L"payload.LastUpdateDate");
OrderStatus = CkJsonObjectW_stringOf(json,L"payload.OrderStatus");
FulfillmentChannel = CkJsonObjectW_stringOf(json,L"payload.FulfillmentChannel");
SalesChannel = CkJsonObjectW_stringOf(json,L"payload.SalesChannel");
ShipServiceLevel = CkJsonObjectW_stringOf(json,L"payload.ShipServiceLevel");
CurrencyCode = CkJsonObjectW_stringOf(json,L"payload.OrderTotal.CurrencyCode");
Amount = CkJsonObjectW_stringOf(json,L"payload.OrderTotal.Amount");
NumberOfItemsShipped = CkJsonObjectW_IntOf(json,L"payload.NumberOfItemsShipped");
NumberOfItemsUnshipped = CkJsonObjectW_IntOf(json,L"payload.NumberOfItemsUnshipped");
PaymentMethod = CkJsonObjectW_stringOf(json,L"payload.PaymentMethod");
IsReplacementOrder = CkJsonObjectW_BoolOf(json,L"payload.IsReplacementOrder");
MarketplaceId = CkJsonObjectW_stringOf(json,L"payload.MarketplaceId");
ShipmentServiceLevelCategory = CkJsonObjectW_stringOf(json,L"payload.ShipmentServiceLevelCategory");
OrderType = CkJsonObjectW_stringOf(json,L"payload.OrderType");
EarliestShipDate = CkJsonObjectW_stringOf(json,L"payload.EarliestShipDate");
LatestShipDate = CkJsonObjectW_stringOf(json,L"payload.LatestShipDate");
EarliestDeliveryDate = CkJsonObjectW_stringOf(json,L"payload.EarliestDeliveryDate");
LatestDeliveryDate = CkJsonObjectW_stringOf(json,L"payload.LatestDeliveryDate");
IsBusinessOrder = CkJsonObjectW_BoolOf(json,L"payload.IsBusinessOrder");
IsPrime = CkJsonObjectW_BoolOf(json,L"payload.IsPrime");
IsGlobalExpressEnabled = CkJsonObjectW_BoolOf(json,L"payload.IsGlobalExpressEnabled");
IsPremiumOrder = CkJsonObjectW_BoolOf(json,L"payload.IsPremiumOrder");
IsSoldByAB = CkJsonObjectW_BoolOf(json,L"payload.IsSoldByAB");
IsIBA = CkJsonObjectW_BoolOf(json,L"payload.IsIBA");
Name = CkJsonObjectW_stringOf(json,L"payload.DefaultShipFromLocationAddress.Name");
AddressLine1 = CkJsonObjectW_stringOf(json,L"payload.DefaultShipFromLocationAddress.AddressLine1");
City = CkJsonObjectW_stringOf(json,L"payload.DefaultShipFromLocationAddress.City");
StateOrRegion = CkJsonObjectW_stringOf(json,L"payload.DefaultShipFromLocationAddress.StateOrRegion");
PostalCode = CkJsonObjectW_stringOf(json,L"payload.DefaultShipFromLocationAddress.PostalCode");
CountryCode = CkJsonObjectW_stringOf(json,L"payload.DefaultShipFromLocationAddress.CountryCode");
Phone = CkJsonObjectW_stringOf(json,L"payload.DefaultShipFromLocationAddress.Phone");
AddressType = CkJsonObjectW_stringOf(json,L"payload.DefaultShipFromLocationAddress.AddressType");
FulfillmentSupplySourceId = CkJsonObjectW_stringOf(json,L"payload.FulfillmentInstruction.FulfillmentSupplySourceId");
IsISPU = CkJsonObjectW_BoolOf(json,L"payload.IsISPU");
IsAccessPointOrder = CkJsonObjectW_BoolOf(json,L"payload.IsAccessPointOrder");
HasAutomatedShippingSettings = CkJsonObjectW_BoolOf(json,L"payload.AutomatedShippingSettings.HasAutomatedShippingSettings");
EasyShipShipmentStatus = CkJsonObjectW_stringOf(json,L"payload.EasyShipShipmentStatus");
ElectronicInvoiceStatus = CkJsonObjectW_stringOf(json,L"payload.ElectronicInvoiceStatus");
i = 0;
count_i = CkJsonObjectW_SizeOfArray(json,L"payload.PaymentMethodDetails");
while (i < count_i) {
CkJsonObjectW_putI(json,i);
strVal = CkJsonObjectW_stringOf(json,L"payload.PaymentMethodDetails[i]");
i = i + 1;
}
wprintf(L"Success!\n");
CkAuthAwsW_Dispose(authAws);
CkJsonObjectW_Dispose(jsonLwaToken);
CkRestW_Dispose(rest);
CkStringBuilderW_Dispose(sbPath);
CkJsonObjectW_Dispose(jsonRc);
CkStringBuilderW_Dispose(sbRequest);
CkStringBuilderW_Dispose(sbResponse);
CkJsonObjectW_Dispose(jsonResp);
CkJsonObjectW_Dispose(json);
}