This commit was generated by cvs2svn to compensate for changes in r11022,
[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);
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     'authorization',
27     ],
28   xmlkeys     => [
29     'orderID',
30     'respDateTime',
31     'approvalStatus',
32     'txRefNum',
33     'authorizationCode',
34     ],
35   'hook'        => sub {
36       if ( !$gateway ) {
37         # find a gateway configuration that has the same merchantID 
38         # as the batch config, if there is one.  If not, leave 
39         # gateway out entirely.
40         my $merchant = (FS::Conf->new->config('batchconfig-paymentech'))[2];
41         my $g = qsearchs({
42               'table'     => 'payment_gateway',
43               'addl_from' => ' JOIN payment_gateway_option USING (gatewaynum) ',
44               'hashref'   => {  disabled    => '',
45                                 optionname  => 'merchant_id',
46                                 optionvalue => $merchant,
47                               },
48               });
49         $gateway = ($g ? $g->gatewaynum . '-' : '') . 'PaymenTech';
50       }
51       my ($hash, $oldhash) = @_;
52       my ($mon, $day, $year, $hour, $min, $sec) = 
53         $hash->{'_date'} =~ /^(..)(..)(....)(..)(..)(..)$/;
54       $hash->{'_date'} = timelocal($sec, $min, $hour, $day, $mon-1, $year);
55       $hash->{'paid'} = $oldhash->{'amount'};
56       $hash->{'paybatch'} = join(':', 
57         $gateway,
58         $hash->{'authorization'},
59         $hash->{'order_number'},
60       );
61     },
62   'approved'    => sub { my $hash = shift;
63                             $hash->{'approvalStatus'} 
64     },
65   'declined'    => sub { my $hash = shift;
66                             ! $hash->{'approvalStatus'} 
67     },
68 );
69
70 my %paytype = (
71   'personal checking' => 'C',
72   'personal savings'  => 'S',
73   'business checking' => 'X',
74   'business savings'  => 'X',
75   );
76
77 %export_info = (
78   init  => sub {
79 # Load this at run time
80     eval "use XML::Writer";
81     die $@ if $@;
82     my $conf = shift;
83     ($bin, $terminalID, $merchantID, $username) =
84        $conf->config('batchconfig-paymentech');
85     },
86 # Here we do all the work in the header function.
87   header => sub {
88     my $pay_batch = shift;
89     my @cust_pay_batch = @{(shift)};
90     my $count = 1;
91     my $output;
92     my $xml = new XML::Writer(OUTPUT => \$output, DATA_MODE => 1, DATA_INDENT => 2);
93     $xml->startTag('transRequest', RequestCount => scalar(@cust_pay_batch) + 1);
94     $xml->startTag('batchFileID');
95     $xml->dataElement(userID => $username);
96     $xml->dataElement(fileDateTime => time2str('%Y%m%d%H%M%S', time));
97     $xml->dataElement(fileID => 'FILEID');
98     $xml->endTag('batchFileID');
99
100     foreach (@cust_pay_batch) {
101       $xml->startTag('newOrder', BatchRequestNo => $count++);
102       tie my %order, 'Tie::IxHash', (
103         industryType => 'EC',
104         transType    => 'AC',
105         bin          => $bin,
106         merchantID   => $merchantID,
107         terminalID   => $terminalID,
108         ($_->payby eq 'CARD') ? (
109           ccAccountNum => $_->payinfo,
110           ccExp        => $_->expmmyy,
111         ) : (
112           ecpCheckRT      => ($_->payinfo =~ /@(\d+)/),
113           ecpCheckDDA     => ($_->payinfo =~ /(\d+)@/),
114           ecpBankAcctType => $paytype{lc($_->cust_main->paytype)},
115           ecpDelvMethod   => 'A',
116         ),
117         avsZip          => substr($_->zip, 0, 10),
118         avsAddress1     => substr($_->address1, 0, 30),
119         avsAddress2     => substr($_->address2, 0, 30),
120         avsCity         => substr($_->city, 0, 20),
121         avsState        => $_->state,
122         avsName        => substr($_->first . ' ' . $_->last, 0, 30),
123         avsCountryCode => $_->country,
124         orderID        => $_->paybatchnum,
125         amount         => $_->amount * 100,
126         );
127       foreach my $key (keys %order) {
128         $xml->dataElement($key, $order{$key})
129       }
130       $xml->endTag('newOrder');
131     }
132     $xml->startTag('endOfDay', BatchRequestNo => $count);
133     $xml->dataElement(bin => $bin);
134     $xml->dataElement(merchantID => $merchantID);
135     $xml->dataElement(terminalID => $terminalID);
136     $xml->endTag('endOfDay');
137     $xml->endTag('transRequest');
138     return $output;
139   },
140   row => sub {},
141 );
142
143 1;
144