Sample code for 30+ languages & platforms
DataFlex

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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoRest
    Boolean iBTls
    Integer iPort
    Boolean iBAutoReconnect
    Variant vPdfData
    Handle hoPdfData
    Handle hoCrypt
    String sMd5Hash
    Variant vSbResponseBody
    Handle hoSbResponseBody
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

    Move False To iSuccess

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

    Get Create (RefClass(cComChilkatRest)) To hoRest
    If (Not(IsComObjectCreated(hoRest))) Begin
        Send CreateComObject of hoRest
    End

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

    Move True To iBTls
    Move 443 To iPort
    Move True To iBAutoReconnect
    Get ComConnect Of hoRest "mws.amazonservices.com" iPort iBTls iBAutoReconnect To iSuccess

    Set ComHost Of hoRest To "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.
    Get ComAddQueryParam Of hoRest "FeedOptions" "metadata:orderid=206-2341234-3455465;metadata:invoicenumber=INT-3431-XJE3;metadata:documenttype=Invoice" To iSuccess

    // Load the PDF invoice file that is to be the body of the HTTP POST request.
    Get Create (RefClass(cComChilkatBinData)) To hoPdfData
    If (Not(IsComObjectCreated(hoPdfData))) Begin
        Send CreateComObject of hoPdfData
    End
    Get ComLoadFile Of hoPdfData "qa_data/pdf/sample.pdf" To iSuccess

    // Get the MD5 hash of the PDF data.
    Get Create (RefClass(cComChilkatCrypt2)) To hoCrypt
    If (Not(IsComObjectCreated(hoCrypt))) Begin
        Send CreateComObject of hoCrypt
    End
    Set ComHashAlgorithm Of hoCrypt To "md5"
    Set ComEncodingMode Of hoCrypt To "base64"
    Get pvComObject of hoPdfData to vPdfData
    Get ComHashBdENC Of hoCrypt vPdfData To sMd5Hash

    Get ComAddQueryParam Of hoRest "AWSAccessKeyId" "0PB842ExampleN4ZTR2" To iSuccess
    Get ComAddQueryParam Of hoRest "Action" "SubmitFeed" To iSuccess
    Get ComAddQueryParam Of hoRest "FeedType" "_UPLOAD_VAT_INVOICE_" To iSuccess
    Get ComAddQueryParam Of hoRest "MWSAuthToken" "EXAMPLE-amzn.mws.4ea38b7b-f563-7709-4bae-87aea-EXAMPLE" To iSuccess

    Get ComAddQueryParam Of hoRest "MarketplaceIdList.Id.1" "ATVExampleDER" To iSuccess
    Get ComAddQueryParam Of hoRest "SellerId" "A1XExample5E6" To iSuccess
    Get ComAddQueryParam Of hoRest "ContentMD5Value" sMd5Hash To iSuccess
    Get ComAddQueryParam Of hoRest "SignatureMethod" "HmacSHA256" To iSuccess
    Get ComAddQueryParam Of hoRest "SignatureVersion" "2" To iSuccess
    Get ComAddQueryParam Of hoRest "Version" "2009-01-01" To iSuccess

    // Add the MWS Signature param.  (Also adds the Timestamp parameter using the curent system date/time.)
    Get ComAddMwsSignature Of hoRest "POST" "/Feeds/2009-01-01" "mws.amazonservices.com" "YOUR_MWS_SECRET_ACCESS_KEY_ID" To iSuccess

    Get ComAddHeader Of hoRest "Content-Type" "application/octet-stream" To iSuccess
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbResponseBody
    If (Not(IsComObjectCreated(hoSbResponseBody))) Begin
        Send CreateComObject of hoSbResponseBody
    End
    Get pvComObject of hoPdfData to vPdfData
    Get pvComObject of hoSbResponseBody to vSbResponseBody
    Get ComFullRequestBd Of hoRest "POST" "/Feeds/2009-01-01" vPdfData vSbResponseBody To iSuccess
    Get ComLastMethodSuccess Of hoRest To bTemp1
    If (bTemp1 <> True) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComResponseStatusCode Of hoRest To iTemp1
    If (iTemp1 <> 200) Begin
        // Examine the request/response to see what happened.
        Get ComResponseStatusCode Of hoRest To iTemp1
        Showln "response status code = " iTemp1
        Get ComResponseStatusText Of hoRest To sTemp1
        Showln "response status text = " sTemp1
        Get ComResponseHeader Of hoRest To sTemp1
        Showln "response header: " sTemp1
        Get ComGetAsString Of hoSbResponseBody To sTemp1
        Showln "response body: " sTemp1
        Showln "---"
        Get ComLastRequestStartLine Of hoRest To sTemp1
        Showln "LastRequestStartLine: " sTemp1
        Get ComLastRequestHeader Of hoRest To sTemp1
        Showln "LastRequestHeader: " sTemp1
    End

    // Examine the XML returned in the response body.
    Get ComGetAsString Of hoSbResponseBody To sTemp1
    Showln sTemp1
    Showln "----"
    Showln "Success."


End_Procedure