Sample code for 30+ languages & platforms
React Native

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 React Native Downloads

React Native
import { BinData, Crypt2, Rest, StringBuilder } from '@chilkat/react-native'

async function chilkatExample() {
  // This example requires the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  const rest = new 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

  const bTls = true;
  const port = 443;
  const bAutoReconnect = true;
  await rest.connectAsync('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.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.
  const pdfData = new BinData();
  pdfData.loadFile('qa_data/pdf/sample.pdf');

  // Get the MD5 hash of the PDF data.
  const crypt = new Crypt2();
  crypt.hashAlgorithm = 'md5';
  crypt.encodingMode = 'base64';
  const md5Hash = crypt.hashBdENC(pdfData);

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

  rest.addQueryParam('MarketplaceIdList.Id.1', 'ATVExampleDER');
  rest.addQueryParam('SellerId', 'A1XExample5E6');
  rest.addQueryParam('ContentMD5Value', md5Hash);
  rest.addQueryParam('SignatureMethod', 'HmacSHA256');
  rest.addQueryParam('SignatureVersion', '2');
  rest.addQueryParam('Version', '2009-01-01');

  // 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');

  rest.addHeader('Content-Type', 'application/octet-stream');
  const sbResponseBody = new StringBuilder();
  try {
    await rest.fullRequestBdAsync('POST', '/Feeds/2009-01-01', pdfData, sbResponseBody);
  } catch {
    console.log(rest.lastErrorText);
    return;
  }

  if (rest.responseStatusCode !== 200) {
    // Examine the request/response to see what happened.
    console.log(`response status code = ${rest.responseStatusCode}`);
    console.log(`response status text = ${rest.responseStatusText}`);
    console.log(`response header: ${rest.responseHeader}`);
    console.log(`response body: ${sbResponseBody.getAsString()}`);
    console.log('---');
    console.log(`LastRequestStartLine: ${rest.lastRequestStartLine}`);
    console.log(`LastRequestHeader: ${rest.lastRequestHeader}`);
  }

  // Examine the XML returned in the response body.
  console.log(sbResponseBody.getAsString());
  console.log('----');
  console.log('Success.');
}