Sample code for 30+ languages & platforms
Swift

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

Swift

func chilkatTest() {
    var success: Bool = false

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

    let rest = CkoRest()!

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

    var bTls: Bool = true
    var port: Int = 443
    var bAutoReconnect: Bool = true
    success = rest.connect(hostname: "mws.amazonservices.com", port: port, tls: bTls, autoReconnect: 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(name: "FeedOptions", value: "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.
    let pdfData = CkoBinData()!
    success = pdfData.loadFile(path: "qa_data/pdf/sample.pdf")

    // Get the MD5 hash of the PDF data.
    let crypt = CkoCrypt2()!
    crypt.hashAlgorithm = "md5"
    crypt.encodingMode = "base64"
    var md5Hash: String? = crypt.hashBdENC(bd: pdfData)

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

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

    // Add the MWS Signature param.  (Also adds the Timestamp parameter using the curent system date/time.)
    rest.addMwsSignature(httpVerb: "POST", uriPath: "/Feeds/2009-01-01", domain: "mws.amazonservices.com", mwsSecretKey: "YOUR_MWS_SECRET_ACCESS_KEY_ID")

    rest.addHeader(name: "Content-Type", value: "application/octet-stream")
    let sbResponseBody = CkoStringBuilder()!
    success = rest.fullRequestBd(httpVerb: "POST", uriPath: "/Feeds/2009-01-01", binData: pdfData, responseBody: sbResponseBody)
    if rest.lastMethodSuccess != true {
        print("\(rest.lastErrorText!)")
        return
    }

    if rest.responseStatusCode.intValue != 200 {
        // Examine the request/response to see what happened.
        print("response status code = \(rest.responseStatusCode.intValue)")
        print("response status text = \(rest.responseStatusText!)")
        print("response header: \(rest.responseHeader!)")
        print("response body: \(sbResponseBody.getAsString()!)")
        print("---")
        print("LastRequestStartLine: \(rest.lastRequestStartLine!)")
        print("LastRequestHeader: \(rest.lastRequestHeader!)")
    }

    // Examine the XML returned in the response body.
    print("\(sbResponseBody.getAsString()!)")
    print("----")
    print("Success.")

}