5 use Date::Format qw(time2str);
6 use File::Temp qw(tempdir);
7 use Net::SFTP::Foreign;
8 use FS::UID qw(adminsuidsetup);
9 use FS::Record qw(qsearch qsearchs);
15 getopts('va:', \%opt);
17 #$Net::SFTP::Foreign::debug = -1;
20 freeside-ipifony-download
23 freesideuser sftpuser@hostname[:path]
34 my $user = shift or die &HELP_MESSAGE;
45 die "no such directory: $opt{a}\n"
47 die "archive directory $opt{a} is not writable by the freeside user\n"
51 #my $tmpdir = File::Temp->newdir();
52 my $tmpdir = tempdir( CLEANUP => 1 ); #DIR=>somewhere?
56 my ($sftpuser, $path);
58 $sftpuser = $1 || $ENV{USER};
62 # for now assume SFTP download as the only method
63 print STDERR "Connecting to $sftpuser\@$host...\n" if $opt{v};
65 my $sftp = Net::SFTP::Foreign->new(
68 # for now we don't support passwords. use authorized_keys.
70 more => ($opt{v} ? '-v' : ''),
72 die "failed to connect to '$sftpuser\@$host'\n(".$sftp->error.")\n"
75 $sftp->setcwd($path) if $path;
77 my $files = $sftp->ls('.', wanted => qr/\.csv$/, names_only => 1);
79 print STDERR "No charge files found.\n" if $opt{v};
82 FILE: foreach my $filename (@$files) {
83 print STDERR "Retrieving $filename\n" if $opt{v};
84 $sftp->get("$filename", "$tmpdir/$filename");
86 warn "failed to download $filename\n";
90 #move to server archive dir
91 $sftp->rename("$filename", "Archive/$filename");
93 warn "failed to archive $filename on server\n";
94 } # process it anyway though
96 #copy to local archive dir
98 print STDERR "Copying $tmpdir/$filename to archive dir $opt{a}\n"
100 copy("$tmpdir/$filename", $opt{a});
101 warn "failed to copy $tmpdir/$filename to $opt{a}: $!" if $!;
104 open my $fh, "<$tmpdir/$filename";
106 if ($header !~ /^cust_id/) {
107 warn "warning: $filename has incorrect header row:\n$header\n";
110 my $csv = Text::CSV->new; # orthodox CSV
112 while (my $line = <$fh>) {
113 $csv->parse($line) or do {
114 warn "can't parse $filename: ".$csv->error_input."\n";
117 @hash{@fields} = $csv->fields();
118 my $cust_main = FS::cust_main->by_key($hash{custnum});
120 warn "customer #$hash{custnum} not found\n";
123 print STDERR "Found customer #$hash{custnum}: ".$cust_main->name."\n"
126 # construct arguments for $cust_main->charge
128 amount => $hash{amount},
129 quantity => $hash{quantity},
130 start_date => $cust_main->next_bill_date,
131 pkg => $hash{date_desc},
133 if (my $classname = $hash{classname}) {
134 if (!exists($classnum_of{$classname}) ) {
136 my $pkg_class = qsearch('pkg_class', { classname => $classname });
137 $classnum_of{$classname} = $pkg_class ? $pkg_class->classnum : '';
139 $opt{classnum} = $classnum_of{$classname};
141 # XXX what's the tax status of these charges?
142 print STDERR " Charging $hash{amount}\n"
144 my $error = $cust_main->charge(\%opt);
146 warn "Error creating charge: $error" if $error;
150 $sum_charges += $hash{amount};
159 Processed files: @$files
160 Created charges: $num_charges
161 Sum of charges: \$".sprintf('%0.2f', $sum_charges)."
168 freeside-eftca-download - Retrieve payment batch responses from EFT Canada.
172 freeside-eftca-download [ -v ] [ -a archivedir ] user
176 Command line tool to download returned payment reports from the EFT Canada
177 gateway and void the returned payments. Uses the login and password from
178 'batchconfig-eft_canada'.
182 -a directory: Archive response files in the provided directory.
184 user: freeside username
188 You need to manually SFTP to ftp.eftcanada.com from the freeside account
189 and accept their key before running this script.