Xbase++
Xbase++
Amazon MWS Upload Invoice
See more Amazon MWS Examples
Demonstrates how to upload an invoice using _UPLOAD_VAT_INVOICE_FeedType to submit an invoice for an order.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oRest
LOCAL nBTls
LOCAL nPort
LOCAL nBAutoReconnect
LOCAL oPdfData
LOCAL oCrypt
LOCAL cMd5Hash
LOCAL oSbResponseBody
nSuccess := 0
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
oRest := CreateObject("Chilkat.Rest")
// Connect to the Amazon MWS REST server.
//
// Make sure to connect to the correct Amazon MWS Endpoint, otherwise
// you'll get an HTTP 401 response code.
//
// See Amazon MWS endpoints and MarketplaceId values
nBTls := 1
nPort := 443
nBAutoReconnect := 1
nSuccess := oRest:Connect("mws.amazonservices.com", nPort, nBTls, nBAutoReconnect)
oRest:Host := "mws.amazonservices.com"
// MarketplaceList.Id parameter �This should be the marketplace in which the order was placed. Only one marketplace must be used per order.T
// Here are the marketplace ID's
// Spain: A1RKKUPIHCS9HS
// UK: A1F83G8C2ARO7P
// France: A13V1IB3VIYZZH
// Germany: A1PA6795UKMFR9
// Italy: APJ6JRA9NG5V4
// ...
// (See https://docs.developer.amazonservices.com/en_US/dev_guide/DG_Endpoints.html)
// FeedOptions parameter � Seller can input key value pairs to give important metadata along with the PDF invoice.
oRest:AddQueryParam("FeedOptions", "metadata:orderid=206-2341234-3455465;metadata:invoicenumber=INT-3431-XJE3;metadata:documenttype=Invoice")
// Load the PDF invoice file that is to be the body of the HTTP POST request.
oPdfData := CreateObject("Chilkat.BinData")
nSuccess := oPdfData:LoadFile("qa_data/pdf/sample.pdf")
// Get the MD5 hash of the PDF data.
oCrypt := CreateObject("Chilkat.Crypt2")
oCrypt:HashAlgorithm := "md5"
oCrypt:EncodingMode := "base64"
cMd5Hash := oCrypt:HashBdENC(oPdfData)
oRest:AddQueryParam("AWSAccessKeyId", "0PB842ExampleN4ZTR2")
oRest:AddQueryParam("Action", "SubmitFeed")
oRest:AddQueryParam("FeedType", "_UPLOAD_VAT_INVOICE_")
oRest:AddQueryParam("MWSAuthToken", "EXAMPLE-amzn.mws.4ea38b7b-f563-7709-4bae-87aea-EXAMPLE")
oRest:AddQueryParam("MarketplaceIdList.Id.1", "ATVExampleDER")
oRest:AddQueryParam("SellerId", "A1XExample5E6")
oRest:AddQueryParam("ContentMD5Value", cMd5Hash)
oRest:AddQueryParam("SignatureMethod", "HmacSHA256")
oRest:AddQueryParam("SignatureVersion", "2")
oRest:AddQueryParam("Version", "2009-01-01")
// Add the MWS Signature param. (Also adds the Timestamp parameter using the curent system date/time.)
oRest:AddMwsSignature("POST", "/Feeds/2009-01-01", "mws.amazonservices.com", "YOUR_MWS_SECRET_ACCESS_KEY_ID")
oRest:AddHeader("Content-Type", "application/octet-stream")
oSbResponseBody := CreateObject("Chilkat.StringBuilder")
nSuccess := oRest:FullRequestBd("POST", "/Feeds/2009-01-01", oPdfData, oSbResponseBody)
IF (oRest:LastMethodSuccess != 1)
? oRest:LastErrorText
oRest:destroy()
oPdfData:destroy()
oCrypt:destroy()
oSbResponseBody:destroy()
RETURN
ENDIF
IF (oRest:ResponseStatusCode != 200)
// Examine the request/response to see what happened.
? "response status code = " + Str(oRest:ResponseStatusCode)
? "response status text = " + oRest:ResponseStatusText
? "response header: " + oRest:ResponseHeader
? "response body: " + oSbResponseBody:GetAsString()
? "---"
? "LastRequestStartLine: " + oRest:LastRequestStartLine
? "LastRequestHeader: " + oRest:LastRequestHeader
ENDIF
// Examine the XML returned in the response body.
? oSbResponseBody:GetAsString()
? "----"
? "Success."
oRest:destroy()
oPdfData:destroy()
oCrypt:destroy()
oSbResponseBody:destroy()