Sample code for 30+ languages & platforms
Delphi DLL Requires Chilkat v11.0.0+

PayPal: List merchant invoices across pages

See more PayPal Examples

List invoices for the authenticated merchant. Request totals, iterate every page, and keep the PayPal ID alongside the human-readable number and current status.

PayPal documentation

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
http: HCkHttp;
resp: HCkHttpResponse;
page: Integer;
lastPage: Integer;
url: HCkStringBuilder;
errorInfo: HCkJsonObject;
result: HCkJsonObject;
parsed: Boolean;
i: Integer;
count: Integer;

begin
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 := CkHttp_Create();
CkHttp_putAuthToken(http,'SANDBOX_ACCESS_TOKEN');
resp := CkHttpResponse_Create();

//  Request total_pages so a merchant with more than one page sees every invoice.
page := 1;
lastPage := 1;
while (page <= lastPage) do
  begin
    url := CkStringBuilder_Create();
    CkStringBuilder_Append(url,'https://api-m.sandbox.paypal.com/v2/invoicing/invoices?total_required=true&page_size=20&page=');
    CkStringBuilder_AppendInt(url,page);
    success := CkHttp_HttpNoBody(http,'GET',CkStringBuilder__getAsString(url),resp);
    if (success <> True) then
      begin
        Memo1.Lines.Add('No HTTP response: ' + CkHttp__lastErrorText(http));
        Exit;
      end;
    if ((CkHttpResponse_getStatusCode(resp) < 200) or (CkHttpResponse_getStatusCode(resp) >= 300)) then
      begin
        errorInfo := CkJsonObject_Create();
        CkJsonObject_Load(errorInfo,CkHttpResponse__bodyStr(resp));
        Memo1.Lines.Add('HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)) + ' ' + CkJsonObject__stringOf(errorInfo,'name') + ' ' + CkJsonObject__stringOf(errorInfo,'details[0].issue'));
        Memo1.Lines.Add('PayPal debug ID: ' + CkJsonObject__stringOf(errorInfo,'debug_id'));
        Exit;
      end;
    result := CkJsonObject_Create();
    parsed := CkJsonObject_Load(result,CkHttpResponse__bodyStr(resp));
    if (parsed <> True) then
      begin
        Memo1.Lines.Add('Expected a JSON response; HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)));
        Exit;
      end;
    i := 0;
    count := CkJsonObject_SizeOfArray(result,'items');
    while (i < count) do
      begin
        CkJsonObject_putI(result,i);
        Memo1.Lines.Add(CkJsonObject__stringOf(result,'items[i].id') + '  ' + CkJsonObject__stringOf(result,'items[i].detail.invoice_number') + '  ' + CkJsonObject__stringOf(result,'items[i].status'));
        i := i + 1;
      end;

    lastPage := CkJsonObject_IntOf(result,'total_pages');
    page := page + 1;
  end;

CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
    CkStringBuilder_Dispose(url);
        CkJsonObject_Dispose(errorInfo);
    CkJsonObject_Dispose(result);