PowerBuilder
PowerBuilder
Amazon SP-API Get Order Items
See more Amazon SP-API Examples
Returns detailed order item information for the order that you specify.Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
string ls_OrderId
oleobject loo_AuthAws
oleobject loo_JsonLwaToken
oleobject loo_Rest
string ls_Lwa_token
oleobject loo_SbPath
oleobject loo_JsonRc
oleobject loo_SbRequest
oleobject loo_SbResponse
string ls_Uri
integer li_StatusCode
oleobject loo_JsonResp
string ls_RestrictedDataToken
oleobject loo_Json
string ls_ASIN
string ls_OrderItemId
string ls_SellerSKU
string ls_Title
integer li_QuantityOrdered
integer li_QuantityShipped
integer li_NumberOfItems
string ls_CurrencyCode
string ls_Amount
string ls_ItemTaxCurrencyCode
string ls_ItemTaxAmount
string ls_PromotionDiscountCurrencyCode
string ls_PromotionDiscountAmount
integer li_IsGift
string ls_ConditionId
string ls_ConditionSubtypeId
integer li_IsTransparency
integer li_SerialNumberRequired
string ls_IossNumber
string ls_DeemedResellerCategory
string ls_StoreChainStoreId
integer li_IsBuyerRequestedCancel
string ls_BuyerCancelReason
string ls_AmazonOrderId
integer i
integer li_Count_i
li_Success = 0
// 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
ls_OrderId = "TEST_CASE_200"
loo_AuthAws = create oleobject
li_rc = loo_AuthAws.ConnectToNewObject("Chilkat.AuthAws")
if li_rc < 0 then
destroy loo_AuthAws
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_AuthAws.AccessKey = "AWS_ACCESS_KEY"
loo_AuthAws.SecretKey = "AWS_SECRET_KEY"
loo_AuthAws.ServiceName = "execute-api"
// Use the region that is correct for your needs.
loo_AuthAws.Region = "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
loo_JsonLwaToken = create oleobject
li_rc = loo_JsonLwaToken.ConnectToNewObject("Chilkat.JsonObject")
li_Success = loo_JsonLwaToken.LoadFile("qa_data/tokens/sp_api_lwa_token.json")
if li_Success = 0 then
Write-Debug "Failed to load LWA access token."
destroy loo_AuthAws
destroy loo_JsonLwaToken
return
end if
// Must use the non-sandbox domain for getting the RDT.
loo_Rest = create oleobject
li_rc = loo_Rest.ConnectToNewObject("Chilkat.Rest")
li_Success = loo_Rest.Connect("sellingpartnerapi-eu.amazon.com",443,1,1)
if li_Success = 0 then
Write-Debug loo_Rest.LastErrorText
destroy loo_AuthAws
destroy loo_JsonLwaToken
destroy loo_Rest
return
end if
li_Success = loo_Rest.SetAuthAws(loo_AuthAws)
// Add the x-amz-access-token request header.
ls_Lwa_token = loo_JsonLwaToken.StringOf("access_token")
loo_Rest.ClearAllHeaders()
loo_Rest.AddHeader("x-amz-access-token",ls_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"]
// }
// ]
// }
loo_SbPath = create oleobject
li_rc = loo_SbPath.ConnectToNewObject("Chilkat.StringBuilder")
loo_SbPath.Append("/orders/v0/orders/")
loo_SbPath.Append(ls_OrderId)
loo_SbPath.Append("/orderItems")
loo_JsonRc = create oleobject
li_rc = loo_JsonRc.ConnectToNewObject("Chilkat.JsonObject")
loo_JsonRc.UpdateString("restrictedResources[0].method","GET")
loo_JsonRc.UpdateString("restrictedResources[0].path",loo_SbPath.GetAsString())
loo_JsonRc.UpdateString("restrictedResources[0].dataElements[0]","buyerInfo")
loo_SbRequest = create oleobject
li_rc = loo_SbRequest.ConnectToNewObject("Chilkat.StringBuilder")
loo_JsonRc.EmitSb(loo_SbRequest)
loo_SbResponse = create oleobject
li_rc = loo_SbResponse.ConnectToNewObject("Chilkat.StringBuilder")
ls_Uri = "/tokens/2021-03-01/restrictedDataToken"
li_Success = loo_Rest.FullRequestSb("POST",ls_Uri,loo_SbRequest,loo_SbResponse)
if li_Success = 0 then
Write-Debug loo_Rest.LastErrorText
destroy loo_AuthAws
destroy loo_JsonLwaToken
destroy loo_Rest
destroy loo_SbPath
destroy loo_JsonRc
destroy loo_SbRequest
destroy loo_SbResponse
return
end if
// Examine the response status.
li_StatusCode = loo_Rest.ResponseStatusCode
if li_StatusCode <> 200 then
Write-Debug "Response status code: " + string(li_StatusCode)
Write-Debug "Response status text: " + loo_Rest.ResponseStatusText
Write-Debug "Response body: "
Write-Debug loo_SbResponse.GetAsString()
Write-Debug "Failed."
destroy loo_AuthAws
destroy loo_JsonLwaToken
destroy loo_Rest
destroy loo_SbPath
destroy loo_JsonRc
destroy loo_SbRequest
destroy loo_SbResponse
return
end if
// Get the restricted data token.
loo_JsonResp = create oleobject
li_rc = loo_JsonResp.ConnectToNewObject("Chilkat.JsonObject")
loo_JsonResp.LoadSb(loo_SbResponse)
ls_RestrictedDataToken = loo_JsonResp.StringOf("restrictedDataToken")
Write-Debug "Restricted Data Token: " + ls_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.)
loo_Rest.Disconnect(100)
li_Success = loo_Rest.Connect("sandbox.sellingpartnerapi-eu.amazon.com",443,1,1)
if li_Success = 0 then
Write-Debug loo_Rest.LastErrorText
destroy loo_AuthAws
destroy loo_JsonLwaToken
destroy loo_Rest
destroy loo_SbPath
destroy loo_JsonRc
destroy loo_SbRequest
destroy loo_SbResponse
destroy loo_JsonResp
return
end if
li_Success = loo_Rest.SetAuthAws(loo_AuthAws)
loo_Rest.ClearAllHeaders()
loo_Rest.AddHeader("x-amz-access-token",ls_RestrictedDataToken)
loo_Rest.ClearAllQueryParams()
loo_Rest.AddQueryParam("MarketplaceIds","ATVPDKIKX0DER")
loo_Rest.ClearAllPathParams()
loo_Rest.AddPathParam("{orderId}",ls_OrderId)
ls_Uri = "/orders/v0/orders/{orderId}/orderItems"
li_Success = loo_Rest.FullRequestNoBodySb("GET",ls_Uri,loo_SbResponse)
if li_Success = 0 then
Write-Debug loo_Rest.LastErrorText
destroy loo_AuthAws
destroy loo_JsonLwaToken
destroy loo_Rest
destroy loo_SbPath
destroy loo_JsonRc
destroy loo_SbRequest
destroy loo_SbResponse
destroy loo_JsonResp
return
end if
// Examine the response status.
li_StatusCode = loo_Rest.ResponseStatusCode
if li_StatusCode <> 200 then
Write-Debug "Response status text: " + loo_Rest.ResponseStatusText
Write-Debug "Response body: "
Write-Debug loo_SbResponse.GetAsString()
Write-Debug "Failed."
destroy loo_AuthAws
destroy loo_JsonLwaToken
destroy loo_Rest
destroy loo_SbPath
destroy loo_JsonRc
destroy loo_SbRequest
destroy loo_SbResponse
destroy loo_JsonResp
return
end if
// 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
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")
loo_Json.LoadSb(loo_SbResponse)
loo_Json.EmitCompact = 0
Write-Debug loo_Json.Emit()
ls_AmazonOrderId = loo_Json.StringOf("payload.AmazonOrderId")
i = 0
li_Count_i = loo_Json.SizeOfArray("payload.OrderItems")
do while i < li_Count_i
loo_Json.I = i
ls_ASIN = loo_Json.StringOf("payload.OrderItems[i].ASIN")
ls_OrderItemId = loo_Json.StringOf("payload.OrderItems[i].OrderItemId")
ls_SellerSKU = loo_Json.StringOf("payload.OrderItems[i].SellerSKU")
ls_Title = loo_Json.StringOf("payload.OrderItems[i].Title")
li_QuantityOrdered = loo_Json.IntOf("payload.OrderItems[i].QuantityOrdered")
li_QuantityShipped = loo_Json.IntOf("payload.OrderItems[i].QuantityShipped")
li_NumberOfItems = loo_Json.IntOf("payload.OrderItems[i].ProductInfo.NumberOfItems")
ls_CurrencyCode = loo_Json.StringOf("payload.OrderItems[i].ItemPrice.CurrencyCode")
ls_Amount = loo_Json.StringOf("payload.OrderItems[i].ItemPrice.Amount")
ls_ItemTaxCurrencyCode = loo_Json.StringOf("payload.OrderItems[i].ItemTax.CurrencyCode")
ls_ItemTaxAmount = loo_Json.StringOf("payload.OrderItems[i].ItemTax.Amount")
ls_PromotionDiscountCurrencyCode = loo_Json.StringOf("payload.OrderItems[i].PromotionDiscount.CurrencyCode")
ls_PromotionDiscountAmount = loo_Json.StringOf("payload.OrderItems[i].PromotionDiscount.Amount")
li_IsGift = loo_Json.BoolOf("payload.OrderItems[i].IsGift")
ls_ConditionId = loo_Json.StringOf("payload.OrderItems[i].ConditionId")
ls_ConditionSubtypeId = loo_Json.StringOf("payload.OrderItems[i].ConditionSubtypeId")
li_IsTransparency = loo_Json.BoolOf("payload.OrderItems[i].IsTransparency")
li_SerialNumberRequired = loo_Json.BoolOf("payload.OrderItems[i].SerialNumberRequired")
ls_IossNumber = loo_Json.StringOf("payload.OrderItems[i].IossNumber")
ls_DeemedResellerCategory = loo_Json.StringOf("payload.OrderItems[i].DeemedResellerCategory")
ls_StoreChainStoreId = loo_Json.StringOf("payload.OrderItems[i].StoreChainStoreId")
li_IsBuyerRequestedCancel = loo_Json.BoolOf("payload.OrderItems[i].BuyerRequestedCancel.IsBuyerRequestedCancel")
ls_BuyerCancelReason = loo_Json.StringOf("payload.OrderItems[i].BuyerRequestedCancel.BuyerCancelReason")
i = i + 1
loop
Write-Debug "Success!"
destroy loo_AuthAws
destroy loo_JsonLwaToken
destroy loo_Rest
destroy loo_SbPath
destroy loo_JsonRc
destroy loo_SbRequest
destroy loo_SbResponse
destroy loo_JsonResp
destroy loo_Json