Batch payment refactoring
[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 FS::Conf;
9 use XML::Simple qw(XMLin XMLout);
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     my $conf = shift;
53     ($bin, $terminalID, $merchantID, $username) =
54        $conf->config('batchconfig-paymentech');
55     },
56 # Here we do all the work in the header function.
57   header => sub {
58     my $pay_batch = shift;
59     my @cust_pay_batch = @{(shift)};
60     my $count = 0;
61     XMLout( {
62       transRequest => {
63         RequestCount => scalar(@cust_pay_batch),
64         batchFileID  => {
65           userID        => $username,
66           fileDateTime  => time2str('%Y%m%d%H%M%s',time),
67           fileID        => 'batch'.time2str('%Y%m%d',time),
68         },
69         newOrder => [ map { {
70           # $_ here refers to a cust_pay_batch record.
71           BatchRequestNo => $count++,
72           industryType   => 'EC',
73           transType      => 'AC',
74           bin            => $bin,
75           merchantID     => $merchantID,
76           terminalID     => $terminalID,
77           ($_->payby eq 'CARD') ? (
78             # Credit card stuff
79             ccAccountNum   => $_->payinfo,
80             ccExp          => time2str('%y%m',str2time($_->exp)),
81           ) : (
82             # ECP (electronic check) stuff
83             ecpCheckRT     => ($_->payinfo =~ /@(\d+)/),
84             ecpCheckDDA    => ($_->payinfo =~ /(\d+)@/),
85             ecpBankAcctType => $paytype{lc($_->cust_main->paytype)},
86             ecpDelvMethod  => 'B'
87           ),
88           avsZip         => $_->zip,
89           avsAddress1    => $_->address1,
90           avsAddress2    => $_->address2,
91           avsCity        => $_->city,
92           avsState       => $_->state,
93           avsName        => $_->first . ' ' . $_->last,
94           avsCountryCode => $_->country,
95           orderID        => $_->paybatchnum,
96           amount         => $_->amount * 100,
97           } } @cust_pay_batch
98         ],
99         endOfDay => {
100           BatchRequestNo => $count++,
101           bin            => $bin,
102           merchantID     => $merchantID,
103           terminalID     => $terminalID
104         },
105       } 
106     }, KeepRoot => 1, NoAttr => 1);
107   },
108   row => sub {},
109 );
110
111 1;
112