1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
#!/usr/bin/perl
use strict;
use Getopt::Std;
use Date::Format qw(time2str);
use File::Temp qw(tempdir);
use Net::SFTP::Foreign;
use FS::UID qw(adminsuidsetup);
use FS::Record qw(qsearch qsearchs);
use FS::cust_main;
use FS::Conf;
use Text::CSV;
my %opt;
getopts('va:P:', \%opt);
#$Net::SFTP::Foreign::debug = -1;
sub HELP_MESSAGE { '
Usage:
freeside-ipifony-download
[ -v ]
[ -a archivedir ]
[ -P port ]
freesideuser sftpuser@hostname[:path]
' }
my @fields = (
'custnum',
'date_desc',
'quantity',
'amount',
'classname',
);
my $user = shift or die &HELP_MESSAGE;
adminsuidsetup $user;
# for statistics
my $num_charges = 0;
my $num_errors = 0;
my $sum_charges = 0;
# cache classnums
my %classnum_of;
if ( $opt{a} ) {
die "no such directory: $opt{a}\n"
unless -d $opt{a};
die "archive directory $opt{a} is not writable by the freeside user\n"
unless -w $opt{a};
}
#my $tmpdir = File::Temp->newdir();
my $tmpdir = tempdir( CLEANUP => 1 ); #DIR=>somewhere?
my $host = shift
or die &HELP_MESSAGE;
my ($sftpuser, $path);
$host =~ s/^(.+)\@//;
$sftpuser = $1 || $ENV{USER};
$host =~ s/:(.*)//;
$path = $1;
my $port = 22;
if ( $opt{P} =~ /^(\d+)$/ ) {
$port = $1;
}
# for now assume SFTP download as the only method
print STDERR "Connecting to $sftpuser\@$host...\n" if $opt{v};
my $sftp = Net::SFTP::Foreign->new(
host => $host,
user => $sftpuser,
port => $port,
# for now we don't support passwords. use authorized_keys.
timeout => 30,
more => ($opt{v} ? '-v' : ''),
);
die "failed to connect to '$sftpuser\@$host'\n(".$sftp->error.")\n"
if $sftp->error;
$sftp->setcwd($path) if $path;
my $files = $sftp->ls('.', wanted => qr/\.csv$/, names_only => 1);
if (!@$files) {
print STDERR "No charge files found.\n" if $opt{v};
exit(-1);
}
FILE: foreach my $filename (@$files) {
print STDERR "Retrieving $filename\n" if $opt{v};
$sftp->get("$filename", "$tmpdir/$filename");
if($sftp->error) {
warn "failed to download $filename\n";
next FILE;
}
# make sure server archive dir exists
if ( !$sftp->stat('Archive') ) {
print STDERR "Creating $path/Archive\n" if $opt{v};
$sftp->mkdir('Archive');
if($sftp->error) {
# something is seriously wrong
die "failed to create archive directory on server:\n".$sftp->error."\n";
}
}
#move to server archive dir
$sftp->rename("$filename", "Archive/$filename");
if($sftp->error) {
warn "failed to archive $filename on server:\n".$sftp->error."\n";
} # process it anyway, I guess/
#copy to local archive dir
if ( $opt{a} ) {
print STDERR "Copying $tmpdir/$filename to archive dir $opt{a}\n"
if $opt{v};
copy("$tmpdir/$filename", $opt{a});
warn "failed to copy $tmpdir/$filename to $opt{a}: $!" if $!;
}
open my $fh, "<$tmpdir/$filename";
my $header = <$fh>;
if ($header !~ /^cust_id/) {
warn "warning: $filename has incorrect header row:\n$header\n";
# but try anyway
}
my $csv = Text::CSV->new; # orthodox CSV
my %hash;
while (my $line = <$fh>) {
$csv->parse($line) or do {
warn "can't parse $filename: ".$csv->error_input."\n";
next FILE;
};
@hash{@fields} = $csv->fields();
my $cust_main = FS::cust_main->by_key($hash{custnum});
if (!$cust_main) {
warn "customer #$hash{custnum} not found\n";
next;
}
print STDERR "Found customer #$hash{custnum}: ".$cust_main->name."\n"
if $opt{v};
# construct arguments for $cust_main->charge
my %opt = (
amount => $hash{amount},
quantity => $hash{quantity},
start_date => $cust_main->next_bill_date,
pkg => $hash{date_desc},
);
if (my $classname = $hash{classname}) {
if (!exists($classnum_of{$classname}) ) {
# then look it up
my $pkg_class = qsearch('pkg_class', { classname => $classname });
$classnum_of{$classname} = $pkg_class ? $pkg_class->classnum : '';
}
$opt{classnum} = $classnum_of{$classname};
}
# XXX what's the tax status of these charges?
print STDERR " Charging $hash{amount}\n"
if $opt{v};
my $error = $cust_main->charge(\%opt);
if ($error) {
warn "Error creating charge: $error" if $error;
$num_errors++;
} else {
$num_charges++;
$sum_charges += $hash{amount};
}
} #while $line
close $fh;
} #FILE
if ($opt{v}) {
print STDERR "
Finished!
Processed files: @$files
Created charges: $num_charges
Sum of charges: \$".sprintf('%0.2f', $sum_charges)."
Errors: $num_errors
";
}
=head1 NAME
freeside-eftca-download - Retrieve payment batch responses from EFT Canada.
=head1 SYNOPSIS
freeside-eftca-download [ -v ] [ -a archivedir ] user
=head1 DESCRIPTION
Command line tool to download returned payment reports from the EFT Canada
gateway and void the returned payments. Uses the login and password from
'batchconfig-eft_canada'.
-v: Be verbose.
-a directory: Archive response files in the provided directory.
user: freeside username
=head1 BUGS
You need to manually SFTP to ftp.eftcanada.com from the freeside account
and accept their key before running this script.
=head1 SEE ALSO
L<FS::pay_batch>
=cut
1;
|