Sample code for 30+ languages & platforms
C

Mastercard ICCP Get Data Source with OAuth1

See more Mastercard Examples

Demonstrates OAuth1 authentication with the Mastercard SOAP API's. This example sends a POST to the sandbox endpoint for the "In Control for Commercial Payments" (ICCP) API.

Chilkat C Downloads

C
#include <C_CkXml.h>
#include <C_CkPfx.h>
#include <C_CkPrivateKey.h>
#include <C_CkHttp.h>
#include <C_CkHttpResponse.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkXml xml;
    HCkPfx pfx;
    HCkPrivateKey privKey;
    HCkHttp http;
    HCkHttpResponse resp;
    HCkXml respXml;

    success = FALSE;

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

    // --------------------------------------------------------------------------------
    // Also see Chilkat's Online WSDL Code Generator
    // to generate code and SOAP Request and Response XML for each operation in a WSDL.
    // --------------------------------------------------------------------------------

    // First build the XML body of the SOAP request:

    //     <soapenv:Envelope
    // 	    xmlns:ser="http://mastercard.com/sd/pc/service"
    // 	    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    // 	    <soapenv:Header>
    // 	    </soapenv:Header>
    // 	    <soapenv:Body>
    // 		    <ser:getDataSourcesRequest></ser:getDataSourcesRequest>
    // 	    </soapenv:Body>
    //     </soapenv:Envelope>

    // Use this online tool to generate code from sample XML: 
    // Generate Code to Create XML

    xml = CkXml_Create();
    CkXml_putTag(xml,"soapenv:Envelope");
    CkXml_AddAttribute(xml,"xmlns:ser","http://mastercard.com/sd/pc/service");
    CkXml_AddAttribute(xml,"xmlns:soapenv","http://schemas.xmlsoap.org/soap/envelope/");
    CkXml_UpdateChildContent(xml,"soapenv:Header","");
    CkXml_UpdateChildContent(xml,"soapenv:Body|ser:getDataSourcesRequest","");

    // We'll need to get our signing RSA key from the PFX (provided by Mastercard)
    pfx = CkPfx_Create();
    success = CkPfx_LoadPfxFile(pfx,"qa_data/pfx/MCD_Sandbox_chilkat_iccp_API_Keys/chilkat_iccp-sandbox.p12","keystorepassword");
    if (success == FALSE) {
        printf("%s\n",CkPfx_lastErrorText(pfx));
        CkXml_Dispose(xml);
        CkPfx_Dispose(pfx);
        return;
    }

    privKey = CkPrivateKey_Create();
    success = CkPfx_PrivateKeyAt(pfx,0,privKey);
    if (success == FALSE) {
        printf("%s\n",CkPfx_lastErrorText(pfx));
        CkXml_Dispose(xml);
        CkPfx_Dispose(pfx);
        CkPrivateKey_Dispose(privKey);
        return;
    }

    http = CkHttp_Create();

    CkHttp_putOAuth1(http,TRUE);
    // Use your own consumer key (this is not a valid consumer key)
    CkHttp_putOAuthConsumerKey(http,"MLBRl0-xxxxxxxxxxxxxxxxxxxxxxxxxx_BUNtu5xxxxx20b!a075a714a5fxxxxxxxxxxxxx59cd02b60000000000000000");
    CkHttp_putOAuthSigMethod(http,"RSA-SHA256");
    success = CkHttp_SetOAuthRsaKey(http,privKey);
    if (success == FALSE) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkXml_Dispose(xml);
        CkPfx_Dispose(pfx);
        CkPrivateKey_Dispose(privKey);
        CkHttp_Dispose(http);
        return;
    }

    // Tell Chilkat to automatically calculate and add the oauth_body_hash field when sending the request.
    CkHttp_putOAuthBodyHash(http,TRUE);

    // Send the SOAP XML request and get the response.
    // Chilkat automaticaly adds the OAuth1 authentication.
    resp = CkHttpResponse_Create();
    success = CkHttp_HttpStr(http,"POST","https://sandbox.api.mastercard.com/iccp/financial",CkXml_getXml(xml),"utf-8","application/xml",resp);
    if (success == FALSE) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkXml_Dispose(xml);
        CkPfx_Dispose(pfx);
        CkPrivateKey_Dispose(privKey);
        CkHttp_Dispose(http);
        CkHttpResponse_Dispose(resp);
        return;
    }

    // Examine the response status code and the XML response body.
    printf("Response status code = %d\n",CkHttpResponse_getStatusCode(resp));

    respXml = CkXml_Create();
    CkHttpResponse_GetBodyXml(resp,respXml);

    printf("Response XML:\n");
    printf("%s\n",CkXml_getXml(respXml));

    // If desired, use this online tool to generate parsing code from response XML.
    // (Run your code once to get a representative sample response, and then generate the parsing code.)
    // Generate Parsing Code from XML


    CkXml_Dispose(xml);
    CkPfx_Dispose(pfx);
    CkPrivateKey_Dispose(privKey);
    CkHttp_Dispose(http);
    CkHttpResponse_Dispose(resp);
    CkXml_Dispose(respXml);

    }