PureBasic
PureBasic
Amazon SP-API Get Orders
See more Amazon SP-API Examples
Demonstrates Amazon SP-API Sellers API -- get orders. Returns orders created or updated during the time frame indicated by the specified parameters.Chilkat PureBasic Downloads
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkAuthAws.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkRest.pb"
Procedure ChilkatExample()
success.i = 0
; This example requires the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.
; IMPORTANT:
; You'll need to make changes to use values specific to your own region and account.
;
; Here is a list of values you'll need to change or provide:
;
; 1) The access key. Change AWS_ACCESS_KEY to your actual access key.
; 2) The secret key. Change AWS_SECRET_KEY to your actual access key.
; 3) Your region. Change "ew-west-1" to your region.
; 4) The domain to which you connect. Change sandbox.sellingpartnerapi-eu.amazon.com to non-sandbox by removing the "sandbox."
; and change the "-eu" part of the domain to your region. See https://developer-docs.amazon.com/sp-api/docs/sp-api-endpoints
; 5) Use the restricted data token (RDT) obtained from your code that previously obtained the RDT.
; If you saved the RDT to a file, update the relative file path "qa_data/tokens/sp_api_rdt_token.json" to your actual file path.
; 6) Update your Marketplace ID(s), such as ATVPDKIKX0DER
; 7) If using the non-sandbox, change the CreatedAfter from "TEST_CASE_200" to an actual date, such as "2022-12-25"
authAws.i = CkAuthAws::ckCreate()
If authAws.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkAuthAws::setCkAccessKey(authAws, "AWS_ACCESS_KEY")
CkAuthAws::setCkSecretKey(authAws, "AWS_SECRET_KEY")
CkAuthAws::setCkServiceName(authAws, "execute-api")
; Use the region that is correct for your needs.
CkAuthAws::setCkRegion(authAws, "eu-west-1")
rest.i = CkRest::ckCreate()
If rest.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
bTls.i = 1
port.i = 443
bAutoReconnect.i = 1
; Make sure to use the correct domain.
; This example is using the sandbox
success = CkRest::ckConnect(rest,"sandbox.sellingpartnerapi-eu.amazon.com",port,bTls,bAutoReconnect)
If success = 0
Debug CkRest::ckLastErrorText(rest)
CkAuthAws::ckDispose(authAws)
CkRest::ckDispose(rest)
ProcedureReturn
EndIf
success = CkRest::ckSetAuthAws(rest,authAws)
; Here's the example to get the RDT access token: Get SP-API Restricted Data Token
; Load the previously obtained RDT access token.
; See Fetch SP-API RDT Access Token
jsonToken.i = CkJsonObject::ckCreate()
If jsonToken.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkJsonObject::ckLoadFile(jsonToken,"qa_data/tokens/sp_api_rdt_token.json")
If success = 0
Debug "Failed to load RDT access token."
CkAuthAws::ckDispose(authAws)
CkRest::ckDispose(rest)
CkJsonObject::ckDispose(jsonToken)
ProcedureReturn
EndIf
; Add the x-amz-access-token request header.
rdt_token.s = CkJsonObject::ckStringOf(jsonToken,"restrictedDataToken")
CkRest::ckClearAllHeaders(rest)
CkRest::ckAddHeader(rest,"x-amz-access-token",rdt_token)
; Indicate the marketplace IDs. Use the marketplace ID for your needs. See https://developer-docs.amazon.com/sp-api/docs/marketplace-ids
CkRest::ckClearAllQueryParams(rest)
; When using the sandbox, use these params literally and exactly has shown here:
CkRest::ckAddQueryParam(rest,"MarketplaceIds","ATVPDKIKX0DER")
CkRest::ckAddQueryParam(rest,"CreatedAfter","TEST_CASE_200")
sbResponse.i = CkStringBuilder::ckCreate()
If sbResponse.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
uri.s = "/orders/v0/orders"
success = CkRest::ckFullRequestNoBodySb(rest,"GET",uri,sbResponse)
If success = 0
Debug CkRest::ckLastErrorText(rest)
CkAuthAws::ckDispose(authAws)
CkRest::ckDispose(rest)
CkJsonObject::ckDispose(jsonToken)
CkStringBuilder::ckDispose(sbResponse)
ProcedureReturn
EndIf
; Examine the response status.
statusCode.i = CkRest::ckResponseStatusCode(rest)
If statusCode <> 200
Debug "Response status text: " + CkRest::ckResponseStatusText(rest)
Debug "Response body: "
Debug CkStringBuilder::ckGetAsString(sbResponse)
Debug "Failed."
CkAuthAws::ckDispose(authAws)
CkRest::ckDispose(rest)
CkJsonObject::ckDispose(jsonToken)
CkStringBuilder::ckDispose(sbResponse)
ProcedureReturn
EndIf
Debug CkStringBuilder::ckGetAsString(sbResponse)
; If successful, gets a JSON response such as the following:
; {
; "payload": {
; "CreatedBefore": "1.569521782042E9",
; "Orders": [
; {
; "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"
; },
; {
; "AmazonOrderId": "902-8745147-1934268",
; "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,
; "IsAccessPointOrder": false,
; "IsGlobalExpressEnabled": false,
; "IsPremiumOrder": false,
; "IsSoldByAB": false,
; "IsIBA": false,
; "EasyShipShipmentStatus": "PendingPickUp",
; "ElectronicInvoiceStatus": "NotRequired"
; }
; ]
; }
; }
; Use this online tool to generate parsing code from sample JSON:
; Generate Parsing Code from JSON
json.i = CkJsonObject::ckCreate()
If json.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckLoadSb(json,sbResponse)
AmazonOrderId.s
PurchaseDate.s
LastUpdateDate.s
OrderStatus.s
FulfillmentChannel.s
SalesChannel.s
ShipServiceLevel.s
CurrencyCode.s
Amount.s
NumberOfItemsShipped.i
NumberOfItemsUnshipped.i
PaymentMethod.s
IsReplacementOrder.i
MarketplaceId.s
ShipmentServiceLevelCategory.s
OrderType.s
EarliestShipDate.s
LatestShipDate.s
EarliestDeliveryDate.s
LatestDeliveryDate.s
IsBusinessOrder.i
IsPrime.i
IsGlobalExpressEnabled.i
IsPremiumOrder.i
IsSoldByAB.i
IsIBA.i
Name.s
AddressLine1.s
City.s
StateOrRegion.s
PostalCode.s
CountryCode.s
Phone.s
AddressType.s
FulfillmentSupplySourceId.s
IsISPU.i
IsAccessPointOrder.i
HasAutomatedShippingSettings.i
EasyShipShipmentStatus.s
ElectronicInvoiceStatus.s
j.i
count_j.i
strVal.s
CreatedBefore.s = CkJsonObject::ckStringOf(json,"payload.CreatedBefore")
i.i = 0
count_i.i = CkJsonObject::ckSizeOfArray(json,"payload.Orders")
While i < count_i
CkJsonObject::setCkI(json, i)
AmazonOrderId = CkJsonObject::ckStringOf(json,"payload.Orders[i].AmazonOrderId")
PurchaseDate = CkJsonObject::ckStringOf(json,"payload.Orders[i].PurchaseDate")
LastUpdateDate = CkJsonObject::ckStringOf(json,"payload.Orders[i].LastUpdateDate")
OrderStatus = CkJsonObject::ckStringOf(json,"payload.Orders[i].OrderStatus")
FulfillmentChannel = CkJsonObject::ckStringOf(json,"payload.Orders[i].FulfillmentChannel")
SalesChannel = CkJsonObject::ckStringOf(json,"payload.Orders[i].SalesChannel")
ShipServiceLevel = CkJsonObject::ckStringOf(json,"payload.Orders[i].ShipServiceLevel")
CurrencyCode = CkJsonObject::ckStringOf(json,"payload.Orders[i].OrderTotal.CurrencyCode")
Amount = CkJsonObject::ckStringOf(json,"payload.Orders[i].OrderTotal.Amount")
NumberOfItemsShipped = CkJsonObject::ckIntOf(json,"payload.Orders[i].NumberOfItemsShipped")
NumberOfItemsUnshipped = CkJsonObject::ckIntOf(json,"payload.Orders[i].NumberOfItemsUnshipped")
PaymentMethod = CkJsonObject::ckStringOf(json,"payload.Orders[i].PaymentMethod")
IsReplacementOrder = CkJsonObject::ckBoolOf(json,"payload.Orders[i].IsReplacementOrder")
MarketplaceId = CkJsonObject::ckStringOf(json,"payload.Orders[i].MarketplaceId")
ShipmentServiceLevelCategory = CkJsonObject::ckStringOf(json,"payload.Orders[i].ShipmentServiceLevelCategory")
OrderType = CkJsonObject::ckStringOf(json,"payload.Orders[i].OrderType")
EarliestShipDate = CkJsonObject::ckStringOf(json,"payload.Orders[i].EarliestShipDate")
LatestShipDate = CkJsonObject::ckStringOf(json,"payload.Orders[i].LatestShipDate")
EarliestDeliveryDate = CkJsonObject::ckStringOf(json,"payload.Orders[i].EarliestDeliveryDate")
LatestDeliveryDate = CkJsonObject::ckStringOf(json,"payload.Orders[i].LatestDeliveryDate")
IsBusinessOrder = CkJsonObject::ckBoolOf(json,"payload.Orders[i].IsBusinessOrder")
IsPrime = CkJsonObject::ckBoolOf(json,"payload.Orders[i].IsPrime")
IsGlobalExpressEnabled = CkJsonObject::ckBoolOf(json,"payload.Orders[i].IsGlobalExpressEnabled")
IsPremiumOrder = CkJsonObject::ckBoolOf(json,"payload.Orders[i].IsPremiumOrder")
IsSoldByAB = CkJsonObject::ckBoolOf(json,"payload.Orders[i].IsSoldByAB")
IsIBA = CkJsonObject::ckBoolOf(json,"payload.Orders[i].IsIBA")
Name = CkJsonObject::ckStringOf(json,"payload.Orders[i].DefaultShipFromLocationAddress.Name")
AddressLine1 = CkJsonObject::ckStringOf(json,"payload.Orders[i].DefaultShipFromLocationAddress.AddressLine1")
City = CkJsonObject::ckStringOf(json,"payload.Orders[i].DefaultShipFromLocationAddress.City")
StateOrRegion = CkJsonObject::ckStringOf(json,"payload.Orders[i].DefaultShipFromLocationAddress.StateOrRegion")
PostalCode = CkJsonObject::ckStringOf(json,"payload.Orders[i].DefaultShipFromLocationAddress.PostalCode")
CountryCode = CkJsonObject::ckStringOf(json,"payload.Orders[i].DefaultShipFromLocationAddress.CountryCode")
Phone = CkJsonObject::ckStringOf(json,"payload.Orders[i].DefaultShipFromLocationAddress.Phone")
AddressType = CkJsonObject::ckStringOf(json,"payload.Orders[i].DefaultShipFromLocationAddress.AddressType")
FulfillmentSupplySourceId = CkJsonObject::ckStringOf(json,"payload.Orders[i].FulfillmentInstruction.FulfillmentSupplySourceId")
IsISPU = CkJsonObject::ckBoolOf(json,"payload.Orders[i].IsISPU")
IsAccessPointOrder = CkJsonObject::ckBoolOf(json,"payload.Orders[i].IsAccessPointOrder")
HasAutomatedShippingSettings = CkJsonObject::ckBoolOf(json,"payload.Orders[i].AutomatedShippingSettings.HasAutomatedShippingSettings")
EasyShipShipmentStatus = CkJsonObject::ckStringOf(json,"payload.Orders[i].EasyShipShipmentStatus")
ElectronicInvoiceStatus = CkJsonObject::ckStringOf(json,"payload.Orders[i].ElectronicInvoiceStatus")
j = 0
count_j = CkJsonObject::ckSizeOfArray(json,"payload.Orders[i].PaymentMethodDetails")
While j < count_j
CkJsonObject::setCkJ(json, j)
strVal = CkJsonObject::ckStringOf(json,"payload.Orders[i].PaymentMethodDetails[j]")
j = j + 1
Wend
i = i + 1
Wend
Debug "Success!"
CkAuthAws::ckDispose(authAws)
CkRest::ckDispose(rest)
CkJsonObject::ckDispose(jsonToken)
CkStringBuilder::ckDispose(sbResponse)
CkJsonObject::ckDispose(json)
ProcedureReturn
EndProcedure