Sample code for 30+ languages & platforms
Java 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 Java Downloads

Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.DateTime;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        // 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 http = new Http();
        http.setAuthToken("SANDBOX_ACCESS_TOKEN");
        HttpResponse resp = new HttpResponse();
        JsonObject body = new JsonObject();
        body.updateString("status[0]", "SENT");
        body.updateString("status[1]", "PARTIALLY_PAID");
        body.updateString("status[2]", "UNPAID");
        body.updateString("status[3]", "SHARED");
        // Yesterday is the last overdue date. Include invoices due in the past 90 days.
        DateTime day = new DateTime();
        day.setFromCurrentSystemTime();
        day.addDays(-1);
        body.updateString("due_date_range.end", day.getAsIso8601("YYYY-MM-DD", false));
        day.addDays(-89);
        body.updateString("due_date_range.start", day.getAsIso8601("YYYY-MM-DD", false));
        int page = 1;
        int lastPage = 1;
        while (page <= lastPage) {
            StringBuilder url = new StringBuilder();
            url.append("https://api-m.sandbox.paypal.com/v2/invoicing/search-invoices?total_required=true&page_size=50&page=");
            url.appendInt(page);
            try {
                http.httpJson("POST", url.getAsString(), body, "application/json", resp);
            } catch (ChilkatException e) {
                System.out.println("No HTTP response: " + e.getLastErrorText());
                return;
            }

            if ((resp.getStatusCode() < 200) || (resp.getStatusCode() >= 300)) {
                JsonObject errorInfo = new JsonObject();
                errorInfo.load(resp.getBodyStr());
                System.out.println("HTTP " + resp.getStatusCode() + " " + errorInfo.stringOf("name") + " " + errorInfo.stringOf("details[0].issue"));
                System.out.println("PayPal debug ID: " + errorInfo.stringOf("debug_id"));
                return;
            }

            JsonObject result = new JsonObject();
            try {
                result.load(resp.getBodyStr());
            } catch (ChilkatException e) {
                System.out.println("Expected a JSON response; HTTP " + resp.getStatusCode());
                return;
            }

            int i = 0;
            int count = result.sizeOfArray("items");
            while (i < count) {
                result.setI(i);
                System.out.println(result.stringOf("items[i].detail.invoice_number") + "  due " + result.stringOf("items[i].detail.payment_term.due_date") + "  " + result.stringOf("items[i].due_amount.value") + " " + result.stringOf("items[i].due_amount.currency_code"));
                i = i + 1;
            }

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