Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

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 Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.BinData,
  Chilkat.Rest,
  Chilkat.StringBuilder,
  Chilkat.Crypt2;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  rest: TRest;
  bTls: Boolean;
  port: Integer;
  bAutoReconnect: Boolean;
  pdfData: TBinData;
  crypt: TCrypt2;
  md5Hash: string;
  sbResponseBody: TStringBuilder;

begin
  success := False;

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

  rest := TRest.Create;

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

  bTls := True;
  port := 443;
  bAutoReconnect := True;
  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.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.
  pdfData := TBinData.Create;
  success := pdfData.LoadFile('qa_data/pdf/sample.pdf');

  //  Get the MD5 hash of the PDF data.
  crypt := TCrypt2.Create;
  crypt.HashAlgorithm := 'md5';
  crypt.EncodingMode := 'base64';
  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');
  sbResponseBody := TStringBuilder.Create;
  success := rest.FullRequestBd('POST','/Feeds/2009-01-01',pdfData,sbResponseBody);
  if (rest.LastMethodSuccess <> True) then
    begin
      WriteLn(rest.LastErrorText);
      Exit;
    end;

  if (rest.ResponseStatusCode <> 200) then
    begin
      //  Examine the request/response to see what happened.
      WriteLn('response status code = ' + rest.ResponseStatusCode);
      WriteLn('response status text = ' + rest.ResponseStatusText);
      WriteLn('response header: ' + rest.ResponseHeader);
      WriteLn('response body: ' + sbResponseBody.GetAsString());
      WriteLn('---');
      WriteLn('LastRequestStartLine: ' + rest.LastRequestStartLine);
      WriteLn('LastRequestHeader: ' + rest.LastRequestHeader);
    end;

  //  Examine the XML returned in the response body.
  WriteLn(sbResponseBody.GetAsString());
  WriteLn('----');
  WriteLn('Success.');


  rest.Free;
  pdfData.Free;
  crypt.Free;
  sbResponseBody.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.