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