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
(Tcl) Amazon SP-API Get Specific OrderSee more Amazon SP-API ExamplesGet a specific Amazon Seller order. For more information, see https://developer-docs.amazon.com/sp-api/docs/orders-api-v0-reference#getorders
load ./chilkat.dll # 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 set orderId "TEST_CASE_200" set authAws [new_CkAuthAws] CkAuthAws_put_AccessKey $authAws "AWS_ACCESS_KEY" CkAuthAws_put_SecretKey $authAws "AWS_SECRET_KEY" CkAuthAws_put_ServiceName $authAws "execute-api" # Use the region that is correct for your needs. CkAuthAws_put_Region $authAws "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 set jsonLwaToken [new_CkJsonObject] set success [CkJsonObject_LoadFile $jsonLwaToken "qa_data/tokens/sp_api_lwa_token.json"] if {$success == 0} then { puts "Failed to load LWA access token." delete_CkAuthAws $authAws delete_CkJsonObject $jsonLwaToken exit } # Must use the non-sandbox domain for getting the RDT. set rest [new_CkRest] set success [CkRest_Connect $rest "sellingpartnerapi-eu.amazon.com" 443 1 1] if {$success == 0} then { puts [CkRest_lastErrorText $rest] delete_CkAuthAws $authAws delete_CkJsonObject $jsonLwaToken delete_CkRest $rest exit } set success [CkRest_SetAuthAws $rest $authAws] # Add the x-amz-access-token request header. set lwa_token [CkJsonObject_stringOf $jsonLwaToken "access_token"] CkRest_ClearAllHeaders $rest CkRest_AddHeader $rest "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"] # } # ] # } set sbPath [new_CkStringBuilder] CkStringBuilder_Append $sbPath "/orders/v0/orders/" CkStringBuilder_Append $sbPath $orderId set jsonRc [new_CkJsonObject] CkJsonObject_UpdateString $jsonRc "restrictedResources[0].method" "GET" CkJsonObject_UpdateString $jsonRc "restrictedResources[0].path" [CkStringBuilder_getAsString $sbPath] CkJsonObject_UpdateString $jsonRc "restrictedResources[0].dataElements[0]" "buyerInfo" CkJsonObject_UpdateString $jsonRc "restrictedResources[0].dataElements[1]" "shippingAddress" set sbRequest [new_CkStringBuilder] CkJsonObject_EmitSb $jsonRc $sbRequest set sbResponse [new_CkStringBuilder] set uri "/tokens/2021-03-01/restrictedDataToken" set success [CkRest_FullRequestSb $rest "POST" $uri $sbRequest $sbResponse] if {$success == 0} then { puts [CkRest_lastErrorText $rest] delete_CkAuthAws $authAws delete_CkJsonObject $jsonLwaToken delete_CkRest $rest delete_CkStringBuilder $sbPath delete_CkJsonObject $jsonRc delete_CkStringBuilder $sbRequest delete_CkStringBuilder $sbResponse exit } # Examine the response status. set statusCode [CkRest_get_ResponseStatusCode $rest] if {$statusCode != 200} then { puts "Response status code: $statusCode" puts "Response status text: [CkRest_responseStatusText $rest]" puts "Response body: " puts [CkStringBuilder_getAsString $sbResponse] puts "Failed." delete_CkAuthAws $authAws delete_CkJsonObject $jsonLwaToken delete_CkRest $rest delete_CkStringBuilder $sbPath delete_CkJsonObject $jsonRc delete_CkStringBuilder $sbRequest delete_CkStringBuilder $sbResponse exit } # Get the restricted data token. set jsonResp [new_CkJsonObject] CkJsonObject_LoadSb $jsonResp $sbResponse set restrictedDataToken [CkJsonObject_stringOf $jsonResp "restrictedDataToken"] puts "Restricted Data Token: $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.) CkRest_Disconnect $rest 100 set success [CkRest_Connect $rest "sandbox.sellingpartnerapi-eu.amazon.com" 443 1 1] if {$success == 0} then { puts [CkRest_lastErrorText $rest] delete_CkAuthAws $authAws delete_CkJsonObject $jsonLwaToken delete_CkRest $rest delete_CkStringBuilder $sbPath delete_CkJsonObject $jsonRc delete_CkStringBuilder $sbRequest delete_CkStringBuilder $sbResponse delete_CkJsonObject $jsonResp exit } set success [CkRest_SetAuthAws $rest $authAws] CkRest_ClearAllHeaders $rest CkRest_AddHeader $rest "x-amz-access-token" $restrictedDataToken CkRest_ClearAllQueryParams $rest CkRest_AddQueryParam $rest "MarketplaceIds" "ATVPDKIKX0DER" CkRest_ClearAllPathParams $rest CkRest_AddPathParam $rest "{orderId}" $orderId set uri "/orders/v0/orders/{orderId}" set success [CkRest_FullRequestNoBodySb $rest "GET" $uri $sbResponse] if {$success == 0} then { puts [CkRest_lastErrorText $rest] delete_CkAuthAws $authAws delete_CkJsonObject $jsonLwaToken delete_CkRest $rest delete_CkStringBuilder $sbPath delete_CkJsonObject $jsonRc delete_CkStringBuilder $sbRequest delete_CkStringBuilder $sbResponse delete_CkJsonObject $jsonResp exit } # Examine the response status. set statusCode [CkRest_get_ResponseStatusCode $rest] if {$statusCode != 200} then { puts "Response status text: [CkRest_responseStatusText $rest]" puts "Response body: " puts [CkStringBuilder_getAsString $sbResponse] puts "Failed." delete_CkAuthAws $authAws delete_CkJsonObject $jsonLwaToken delete_CkRest $rest delete_CkStringBuilder $sbPath delete_CkJsonObject $jsonRc delete_CkStringBuilder $sbRequest delete_CkStringBuilder $sbResponse delete_CkJsonObject $jsonResp exit } puts [CkStringBuilder_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 set json [new_CkJsonObject] CkJsonObject_LoadSb $json $sbResponse set AmazonOrderId [CkJsonObject_stringOf $json "payload.AmazonOrderId"] set PurchaseDate [CkJsonObject_stringOf $json "payload.PurchaseDate"] set LastUpdateDate [CkJsonObject_stringOf $json "payload.LastUpdateDate"] set OrderStatus [CkJsonObject_stringOf $json "payload.OrderStatus"] set FulfillmentChannel [CkJsonObject_stringOf $json "payload.FulfillmentChannel"] set SalesChannel [CkJsonObject_stringOf $json "payload.SalesChannel"] set ShipServiceLevel [CkJsonObject_stringOf $json "payload.ShipServiceLevel"] set CurrencyCode [CkJsonObject_stringOf $json "payload.OrderTotal.CurrencyCode"] set Amount [CkJsonObject_stringOf $json "payload.OrderTotal.Amount"] set NumberOfItemsShipped [CkJsonObject_IntOf $json "payload.NumberOfItemsShipped"] set NumberOfItemsUnshipped [CkJsonObject_IntOf $json "payload.NumberOfItemsUnshipped"] set PaymentMethod [CkJsonObject_stringOf $json "payload.PaymentMethod"] set IsReplacementOrder [CkJsonObject_BoolOf $json "payload.IsReplacementOrder"] set MarketplaceId [CkJsonObject_stringOf $json "payload.MarketplaceId"] set ShipmentServiceLevelCategory [CkJsonObject_stringOf $json "payload.ShipmentServiceLevelCategory"] set OrderType [CkJsonObject_stringOf $json "payload.OrderType"] set EarliestShipDate [CkJsonObject_stringOf $json "payload.EarliestShipDate"] set LatestShipDate [CkJsonObject_stringOf $json "payload.LatestShipDate"] set EarliestDeliveryDate [CkJsonObject_stringOf $json "payload.EarliestDeliveryDate"] set LatestDeliveryDate [CkJsonObject_stringOf $json "payload.LatestDeliveryDate"] set IsBusinessOrder [CkJsonObject_BoolOf $json "payload.IsBusinessOrder"] set IsPrime [CkJsonObject_BoolOf $json "payload.IsPrime"] set IsGlobalExpressEnabled [CkJsonObject_BoolOf $json "payload.IsGlobalExpressEnabled"] set IsPremiumOrder [CkJsonObject_BoolOf $json "payload.IsPremiumOrder"] set IsSoldByAB [CkJsonObject_BoolOf $json "payload.IsSoldByAB"] set IsIBA [CkJsonObject_BoolOf $json "payload.IsIBA"] set Name [CkJsonObject_stringOf $json "payload.DefaultShipFromLocationAddress.Name"] set AddressLine1 [CkJsonObject_stringOf $json "payload.DefaultShipFromLocationAddress.AddressLine1"] set City [CkJsonObject_stringOf $json "payload.DefaultShipFromLocationAddress.City"] set StateOrRegion [CkJsonObject_stringOf $json "payload.DefaultShipFromLocationAddress.StateOrRegion"] set PostalCode [CkJsonObject_stringOf $json "payload.DefaultShipFromLocationAddress.PostalCode"] set CountryCode [CkJsonObject_stringOf $json "payload.DefaultShipFromLocationAddress.CountryCode"] set Phone [CkJsonObject_stringOf $json "payload.DefaultShipFromLocationAddress.Phone"] set AddressType [CkJsonObject_stringOf $json "payload.DefaultShipFromLocationAddress.AddressType"] set FulfillmentSupplySourceId [CkJsonObject_stringOf $json "payload.FulfillmentInstruction.FulfillmentSupplySourceId"] set IsISPU [CkJsonObject_BoolOf $json "payload.IsISPU"] set IsAccessPointOrder [CkJsonObject_BoolOf $json "payload.IsAccessPointOrder"] set HasAutomatedShippingSettings [CkJsonObject_BoolOf $json "payload.AutomatedShippingSettings.HasAutomatedShippingSettings"] set EasyShipShipmentStatus [CkJsonObject_stringOf $json "payload.EasyShipShipmentStatus"] set ElectronicInvoiceStatus [CkJsonObject_stringOf $json "payload.ElectronicInvoiceStatus"] set i 0 set count_i [CkJsonObject_SizeOfArray $json "payload.PaymentMethodDetails"] while {$i < $count_i} { CkJsonObject_put_I $json $i set strVal [CkJsonObject_stringOf $json "payload.PaymentMethodDetails[i]"] set i [expr $i + 1] } puts "Success!" delete_CkAuthAws $authAws delete_CkJsonObject $jsonLwaToken delete_CkRest $rest delete_CkStringBuilder $sbPath delete_CkJsonObject $jsonRc delete_CkStringBuilder $sbRequest delete_CkStringBuilder $sbResponse delete_CkJsonObject $jsonResp delete_CkJsonObject $json |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.