Sample code for 30+ languages & platforms
PureBasic

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

PureBasic
IncludeFile "CkBinData.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkRest.pb"
IncludeFile "CkCrypt2.pb"

Procedure ChilkatExample()

    success.i = 0

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

    rest.i = CkRest::ckCreate()
    If rest.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; 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.i = 1
    port.i = 443
    bAutoReconnect.i = 1
    success = CkRest::ckConnect(rest,"mws.amazonservices.com",port,bTls,bAutoReconnect)

    CkRest::setCkHost(rest, "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.
    CkRest::ckAddQueryParam(rest,"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.i = CkBinData::ckCreate()
    If pdfData.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkBinData::ckLoadFile(pdfData,"qa_data/pdf/sample.pdf")

    ; Get the MD5 hash of the PDF data.
    crypt.i = CkCrypt2::ckCreate()
    If crypt.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkCrypt2::setCkHashAlgorithm(crypt, "md5")
    CkCrypt2::setCkEncodingMode(crypt, "base64")
    md5Hash.s = CkCrypt2::ckHashBdENC(crypt,pdfData)

    CkRest::ckAddQueryParam(rest,"AWSAccessKeyId","0PB842ExampleN4ZTR2")
    CkRest::ckAddQueryParam(rest,"Action","SubmitFeed")
    CkRest::ckAddQueryParam(rest,"FeedType","_UPLOAD_VAT_INVOICE_")
    CkRest::ckAddQueryParam(rest,"MWSAuthToken","EXAMPLE-amzn.mws.4ea38b7b-f563-7709-4bae-87aea-EXAMPLE")

    CkRest::ckAddQueryParam(rest,"MarketplaceIdList.Id.1","ATVExampleDER")
    CkRest::ckAddQueryParam(rest,"SellerId","A1XExample5E6")
    CkRest::ckAddQueryParam(rest,"ContentMD5Value",md5Hash)
    CkRest::ckAddQueryParam(rest,"SignatureMethod","HmacSHA256")
    CkRest::ckAddQueryParam(rest,"SignatureVersion","2")
    CkRest::ckAddQueryParam(rest,"Version","2009-01-01")

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

    CkRest::ckAddHeader(rest,"Content-Type","application/octet-stream")
    sbResponseBody.i = CkStringBuilder::ckCreate()
    If sbResponseBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkRest::ckFullRequestBd(rest,"POST","/Feeds/2009-01-01",pdfData,sbResponseBody)
    If CkRest::ckLastMethodSuccess(rest) <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkBinData::ckDispose(pdfData)
        CkCrypt2::ckDispose(crypt)
        CkStringBuilder::ckDispose(sbResponseBody)
        ProcedureReturn
    EndIf

    If CkRest::ckResponseStatusCode(rest) <> 200
        ; Examine the request/response to see what happened.
        Debug "response status code = " + Str(CkRest::ckResponseStatusCode(rest))
        Debug "response status text = " + CkRest::ckResponseStatusText(rest)
        Debug "response header: " + CkRest::ckResponseHeader(rest)
        Debug "response body: " + CkStringBuilder::ckGetAsString(sbResponseBody)
        Debug "---"
        Debug "LastRequestStartLine: " + CkRest::ckLastRequestStartLine(rest)
        Debug "LastRequestHeader: " + CkRest::ckLastRequestHeader(rest)
    EndIf

    ; Examine the XML returned in the response body.
    Debug CkStringBuilder::ckGetAsString(sbResponseBody)
    Debug "----"
    Debug "Success."


    CkRest::ckDispose(rest)
    CkBinData::ckDispose(pdfData)
    CkCrypt2::ckDispose(crypt)
    CkStringBuilder::ckDispose(sbResponseBody)


    ProcedureReturn
EndProcedure