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:P:', \%opt);
17 #$Net::SFTP::Foreign::debug = -1;
20 freeside-ipifony-download
24 freesideuser sftpuser@hostname[:path]
35 my $user = shift or die &HELP_MESSAGE;
46 die "no such directory: $opt{a}\n"
48 die "archive directory $opt{a} is not writable by the freeside user\n"
52 #my $tmpdir = File::Temp->newdir();
53 my $tmpdir = tempdir( CLEANUP => 1 ); #DIR=>somewhere?
57 my ($sftpuser, $path);
59 $sftpuser = $1 || $ENV{USER};
64 if ( $opt{P} =~ /^(\d+)$/ ) {
68 # for now assume SFTP download as the only method
69 print STDERR "Connecting to $sftpuser\@$host...\n" if $opt{v};
71 my $sftp = Net::SFTP::Foreign->new(
75 # for now we don't support passwords. use authorized_keys.
77 more => ($opt{v} ? '-v' : ''),
79 die "failed to connect to '$sftpuser\@$host'\n(".$sftp->error.")\n"
82 $sftp->setcwd($path) if $path;
84 my $files = $sftp->ls('.', wanted => qr/\.csv$/, names_only => 1);
86 print STDERR "No charge files found.\n" if $opt{v};
89 FILE: foreach my $filename (@$files) {
90 print STDERR "Retrieving $filename\n" if $opt{v};
91 $sftp->get("$filename", "$tmpdir/$filename");
93 warn "failed to download $filename\n";
97 # make sure server archive dir exists
98 if ( !$sftp->stat('Archive') ) {
99 print STDERR "Creating $path/Archive\n" if $opt{v};
100 $sftp->mkdir('Archive');
102 # something is seriously wrong
103 die "failed to create archive directory on server:\n".$sftp->error."\n";
106 #move to server archive dir
107 $sftp->rename("$filename", "Archive/$filename");
109 warn "failed to archive $filename on server:\n".$sftp->error."\n";
110 } # process it anyway, I guess/
112 #copy to local archive dir
114 print STDERR "Copying $tmpdir/$filename to archive dir $opt{a}\n"
116 copy("$tmpdir/$filename", $opt{a});
117 warn "failed to copy $tmpdir/$filename to $opt{a}: $!" if $!;
120 open my $fh, "<$tmpdir/$filename";
122 if ($header !~ /^cust_id/) {
123 warn "warning: $filename has incorrect header row:\n$header\n";
126 my $csv = Text::CSV->new; # orthodox CSV
128 while (my $line = <$fh>) {
129 $csv->parse($line) or do {
130 warn "can't parse $filename: ".$csv->error_input."\n";
133 @hash{@fields} = $csv->fields();
134 my $cust_main = FS::cust_main->by_key($hash{custnum});
136 warn "customer #$hash{custnum} not found\n";
139 print STDERR "Found customer #$hash{custnum}: ".$cust_main->name."\n"
142 # construct arguments for $cust_main->charge
144 amount => $hash{amount},
145 quantity => $hash{quantity},
146 start_date => $cust_main->next_bill_date,
147 pkg => $hash{date_desc},
149 if (my $classname = $hash{classname}) {
150 if (!exists($classnum_of{$classname}) ) {
152 my $pkg_class = qsearch('pkg_class', { classname => $classname });
153 $classnum_of{$classname} = $pkg_class ? $pkg_class->classnum : '';
155 $opt{classnum} = $classnum_of{$classname};
157 # XXX what's the tax status of these charges?
158 print STDERR " Charging $hash{amount}\n"
160 my $error = $cust_main->charge(\%opt);
162 warn "Error creating charge: $error" if $error;
166 $sum_charges += $hash{amount};
175 Processed files: @$files
176 Created charges: $num_charges
177 Sum of charges: \$".sprintf('%0.2f', $sum_charges)."
184 freeside-eftca-download - Retrieve payment batch responses from EFT Canada.
188 freeside-eftca-download [ -v ] [ -a archivedir ] user
192 Command line tool to download returned payment reports from the EFT Canada
193 gateway and void the returned payments. Uses the login and password from
194 'batchconfig-eft_canada'.
198 -a directory: Archive response files in the provided directory.
200 user: freeside username
204 You need to manually SFTP to ftp.eftcanada.com from the freeside account
205 and accept their key before running this script.