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

AutoIt
Local $bSuccess = 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.
$oHttp = ObjCreate("Chilkat.Http")
$oHttp.AuthToken = "SANDBOX_ACCESS_TOKEN"
$oResp = ObjCreate("Chilkat.HttpResponse")
;  Export yesterday in UTC; choose an earlier date if the report is still refreshing.
$oDay = ObjCreate("Chilkat.CkDateTime")
$oDay.SetFromCurrentSystemTime()
$oDay.AddDays(-1)
Local $sUtcDate = $oDay.GetAsIso8601("YYYY-MM-DD",False)
$oStartTime = ObjCreate("Chilkat.StringBuilder")
$oStartTime.Append($sUtcDate)
$oStartTime.Append("T00:00:00Z")
$oEndTime = ObjCreate("Chilkat.StringBuilder")
$oEndTime.Append($sUtcDate)
$oEndTime.Append("T23:59:59.999Z")
$oQuery = ObjCreate("Chilkat.JsonObject")
$oQuery.UpdateString("start_date",$oStartTime.GetAsString())
$oQuery.UpdateString("end_date",$oEndTime.GetAsString())
$oQuery.UpdateString("balance_affecting_records_only","Y")
$oQuery.UpdateString("fields","transaction_info")
$oQuery.UpdateInt("page_size",100)
$oCsv = ObjCreate("Chilkat.Csv")
$oCsv.HasColumnNames = True
$oCsv.SetColumnName(0,"transaction_id")
$oCsv.SetColumnName(1,"event_code")
$oCsv.SetColumnName(2,"status")
$oCsv.SetColumnName(3,"currency")
$oCsv.SetColumnName(4,"amount")
$oCsv.SetColumnName(5,"fee_currency")
$oCsv.SetColumnName(6,"fee")
Local $iRow = 0
Local $iPage = 1
Local $iLastPage = 1
While ($iPage <= $iLastPage)
    $oQuery.UpdateInt("page",$iPage)
    $bSuccess = $oHttp.HttpParams("GET","https://api-m.sandbox.paypal.com/v1/reporting/transactions",$oQuery,$oResp)
    If ($bSuccess <> True) Then
        ConsoleWrite("No HTTP response: " & $oHttp.LastErrorText & @CRLF)
        Exit
    EndIf

    If (($oResp.StatusCode < 200) Or ($oResp.StatusCode >= 300)) Then
        $oErrorInfo = ObjCreate("Chilkat.JsonObject")
        $oErrorInfo.Load($oResp.BodyStr)
        ConsoleWrite("HTTP " & $oResp.StatusCode & " " & $oErrorInfo.StringOf("name") & " " & $oErrorInfo.StringOf("details[0].issue") & @CRLF)
        ConsoleWrite("PayPal debug ID: " & $oErrorInfo.StringOf("debug_id") & @CRLF)
        Exit
    EndIf

    $oResult = ObjCreate("Chilkat.JsonObject")
Local $bParsedResult = $oResult.Load($oResp.BodyStr)
    If ($bParsedResult <> True) Then
        ConsoleWrite("Expected a JSON response; HTTP " & $oResp.StatusCode & @CRLF)
        Exit
    EndIf

Local $i = 0
Local $iCount = $oResult.SizeOfArray("transaction_details")
    While ($i < $iCount)
        $oResult.I = $i
        $oCsv.SetCell($iRow,0,$oResult.StringOf("transaction_details[i].transaction_info.transaction_id"))
        $oCsv.SetCell($iRow,1,$oResult.StringOf("transaction_details[i].transaction_info.transaction_event_code"))
        $oCsv.SetCell($iRow,2,$oResult.StringOf("transaction_details[i].transaction_info.transaction_status"))
        $oCsv.SetCell($iRow,3,$oResult.StringOf("transaction_details[i].transaction_info.transaction_amount.currency_code"))
        $oCsv.SetCell($iRow,4,$oResult.StringOf("transaction_details[i].transaction_info.transaction_amount.value"))
        $oCsv.SetCell($iRow,5,$oResult.StringOf("transaction_details[i].transaction_info.fee_amount.currency_code"))
        $oCsv.SetCell($iRow,6,$oResult.StringOf("transaction_details[i].transaction_info.fee_amount.value"))
        $iRow = $iRow + 1
        $i = $i + 1
    Wend
    $iLastPage = $oResult.IntOf("total_pages")
    $iPage = $iPage + 1
Wend
$oFilename = ObjCreate("Chilkat.StringBuilder")
$oFilename.Append("paypal-activity-")
$oFilename.Append($sUtcDate)
$oFilename.Append(".csv")
Local $bSaved = $oCsv.SaveFile2($oFilename.GetAsString(),"utf-8")
If ($bSaved <> True) Then
    ConsoleWrite($oCsv.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite("Exported " & $iRow & " rows to " & $oFilename.GetAsString() & @CRLF)