Lua
Lua
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 Lua Downloads
local chilkat = require("chilkat")
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local rest = chilkat.Rest.new()
-- 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
local bTls = true
local port = 443
local bAutoReconnect = true
local success = rest:connect("mws.amazonservices.com", port, bTls, bAutoReconnect)
rest.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.
rest:add_query_param("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.
local pdfData = chilkat.BinData.new()
success = pdfData:load_file("qa_data/pdf/sample.pdf")
-- Get the MD5 hash of the PDF data.
local crypt = chilkat.Crypt2.new()
crypt.hash_algorithm = "md5"
crypt.encoding_mode = "base64"
local md5Hash = crypt:hash_bd_enc(pdfData)
rest:add_query_param("AWSAccessKeyId", "0PB842ExampleN4ZTR2")
rest:add_query_param("Action", "SubmitFeed")
rest:add_query_param("FeedType", "_UPLOAD_VAT_INVOICE_")
rest:add_query_param("MWSAuthToken", "EXAMPLE-amzn.mws.4ea38b7b-f563-7709-4bae-87aea-EXAMPLE")
rest:add_query_param("MarketplaceIdList.Id.1", "ATVExampleDER")
rest:add_query_param("SellerId", "A1XExample5E6")
rest:add_query_param("ContentMD5Value", md5Hash)
rest:add_query_param("SignatureMethod", "HmacSHA256")
rest:add_query_param("SignatureVersion", "2")
rest:add_query_param("Version", "2009-01-01")
-- Add the MWS Signature param. (Also adds the Timestamp parameter using the curent system date/time.)
rest:add_mws_signature("POST", "/Feeds/2009-01-01", "mws.amazonservices.com", "YOUR_MWS_SECRET_ACCESS_KEY_ID")
rest:add_header("Content-Type", "application/octet-stream")
local sbResponseBody = chilkat.StringBuilder.new()
success = rest:full_request_bd("POST", "/Feeds/2009-01-01", pdfData, sbResponseBody)
if not rest.last_method_success then
print(rest.last_error_text)
return
end
if rest.response_status_code ~= 200 then
-- Examine the request/response to see what happened.
print("response status code = " .. rest.response_status_code)
print("response status text = " .. rest.response_status_text)
print("response header: " .. rest.response_header)
print("response body: " .. sbResponseBody:get_as_string())
print("---")
print("LastRequestStartLine: " .. rest.last_request_start_line)
print("LastRequestHeader: " .. rest.last_request_header)
end
-- Examine the XML returned in the response body.
print(sbResponseBody:get_as_string())
print("----")
print("Success.")