Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Tcl Web API Examples

Primary Categories

ABN AMRO
AWS Secrets Manager
AWS Security Token Service
AWS Translate
Activix CRM
Adyen
Alibaba Cloud OSS
Amazon Cognito
Amazon DynamoDB
Amazon MWS
Amazon Pay
Amazon Rekognition
Amazon SP-API
Amazon Voice ID
Aruba Fatturazione
Azure Maps
Azure Monitor
Azure OAuth2
Azure Storage Accounts
Backblaze S3
Banco Inter
Belgian eHealth Platform
Bitfinex v2 REST
Bluzone
BrickLink
Bunny CDN
CallRail
CardConnect
Cerved
ClickBank
Clickatell
Cloudfare
Constant Contact
DocuSign
Duo Auth MFA
ETrade
Ecwid
Egypt ITIDA
Egypt eReceipt
Etsy
Facebook
Faire
Frame.io
GeoOp
GetHarvest
Global Payments
Google People
Google Search Console
Google Translate
Google Vision
Hungary NAV Invoicing
IBM Text to Speech
Ibanity
IntakeQ
Jira
Lightspeed
MYOB
Magento
Mailgun

Mastercard
MedTunnel
MercadoLibre
MessageMedia
Microsoft Calendar
Microsoft Group
Microsoft Tasks and Plans
Microsoft Teams
Moody's
Okta OAuth/OIDC
OneLogin OIDC
OneNote
OpenAI ChatGPT
PRODA
PayPal
Paynow.pl
Peoplevox
Populi
QuickBooks
Rabobank
Refinitiv
Royal Mail OBA
SCiS Schools Catalogue
SII Chile
SMSAPI
SOAP finkok.com
SendGrid
Shippo
Shopify
Shopware
Shopware 6
SimpleTexting
Square
Stripe
SugarCRM
TicketBAI
Trello
Twilio
Twitter API v2
Twitter v1
UPS
UniPin
VoiceBase
Vonage
WaTrend
Walmart v3
Wasabi
WhatsApp
WiX
WooCommerce
WordPress
Xero
Yahoo Mail
Yapily
Yousign
ZATCA
Zendesk
Zoom
_Miscellaneous_
eBay
effectconnect
hacienda.go.cr

 

 

 

(Tcl) Amazon SP-API Get Order Items

See more Amazon SP-API Examples

Returns 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

Chilkat Tcl Extension Downloads

Chilkat Tcl Extension Downloads

load ./chilkat.dll

# 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
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}/orderItems",
#       "dataElements": ["buyerInfo"]
#     }
#   ]
# }

set sbPath [new_CkStringBuilder]

CkStringBuilder_Append $sbPath "/orders/v0/orders/"
CkStringBuilder_Append $sbPath $orderId
CkStringBuilder_Append $sbPath "/orderItems"

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"

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}/orderItems"
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
}

# 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

set json [new_CkJsonObject]

CkJsonObject_LoadSb $json $sbResponse
CkJsonObject_put_EmitCompact $json 0
puts [CkJsonObject_emit $json]

set AmazonOrderId [CkJsonObject_stringOf $json "payload.AmazonOrderId"]
set i 0
set count_i [CkJsonObject_SizeOfArray $json "payload.OrderItems"]
while {$i < $count_i} {
    CkJsonObject_put_I $json $i
    set ASIN [CkJsonObject_stringOf $json "payload.OrderItems[i].ASIN"]
    set OrderItemId [CkJsonObject_stringOf $json "payload.OrderItems[i].OrderItemId"]
    set SellerSKU [CkJsonObject_stringOf $json "payload.OrderItems[i].SellerSKU"]
    set Title [CkJsonObject_stringOf $json "payload.OrderItems[i].Title"]
    set QuantityOrdered [CkJsonObject_IntOf $json "payload.OrderItems[i].QuantityOrdered"]
    set QuantityShipped [CkJsonObject_IntOf $json "payload.OrderItems[i].QuantityShipped"]
    set NumberOfItems [CkJsonObject_IntOf $json "payload.OrderItems[i].ProductInfo.NumberOfItems"]
    set CurrencyCode [CkJsonObject_stringOf $json "payload.OrderItems[i].ItemPrice.CurrencyCode"]
    set Amount [CkJsonObject_stringOf $json "payload.OrderItems[i].ItemPrice.Amount"]
    set ItemTaxCurrencyCode [CkJsonObject_stringOf $json "payload.OrderItems[i].ItemTax.CurrencyCode"]
    set ItemTaxAmount [CkJsonObject_stringOf $json "payload.OrderItems[i].ItemTax.Amount"]
    set PromotionDiscountCurrencyCode [CkJsonObject_stringOf $json "payload.OrderItems[i].PromotionDiscount.CurrencyCode"]
    set PromotionDiscountAmount [CkJsonObject_stringOf $json "payload.OrderItems[i].PromotionDiscount.Amount"]
    set IsGift [CkJsonObject_BoolOf $json "payload.OrderItems[i].IsGift"]
    set ConditionId [CkJsonObject_stringOf $json "payload.OrderItems[i].ConditionId"]
    set ConditionSubtypeId [CkJsonObject_stringOf $json "payload.OrderItems[i].ConditionSubtypeId"]
    set IsTransparency [CkJsonObject_BoolOf $json "payload.OrderItems[i].IsTransparency"]
    set SerialNumberRequired [CkJsonObject_BoolOf $json "payload.OrderItems[i].SerialNumberRequired"]
    set IossNumber [CkJsonObject_stringOf $json "payload.OrderItems[i].IossNumber"]
    set DeemedResellerCategory [CkJsonObject_stringOf $json "payload.OrderItems[i].DeemedResellerCategory"]
    set StoreChainStoreId [CkJsonObject_stringOf $json "payload.OrderItems[i].StoreChainStoreId"]
    set IsBuyerRequestedCancel [CkJsonObject_BoolOf $json "payload.OrderItems[i].BuyerRequestedCancel.IsBuyerRequestedCancel"]
    set BuyerCancelReason [CkJsonObject_stringOf $json "payload.OrderItems[i].BuyerRequestedCancel.BuyerCancelReason"]
    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.