Sample code for 30+ languages & platforms
PowerBuilder

Amazon SP-API Get Specific Order

See more Amazon SP-API Examples

Get a specific Amazon Seller order.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long 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
long li_StatusCode
oleobject loo_JsonResp
string ls_RestrictedDataToken
oleobject loo_Json
string ls_StrVal
string ls_AmazonOrderId
string ls_PurchaseDate
string ls_LastUpdateDate
string ls_OrderStatus
string ls_FulfillmentChannel
string ls_SalesChannel
string ls_ShipServiceLevel
string ls_CurrencyCode
string ls_Amount
long li_NumberOfItemsShipped
long li_NumberOfItemsUnshipped
string ls_PaymentMethod
long li_IsReplacementOrder
string ls_MarketplaceId
string ls_ShipmentServiceLevelCategory
string ls_OrderType
string ls_EarliestShipDate
string ls_LatestShipDate
string ls_EarliestDeliveryDate
string ls_LatestDeliveryDate
long li_IsBusinessOrder
long li_IsPrime
long li_IsGlobalExpressEnabled
long li_IsPremiumOrder
long li_IsSoldByAB
long li_IsIBA
string ls_Name
string ls_AddressLine1
string ls_City
string ls_StateOrRegion
string ls_PostalCode
string ls_CountryCode
string ls_Phone
string ls_AddressType
string ls_FulfillmentSupplySourceId
long li_IsISPU
long li_IsAccessPointOrder
long li_HasAutomatedShippingSettings
string ls_EasyShipShipmentStatus
string ls_ElectronicInvoiceStatus
long i
long 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 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
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}",
//        "dataElements": ["buyerInfo", "shippingAddress"]
//      }
//    ]
//  }

loo_SbPath = create oleobject
li_rc = loo_SbPath.ConnectToNewObject("Chilkat.StringBuilder")

loo_SbPath.Append("/orders/v0/orders/")
loo_SbPath.Append(ls_OrderId)

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_JsonRc.UpdateString("restrictedResources[0].dataElements[1]","shippingAddress")

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

Write-Debug loo_SbResponse.GetAsString()

//  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

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_Json.LoadSb(loo_SbResponse)

ls_AmazonOrderId = loo_Json.StringOf("payload.AmazonOrderId")
ls_PurchaseDate = loo_Json.StringOf("payload.PurchaseDate")
ls_LastUpdateDate = loo_Json.StringOf("payload.LastUpdateDate")
ls_OrderStatus = loo_Json.StringOf("payload.OrderStatus")
ls_FulfillmentChannel = loo_Json.StringOf("payload.FulfillmentChannel")
ls_SalesChannel = loo_Json.StringOf("payload.SalesChannel")
ls_ShipServiceLevel = loo_Json.StringOf("payload.ShipServiceLevel")
ls_CurrencyCode = loo_Json.StringOf("payload.OrderTotal.CurrencyCode")
ls_Amount = loo_Json.StringOf("payload.OrderTotal.Amount")
li_NumberOfItemsShipped = loo_Json.IntOf("payload.NumberOfItemsShipped")
li_NumberOfItemsUnshipped = loo_Json.IntOf("payload.NumberOfItemsUnshipped")
ls_PaymentMethod = loo_Json.StringOf("payload.PaymentMethod")
li_IsReplacementOrder = loo_Json.BoolOf("payload.IsReplacementOrder")
ls_MarketplaceId = loo_Json.StringOf("payload.MarketplaceId")
ls_ShipmentServiceLevelCategory = loo_Json.StringOf("payload.ShipmentServiceLevelCategory")
ls_OrderType = loo_Json.StringOf("payload.OrderType")
ls_EarliestShipDate = loo_Json.StringOf("payload.EarliestShipDate")
ls_LatestShipDate = loo_Json.StringOf("payload.LatestShipDate")
ls_EarliestDeliveryDate = loo_Json.StringOf("payload.EarliestDeliveryDate")
ls_LatestDeliveryDate = loo_Json.StringOf("payload.LatestDeliveryDate")
li_IsBusinessOrder = loo_Json.BoolOf("payload.IsBusinessOrder")
li_IsPrime = loo_Json.BoolOf("payload.IsPrime")
li_IsGlobalExpressEnabled = loo_Json.BoolOf("payload.IsGlobalExpressEnabled")
li_IsPremiumOrder = loo_Json.BoolOf("payload.IsPremiumOrder")
li_IsSoldByAB = loo_Json.BoolOf("payload.IsSoldByAB")
li_IsIBA = loo_Json.BoolOf("payload.IsIBA")
ls_Name = loo_Json.StringOf("payload.DefaultShipFromLocationAddress.Name")
ls_AddressLine1 = loo_Json.StringOf("payload.DefaultShipFromLocationAddress.AddressLine1")
ls_City = loo_Json.StringOf("payload.DefaultShipFromLocationAddress.City")
ls_StateOrRegion = loo_Json.StringOf("payload.DefaultShipFromLocationAddress.StateOrRegion")
ls_PostalCode = loo_Json.StringOf("payload.DefaultShipFromLocationAddress.PostalCode")
ls_CountryCode = loo_Json.StringOf("payload.DefaultShipFromLocationAddress.CountryCode")
ls_Phone = loo_Json.StringOf("payload.DefaultShipFromLocationAddress.Phone")
ls_AddressType = loo_Json.StringOf("payload.DefaultShipFromLocationAddress.AddressType")
ls_FulfillmentSupplySourceId = loo_Json.StringOf("payload.FulfillmentInstruction.FulfillmentSupplySourceId")
li_IsISPU = loo_Json.BoolOf("payload.IsISPU")
li_IsAccessPointOrder = loo_Json.BoolOf("payload.IsAccessPointOrder")
li_HasAutomatedShippingSettings = loo_Json.BoolOf("payload.AutomatedShippingSettings.HasAutomatedShippingSettings")
ls_EasyShipShipmentStatus = loo_Json.StringOf("payload.EasyShipShipmentStatus")
ls_ElectronicInvoiceStatus = loo_Json.StringOf("payload.ElectronicInvoiceStatus")
i = 0
li_Count_i = loo_Json.SizeOfArray("payload.PaymentMethodDetails")
do while i < li_Count_i
    loo_Json.I = i
    ls_StrVal = loo_Json.StringOf("payload.PaymentMethodDetails[i]")
    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