Xbase++
Xbase++
Amazon SP-API Get Order Items
See more Amazon SP-API Examples
Returns detailed order item information for the order that you specify.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL cOrderId
LOCAL oAuthAws
LOCAL oJsonLwaToken
LOCAL oRest
LOCAL cLwa_token
LOCAL oSbPath
LOCAL oJsonRc
LOCAL oSbRequest
LOCAL oSbResponse
LOCAL cUri
LOCAL nStatusCode
LOCAL oJsonResp
LOCAL cRestrictedDataToken
LOCAL oJson
LOCAL cASIN
LOCAL cOrderItemId
LOCAL cSellerSKU
LOCAL cTitle
LOCAL nQuantityOrdered
LOCAL nQuantityShipped
LOCAL nNumberOfItems
LOCAL cCurrencyCode
LOCAL cAmount
LOCAL cItemTaxCurrencyCode
LOCAL cItemTaxAmount
LOCAL cPromotionDiscountCurrencyCode
LOCAL cPromotionDiscountAmount
LOCAL nIsGift
LOCAL cConditionId
LOCAL cConditionSubtypeId
LOCAL nIsTransparency
LOCAL nSerialNumberRequired
LOCAL cIossNumber
LOCAL cDeemedResellerCategory
LOCAL cStoreChainStoreId
LOCAL nIsBuyerRequestedCancel
LOCAL cBuyerCancelReason
LOCAL cAmazonOrderId
LOCAL i
LOCAL nCount_i
nSuccess := 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
cOrderId := "TEST_CASE_200"
oAuthAws := CreateObject("Chilkat.AuthAws")
oAuthAws:AccessKey := "AWS_ACCESS_KEY"
oAuthAws:SecretKey := "AWS_SECRET_KEY"
oAuthAws:ServiceName := "execute-api"
// Use the region that is correct for your needs.
oAuthAws: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
oJsonLwaToken := CreateObject("Chilkat.JsonObject")
nSuccess := oJsonLwaToken:LoadFile("qa_data/tokens/sp_api_lwa_token.json")
IF (nSuccess == 0)
? "Failed to load LWA access token."
oAuthAws:destroy()
oJsonLwaToken:destroy()
RETURN
ENDIF
// Must use the non-sandbox domain for getting the RDT.
oRest := CreateObject("Chilkat.Rest")
nSuccess := oRest:Connect("sellingpartnerapi-eu.amazon.com", 443, 1, 1)
IF (nSuccess == 0)
? oRest:LastErrorText
oAuthAws:destroy()
oJsonLwaToken:destroy()
oRest:destroy()
RETURN
ENDIF
nSuccess := oRest:SetAuthAws(oAuthAws)
// Add the x-amz-access-token request header.
cLwa_token := oJsonLwaToken:StringOf("access_token")
oRest:ClearAllHeaders()
oRest:AddHeader("x-amz-access-token", cLwa_token)
// We're going to send a POST with the following JSON body:
// {
// "restrictedResources": [
// {
// "method": "GET",
// "path": "/orders/v0/orders/{orderId}/orderItems",
// "dataElements": ["buyerInfo"]
// }
// ]
// }
oSbPath := CreateObject("Chilkat.StringBuilder")
oSbPath:Append("/orders/v0/orders/")
oSbPath:Append(cOrderId)
oSbPath:Append("/orderItems")
oJsonRc := CreateObject("Chilkat.JsonObject")
oJsonRc:UpdateString("restrictedResources[0].method", "GET")
oJsonRc:UpdateString("restrictedResources[0].path", oSbPath:GetAsString())
oJsonRc:UpdateString("restrictedResources[0].dataElements[0]", "buyerInfo")
oSbRequest := CreateObject("Chilkat.StringBuilder")
oJsonRc:EmitSb(oSbRequest)
oSbResponse := CreateObject("Chilkat.StringBuilder")
cUri := "/tokens/2021-03-01/restrictedDataToken"
nSuccess := oRest:FullRequestSb("POST", cUri, oSbRequest, oSbResponse)
IF (nSuccess == 0)
? oRest:LastErrorText
oAuthAws:destroy()
oJsonLwaToken:destroy()
oRest:destroy()
oSbPath:destroy()
oJsonRc:destroy()
oSbRequest:destroy()
oSbResponse:destroy()
RETURN
ENDIF
// Examine the response status.
nStatusCode := oRest:ResponseStatusCode
IF (nStatusCode != 200)
? "Response status code: " + Str(nStatusCode)
? "Response status text: " + oRest:ResponseStatusText
? "Response body: "
? oSbResponse:GetAsString()
? "Failed."
oAuthAws:destroy()
oJsonLwaToken:destroy()
oRest:destroy()
oSbPath:destroy()
oJsonRc:destroy()
oSbRequest:destroy()
oSbResponse:destroy()
RETURN
ENDIF
// Get the restricted data token.
oJsonResp := CreateObject("Chilkat.JsonObject")
oJsonResp:LoadSb(oSbResponse)
cRestrictedDataToken := oJsonResp:StringOf("restrictedDataToken")
? "Restricted Data Token: " + cRestrictedDataToken
// ------------------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------------------
// 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.)
oRest:Disconnect(100)
nSuccess := oRest:Connect("sandbox.sellingpartnerapi-eu.amazon.com", 443, 1, 1)
IF (nSuccess == 0)
? oRest:LastErrorText
oAuthAws:destroy()
oJsonLwaToken:destroy()
oRest:destroy()
oSbPath:destroy()
oJsonRc:destroy()
oSbRequest:destroy()
oSbResponse:destroy()
oJsonResp:destroy()
RETURN
ENDIF
nSuccess := oRest:SetAuthAws(oAuthAws)
oRest:ClearAllHeaders()
oRest:AddHeader("x-amz-access-token", cRestrictedDataToken)
oRest:ClearAllQueryParams()
oRest:AddQueryParam("MarketplaceIds", "ATVPDKIKX0DER")
oRest:ClearAllPathParams()
oRest:AddPathParam("{orderId}", cOrderId)
cUri := "/orders/v0/orders/{orderId}/orderItems"
nSuccess := oRest:FullRequestNoBodySb("GET", cUri, oSbResponse)
IF (nSuccess == 0)
? oRest:LastErrorText
oAuthAws:destroy()
oJsonLwaToken:destroy()
oRest:destroy()
oSbPath:destroy()
oJsonRc:destroy()
oSbRequest:destroy()
oSbResponse:destroy()
oJsonResp:destroy()
RETURN
ENDIF
// Examine the response status.
nStatusCode := oRest:ResponseStatusCode
IF (nStatusCode != 200)
? "Response status text: " + oRest:ResponseStatusText
? "Response body: "
? oSbResponse:GetAsString()
? "Failed."
oAuthAws:destroy()
oJsonLwaToken:destroy()
oRest:destroy()
oSbPath:destroy()
oJsonRc:destroy()
oSbRequest:destroy()
oSbResponse:destroy()
oJsonResp:destroy()
RETURN
ENDIF
// 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
oJson := CreateObject("Chilkat.JsonObject")
oJson:LoadSb(oSbResponse)
oJson:EmitCompact := 0
? oJson:Emit()
cAmazonOrderId := oJson:StringOf("payload.AmazonOrderId")
i := 0
nCount_i := oJson:SizeOfArray("payload.OrderItems")
DO WHILE i < nCount_i
oJson:I := i
cASIN := oJson:StringOf("payload.OrderItems[i].ASIN")
cOrderItemId := oJson:StringOf("payload.OrderItems[i].OrderItemId")
cSellerSKU := oJson:StringOf("payload.OrderItems[i].SellerSKU")
cTitle := oJson:StringOf("payload.OrderItems[i].Title")
nQuantityOrdered := oJson:IntOf("payload.OrderItems[i].QuantityOrdered")
nQuantityShipped := oJson:IntOf("payload.OrderItems[i].QuantityShipped")
nNumberOfItems := oJson:IntOf("payload.OrderItems[i].ProductInfo.NumberOfItems")
cCurrencyCode := oJson:StringOf("payload.OrderItems[i].ItemPrice.CurrencyCode")
cAmount := oJson:StringOf("payload.OrderItems[i].ItemPrice.Amount")
cItemTaxCurrencyCode := oJson:StringOf("payload.OrderItems[i].ItemTax.CurrencyCode")
cItemTaxAmount := oJson:StringOf("payload.OrderItems[i].ItemTax.Amount")
cPromotionDiscountCurrencyCode := oJson:StringOf("payload.OrderItems[i].PromotionDiscount.CurrencyCode")
cPromotionDiscountAmount := oJson:StringOf("payload.OrderItems[i].PromotionDiscount.Amount")
nIsGift := oJson:BoolOf("payload.OrderItems[i].IsGift")
cConditionId := oJson:StringOf("payload.OrderItems[i].ConditionId")
cConditionSubtypeId := oJson:StringOf("payload.OrderItems[i].ConditionSubtypeId")
nIsTransparency := oJson:BoolOf("payload.OrderItems[i].IsTransparency")
nSerialNumberRequired := oJson:BoolOf("payload.OrderItems[i].SerialNumberRequired")
cIossNumber := oJson:StringOf("payload.OrderItems[i].IossNumber")
cDeemedResellerCategory := oJson:StringOf("payload.OrderItems[i].DeemedResellerCategory")
cStoreChainStoreId := oJson:StringOf("payload.OrderItems[i].StoreChainStoreId")
nIsBuyerRequestedCancel := oJson:BoolOf("payload.OrderItems[i].BuyerRequestedCancel.IsBuyerRequestedCancel")
cBuyerCancelReason := oJson:StringOf("payload.OrderItems[i].BuyerRequestedCancel.BuyerCancelReason")
i := i + 1
ENDDO
? "Success!"
oAuthAws:destroy()
oJsonLwaToken:destroy()
oRest:destroy()
oSbPath:destroy()
oJsonRc:destroy()
oSbRequest:destroy()
oSbResponse:destroy()
oJsonResp:destroy()
oJson:destroy()