Delphi ActiveX Requires Chilkat v11.0.0+
Delphi ActiveX
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.
Chilkat Delphi ActiveX Downloads
var
success: Integer;
http: TChilkatHttp;
resp: TChilkatHttpResponse;
body: TChilkatJsonObject;
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);
// Use a JSON search body on every page; the recipient email does not belong in the
// URL.
body := TChilkatJsonObject.Create(Self);
body.UpdateString('recipient_email','buyer@example.com');
page := 1;
lastPage := 1;
while (page <= lastPage) do
begin
url := TChilkatStringBuilder.Create(Self);
url.Append('https://api-m.sandbox.paypal.com/v2/invoicing/search-invoices?total_required=true&page_size=20&page=');
url.AppendInt(page);
success := http.HttpJson('POST',url.GetAsString(),body.ControlInterface,'application/json',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;