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

Delphi ActiveX
var
success: Integer;
http: TChilkatHttp;
resp: TChilkatHttpResponse;
page: Integer;
lastPage: Integer;
url: TChilkatStringBuilder;
errorInfo: TChilkatJsonObject;
result: TChilkatJsonObject;
parsed: Integer;
i: Integer;
count: Integer;

begin
success := 0;

//  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 := TChilkatHttp.Create(Self);
http.AuthToken := 'SANDBOX_ACCESS_TOKEN';
resp := TChilkatHttpResponse.Create(Self);

//  Request total_pages so a merchant with more than one page sees every invoice.
page := 1;
lastPage := 1;
while (page <= lastPage) do
  begin
    url := TChilkatStringBuilder.Create(Self);
    url.Append('https://api-m.sandbox.paypal.com/v2/invoicing/invoices?total_required=true&page_size=20&page=');
    url.AppendInt(page);
    success := http.HttpNoBody('GET',url.GetAsString(),resp.ControlInterface);
    if (success <> 1) then
      begin
        Memo1.Lines.Add('No HTTP response: ' + http.LastErrorText);
        Exit;
      end;
    if ((resp.StatusCode < 200) or (resp.StatusCode >= 300)) then
      begin
        errorInfo := TChilkatJsonObject.Create(Self);
        errorInfo.Load(resp.BodyStr);
        Memo1.Lines.Add('HTTP ' + IntToStr(resp.StatusCode) + ' ' + errorInfo.StringOf('name') + ' ' + errorInfo.StringOf('details[0].issue'));
        Memo1.Lines.Add('PayPal debug ID: ' + errorInfo.StringOf('debug_id'));
        Exit;
      end;
    result := TChilkatJsonObject.Create(Self);
    parsed := result.Load(resp.BodyStr);
    if (parsed <> 1) then
      begin
        Memo1.Lines.Add('Expected a JSON response; HTTP ' + IntToStr(resp.StatusCode));
        Exit;
      end;
    i := 0;
    count := result.SizeOfArray('items');
    while (i < count) do
      begin
        result.I := i;
        Memo1.Lines.Add(result.StringOf('items[i].id') + '  ' + result.StringOf('items[i].detail.invoice_number') + '  ' + result.StringOf('items[i].status'));
        i := i + 1;
      end;

    lastPage := result.IntOf('total_pages');
    page := page + 1;
  end;