Sample code for 30+ languages & platforms
Zig

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 Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const rest = try chilkat.Rest.init();
    defer rest.deinit();

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

    const b_tls = true;
    const port = 443;
    const b_auto_reconnect = true;
    try rest.connect("mws.amazonservices.com", port, b_tls, b_auto_reconnect);

    rest.setHost("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.addQueryParam("FeedOptions", "metadata:orderid=206-2341234-3455465;metadata:invoicenumber=INT-3431-XJE3;metadata:documenttype=Invoice") catch {};

    // Load the PDF invoice file that is to be the body of the HTTP POST request.
    const pdf_data = try chilkat.BinData.init();
    defer pdf_data.deinit();
    try pdf_data.loadFile("qa_data/pdf/sample.pdf");

    // Get the MD5 hash of the PDF data.
    const crypt = try chilkat.Crypt2.init();
    defer crypt.deinit();
    crypt.setHashAlgorithm("md5");
    crypt.setEncodingMode("base64");
    const md5_hash = try crypt.hashBdENC(alloc, pdf_data);

    rest.addQueryParam("AWSAccessKeyId", "0PB842ExampleN4ZTR2") catch {};
    rest.addQueryParam("Action", "SubmitFeed") catch {};
    rest.addQueryParam("FeedType", "_UPLOAD_VAT_INVOICE_") catch {};
    rest.addQueryParam("MWSAuthToken", "EXAMPLE-amzn.mws.4ea38b7b-f563-7709-4bae-87aea-EXAMPLE") catch {};

    rest.addQueryParam("MarketplaceIdList.Id.1", "ATVExampleDER") catch {};
    rest.addQueryParam("SellerId", "A1XExample5E6") catch {};
    rest.addQueryParam("ContentMD5Value", md5_hash) catch {};
    rest.addQueryParam("SignatureMethod", "HmacSHA256") catch {};
    rest.addQueryParam("SignatureVersion", "2") catch {};
    rest.addQueryParam("Version", "2009-01-01") catch {};

    // Add the MWS Signature param.  (Also adds the Timestamp parameter using the curent system date/time.)
    rest.addMwsSignature("POST", "/Feeds/2009-01-01", "mws.amazonservices.com", "YOUR_MWS_SECRET_ACCESS_KEY_ID") catch {};

    rest.addHeader("Content-Type", "application/octet-stream") catch {};
    const sb_response_body = try chilkat.StringBuilder.init();
    defer sb_response_body.deinit();
    rest.fullRequestBd("POST", "/Feeds/2009-01-01", pdf_data, sb_response_body) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    if (rest.getResponseStatusCode() != 200) {
        // Examine the request/response to see what happened.
        std.debug.print("response status code = {d}\n", .{rest.getResponseStatusCode()});
        std.debug.print("response status text = {s}\n", .{try rest.getResponseStatusText(alloc)});
        std.debug.print("response header: {s}\n", .{try rest.getResponseHeader(alloc)});
        std.debug.print("response body: {s}\n", .{try sb_response_body.getAsString(alloc)});
        std.debug.print("---\n", .{});
        std.debug.print("LastRequestStartLine: {s}\n", .{try rest.getLastRequestStartLine(alloc)});
        std.debug.print("LastRequestHeader: {s}\n", .{try rest.getLastRequestHeader(alloc)});
    }

    // Examine the XML returned in the response body.
    std.debug.print("{s}\n", .{try sb_response_body.getAsString(alloc)});
    std.debug.print("----\n", .{});
    std.debug.print("Success.\n", .{});
}