1 package FS::pay_batch::paymentech;
4 use vars qw(@ISA %import_info %export_info $name);
5 use FS::Record 'qsearchs';
7 use Date::Format 'time2str';
8 use Date::Parse 'str2time';
13 my ($bin, $merchantID, $terminalID, $username, $password, $with_recurringInd);
20 xmlrow => [ qw(transResponse newOrderResp) ],
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];
48 'table' => 'payment_gateway',
49 'addl_from' => ' JOIN payment_gateway_option USING (gatewaynum) ',
50 'hashref' => { disabled => '',
51 optionname => 'merchant_id',
52 optionvalue => $merchant,
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',
66 $hash->{'order_number'},
68 if ( $hash->{'procStatus'} == 0 ) {
69 $hash->{'error_message'} = $hash->{'respCodeMessage'};
71 $hash->{'error_message'} = $hash->{'procStatusMessage'};
74 'approved' => sub { my $hash = shift;
75 $hash->{'approvalStatus'}
77 'declined' => sub { my $hash = shift;
78 ! $hash->{'approvalStatus'}
83 'personal checking' => 'C',
84 'personal savings' => 'S',
85 'business checking' => 'X',
86 'business savings' => 'X',
89 my %paymentech_countries = map { $_ => 1 } qw( US CA GB UK );
93 # Load this at run time
94 eval "use XML::Writer";
97 ($bin, $terminalID, $merchantID, $username, $password, $with_recurringInd) =
98 $conf->config('batchconfig-paymentech');
100 # Here we do all the work in the header function.
102 my $pay_batch = shift;
103 my @cust_pay_batch = @{(shift)};
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');
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',
121 merchantID => $merchantID,
122 terminalID => $terminalID,
123 ($_->payby eq 'CARD') ? (
124 ccAccountNum => $_->payinfo,
125 ccExp => $_->expmmyy,
127 ecpCheckRT => ($_->payinfo =~ /@(\d+)/),
128 ecpCheckDDA => ($_->payinfo =~ /(\d+)@/),
129 ecpBankAcctType => $paytype{lc($_->cust_main->paytype)},
130 ecpDelvMethod => 'A',
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 }
142 orderID => $_->paybatchnum,
143 amount => $_->amount * 100,
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,
150 $order{'recurringInd'} = $_->payinfo_used ? 'RS' : 'RF';
152 foreach my $key (keys %order) {
153 $xml->dataElement($key, $order{$key})
155 $xml->endTag('newOrder');
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');