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
(DataFlex) 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
Use ChilkatAx-win32.pkg Procedure Test String sOrderId Variant vAuthAws Handle hoAuthAws Handle hoJsonLwaToken Boolean iSuccess Handle hoRest String sLwa_token Handle hoSbPath Handle hoJsonRc Variant vSbRequest Handle hoSbRequest Variant vSbResponse Handle hoSbResponse String sUri Integer iStatusCode Handle hoJsonResp String sRestrictedDataToken Handle hoJson String sASIN String sOrderItemId String sSellerSKU String sTitle Integer iQuantityOrdered Integer iQuantityShipped Integer iNumberOfItems String sCurrencyCode String sAmount String sItemTaxCurrencyCode String sItemTaxAmount String sPromotionDiscountCurrencyCode String sPromotionDiscountAmount Boolean iIsGift String sConditionId String sConditionSubtypeId Boolean iIsTransparency Boolean iSerialNumberRequired String sIossNumber String sDeemedResellerCategory String sStoreChainStoreId Boolean iIsBuyerRequestedCancel String sBuyerCancelReason String sAmazonOrderId Integer i Integer iCount_i String sTemp1 // 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 Move "TEST_CASE_200" To sOrderId Get Create (RefClass(cComChilkatAuthAws)) To hoAuthAws If (Not(IsComObjectCreated(hoAuthAws))) Begin Send CreateComObject of hoAuthAws End Set ComAccessKey Of hoAuthAws To "AWS_ACCESS_KEY" Set ComSecretKey Of hoAuthAws To "AWS_SECRET_KEY" Set ComServiceName Of hoAuthAws To "execute-api" // Use the region that is correct for your needs. Set ComRegion Of hoAuthAws To "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 Get Create (RefClass(cComChilkatJsonObject)) To hoJsonLwaToken If (Not(IsComObjectCreated(hoJsonLwaToken))) Begin Send CreateComObject of hoJsonLwaToken End Get ComLoadFile Of hoJsonLwaToken "qa_data/tokens/sp_api_lwa_token.json" To iSuccess If (iSuccess = False) Begin Showln "Failed to load LWA access token." Procedure_Return End // Must use the non-sandbox domain for getting the RDT. Get Create (RefClass(cComChilkatRest)) To hoRest If (Not(IsComObjectCreated(hoRest))) Begin Send CreateComObject of hoRest End Get ComConnect Of hoRest "sellingpartnerapi-eu.amazon.com" 443 True True To iSuccess If (iSuccess = False) Begin Get ComLastErrorText Of hoRest To sTemp1 Showln sTemp1 Procedure_Return End Get pvComObject of hoAuthAws to vAuthAws Get ComSetAuthAws Of hoRest vAuthAws To iSuccess // Add the x-amz-access-token request header. Get ComStringOf Of hoJsonLwaToken "access_token" To sLwa_token Get ComClearAllHeaders Of hoRest To iSuccess Get ComAddHeader Of hoRest "x-amz-access-token" sLwa_token To iSuccess // We're going to send a POST with the following JSON body: // { // "restrictedResources": [ // { // "method": "GET", // "path": "/orders/v0/orders/{orderId}/orderItems", // "dataElements": ["buyerInfo"] // } // ] // } Get Create (RefClass(cComChilkatStringBuilder)) To hoSbPath If (Not(IsComObjectCreated(hoSbPath))) Begin Send CreateComObject of hoSbPath End Get ComAppend Of hoSbPath "/orders/v0/orders/" To iSuccess Get ComAppend Of hoSbPath sOrderId To iSuccess Get ComAppend Of hoSbPath "/orderItems" To iSuccess Get Create (RefClass(cComChilkatJsonObject)) To hoJsonRc If (Not(IsComObjectCreated(hoJsonRc))) Begin Send CreateComObject of hoJsonRc End Get ComUpdateString Of hoJsonRc "restrictedResources[0].method" "GET" To iSuccess Get ComGetAsString Of hoSbPath To sTemp1 Get ComUpdateString Of hoJsonRc "restrictedResources[0].path" sTemp1 To iSuccess Get ComUpdateString Of hoJsonRc "restrictedResources[0].dataElements[0]" "buyerInfo" To iSuccess Get Create (RefClass(cComChilkatStringBuilder)) To hoSbRequest If (Not(IsComObjectCreated(hoSbRequest))) Begin Send CreateComObject of hoSbRequest End Get pvComObject of hoSbRequest to vSbRequest Get ComEmitSb Of hoJsonRc vSbRequest To iSuccess Get Create (RefClass(cComChilkatStringBuilder)) To hoSbResponse If (Not(IsComObjectCreated(hoSbResponse))) Begin Send CreateComObject of hoSbResponse End Move "/tokens/2021-03-01/restrictedDataToken" To sUri Get pvComObject of hoSbRequest to vSbRequest Get pvComObject of hoSbResponse to vSbResponse Get ComFullRequestSb Of hoRest "POST" sUri vSbRequest vSbResponse To iSuccess If (iSuccess = False) Begin Get ComLastErrorText Of hoRest To sTemp1 Showln sTemp1 Procedure_Return End // Examine the response status. Get ComResponseStatusCode Of hoRest To iStatusCode If (iStatusCode <> 200) Begin Showln "Response status code: " iStatusCode Get ComResponseStatusText Of hoRest To sTemp1 Showln "Response status text: " sTemp1 Showln "Response body: " Get ComGetAsString Of hoSbResponse To sTemp1 Showln sTemp1 Showln "Failed." Procedure_Return End // Get the restricted data token. Get Create (RefClass(cComChilkatJsonObject)) To hoJsonResp If (Not(IsComObjectCreated(hoJsonResp))) Begin Send CreateComObject of hoJsonResp End Get pvComObject of hoSbResponse to vSbResponse Get ComLoadSb Of hoJsonResp vSbResponse To iSuccess Get ComStringOf Of hoJsonResp "restrictedDataToken" To sRestrictedDataToken Showln "Restricted Data Token: " sRestrictedDataToken // ------------------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------------------ // 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.) Get ComDisconnect Of hoRest 100 To iSuccess Get ComConnect Of hoRest "sandbox.sellingpartnerapi-eu.amazon.com" 443 True True To iSuccess If (iSuccess = False) Begin Get ComLastErrorText Of hoRest To sTemp1 Showln sTemp1 Procedure_Return End Get pvComObject of hoAuthAws to vAuthAws Get ComSetAuthAws Of hoRest vAuthAws To iSuccess Get ComClearAllHeaders Of hoRest To iSuccess Get ComAddHeader Of hoRest "x-amz-access-token" sRestrictedDataToken To iSuccess Get ComClearAllQueryParams Of hoRest To iSuccess Get ComAddQueryParam Of hoRest "MarketplaceIds" "ATVPDKIKX0DER" To iSuccess Get ComClearAllPathParams Of hoRest To iSuccess Get ComAddPathParam Of hoRest "{orderId}" sOrderId To iSuccess Move "/orders/v0/orders/{orderId}/orderItems" To sUri Get pvComObject of hoSbResponse to vSbResponse Get ComFullRequestNoBodySb Of hoRest "GET" sUri vSbResponse To iSuccess If (iSuccess = False) Begin Get ComLastErrorText Of hoRest To sTemp1 Showln sTemp1 Procedure_Return End // Examine the response status. Get ComResponseStatusCode Of hoRest To iStatusCode If (iStatusCode <> 200) Begin Get ComResponseStatusText Of hoRest To sTemp1 Showln "Response status text: " sTemp1 Showln "Response body: " Get ComGetAsString Of hoSbResponse To sTemp1 Showln sTemp1 Showln "Failed." Procedure_Return End // 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 Get Create (RefClass(cComChilkatJsonObject)) To hoJson If (Not(IsComObjectCreated(hoJson))) Begin Send CreateComObject of hoJson End Get pvComObject of hoSbResponse to vSbResponse Get ComLoadSb Of hoJson vSbResponse To iSuccess Set ComEmitCompact Of hoJson To False Get ComEmit Of hoJson To sTemp1 Showln sTemp1 Get ComStringOf Of hoJson "payload.AmazonOrderId" To sAmazonOrderId Move 0 To i Get ComSizeOfArray Of hoJson "payload.OrderItems" To iCount_i While (i < iCount_i) Set ComI Of hoJson To i Get ComStringOf Of hoJson "payload.OrderItems[i].ASIN" To sASIN Get ComStringOf Of hoJson "payload.OrderItems[i].OrderItemId" To sOrderItemId Get ComStringOf Of hoJson "payload.OrderItems[i].SellerSKU" To sSellerSKU Get ComStringOf Of hoJson "payload.OrderItems[i].Title" To sTitle Get ComIntOf Of hoJson "payload.OrderItems[i].QuantityOrdered" To iQuantityOrdered Get ComIntOf Of hoJson "payload.OrderItems[i].QuantityShipped" To iQuantityShipped Get ComIntOf Of hoJson "payload.OrderItems[i].ProductInfo.NumberOfItems" To iNumberOfItems Get ComStringOf Of hoJson "payload.OrderItems[i].ItemPrice.CurrencyCode" To sCurrencyCode Get ComStringOf Of hoJson "payload.OrderItems[i].ItemPrice.Amount" To sAmount Get ComStringOf Of hoJson "payload.OrderItems[i].ItemTax.CurrencyCode" To sItemTaxCurrencyCode Get ComStringOf Of hoJson "payload.OrderItems[i].ItemTax.Amount" To sItemTaxAmount Get ComStringOf Of hoJson "payload.OrderItems[i].PromotionDiscount.CurrencyCode" To sPromotionDiscountCurrencyCode Get ComStringOf Of hoJson "payload.OrderItems[i].PromotionDiscount.Amount" To sPromotionDiscountAmount Get ComBoolOf Of hoJson "payload.OrderItems[i].IsGift" To iIsGift Get ComStringOf Of hoJson "payload.OrderItems[i].ConditionId" To sConditionId Get ComStringOf Of hoJson "payload.OrderItems[i].ConditionSubtypeId" To sConditionSubtypeId Get ComBoolOf Of hoJson "payload.OrderItems[i].IsTransparency" To iIsTransparency Get ComBoolOf Of hoJson "payload.OrderItems[i].SerialNumberRequired" To iSerialNumberRequired Get ComStringOf Of hoJson "payload.OrderItems[i].IossNumber" To sIossNumber Get ComStringOf Of hoJson "payload.OrderItems[i].DeemedResellerCategory" To sDeemedResellerCategory Get ComStringOf Of hoJson "payload.OrderItems[i].StoreChainStoreId" To sStoreChainStoreId Get ComBoolOf Of hoJson "payload.OrderItems[i].BuyerRequestedCancel.IsBuyerRequestedCancel" To iIsBuyerRequestedCancel Get ComStringOf Of hoJson "payload.OrderItems[i].BuyerRequestedCancel.BuyerCancelReason" To sBuyerCancelReason Move (i + 1) To i Loop Showln "Success!" End_Procedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.