Sample code for 30+ languages & platforms
Lua 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 Lua Downloads

Lua
local chilkat = require("chilkat")

local 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.
local http = chilkat.Http.new()
http.auth_token = "SANDBOX_ACCESS_TOKEN"
local resp = chilkat.HttpResponse.new()
-- Export yesterday in UTC; choose an earlier date if the report is still refreshing.
local day = chilkat.DateTime.new()
day:set_from_current_system_time()
day:add_days(-1)
local utcDate = day:get_as_iso8601("YYYY-MM-DD", false)
local startTime = chilkat.StringBuilder.new()
startTime:append(utcDate)
startTime:append("T00:00:00Z")
local endTime = chilkat.StringBuilder.new()
endTime:append(utcDate)
endTime:append("T23:59:59.999Z")
local query = chilkat.JsonObject.new()
query:update_string("start_date", startTime:get_as_string())
query:update_string("end_date", endTime:get_as_string())
query:update_string("balance_affecting_records_only", "Y")
query:update_string("fields", "transaction_info")
query:update_int("page_size", 100)
local csv = chilkat.Csv.new()
csv.has_column_names = true
csv:set_column_name(0, "transaction_id")
csv:set_column_name(1, "event_code")
csv:set_column_name(2, "status")
csv:set_column_name(3, "currency")
csv:set_column_name(4, "amount")
csv:set_column_name(5, "fee_currency")
csv:set_column_name(6, "fee")
local row = 0
local page = 1
local lastPage = 1
while page <= lastPage do
    query:update_int("page", page)
    success = http:http_params("GET", "https://api-m.sandbox.paypal.com/v1/reporting/transactions", query, resp)
    if not success then
        print("No HTTP response: " .. http.last_error_text)
        return
    end

    if (resp.status_code < 200) or (resp.status_code >= 300) then
        local errorInfo = chilkat.JsonObject.new()
        errorInfo:load(resp.body_str)
        print("HTTP " .. resp.status_code .. " " .. errorInfo:string_of("name") .. " " .. errorInfo:string_of("details[0].issue"))
        print("PayPal debug ID: " .. errorInfo:string_of("debug_id"))
        return
    end

    local result = chilkat.JsonObject.new()
    local parsedResult = result:load(resp.body_str)
    if not parsedResult then
        print("Expected a JSON response; HTTP " .. resp.status_code)
        return
    end

    local i = 0
    local count = result:size_of_array("transaction_details")
    while i < count do
        result.i = i
        csv:set_cell(row, 0, result:string_of("transaction_details[i].transaction_info.transaction_id"))
        csv:set_cell(row, 1, result:string_of("transaction_details[i].transaction_info.transaction_event_code"))
        csv:set_cell(row, 2, result:string_of("transaction_details[i].transaction_info.transaction_status"))
        csv:set_cell(row, 3, result:string_of("transaction_details[i].transaction_info.transaction_amount.currency_code"))
        csv:set_cell(row, 4, result:string_of("transaction_details[i].transaction_info.transaction_amount.value"))
        csv:set_cell(row, 5, result:string_of("transaction_details[i].transaction_info.fee_amount.currency_code"))
        csv:set_cell(row, 6, result:string_of("transaction_details[i].transaction_info.fee_amount.value"))
        row = row + 1
        i = i + 1
    end

    lastPage = result:int_of("total_pages")
    page = page + 1
end

local filename = chilkat.StringBuilder.new()
filename:append("paypal-activity-")
filename:append(utcDate)
filename:append(".csv")
local saved = csv:save_file2(filename:get_as_string(), "utf-8")
if not saved then
    print(csv.last_error_text)
    return
end

print("Exported " .. row .. " rows to " .. filename:get_as_string())