Paypal to Freeside batch payments

From PrgmrWiki

Freeside can import payments in batches by uploading a csv file, but it can only take customer numbers for who the payment is from instead of invoice numbers. Here is a perl script that takes the csv file from paypal and looks up the customer number and prints a csv file for to upload to freeside. I took the basic structure of this script from

use strict;
use warnings;

use DBI;

my ($file, @fields, $transactiontype, $itemnum, $custnum, $amount, $transactionid, $itemtitle, $name, $fromemail, $date);

my $dbh = DBI->connect("DBI:Pg:dbname=freeside;", "freeside", '', {'RaiseError' => 1});
my $fetchcustnum=$dbh->prepare("SELECT custnum FROM cust_bill WHERE invnum=?;");

open (INPUT, $file)     || die "can't open $file: $!";
while (<INPUT>) {

        if ($transactiontype=~/Payment Received$/) {
                if ($itemtitle=~/pr[mg]{2}r Xen VPS invoice/) {


                print "$custnum, ,$amount,pp#$transactionid\n";
                else { print STDERR "$amount $transactionid payment without an invoice from $name $fromemail on $date\n"; }

        # do something with $_
close(INPUT)            || die "can't close $file: $!";