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

PayPal: Build a worklist of overdue invoices

See more PayPal Examples

Find open invoices due in the past 90 days, including partial payments. Walk all result pages and display the invoice number, due date, and remaining balance for the collections team.

PayPal documentation

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkHttpW.h>
#include <CkHttpResponseW.h>
#include <CkJsonObjectW.h>
#include <CkDateTimeW.h>
#include <CkStringBuilderW.h>

void ChilkatSample(void)
    {
    bool 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.
    CkHttpW http;
    http.put_AuthToken(L"SANDBOX_ACCESS_TOKEN");
    CkHttpResponseW resp;
    CkJsonObjectW body;
    body.UpdateString(L"status[0]",L"SENT");
    body.UpdateString(L"status[1]",L"PARTIALLY_PAID");
    body.UpdateString(L"status[2]",L"UNPAID");
    body.UpdateString(L"status[3]",L"SHARED");
    //  Yesterday is the last overdue date. Include invoices due in the past 90 days.
    CkDateTimeW day;
    day.SetFromCurrentSystemTime();
    day.AddDays(-1);
    body.UpdateString(L"due_date_range.end",day.getAsIso8601(L"YYYY-MM-DD",false));
    day.AddDays(-89);
    body.UpdateString(L"due_date_range.start",day.getAsIso8601(L"YYYY-MM-DD",false));
    int page = 1;
    int lastPage = 1;
    while ((page <= lastPage)) {
        CkStringBuilderW url;
        url.Append(L"https://api-m.sandbox.paypal.com/v2/invoicing/search-invoices?total_required=true&page_size=50&page=");
        url.AppendInt(page);
        success = http.HttpJson(L"POST",url.getAsString(),body,L"application/json",resp);
        if (success != true) {
            wprintf(L"No HTTP response: %s\n",http.lastErrorText());
            return;
        }

        if ((resp.get_StatusCode() < 200) || (resp.get_StatusCode() >= 300)) {
            CkJsonObjectW errorInfo;
            errorInfo.Load(resp.bodyStr());
            wprintf(L"HTTP %d %s %s\n",resp.get_StatusCode(),errorInfo.stringOf(L"name"),errorInfo.stringOf(L"details[0].issue"));
            wprintf(L"PayPal debug ID: %s\n",errorInfo.stringOf(L"debug_id"));
            return;
        }

        CkJsonObjectW result;
        bool parsedResult = result.Load(resp.bodyStr());
        if (parsedResult != true) {
            wprintf(L"Expected a JSON response; HTTP %d\n",resp.get_StatusCode());
            return;
        }

        int i = 0;
        int count = result.SizeOfArray(L"items");
        while ((i < count)) {
            result.put_I(i);
            wprintf(L"%s  due %s  %s %s\n",result.stringOf(L"items[i].detail.invoice_number"),result.stringOf(L"items[i].detail.payment_term.due_date"),result.stringOf(L"items[i].due_amount.value")
                ,result.stringOf(L"items[i].due_amount.currency_code"));
            i = i + 1;
        }

        lastPage = result.IntOf(L"total_pages");
        page = page + 1;
    }
    }