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

PayPal: Page through merchant transactions

See more PayPal Examples

Read yesterday-to-now account activity one page at a time. Enable Transaction Search for the REST app and obtain a token with reporting access. Data can lag by up to three hours; use order or capture lookup for a checkout in progress.

PayPal documentation

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
http: HCkHttp;
resp: HCkHttpResponse;
dt: HCkDateTime;
query: HCkJsonObject;
page: Integer;
lastPage: Integer;
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();
//  Use the UTC window containing the transaction (at most 31 days per request).
dt := CkDateTime_Create();
CkDateTime_SetFromCurrentSystemTime(dt);
query := CkJsonObject_Create();
CkJsonObject_UpdateString(query,'end_date',CkDateTime__getAsTimestamp(dt,False));
CkDateTime_AddDays(dt,-1);
CkJsonObject_UpdateString(query,'start_date',CkDateTime__getAsTimestamp(dt,False));
CkJsonObject_UpdateString(query,'fields','transaction_info');
CkJsonObject_UpdateInt(query,'page_size',100);
page := 1;
lastPage := 1;
while (page <= lastPage) do
  begin
    CkJsonObject_UpdateInt(query,'page',page);
    success := CkHttp_HttpParams(http,'GET','https://api-m.sandbox.paypal.com/v1/reporting/transactions',query,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,'transaction_details');
    while (i < count) do
      begin
        CkJsonObject_putI(result,i);
        Memo1.Lines.Add(CkJsonObject__stringOf(result,'transaction_details[i].transaction_info.transaction_id') + '  ' + CkJsonObject__stringOf(result,'transaction_details[i].transaction_info.transaction_event_code')
             + '  ' + CkJsonObject__stringOf(result,'transaction_details[i].transaction_info.transaction_status'));
        Memo1.Lines.Add('  ' + CkJsonObject__stringOf(result,'transaction_details[i].transaction_info.transaction_amount.value') + ' ' + CkJsonObject__stringOf(result,'transaction_details[i].transaction_info.transaction_amount.currency_code'));
        i := i + 1;
      end;

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

CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkDateTime_Dispose(dt);
CkJsonObject_Dispose(query);
        CkJsonObject_Dispose(errorInfo);
    CkJsonObject_Dispose(result);