89f0c77238056be7f6801e0d4328eb947895438f
[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 Time::Local;
6 use Date::Format 'time2str';
7 use Date::Parse 'str2time';
8 use Tie::IxHash;
9 use FS::Conf;
10
11 my $conf;
12 my ($bin, $merchantID, $terminalID, $username);
13 $name = 'paymentech';
14
15 %import_info = (
16   filetype    => 'XML',
17   xmlrow         => [ qw(transResponse newOrderResp) ],
18   fields      => [
19     'paybatchnum',
20     '_date',
21     'approvalStatus',
22     ],
23   xmlkeys     => [
24     'orderID',
25     'respDateTime',
26     'approvalStatus',
27     ],
28   'hook'        => sub {
29       my ($hash, $oldhash) = @_;
30       my ($mon, $day, $year, $hour, $min, $sec) = 
31         $hash->{'_date'} =~ /^(..)(..)(....)(..)(..)(..)$/;
32       $hash->{'_date'} = timelocal($sec, $min, $hour, $day, $mon-1, $year);
33       $hash->{'paid'} = $oldhash->{'amount'};
34     },
35   'approved'    => sub { my $hash = shift;
36                             $hash->{'approvalStatus'} 
37     },
38   'declined'    => sub { my $hash = shift;
39                             ! $hash->{'approvalStatus'} 
40     },
41 );
42
43 my %paytype = (
44   'personal checking' => 'C',
45   'personal savings'  => 'S',
46   'business checking' => 'X',
47   'business savings'  => 'X',
48   );
49
50 %export_info = (
51   init  => sub {
52 # Load this at run time
53     eval "use XML::Writer";
54     die $@ if $@;
55     my $conf = shift;
56     ($bin, $terminalID, $merchantID, $username) =
57        $conf->config('batchconfig-paymentech');
58     },
59 # Here we do all the work in the header function.
60   header => sub {
61     my $pay_batch = shift;
62     my @cust_pay_batch = @{(shift)};
63     my $count = 1;
64     my $output;
65     my $xml = new XML::Writer(OUTPUT => \$output, DATA_MODE => 1, DATA_INDENT => 2);
66     $xml->startTag('transRequest', RequestCount => scalar(@cust_pay_batch) + 1);
67     $xml->startTag('batchFileID');
68     $xml->dataElement(userID => $username);
69     $xml->dataElement(fileDateTime => time2str('%Y%m%d%H%M%S', time));
70     $xml->dataElement(fileID => 'FILEID');
71     $xml->endTag('batchFileID');
72
73     foreach (@cust_pay_batch) {
74       $xml->startTag('newOrder', BatchRequestNo => $count++);
75       tie my %order, 'Tie::IxHash', (
76         industryType => 'EC',
77         transType    => 'AC',
78         bin          => $bin,
79         merchantID   => $merchantID,
80         terminalID   => $terminalID,
81         ($_->payby eq 'CARD') ? (
82           ccAccountNum => $_->payinfo,
83           ccExp        => time2str('%m%y', str2time($_->exp))
84         ) : (
85           ecpCheckRT      => ($_->payinfo =~ /@(\d+)/),
86           ecpCheckDDA     => ($_->payinfo =~ /(\d+)@/),
87           ecpBankAcctType => $paytype{lc($_->cust_main->paytype)},
88           ecpDelvMethod   => 'A',
89         ),
90         avsZip          => substr($_->zip, 0, 10),
91         avsAddress1     => substr($_->address1, 0, 30),
92         avsAddress2     => substr($_->address2, 0, 30),
93         avsCity         => substr($_->city, 0, 20),
94         avsState        => $_->state,
95         avsName        => substr($_->first . ' ' . $_->last, 0, 30),
96         avsCountryCode => $_->country,
97         orderID        => $_->paybatchnum,
98         amount         => $_->amount * 100,
99         );
100       foreach my $key (keys %order) {
101         $xml->dataElement($key, $order{$key})
102       }
103       $xml->endTag('newOrder');
104     }
105     $xml->startTag('endOfDay', BatchRequestNo => $count);
106     $xml->dataElement(bin => $bin);
107     $xml->dataElement(merchantID => $merchantID);
108     $xml->dataElement(terminalID => $terminalID);
109     $xml->endTag('endOfDay');
110     $xml->endTag('transRequest');
111     return $output;
112   },
113   row => sub {},
114 );
115
116 1;
117