don't even send an empty avsCountryCode for non-US/CA/GB/UK country, RT#21415
[freeside.git] / FS / FS / pay_batch / paymentech.pm
1 package FS::pay_batch::paymentech;
2
3 use strict;
4 use vars qw(@ISA %import_info %export_info $name);
5 use FS::Record 'qsearchs';
6 use Time::Local;
7 use Date::Format 'time2str';
8 use Date::Parse 'str2time';
9 use Tie::IxHash;
10 use FS::Conf;
11
12 my $conf;
13 my ($bin, $merchantID, $terminalID, $username, $password, $with_recurringInd);
14 $name = 'paymentech';
15
16 my $gateway;
17
18 %import_info = (
19   filetype    => 'XML',
20   xmlrow         => [ qw(transResponse newOrderResp) ],
21   fields      => [
22     'paybatchnum',
23     '_date',
24     'approvalStatus',
25     'order_number',
26     'auth',
27     'procStatus',
28     'procStatusMessage',
29     'respCodeMessage',
30     ],
31   xmlkeys     => [
32     'orderID',
33     'respDateTime',
34     'approvalStatus',
35     'txRefNum',
36     'authorizationCode',
37     'procStatus',
38     'procStatusMessage',
39     'respCodeMessage',
40     ],
41   'hook'        => sub {
42       if ( !$gateway ) {
43         # find a gateway configuration that has the same merchantID 
44         # as the batch config, if there is one.  If not, leave 
45         # gateway out entirely.
46         my $merchant = (FS::Conf->new->config('batchconfig-paymentech'))[2];
47         $gateway = qsearchs({
48               'table'     => 'payment_gateway',
49               'addl_from' => ' JOIN payment_gateway_option USING (gatewaynum) ',
50               'hashref'   => {  disabled    => '',
51                                 optionname  => 'merchant_id',
52                                 optionvalue => $merchant,
53                               },
54               });
55       }
56       my ($hash, $oldhash) = @_;
57       $hash->{'gatewaynum'} = $gateway->gatewaynum if $gateway;
58       $hash->{'processor'} = 'PaymenTech';
59       my ($mon, $day, $year, $hour, $min, $sec) = 
60         $hash->{'_date'} =~ /^(..)(..)(....)(..)(..)(..)$/;
61       $hash->{'_date'} = timelocal($sec, $min, $hour, $day, $mon-1, $year);
62       $hash->{'paid'} = $oldhash->{'amount'};
63
64       my $gateway_label = $hash->{'processor'};
65       $gateway_label = $hash->{'gatewaynum'} . '-' . $gateway_label
66         if $hash->{'gatewaynum'};
67       $hash->{'paybatch'} = join(':',
68         $gateway_label,
69         $hash->{'auth'},
70         $hash->{'order_number'},
71       );
72       if ( $hash->{'procStatus'} == 0 ) {
73         $hash->{'error_message'} = $hash->{'respCodeMessage'};
74       } else {
75         $hash->{'error_message'} = $hash->{'procStatusMessage'};
76       }
77     },
78   'approved'    => sub { my $hash = shift;
79                             $hash->{'approvalStatus'} 
80     },
81   'declined'    => sub { my $hash = shift;
82                             ! $hash->{'approvalStatus'} 
83     },
84 );
85
86 my %paytype = (
87   'personal checking' => 'C',
88   'personal savings'  => 'S',
89   'business checking' => 'X',
90   'business savings'  => 'X',
91 );
92
93 my %paymentech_countries = map { $_ => 1 } qw( US CA GB UK );
94
95 %export_info = (
96   init  => sub {
97 # Load this at run time
98     eval "use XML::Writer";
99     die $@ if $@;
100     my $conf = shift;
101     ($bin, $terminalID, $merchantID, $username, $password, $with_recurringInd) =
102        $conf->config('batchconfig-paymentech');
103     },
104 # Here we do all the work in the header function.
105   header => sub {
106     my $pay_batch = shift;
107     my @cust_pay_batch = @{(shift)};
108     my $count = 1;
109     my $output;
110     my $xml = new XML::Writer(OUTPUT => \$output, DATA_MODE => 1, DATA_INDENT => 2);
111     $xml->startTag('transRequest', RequestCount => scalar(@cust_pay_batch) + 1);
112     $xml->startTag('batchFileID');
113     $xml->dataElement(userID => $username);
114     $xml->dataElement(fileDateTime => time2str('%Y%m%d%H%M%S', time));
115     $xml->dataElement(fileID => 'FILEID');
116     $xml->endTag('batchFileID');
117
118     foreach (@cust_pay_batch) {
119       $xml->startTag('newOrder', BatchRequestNo => $count++);
120       my $status = $_->cust_main->status;
121       tie my %order, 'Tie::IxHash', (
122         industryType    => 'EC',
123         transType       => 'AC',
124         bin             => $bin,
125         merchantID      => $merchantID,
126         terminalID      => $terminalID,
127         ($_->payby eq 'CARD') ? (
128           ccAccountNum    => $_->payinfo,
129           ccExp           => $_->expmmyy,
130         ) : (
131           ecpCheckRT      => ($_->payinfo =~ /@(\d+)/),
132           ecpCheckDDA     => ($_->payinfo =~ /(\d+)@/),
133           ecpBankAcctType => $paytype{lc($_->cust_main->paytype)},
134           ecpDelvMethod   => 'A',
135         ),
136         avsZip          => substr($_->zip,      0, 10),
137         avsAddress1     => substr($_->address1, 0, 30),
138         avsAddress2     => substr($_->address2, 0, 30),
139         avsCity         => substr($_->city,     0, 20),
140         avsState        => substr($_->state,    0, 2),
141         avsName         => substr($_->first. ' '. $_->last, 0, 30),
142         ( $paymentech_countries{ $_->country }
143           ? ( avsCountryCode  => $_->country )
144           : ()
145         ),
146         orderID           => $_->paybatchnum,
147         amount            => $_->amount * 100,
148         );
149       # only do this if recurringInd is enabled in config, 
150       # and the customer has at least one non-canceled recurring package
151       if ( $with_recurringInd and $status =~ /^active|suspended|ordered$/ ) {
152         # then send RF if this is the first payment on this payinfo,
153         # RS otherwise.
154         $order{'recurringInd'} = $_->payinfo_used ? 'RS' : 'RF';
155       }
156       foreach my $key (keys %order) {
157         $xml->dataElement($key, $order{$key})
158       }
159       $xml->endTag('newOrder');
160     }
161     $xml->startTag('endOfDay', BatchRequestNo => $count);
162     $xml->dataElement(bin => $bin);
163     $xml->dataElement(merchantID => $merchantID);
164     $xml->dataElement(terminalID => $terminalID);
165     $xml->endTag('endOfDay');
166     $xml->endTag('transRequest');
167     return $output;
168   },
169   row => sub {},
170 );
171
172 1;
173