Accounting with PayPal Data: Translate PayPal API to QIF

From:

Cached here as of 2010/03/30 11:20:00

<?php
002.
require_once('paypal.nvp.class.php');
003.
 
004.
# Create PayPal object
005.
$PayPalConfig = array('Sandbox' => $sandbox);
006.
$PayPal = new PayPalPro($PayPalConfig);
007.
 
008.
# Generate Start/End Dates
009.
$DaysBack = isset($_POST['DaysBack']) ? $_POST['DaysBack'] : 1;
010.
$Timestamp = strtotime('now -' . $DaysBack . ' days');
011.
$StartDate = gmdate('Y-m-d\TH:i:s.00\Z', $Timestamp);
012.
$Timestamp = strtotime('now');
013.
$EndDate = gmdate('Y-m-d\TH:i:s.00\Z', $Timestamp);
014.
 
015.
# Send request to PayPal APIv
016.
$TSFields = array(
017.
'startdate' => $StartDate,                           // Required.  The earliest transaction date you want returned.  Must be in UTC/GMT format.  2008-08-30T05:00:00.00Z
018.
'enddate' => $EndDate,                               // The latest transaction date you want to be included.
019.
'status' => 'Success'                                // Search by transaction status.  Possible values: Pending, Processing, Success, Denied, Reversed
020.
);
021.
 
022.
$TransactionSearchData = array('TSFields' => $TSFields);
023.
$TSResult = $PayPal -> TransactionSearch($TransactionSearchData);
024.
 
025.
# Display any errors returned from PayPal
026.
$Errors = $TSResult['ERRORS'];
027.
if(count($Errors) > 0)
028.
{
029.
echo '<pre />';
030.
print_r($Errors);
031.
}
032.
 
033.
# Store transactions in $Transactions
034.
$Transactions = isset($TSResult['SEARCHRESULTS']) ? $TSResult['SEARCHRESULTS'] : array();
035.
 
036.
/*
037.
Generate QIF file for MS Money
038.
 
039.
D   Date
040.
T   Amount
041.
C   Cleared status
042.
N   Num (check or reference number)
043.
P   Payee
044.
M   Memo
045.
A   Address (up to five lines; the sixth line is an optional message)
046.
L   Category (Category/Subcategory/Transfer/Class)
047.
S   Category in split (Category/Transfer/Class)
048.
E   Memo in split
049.
$   Dollar amount of split
050.
^   End of entry
051.
*/
052.
$qif = '!Type:Bank' . chr(10);
053.
 
054.
foreach($Transactions as $index => $Transaction)
055.
{
056.
$Timestamp = $Transaction['L_TIMESTAMP'];
057.
$Type = $Transaction['L_TYPE'];
058.
$Email = $Transaction['L_EMAIL'];
059.
$Name = $Transaction['L_NAME'];
060.
$TransactionID = $Transaction['L_TRANSACTIONID'];
061.
$Status = $Transaction['L_STATUS'];
062.
$Amt = $Transaction['L_AMT'];
063.
$FeeAmt = $Transaction['L_FEEAMT'];
064.
$NetAmt = $Transaction['L_NETAMT'];
065.
 
066.
# Reformat Date
067.
$Date = substr($Timestamp, 0, 10);
068.
$DateSplit = explode('-',$Date);
069.
$DateYear = $DateSplit[0];
070.
$DateMo = $DateSplit[1];
071.
$DateDay = $DateSplit[2];
072.
$Date = $DateMo . '/' . $DateDay . '\'' . $DateYear;
073.
 
074.
# You may want some logic here to set category and notes depending on the other transaction data.
075.
$Category = 'Sales Income';
076.
$Notes = 'PayPal Trans: ' . $TransactionID;
077.
 
078.
# Add transaction
079.
$qif .= 'D' . $Date . chr(10) .
080.
'T' . $Amt . chr(10) .
081.
'CX' . chr(10) .
082.
'P' . $Name . chr(10) .
083.
'L' . $Category . chr(10) .
084.
'M' . $Notes . chr(10) .
085.
'^' . chr(10);
086.
 
087.
# If a fee was included, add it separately.
088.
if($FeeAmt < 0)
089.
{
090.
$qif .= 'D' . $Date . chr(10) .
091.
'T' . $FeeAmt . chr(10) .
092.
'CX' . chr(10) .
093.
'PPayPal' . chr(10) .
094.
'LPayPal Fee' . chr(10) .
095.
'MPayPal Fee' . chr(10) .
096.
'^' . chr(10);
097.
}
098.
}
099.
 
100.
# Write final .qif file to disk.
101.
$qif_file_path = mktime() . '.qif';
102.
$qif_file = fopen($qif_file_path, 'w') or die ('Can\'t open file.');
103.
fwrite($qif_file, $qif);
104.
fclose($qif_file);
105.
 
106.
# Show download link
107.
echo '<a href="' . $qif_file_path . '">Download QIF</a>';
108.
?>