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

Classic ASP
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
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.
set http = Server.CreateObject("Chilkat.Http")
http.AuthToken = "SANDBOX_ACCESS_TOKEN"
set resp = Server.CreateObject("Chilkat.HttpResponse")
'  Export yesterday in UTC; choose an earlier date if the report is still refreshing.
set day = Server.CreateObject("Chilkat.CkDateTime")
success = day.SetFromCurrentSystemTime()
success = day.AddDays(-1)
utcDate = day.GetAsIso8601("YYYY-MM-DD",0)
set startTime = Server.CreateObject("Chilkat.StringBuilder")
success = startTime.Append(utcDate)
success = startTime.Append("T00:00:00Z")
set endTime = Server.CreateObject("Chilkat.StringBuilder")
success = endTime.Append(utcDate)
success = endTime.Append("T23:59:59.999Z")
set query = Server.CreateObject("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)
set csv = Server.CreateObject("Chilkat.Csv")
csv.HasColumnNames = 1
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")
row = 0
page = 1
lastPage = 1
Do 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 <> 1) Then
        Response.Write "<pre>" & Server.HTMLEncode( "No HTTP response: " & http.LastErrorText) & "</pre>"
        Response.End
    End If

    If ((resp.StatusCode < 200) Or (resp.StatusCode >= 300)) Then
        set errorInfo = Server.CreateObject("Chilkat.JsonObject")
        success = errorInfo.Load(resp.BodyStr)
        Response.Write "<pre>" & Server.HTMLEncode( "HTTP " & resp.StatusCode & " " & errorInfo.StringOf("name") & " " & errorInfo.StringOf("details[0].issue")) & "</pre>"
        Response.Write "<pre>" & Server.HTMLEncode( "PayPal debug ID: " & errorInfo.StringOf("debug_id")) & "</pre>"
        Response.End
    End If

    set result = Server.CreateObject("Chilkat.JsonObject")
    parsedResult = result.Load(resp.BodyStr)
    If (parsedResult <> 1) Then
        Response.Write "<pre>" & Server.HTMLEncode( "Expected a JSON response; HTTP " & resp.StatusCode) & "</pre>"
        Response.End
    End If

    i = 0
    count = result.SizeOfArray("transaction_details")
    Do 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
    Loop
    lastPage = result.IntOf("total_pages")
    page = page + 1
Loop
set filename = Server.CreateObject("Chilkat.StringBuilder")
success = filename.Append("paypal-activity-")
success = filename.Append(utcDate)
success = filename.Append(".csv")
saved = csv.SaveFile2(filename.GetAsString(),"utf-8")
If (saved <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( csv.LastErrorText) & "</pre>"
    Response.End
End If

Response.Write "<pre>" & Server.HTMLEncode( "Exported " & row & " rows to " & filename.GetAsString()) & "</pre>"

%>
</body>
</html>