Delphi ActiveX Requires Chilkat v11.0.0+
Delphi ActiveX
PayPal: Export a day of balance activity to CSV
See more PayPal Examples
Create a UTF-8 ledger export for the previous UTC day. Preserve signed amounts and separate currencies, and leave absent fees blank. The file is written after all pages have been read. Transaction Search can lag by several hours.
Chilkat Delphi ActiveX Downloads
var
success: Integer;
http: TChilkatHttp;
resp: TChilkatHttpResponse;
day: TCkDateTime;
utcDate: WideString;
startTime: TChilkatStringBuilder;
endTime: TChilkatStringBuilder;
query: TChilkatJsonObject;
csv: TChilkatCsv;
row: Integer;
page: Integer;
lastPage: Integer;
errorInfo: TChilkatJsonObject;
result: TChilkatJsonObject;
parsedResult: Integer;
i: Integer;
count: Integer;
filename: TChilkatStringBuilder;
saved: 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);
// Export yesterday in UTC; choose an earlier date if the report is still refreshing.
day := TCkDateTime.Create(Self);
day.SetFromCurrentSystemTime();
day.AddDays(-1);
utcDate := day.GetAsIso8601('YYYY-MM-DD',0);
startTime := TChilkatStringBuilder.Create(Self);
startTime.Append(utcDate);
startTime.Append('T00:00:00Z');
endTime := TChilkatStringBuilder.Create(Self);
endTime.Append(utcDate);
endTime.Append('T23:59:59.999Z');
query := TChilkatJsonObject.Create(Self);
query.UpdateString('start_date',startTime.GetAsString());
query.UpdateString('end_date',endTime.GetAsString());
query.UpdateString('balance_affecting_records_only','Y');
query.UpdateString('fields','transaction_info');
query.UpdateInt('page_size',100);
csv := TChilkatCsv.Create(Self);
csv.HasColumnNames := 1;
csv.SetColumnName(0,'transaction_id');
csv.SetColumnName(1,'event_code');
csv.SetColumnName(2,'status');
csv.SetColumnName(3,'currency');
csv.SetColumnName(4,'amount');
csv.SetColumnName(5,'fee_currency');
csv.SetColumnName(6,'fee');
row := 0;
page := 1;
lastPage := 1;
while (page <= lastPage) do
begin
query.UpdateInt('page',page);
success := http.HttpParams('GET','https://api-m.sandbox.paypal.com/v1/reporting/transactions',query.ControlInterface,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);
parsedResult := result.Load(resp.BodyStr);
if (parsedResult <> 1) then
begin
Memo1.Lines.Add('Expected a JSON response; HTTP ' + IntToStr(resp.StatusCode));
Exit;
end;
i := 0;
count := result.SizeOfArray('transaction_details');
while (i < count) do
begin
result.I := i;
csv.SetCell(row,0,result.StringOf('transaction_details[i].transaction_info.transaction_id'));
csv.SetCell(row,1,result.StringOf('transaction_details[i].transaction_info.transaction_event_code'));
csv.SetCell(row,2,result.StringOf('transaction_details[i].transaction_info.transaction_status'));
csv.SetCell(row,3,result.StringOf('transaction_details[i].transaction_info.transaction_amount.currency_code'));
csv.SetCell(row,4,result.StringOf('transaction_details[i].transaction_info.transaction_amount.value'));
csv.SetCell(row,5,result.StringOf('transaction_details[i].transaction_info.fee_amount.currency_code'));
csv.SetCell(row,6,result.StringOf('transaction_details[i].transaction_info.fee_amount.value'));
row := row + 1;
i := i + 1;
end;
lastPage := result.IntOf('total_pages');
page := page + 1;
end;
filename := TChilkatStringBuilder.Create(Self);
filename.Append('paypal-activity-');
filename.Append(utcDate);
filename.Append('.csv');
saved := csv.SaveFile2(filename.GetAsString(),'utf-8');
if (saved <> 1) then
begin
Memo1.Lines.Add(csv.LastErrorText);
Exit;
end;
Memo1.Lines.Add('Exported ' + IntToStr(row) + ' rows to ' + filename.GetAsString());