97055037851cf8c0edbdb365ef7a759f7ca7a067
[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       $hash->{'paybatch'} = join(':',
64         $gateway->gatewaynum . '-PaymenTech',
65         $hash->{'auth'},
66         $hash->{'order_number'},
67       );
68       if ( $hash->{'procStatus'} == 0 ) {
69         $hash->{'error_message'} = $hash->{'respCodeMessage'};
70       } else {
71         $hash->{'error_message'} = $hash->{'procStatusMessage'};
72       }
73     },
74   'approved'    => sub { my $hash = shift;
75                             $hash->{'approvalStatus'} 
76     },
77   'declined'    => sub { my $hash = shift;
78                             ! $hash->{'approvalStatus'} 
79     },
80 );
81
82 my %paytype = (
83   'personal checking' => 'C',
84   'personal savings'  => 'S',
85   'business checking' => 'X',
86   'business savings'  => 'X',
87 );
88
89 my %paymentech_countries = map { $_ => 1 } qw( US CA GB UK );
90
91 %export_info = (
92   init  => sub {
93 # Load this at run time
94     eval "use XML::Writer";
95     die $@ if $@;
96     my $conf = shift;
97     ($bin, $terminalID, $merchantID, $username, $password, $with_recurringInd) =
98        $conf->config('batchconfig-paymentech');
99     },
100 # Here we do all the work in the header function.
101   header => sub {
102     my $pay_batch = shift;
103     my @cust_pay_batch = @{(shift)};
104     my $count = 1;
105     my $output;
106     my $xml = new XML::Writer(OUTPUT => \$output, DATA_MODE => 1, DATA_INDENT => 2);
107     $xml->startTag('transRequest', RequestCount => scalar(@cust_pay_batch) + 1);
108     $xml->startTag('batchFileID');
109     $xml->dataElement(userID => $username);
110     $xml->dataElement(fileDateTime => time2str('%Y%m%d%H%M%S', time));
111     $xml->dataElement(fileID => 'FILEID');
112     $xml->endTag('batchFileID');
113
114     foreach (@cust_pay_batch) {
115       $xml->startTag('newOrder', BatchRequestNo => $count++);
116       my $status = $_->cust_main->status;
117       tie my %order, 'Tie::IxHash', (
118         industryType    => 'EC',
119         transType       => 'AC',
120         bin             => $bin,
121         merchantID      => $merchantID,
122         terminalID      => $terminalID,
123         ($_->payby eq 'CARD') ? (
124           ccAccountNum    => $_->payinfo,
125           ccExp           => $_->expmmyy,
126         ) : (
127           ecpCheckRT      => ($_->payinfo =~ /@(\d+)/),
128           ecpCheckDDA     => ($_->payinfo =~ /(\d+)@/),
129           ecpBankAcctType => $paytype{lc($_->cust_main->paytype)},
130           ecpDelvMethod   => 'A',
131         ),
132         avsZip          => substr($_->zip,      0, 10),
133         avsAddress1     => substr($_->address1, 0, 30),
134         avsAddress2     => substr($_->address2, 0, 30),
135         avsCity         => substr($_->city,     0, 20),
136         avsState        => substr($_->state,    0, 2),
137         avsName         => substr($_->first. ' '. $_->last, 0, 30),
138         avsCountryCode  => ( $paymentech_countries{ $_->country }
139                                  ? $_->country
140                                  : ''
141                              ),
142         orderID           => $_->paybatchnum,
143         amount            => $_->amount * 100,
144         );
145       # only do this if recurringInd is enabled in config, 
146       # and the customer has at least one non-canceled recurring package
147       if ( $with_recurringInd and $status =~ /^active|suspended|ordered$/ ) {
148         # then send RF if this is the first payment on this payinfo,
149         # RS otherwise.
150         $order{'recurringInd'} = $_->payinfo_used ? 'RS' : 'RF';
151       }
152       foreach my $key (keys %order) {
153         $xml->dataElement($key, $order{$key})
154       }
155       $xml->endTag('newOrder');
156     }
157     $xml->startTag('endOfDay', BatchRequestNo => $count);
158     $xml->dataElement(bin => $bin);
159     $xml->dataElement(merchantID => $merchantID);
160     $xml->dataElement(terminalID => $terminalID);
161     $xml->endTag('endOfDay');
162     $xml->endTag('transRequest');
163     return $output;
164   },
165   row => sub {},
166 );
167
168 1;
169