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

Xojo Plugin
Dim success As 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.
Dim http As New Chilkat.Http
http.AuthToken = "SANDBOX_ACCESS_TOKEN"
Dim resp As New Chilkat.HttpResponse
//  Export yesterday in UTC; choose an earlier date if the report is still refreshing.
Dim day As New Chilkat.CkDateTime
success = day.SetFromCurrentSystemTime()
success = day.AddDays(-1)
Dim utcDate As String
utcDate = day.GetAsIso8601("YYYY-MM-DD",False)
Dim startTime As New Chilkat.StringBuilder
success = startTime.Append(utcDate)
success = startTime.Append("T00:00:00Z")
Dim endTime As New Chilkat.StringBuilder
success = endTime.Append(utcDate)
success = endTime.Append("T23:59:59.999Z")
Dim query As New Chilkat.JsonObject
success = query.UpdateString("start_date",startTime.GetAsString())
success = query.UpdateString("end_date",endTime.GetAsString())
success = query.UpdateString("balance_affecting_records_only","Y")
success = query.UpdateString("fields","transaction_info")
success = query.UpdateInt("page_size",100)
Dim csv As New Chilkat.Csv
csv.HasColumnNames = True
success = csv.SetColumnName(0,"transaction_id")
success = csv.SetColumnName(1,"event_code")
success = csv.SetColumnName(2,"status")
success = csv.SetColumnName(3,"currency")
success = csv.SetColumnName(4,"amount")
success = csv.SetColumnName(5,"fee_currency")
success = csv.SetColumnName(6,"fee")
Dim row As Int32
row = 0
Dim page As Int32
page = 1
Dim lastPage As Int32
lastPage = 1
While (page <= lastPage)
    success = query.UpdateInt("page",page)
    success = http.HttpParams("GET","https://api-m.sandbox.paypal.com/v1/reporting/transactions",query,resp)
    If (success <> True) Then
        System.DebugLog("No HTTP response: " + http.LastErrorText)
        Return
    End If

    If ((resp.StatusCode < 200) Or (resp.StatusCode >= 300)) Then
        Dim errorInfo As New Chilkat.JsonObject
        success = errorInfo.Load(resp.BodyStr)
        System.DebugLog("HTTP " + Str(resp.StatusCode) + " " + errorInfo.StringOf("name") + " " + errorInfo.StringOf("details[0].issue"))
        System.DebugLog("PayPal debug ID: " + errorInfo.StringOf("debug_id"))
        Return
    End If

    Dim result As New Chilkat.JsonObject
    Dim parsedResult As Boolean
    parsedResult = result.Load(resp.BodyStr)
    If (parsedResult <> True) Then
        System.DebugLog("Expected a JSON response; HTTP " + Str(resp.StatusCode))
        Return
    End If

    Dim i As Int32
    i = 0
    Dim count As Int32
    count = result.SizeOfArray("transaction_details")
    While (i < count)
        result.I = i
        success = csv.SetCell(row,0,result.StringOf("transaction_details[i].transaction_info.transaction_id"))
        success = csv.SetCell(row,1,result.StringOf("transaction_details[i].transaction_info.transaction_event_code"))
        success = csv.SetCell(row,2,result.StringOf("transaction_details[i].transaction_info.transaction_status"))
        success = csv.SetCell(row,3,result.StringOf("transaction_details[i].transaction_info.transaction_amount.currency_code"))
        success = csv.SetCell(row,4,result.StringOf("transaction_details[i].transaction_info.transaction_amount.value"))
        success = csv.SetCell(row,5,result.StringOf("transaction_details[i].transaction_info.fee_amount.currency_code"))
        success = csv.SetCell(row,6,result.StringOf("transaction_details[i].transaction_info.fee_amount.value"))
        row = row + 1
        i = i + 1
    Wend
    lastPage = result.IntOf("total_pages")
    page = page + 1
Wend
Dim filename As New Chilkat.StringBuilder
success = filename.Append("paypal-activity-")
success = filename.Append(utcDate)
success = filename.Append(".csv")
Dim saved As Boolean
saved = csv.SaveFile2(filename.GetAsString(),"utf-8")
If (saved <> True) Then
    System.DebugLog(csv.LastErrorText)
    Return
End If

System.DebugLog("Exported " + Str(row) + " rows to " + filename.GetAsString())