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) Amazon SP-API Get Order ItemsSee more Amazon SP-API ExamplesReturns detailed order item information for the order that you specify. For more information, see https://developer-docs.amazon.com/sp-api/docs/orders-api-v0-reference#get-ordersv0ordersorderidorderitems
#include <C_CkAuthAwsW.h> #include <C_CkJsonObjectW.h> #include <C_CkRestW.h> #include <C_CkStringBuilderW.h> void ChilkatSample(void) { const wchar_t *orderId; HCkAuthAwsW authAws; HCkJsonObjectW jsonLwaToken; BOOL success; 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 *ASIN; const wchar_t *OrderItemId; const wchar_t *SellerSKU; const wchar_t *Title; int QuantityOrdered; int QuantityShipped; int NumberOfItems; const wchar_t *CurrencyCode; const wchar_t *Amount; const wchar_t *ItemTaxCurrencyCode; const wchar_t *ItemTaxAmount; const wchar_t *PromotionDiscountCurrencyCode; const wchar_t *PromotionDiscountAmount; BOOL IsGift; const wchar_t *ConditionId; const wchar_t *ConditionSubtypeId; BOOL IsTransparency; BOOL SerialNumberRequired; const wchar_t *IossNumber; const wchar_t *DeemedResellerCategory; const wchar_t *StoreChainStoreId; BOOL IsBuyerRequestedCancel; const wchar_t *BuyerCancelReason; const wchar_t *AmazonOrderId; int i; int count_i; // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. // Gets detailed order item information for a specified 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}/orderItems", // "dataElements": ["buyerInfo"] // } // ] // } sbPath = CkStringBuilderW_Create(); CkStringBuilderW_Append(sbPath,L"/orders/v0/orders/"); CkStringBuilderW_Append(sbPath,orderId); CkStringBuilderW_Append(sbPath,L"/orderItems"); 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"); 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}/orderItems"; 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; } // If successful, gets a JSON response such as the following: // { // "payload": { // "AmazonOrderId": "902-1845936-5435065", // "OrderItems": [ // { // "ASIN": "B00551Q3CS", // "OrderItemId": "05015851154158", // "SellerSKU": "NABetaASINB00551Q3CS", // "Title": "B00551Q3CS [Card Book]", // "QuantityOrdered": 1, // "QuantityShipped": 0, // "ProductInfo": { // "NumberOfItems": 1 // }, // "ItemPrice": { // "CurrencyCode": "USD", // "Amount": "10.00" // }, // "ItemTax": { // "CurrencyCode": "USD", // "Amount": "1.01" // }, // "PromotionDiscount": { // "CurrencyCode": "USD", // "Amount": "0.00" // }, // "IsGift": false, // "ConditionId": "New", // "ConditionSubtypeId": "New", // "IsTransparency": false, // "SerialNumberRequired": false, // "IossNumber": "", // "DeemedResellerCategory": "IOSS", // "StoreChainStoreId": "ISPU_StoreId", // "BuyerRequestedCancel": { // "IsBuyerRequestedCancel": true, // "BuyerCancelReason": "Found cheaper somewhere else." // } // } // ] // } // } // Use this online tool to generate parsing code from sample JSON: // Generate Parsing Code from JSON json = CkJsonObjectW_Create(); CkJsonObjectW_LoadSb(json,sbResponse); CkJsonObjectW_putEmitCompact(json,FALSE); wprintf(L"%s\n",CkJsonObjectW_emit(json)); AmazonOrderId = CkJsonObjectW_stringOf(json,L"payload.AmazonOrderId"); i = 0; count_i = CkJsonObjectW_SizeOfArray(json,L"payload.OrderItems"); while (i < count_i) { CkJsonObjectW_putI(json,i); ASIN = CkJsonObjectW_stringOf(json,L"payload.OrderItems[i].ASIN"); OrderItemId = CkJsonObjectW_stringOf(json,L"payload.OrderItems[i].OrderItemId"); SellerSKU = CkJsonObjectW_stringOf(json,L"payload.OrderItems[i].SellerSKU"); Title = CkJsonObjectW_stringOf(json,L"payload.OrderItems[i].Title"); QuantityOrdered = CkJsonObjectW_IntOf(json,L"payload.OrderItems[i].QuantityOrdered"); QuantityShipped = CkJsonObjectW_IntOf(json,L"payload.OrderItems[i].QuantityShipped"); NumberOfItems = CkJsonObjectW_IntOf(json,L"payload.OrderItems[i].ProductInfo.NumberOfItems"); CurrencyCode = CkJsonObjectW_stringOf(json,L"payload.OrderItems[i].ItemPrice.CurrencyCode"); Amount = CkJsonObjectW_stringOf(json,L"payload.OrderItems[i].ItemPrice.Amount"); ItemTaxCurrencyCode = CkJsonObjectW_stringOf(json,L"payload.OrderItems[i].ItemTax.CurrencyCode"); ItemTaxAmount = CkJsonObjectW_stringOf(json,L"payload.OrderItems[i].ItemTax.Amount"); PromotionDiscountCurrencyCode = CkJsonObjectW_stringOf(json,L"payload.OrderItems[i].PromotionDiscount.CurrencyCode"); PromotionDiscountAmount = CkJsonObjectW_stringOf(json,L"payload.OrderItems[i].PromotionDiscount.Amount"); IsGift = CkJsonObjectW_BoolOf(json,L"payload.OrderItems[i].IsGift"); ConditionId = CkJsonObjectW_stringOf(json,L"payload.OrderItems[i].ConditionId"); ConditionSubtypeId = CkJsonObjectW_stringOf(json,L"payload.OrderItems[i].ConditionSubtypeId"); IsTransparency = CkJsonObjectW_BoolOf(json,L"payload.OrderItems[i].IsTransparency"); SerialNumberRequired = CkJsonObjectW_BoolOf(json,L"payload.OrderItems[i].SerialNumberRequired"); IossNumber = CkJsonObjectW_stringOf(json,L"payload.OrderItems[i].IossNumber"); DeemedResellerCategory = CkJsonObjectW_stringOf(json,L"payload.OrderItems[i].DeemedResellerCategory"); StoreChainStoreId = CkJsonObjectW_stringOf(json,L"payload.OrderItems[i].StoreChainStoreId"); IsBuyerRequestedCancel = CkJsonObjectW_BoolOf(json,L"payload.OrderItems[i].BuyerRequestedCancel.IsBuyerRequestedCancel"); BuyerCancelReason = CkJsonObjectW_stringOf(json,L"payload.OrderItems[i].BuyerRequestedCancel.BuyerCancelReason"); 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); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.