Sample code for 30+ languages & platforms
Unicode C

PayPal Send Invoice

See more PayPal Examples

Sends a PayPal invoice to a customer.

See also Send Invoice

Chilkat Unicode C Downloads

Unicode C
#include <C_CkJsonObjectW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkRestW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkJsonObjectW jsonToken;
    HCkStringBuilderW sbAuth;
    HCkRestW rest;
    BOOL bAutoReconnect;
    HCkStringBuilderW sbPath;
    int numReplacements;
    const wchar_t *responseBody;

    success = FALSE;

    // Note: Requires Chilkat v9.5.0.64 or greater.

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

    // Load our previously obtained access token. (see PayPal OAuth2 Access Token)
    jsonToken = CkJsonObjectW_Create();
    CkJsonObjectW_LoadFile(jsonToken,L"qa_data/tokens/paypal.json");

    // Build the Authorization request header field value.
    sbAuth = CkStringBuilderW_Create();
    // token_type should be "Bearer"
    CkStringBuilderW_Append(sbAuth,CkJsonObjectW_stringOf(jsonToken,L"token_type"));
    CkStringBuilderW_Append(sbAuth,L" ");
    CkStringBuilderW_Append(sbAuth,CkJsonObjectW_stringOf(jsonToken,L"access_token"));

    // Make the initial connection.
    // A single REST object, once connected, can be used for many PayPal REST API calls.
    // The auto-reconnect indicates that if the already-established HTTPS connection is closed,
    // then it will be automatically re-established as needed.
    rest = CkRestW_Create();
    bAutoReconnect = TRUE;
    success = CkRestW_Connect(rest,L"api.sandbox.paypal.com",443,TRUE,bAutoReconnect);
    if (success != TRUE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkJsonObjectW_Dispose(jsonToken);
        CkStringBuilderW_Dispose(sbAuth);
        CkRestW_Dispose(rest);
        return;
    }

    // ----------------------------------------------------------------------------------------------
    // The code above this comment could be placed inside a function/subroutine within the application
    // because the connection does not need to be made for every request.  Once the connection is made
    // the app may send many requests..
    // ----------------------------------------------------------------------------------------------

    // Build a path using a draft invoice ID.
    sbPath = CkStringBuilderW_Create();
    CkStringBuilderW_Append(sbPath,L"/v1/invoicing/invoices/invoice_id/send");
    numReplacements = CkStringBuilderW_Replace(sbPath,L"invoice_id",L"INV2-QCHR-QHSD-M2VU-AVXC");

    CkRestW_AddHeader(rest,L"Authorization",CkStringBuilderW_getAsString(sbAuth));
    CkRestW_AddQueryParam(rest,L"notify_merchant",L"true");

    responseBody = CkRestW_fullRequestFormUrlEncoded(rest,L"POST",CkStringBuilderW_getAsString(sbPath));
    if (CkRestW_getLastMethodSuccess(rest) != TRUE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkJsonObjectW_Dispose(jsonToken);
        CkStringBuilderW_Dispose(sbAuth);
        CkRestW_Dispose(rest);
        CkStringBuilderW_Dispose(sbPath);
        return;
    }

    wprintf(L"Response Status Code = %d\n",CkRestW_getResponseStatusCode(rest));

    // Did we get a 202 accepted response?
    if (CkRestW_getResponseStatusCode(rest) != 202) {
        wprintf(L"%s\n",responseBody);
        wprintf(L"Failed.\n");
        CkJsonObjectW_Dispose(jsonToken);
        CkStringBuilderW_Dispose(sbAuth);
        CkRestW_Dispose(rest);
        CkStringBuilderW_Dispose(sbPath);
        return;
    }

    // If the 202 response is received, the response body would be empty...
    wprintf(L"Success.\n");


    CkJsonObjectW_Dispose(jsonToken);
    CkStringBuilderW_Dispose(sbAuth);
    CkRestW_Dispose(rest);
    CkStringBuilderW_Dispose(sbPath);

    }