Sample code for 30+ languages & platforms
C

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

C
#include <C_CkRest.h>
#include <C_CkBinData.h>
#include <C_CkCrypt2.h>
#include <C_CkStringBuilder.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkRest rest;
    BOOL bTls;
    int port;
    BOOL bAutoReconnect;
    HCkBinData pdfData;
    HCkCrypt2 crypt;
    const char *md5Hash;
    HCkStringBuilder sbResponseBody;

    success = FALSE;

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

    rest = CkRest_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 = CkRest_Connect(rest,"mws.amazonservices.com",port,bTls,bAutoReconnect);

    CkRest_putHost(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_AddQueryParam(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 = CkBinData_Create();
    success = CkBinData_LoadFile(pdfData,"qa_data/pdf/sample.pdf");

    // Get the MD5 hash of the PDF data.
    crypt = CkCrypt2_Create();
    CkCrypt2_putHashAlgorithm(crypt,"md5");
    CkCrypt2_putEncodingMode(crypt,"base64");
    md5Hash = CkCrypt2_hashBdENC(crypt,pdfData);

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

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

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

    CkRest_AddHeader(rest,"Content-Type","application/octet-stream");
    sbResponseBody = CkStringBuilder_Create();
    success = CkRest_FullRequestBd(rest,"POST","/Feeds/2009-01-01",pdfData,sbResponseBody);
    if (CkRest_getLastMethodSuccess(rest) != TRUE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_Dispose(rest);
        CkBinData_Dispose(pdfData);
        CkCrypt2_Dispose(crypt);
        CkStringBuilder_Dispose(sbResponseBody);
        return;
    }

    if (CkRest_getResponseStatusCode(rest) != 200) {
        // Examine the request/response to see what happened.
        printf("response status code = %d\n",CkRest_getResponseStatusCode(rest));
        printf("response status text = %s\n",CkRest_responseStatusText(rest));
        printf("response header: %s\n",CkRest_responseHeader(rest));
        printf("response body: %s\n",CkStringBuilder_getAsString(sbResponseBody));
        printf("---\n");
        printf("LastRequestStartLine: %s\n",CkRest_lastRequestStartLine(rest));
        printf("LastRequestHeader: %s\n",CkRest_lastRequestHeader(rest));
    }

    // Examine the XML returned in the response body.
    printf("%s\n",CkStringBuilder_getAsString(sbResponseBody));
    printf("----\n");
    printf("Success.\n");


    CkRest_Dispose(rest);
    CkBinData_Dispose(pdfData);
    CkCrypt2_Dispose(crypt);
    CkStringBuilder_Dispose(sbResponseBody);

    }