Android™ Requires Chilkat v11.0.0+
Android™
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 Android™ Downloads
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;
import android.app.Activity;
import com.chilkatsoft.*;
import android.widget.TextView;
import android.os.Bundle;
public class SimpleActivity extends Activity {
private static final String TAG = "Chilkat";
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean 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.
CkHttp http = new CkHttp();
http.put_AuthToken("SANDBOX_ACCESS_TOKEN");
CkHttpResponse resp = new CkHttpResponse();
// Export yesterday in UTC; choose an earlier date if the report is still refreshing.
CkDateTime day = new CkDateTime();
day.SetFromCurrentSystemTime();
day.AddDays(-1);
String utcDate = day.getAsIso8601("YYYY-MM-DD",false);
CkStringBuilder startTime = new CkStringBuilder();
startTime.Append(utcDate);
startTime.Append("T00:00:00Z");
CkStringBuilder endTime = new CkStringBuilder();
endTime.Append(utcDate);
endTime.Append("T23:59:59.999Z");
CkJsonObject query = new CkJsonObject();
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);
CkCsv csv = new CkCsv();
csv.put_HasColumnNames(true);
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");
int row = 0;
int page = 1;
int lastPage = 1;
while ((page <= lastPage)) {
query.UpdateInt("page",page);
success = http.HttpParams("GET","https://api-m.sandbox.paypal.com/v1/reporting/transactions",query,resp);
if (success != true) {
Log.i(TAG, "No HTTP response: " + http.lastErrorText());
return;
}
if ((resp.get_StatusCode() < 200) or (resp.get_StatusCode() >= 300)) {
CkJsonObject errorInfo = new CkJsonObject();
errorInfo.Load(resp.bodyStr());
Log.i(TAG, "HTTP " + String.valueOf(resp.get_StatusCode()) + " " + errorInfo.stringOf("name") + " " + errorInfo.stringOf("details[0].issue"));
Log.i(TAG, "PayPal debug ID: " + errorInfo.stringOf("debug_id"));
return;
}
CkJsonObject result = new CkJsonObject();
boolean parsedResult = result.Load(resp.bodyStr());
if (parsedResult != true) {
Log.i(TAG, "Expected a JSON response; HTTP " + String.valueOf(resp.get_StatusCode()));
return;
}
int i = 0;
int count = result.SizeOfArray("transaction_details");
while ((i < count)) {
result.put_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;
}
lastPage = result.IntOf("total_pages");
page = page + 1;
}
CkStringBuilder filename = new CkStringBuilder();
filename.Append("paypal-activity-");
filename.Append(utcDate);
filename.Append(".csv");
boolean saved = csv.SaveFile2(filename.getAsString(),"utf-8");
if (saved != true) {
Log.i(TAG, csv.lastErrorText());
return;
}
Log.i(TAG, "Exported " + String.valueOf(row) + " rows to " + filename.getAsString());
}
static {
System.loadLibrary("chilkat");
// Note: If the incorrect library name is passed to System.loadLibrary,
// then you will see the following error message at application startup:
//"The application <your-application-name> has stopped unexpectedly. Please try again."
}
}