Generate Microsoft Money, Quicken, Quickbooks QIF File from PayPal Account Transactions


I finally got annoyed enough with hand entering transactions into PayPal that I decided to throw together a basic QIF generator similar to what you get when you download transactions from a bank web site. PayPal’s exporting features just don’t do the trick in my opinion.

I used my NVP Class for PayPal so if you don’t have that already you’ll need it to use this script. Just make sure to add your API credentials into it.

This is very basic at the moment but it’s already saved me lots of time because it splits out the fees and sets the categories for them. It also adds the PayPal transaction ID to the notes of the imported transaction. You may want to add some of your own logic to set different categories based on the other transaction data. There’s a variable called $DaysBack that you can hard-code in the script or form post to so you can set how many days back you want to the script to grab transactions for.

I based this off a .qif I got from Bank of America so it should work pretty smoothly all-around. Let me know otherwise.

Download Zip File

<?php
require_once('paypal.nvp.class.php');

# Create PayPal object
$PayPalConfig = array('Sandbox' => $sandbox);
$PayPal = new PayPalPro($PayPalConfig);

# Generate Start/End Dates
$DaysBack = isset($_POST['DaysBack']) ? $_POST['DaysBack'] : 1;
$Timestamp = strtotime('now -' . $DaysBack . ' days');
$StartDate = gmdate('Y-m-d\TH:i:s.00\Z', $Timestamp);
$Timestamp = strtotime('now');
$EndDate = gmdate('Y-m-d\TH:i:s.00\Z', $Timestamp);

# Send request to PayPal APIv
$TSFields = array(
					'startdate' => $StartDate, 							// Required.  The earliest transaction date you want returned.  Must be in UTC/GMT format.  2008-08-30T05:00:00.00Z
					'enddate' => $EndDate, 								// The latest transaction date you want to be included.
					'status' => 'Success' 								// Search by transaction status.  Possible values: Pending, Processing, Success, Denied, Reversed
				);

$TransactionSearchData = array('TSFields' => $TSFields);
$TSResult = $PayPal -> TransactionSearch($TransactionSearchData);

# Display any errors returned from PayPal
$Errors = $TSResult['ERRORS'];
if(count($Errors) > 0)
{
	echo '<pre />';
	print_r($Errors);
}

# Store transactions in $Transactions
$Transactions = isset($TSResult['SEARCHRESULTS']) ? $TSResult['SEARCHRESULTS'] : array();

/*
Generate QIF file for MS Money

D 	Date
T 	Amount
C 	Cleared status
N 	Num (check or reference number)
P 	Payee
M 	Memo
A 	Address (up to five lines; the sixth line is an optional message)
L 	Category (Category/Subcategory/Transfer/Class)
S 	Category in split (Category/Transfer/Class)
E 	Memo in split
$ 	Dollar amount of split
^ 	End of entry
*/
$qif = '!Type:Bank' . chr(10);

foreach($Transactions as $index => $Transaction)
{
	$Timestamp = $Transaction['L_TIMESTAMP'];
	$Type = $Transaction['L_TYPE'];
	$Email = $Transaction['L_EMAIL'];
	$Name = $Transaction['L_NAME'];
	$TransactionID = $Transaction['L_TRANSACTIONID'];
	$Status = $Transaction['L_STATUS'];
	$Amt = $Transaction['L_AMT'];
	$FeeAmt = $Transaction['L_FEEAMT'];
	$NetAmt = $Transaction['L_NETAMT'];

	# Reformat Date
	$Date = substr($Timestamp, 0, 10);
	$DateSplit = explode('-',$Date);
	$DateYear = $DateSplit[0];
	$DateMo = $DateSplit[1];
	$DateDay = $DateSplit[2];
	$Date = $DateMo . '/' . $DateDay . '\'' . $DateYear;

	# You may want some logic here to set category and notes depending on the other transaction data.
	$Category = 'Sales Income';
	$Notes = 'PayPal Trans: ' . $TransactionID;

	# Add transaction
	$qif .= 'D' . $Date . chr(10) .
			'T' . $Amt . chr(10) .
			'CX' . chr(10) .
			'P' . $Name . chr(10) .
			'L' . $Category . chr(10) .
			'M' . $Notes . chr(10) .
			'^' . chr(10);

	# If a fee was included, add it separately.
	if($FeeAmt < 0)
	{
		$qif .= 'D' . $Date . chr(10) .
				'T' . $FeeAmt . chr(10) .
				'CX' . chr(10) .
				'PPayPal' . chr(10) .
				'LPayPal Fee' . chr(10) .
				'MPayPal Fee' . chr(10) .
				'^' . chr(10);
	}
}

# Write final .qif file to disk.
$qif_file_path = mktime() . '.qif';
$qif_file = fopen($qif_file_path, 'w') or die ('Can\'t open file.');
fwrite($qif_file, $qif);
fclose($qif_file);

# Show download link
echo '<a href="' . $qif_file_path . '">Download QIF</a>';
?>
Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Reddit
  • Slashdot
  • TwitThis
  1. No comments yet.
(will not be published)

Spam Protection by WP-SpamFree

  1. No trackbacks yet.