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

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.

PayPal documentation

Chilkat Java Downloads

Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Csv;
import com.chilkatsoft.chilkat2.DateTime;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        // 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 http = new Http();
        http.setAuthToken("SANDBOX_ACCESS_TOKEN");
        HttpResponse resp = new HttpResponse();
        // Export yesterday in UTC; choose an earlier date if the report is still refreshing.
        DateTime day = new DateTime();
        day.setFromCurrentSystemTime();
        day.addDays(-1);
        String utcDate = day.getAsIso8601("YYYY-MM-DD", false);
        StringBuilder startTime = new StringBuilder();
        startTime.append(utcDate);
        startTime.append("T00:00:00Z");
        StringBuilder endTime = new StringBuilder();
        endTime.append(utcDate);
        endTime.append("T23:59:59.999Z");
        JsonObject query = new JsonObject();
        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 csv = new Csv();
        csv.setHasColumnNames(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);
            try {
                http.httpParams("GET", "https://api-m.sandbox.paypal.com/v1/reporting/transactions", query, resp);
            } catch (ChilkatException e) {
                System.out.println("No HTTP response: " + e.getLastErrorText());
                return;
            }

            if ((resp.getStatusCode() < 200) || (resp.getStatusCode() >= 300)) {
                JsonObject errorInfo = new JsonObject();
                errorInfo.load(resp.getBodyStr());
                System.out.println("HTTP " + resp.getStatusCode() + " " + errorInfo.stringOf("name") + " " + errorInfo.stringOf("details[0].issue"));
                System.out.println("PayPal debug ID: " + errorInfo.stringOf("debug_id"));
                return;
            }

            JsonObject result = new JsonObject();
            try {
                result.load(resp.getBodyStr());
            } catch (ChilkatException e) {
                System.out.println("Expected a JSON response; HTTP " + resp.getStatusCode());
                return;
            }

            int i = 0;
            int count = result.sizeOfArray("transaction_details");
            while (i < count) {
                result.setI(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;
        }

        StringBuilder filename = new StringBuilder();
        filename.append("paypal-activity-");
        filename.append(utcDate);
        filename.append(".csv");
        try {
            csv.saveFile2(filename.getAsString(), "utf-8");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("Exported " + row + " rows to " + filename.getAsString());
    }
}