SQL Server
SQL Server
Amazon SP-API Get Specific Order
See more Amazon SP-API Examples
Get a specific Amazon Seller order.Chilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @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
DECLARE @orderId nvarchar(4000)
SELECT @orderId = 'TEST_CASE_200'
DECLARE @authAws int
EXEC @hr = sp_OACreate 'Chilkat.AuthAws', @authAws OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OASetProperty @authAws, 'AccessKey', 'AWS_ACCESS_KEY'
EXEC sp_OASetProperty @authAws, 'SecretKey', 'AWS_SECRET_KEY'
EXEC sp_OASetProperty @authAws, 'ServiceName', 'execute-api'
-- Use the region that is correct for your needs.
EXEC sp_OASetProperty @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
DECLARE @jsonLwaToken int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonLwaToken OUT
EXEC sp_OAMethod @jsonLwaToken, 'LoadFile', @success OUT, 'qa_data/tokens/sp_api_lwa_token.json'
IF @success = 0
BEGIN
PRINT 'Failed to load LWA access token.'
EXEC @hr = sp_OADestroy @authAws
EXEC @hr = sp_OADestroy @jsonLwaToken
RETURN
END
-- Must use the non-sandbox domain for getting the RDT.
DECLARE @rest int
EXEC @hr = sp_OACreate 'Chilkat.Rest', @rest OUT
EXEC sp_OAMethod @rest, 'Connect', @success OUT, 'sellingpartnerapi-eu.amazon.com', 443, 1, 1
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @authAws
EXEC @hr = sp_OADestroy @jsonLwaToken
EXEC @hr = sp_OADestroy @rest
RETURN
END
EXEC sp_OAMethod @rest, 'SetAuthAws', @success OUT, @authAws
-- Add the x-amz-access-token request header.
DECLARE @lwa_token nvarchar(4000)
EXEC sp_OAMethod @jsonLwaToken, 'StringOf', @lwa_token OUT, 'access_token'
EXEC sp_OAMethod @rest, 'ClearAllHeaders', @success OUT
EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, '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}",
-- "dataElements": ["buyerInfo", "shippingAddress"]
-- }
-- ]
-- }
DECLARE @sbPath int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbPath OUT
EXEC sp_OAMethod @sbPath, 'Append', @success OUT, '/orders/v0/orders/'
EXEC sp_OAMethod @sbPath, 'Append', @success OUT, @orderId
DECLARE @jsonRc int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonRc OUT
EXEC sp_OAMethod @jsonRc, 'UpdateString', @success OUT, 'restrictedResources[0].method', 'GET'
EXEC sp_OAMethod @sbPath, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @jsonRc, 'UpdateString', @success OUT, 'restrictedResources[0].path', @sTmp0
EXEC sp_OAMethod @jsonRc, 'UpdateString', @success OUT, 'restrictedResources[0].dataElements[0]', 'buyerInfo'
EXEC sp_OAMethod @jsonRc, 'UpdateString', @success OUT, 'restrictedResources[0].dataElements[1]', 'shippingAddress'
DECLARE @sbRequest int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbRequest OUT
EXEC sp_OAMethod @jsonRc, 'EmitSb', @success OUT, @sbRequest
DECLARE @sbResponse int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbResponse OUT
DECLARE @uri nvarchar(4000)
SELECT @uri = '/tokens/2021-03-01/restrictedDataToken'
EXEC sp_OAMethod @rest, 'FullRequestSb', @success OUT, 'POST', @uri, @sbRequest, @sbResponse
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @authAws
EXEC @hr = sp_OADestroy @jsonLwaToken
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @sbPath
EXEC @hr = sp_OADestroy @jsonRc
EXEC @hr = sp_OADestroy @sbRequest
EXEC @hr = sp_OADestroy @sbResponse
RETURN
END
-- Examine the response status.
DECLARE @statusCode int
EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @statusCode OUT
IF @statusCode <> 200
BEGIN
PRINT 'Response status code: ' + @statusCode
EXEC sp_OAGetProperty @rest, 'ResponseStatusText', @sTmp0 OUT
PRINT 'Response status text: ' + @sTmp0
PRINT 'Response body: '
EXEC sp_OAMethod @sbResponse, 'GetAsString', @sTmp0 OUT
PRINT @sTmp0
PRINT 'Failed.'
EXEC @hr = sp_OADestroy @authAws
EXEC @hr = sp_OADestroy @jsonLwaToken
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @sbPath
EXEC @hr = sp_OADestroy @jsonRc
EXEC @hr = sp_OADestroy @sbRequest
EXEC @hr = sp_OADestroy @sbResponse
RETURN
END
-- Get the restricted data token.
DECLARE @jsonResp int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonResp OUT
EXEC sp_OAMethod @jsonResp, 'LoadSb', @success OUT, @sbResponse
DECLARE @restrictedDataToken nvarchar(4000)
EXEC sp_OAMethod @jsonResp, 'StringOf', @restrictedDataToken OUT, 'restrictedDataToken'
PRINT '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.)
EXEC sp_OAMethod @rest, 'Disconnect', @success OUT, 100
EXEC sp_OAMethod @rest, 'Connect', @success OUT, 'sandbox.sellingpartnerapi-eu.amazon.com', 443, 1, 1
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @authAws
EXEC @hr = sp_OADestroy @jsonLwaToken
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @sbPath
EXEC @hr = sp_OADestroy @jsonRc
EXEC @hr = sp_OADestroy @sbRequest
EXEC @hr = sp_OADestroy @sbResponse
EXEC @hr = sp_OADestroy @jsonResp
RETURN
END
EXEC sp_OAMethod @rest, 'SetAuthAws', @success OUT, @authAws
EXEC sp_OAMethod @rest, 'ClearAllHeaders', @success OUT
EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'x-amz-access-token', @restrictedDataToken
EXEC sp_OAMethod @rest, 'ClearAllQueryParams', @success OUT
EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'MarketplaceIds', 'ATVPDKIKX0DER'
EXEC sp_OAMethod @rest, 'ClearAllPathParams', @success OUT
EXEC sp_OAMethod @rest, 'AddPathParam', @success OUT, '{orderId}', @orderId
SELECT @uri = '/orders/v0/orders/{orderId}'
EXEC sp_OAMethod @rest, 'FullRequestNoBodySb', @success OUT, 'GET', @uri, @sbResponse
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @authAws
EXEC @hr = sp_OADestroy @jsonLwaToken
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @sbPath
EXEC @hr = sp_OADestroy @jsonRc
EXEC @hr = sp_OADestroy @sbRequest
EXEC @hr = sp_OADestroy @sbResponse
EXEC @hr = sp_OADestroy @jsonResp
RETURN
END
-- Examine the response status.
EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @statusCode OUT
IF @statusCode <> 200
BEGIN
EXEC sp_OAGetProperty @rest, 'ResponseStatusText', @sTmp0 OUT
PRINT 'Response status text: ' + @sTmp0
PRINT 'Response body: '
EXEC sp_OAMethod @sbResponse, 'GetAsString', @sTmp0 OUT
PRINT @sTmp0
PRINT 'Failed.'
EXEC @hr = sp_OADestroy @authAws
EXEC @hr = sp_OADestroy @jsonLwaToken
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @sbPath
EXEC @hr = sp_OADestroy @jsonRc
EXEC @hr = sp_OADestroy @sbRequest
EXEC @hr = sp_OADestroy @sbResponse
EXEC @hr = sp_OADestroy @jsonResp
RETURN
END
EXEC sp_OAMethod @sbResponse, 'GetAsString', @sTmp0 OUT
PRINT @sTmp0
-- 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
DECLARE @json int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT
EXEC sp_OAMethod @json, 'LoadSb', @success OUT, @sbResponse
DECLARE @strVal nvarchar(4000)
DECLARE @AmazonOrderId nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @AmazonOrderId OUT, 'payload.AmazonOrderId'
DECLARE @PurchaseDate nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @PurchaseDate OUT, 'payload.PurchaseDate'
DECLARE @LastUpdateDate nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @LastUpdateDate OUT, 'payload.LastUpdateDate'
DECLARE @OrderStatus nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @OrderStatus OUT, 'payload.OrderStatus'
DECLARE @FulfillmentChannel nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @FulfillmentChannel OUT, 'payload.FulfillmentChannel'
DECLARE @SalesChannel nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @SalesChannel OUT, 'payload.SalesChannel'
DECLARE @ShipServiceLevel nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @ShipServiceLevel OUT, 'payload.ShipServiceLevel'
DECLARE @CurrencyCode nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @CurrencyCode OUT, 'payload.OrderTotal.CurrencyCode'
DECLARE @Amount nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @Amount OUT, 'payload.OrderTotal.Amount'
DECLARE @NumberOfItemsShipped int
EXEC sp_OAMethod @json, 'IntOf', @NumberOfItemsShipped OUT, 'payload.NumberOfItemsShipped'
DECLARE @NumberOfItemsUnshipped int
EXEC sp_OAMethod @json, 'IntOf', @NumberOfItemsUnshipped OUT, 'payload.NumberOfItemsUnshipped'
DECLARE @PaymentMethod nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @PaymentMethod OUT, 'payload.PaymentMethod'
DECLARE @IsReplacementOrder int
EXEC sp_OAMethod @json, 'BoolOf', @IsReplacementOrder OUT, 'payload.IsReplacementOrder'
DECLARE @MarketplaceId nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @MarketplaceId OUT, 'payload.MarketplaceId'
DECLARE @ShipmentServiceLevelCategory nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @ShipmentServiceLevelCategory OUT, 'payload.ShipmentServiceLevelCategory'
DECLARE @OrderType nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @OrderType OUT, 'payload.OrderType'
DECLARE @EarliestShipDate nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @EarliestShipDate OUT, 'payload.EarliestShipDate'
DECLARE @LatestShipDate nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @LatestShipDate OUT, 'payload.LatestShipDate'
DECLARE @EarliestDeliveryDate nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @EarliestDeliveryDate OUT, 'payload.EarliestDeliveryDate'
DECLARE @LatestDeliveryDate nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @LatestDeliveryDate OUT, 'payload.LatestDeliveryDate'
DECLARE @IsBusinessOrder int
EXEC sp_OAMethod @json, 'BoolOf', @IsBusinessOrder OUT, 'payload.IsBusinessOrder'
DECLARE @IsPrime int
EXEC sp_OAMethod @json, 'BoolOf', @IsPrime OUT, 'payload.IsPrime'
DECLARE @IsGlobalExpressEnabled int
EXEC sp_OAMethod @json, 'BoolOf', @IsGlobalExpressEnabled OUT, 'payload.IsGlobalExpressEnabled'
DECLARE @IsPremiumOrder int
EXEC sp_OAMethod @json, 'BoolOf', @IsPremiumOrder OUT, 'payload.IsPremiumOrder'
DECLARE @IsSoldByAB int
EXEC sp_OAMethod @json, 'BoolOf', @IsSoldByAB OUT, 'payload.IsSoldByAB'
DECLARE @IsIBA int
EXEC sp_OAMethod @json, 'BoolOf', @IsIBA OUT, 'payload.IsIBA'
DECLARE @Name nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @Name OUT, 'payload.DefaultShipFromLocationAddress.Name'
DECLARE @AddressLine1 nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @AddressLine1 OUT, 'payload.DefaultShipFromLocationAddress.AddressLine1'
DECLARE @City nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @City OUT, 'payload.DefaultShipFromLocationAddress.City'
DECLARE @StateOrRegion nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @StateOrRegion OUT, 'payload.DefaultShipFromLocationAddress.StateOrRegion'
DECLARE @PostalCode nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @PostalCode OUT, 'payload.DefaultShipFromLocationAddress.PostalCode'
DECLARE @CountryCode nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @CountryCode OUT, 'payload.DefaultShipFromLocationAddress.CountryCode'
DECLARE @Phone nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @Phone OUT, 'payload.DefaultShipFromLocationAddress.Phone'
DECLARE @AddressType nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @AddressType OUT, 'payload.DefaultShipFromLocationAddress.AddressType'
DECLARE @FulfillmentSupplySourceId nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @FulfillmentSupplySourceId OUT, 'payload.FulfillmentInstruction.FulfillmentSupplySourceId'
DECLARE @IsISPU int
EXEC sp_OAMethod @json, 'BoolOf', @IsISPU OUT, 'payload.IsISPU'
DECLARE @IsAccessPointOrder int
EXEC sp_OAMethod @json, 'BoolOf', @IsAccessPointOrder OUT, 'payload.IsAccessPointOrder'
DECLARE @HasAutomatedShippingSettings int
EXEC sp_OAMethod @json, 'BoolOf', @HasAutomatedShippingSettings OUT, 'payload.AutomatedShippingSettings.HasAutomatedShippingSettings'
DECLARE @EasyShipShipmentStatus nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @EasyShipShipmentStatus OUT, 'payload.EasyShipShipmentStatus'
DECLARE @ElectronicInvoiceStatus nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @ElectronicInvoiceStatus OUT, 'payload.ElectronicInvoiceStatus'
DECLARE @i int
SELECT @i = 0
DECLARE @count_i int
EXEC sp_OAMethod @json, 'SizeOfArray', @count_i OUT, 'payload.PaymentMethodDetails'
WHILE @i < @count_i
BEGIN
EXEC sp_OASetProperty @json, 'I', @i
EXEC sp_OAMethod @json, 'StringOf', @strVal OUT, 'payload.PaymentMethodDetails[i]'
SELECT @i = @i + 1
END
PRINT 'Success!'
EXEC @hr = sp_OADestroy @authAws
EXEC @hr = sp_OADestroy @jsonLwaToken
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @sbPath
EXEC @hr = sp_OADestroy @jsonRc
EXEC @hr = sp_OADestroy @sbRequest
EXEC @hr = sp_OADestroy @sbResponse
EXEC @hr = sp_OADestroy @jsonResp
EXEC @hr = sp_OADestroy @json
END
GO