Sample code for 30+ languages & platforms
Unicode C Requires Chilkat v11.0.0+

PayPal: Find invoices for a recipient

See more PayPal Examples

Filter by a sandbox recipient email address in the JSON body. Pagination belongs in the URL. Read every matching invoice ID; several invoices can share the same recipient.

PayPal documentation

Chilkat Unicode C Downloads

Unicode C
#include <C_CkHttpW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkStringBuilderW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttpW http;
    HCkHttpResponseW resp;
    HCkJsonObjectW body;
    int page;
    int lastPage;
    HCkStringBuilderW url;
    HCkJsonObjectW errorInfo;
    HCkJsonObjectW result;
    BOOL parsed;
    int i;
    int count;

    success = FALSE;

    //  See Unlock Chilkat before making requests.
    //  For the merchant's internal backoffice application. Load the sandbox token
    //  from the merchant's credential store. Keep it out of publicly distributed apps.
    //  See Get a PayPal access token.
    http = CkHttpW_Create();
    CkHttpW_putAuthToken(http,L"SANDBOX_ACCESS_TOKEN");
    resp = CkHttpResponseW_Create();

    //  Use a JSON search body on every page; the recipient email does not belong in the
    //  URL.
    body = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(body,L"recipient_email",L"buyer@example.com");
    page = 1;
    lastPage = 1;
    while ((page <= lastPage)) {
        url = CkStringBuilderW_Create();
        CkStringBuilderW_Append(url,L"https://api-m.sandbox.paypal.com/v2/invoicing/search-invoices?total_required=true&page_size=20&page=");
        CkStringBuilderW_AppendInt(url,page);
        success = CkHttpW_HttpJson(http,L"POST",CkStringBuilderW_getAsString(url),body,L"application/json",resp);
        if (success != TRUE) {
            wprintf(L"No HTTP response: %s\n",CkHttpW_lastErrorText(http));
            CkHttpW_Dispose(http);
            CkHttpResponseW_Dispose(resp);
            CkJsonObjectW_Dispose(body);
            CkStringBuilderW_Dispose(url);
            return;
        }

        if ((CkHttpResponseW_getStatusCode(resp) < 200) || (CkHttpResponseW_getStatusCode(resp) >= 300)) {
            errorInfo = CkJsonObjectW_Create();
            CkJsonObjectW_Load(errorInfo,CkHttpResponseW_bodyStr(resp));
            wprintf(L"HTTP %d %s %s\n",CkHttpResponseW_getStatusCode(resp),CkJsonObjectW_stringOf(errorInfo,L"name"),CkJsonObjectW_stringOf(errorInfo,L"details[0].issue"));
            wprintf(L"PayPal debug ID: %s\n",CkJsonObjectW_stringOf(errorInfo,L"debug_id"));
            CkHttpW_Dispose(http);
            CkHttpResponseW_Dispose(resp);
            CkJsonObjectW_Dispose(body);
            CkStringBuilderW_Dispose(url);
            CkJsonObjectW_Dispose(errorInfo);
            return;
        }

        result = CkJsonObjectW_Create();
        parsed = CkJsonObjectW_Load(result,CkHttpResponseW_bodyStr(resp));
        if (parsed != TRUE) {
            wprintf(L"Expected a JSON response; HTTP %d\n",CkHttpResponseW_getStatusCode(resp));
            CkHttpW_Dispose(http);
            CkHttpResponseW_Dispose(resp);
            CkJsonObjectW_Dispose(body);
            CkStringBuilderW_Dispose(url);
            CkJsonObjectW_Dispose(errorInfo);
            CkJsonObjectW_Dispose(result);
            return;
        }

        i = 0;
        count = CkJsonObjectW_SizeOfArray(result,L"items");
        while ((i < count)) {
            CkJsonObjectW_putI(result,i);
            wprintf(L"%s  %s  %s\n",CkJsonObjectW_stringOf(result,L"items[i].id"),CkJsonObjectW_stringOf(result,L"items[i].detail.invoice_number"),CkJsonObjectW_stringOf(result,L"items[i].status"));
            i = i + 1;
        }

        lastPage = CkJsonObjectW_IntOf(result,L"total_pages");
        page = page + 1;
    }



    CkHttpW_Dispose(http);
    CkHttpResponseW_Dispose(resp);
    CkJsonObjectW_Dispose(body);
    CkStringBuilderW_Dispose(url);
    CkJsonObjectW_Dispose(errorInfo);
    CkJsonObjectW_Dispose(result);

    }