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';
11 use FS::Misc 'bytes_substr';
14 my ($bin, $merchantID, $terminalID, $username, $password, $with_recurringInd);
21 xmlrow => [ qw(transResponse newOrderResp) ],
44 # find a gateway configuration that has the same merchantID
45 # as the batch config, if there is one. If not, leave
46 # gateway out entirely.
47 my $merchant = (FS::Conf->new->config('batchconfig-paymentech'))[2];
49 'table' => 'payment_gateway',
50 'addl_from' => ' JOIN payment_gateway_option USING (gatewaynum) ',
51 'hashref' => { disabled => '',
52 optionname => 'merchant_id',
53 optionvalue => $merchant,
57 my ($hash, $oldhash) = @_;
58 $hash->{'gatewaynum'} = $gateway->gatewaynum if $gateway;
59 $hash->{'processor'} = 'PaymenTech';
60 my ($mon, $day, $year, $hour, $min, $sec) =
61 $hash->{'_date'} =~ /^(..)(..)(....)(..)(..)(..)$/;
62 $hash->{'_date'} = timelocal($sec, $min, $hour, $day, $mon-1, $year);
63 $hash->{'paid'} = $oldhash->{'amount'};
64 if ( $hash->{'procStatus'} == 0 ) {
65 $hash->{'error_message'} = $hash->{'respCodeMessage'};
67 $hash->{'error_message'} = $hash->{'procStatusMessage'};
70 'approved' => sub { my $hash = shift;
71 $hash->{'approvalStatus'}
73 'declined' => sub { my $hash = shift;
74 ! $hash->{'approvalStatus'}
79 'personal checking' => 'C',
80 'personal savings' => 'S',
81 'business checking' => 'X',
82 'business savings' => 'X',
85 my %paymentech_countries = map { $_ => 1 } qw( US CA GB UK );
89 # Load this at run time
90 eval "use XML::Writer";
93 ($bin, $terminalID, $merchantID, $username, $password, $with_recurringInd) =
94 $conf->config('batchconfig-paymentech');
96 # Here we do all the work in the header function.
98 my $pay_batch = shift;
99 my @cust_pay_batch = @{(shift)};
102 my $xml = XML::Writer->new(
108 $xml->xmlDecl(); # it is in the spec
109 $xml->startTag('transRequest', RequestCount => scalar(@cust_pay_batch) + 1);
110 $xml->startTag('batchFileID');
111 $xml->dataElement(userID => $username);
112 $xml->dataElement(fileDateTime => time2str('%Y%m%d%H%M%S', time));
113 $xml->dataElement(fileID => 'FILEID');
114 $xml->endTag('batchFileID');
116 foreach (@cust_pay_batch) {
117 $xml->startTag('newOrder', BatchRequestNo => $count++);
118 my $status = $_->cust_main->status;
119 tie my %order, 'Tie::IxHash', (
120 industryType => 'EC',
123 merchantID => $merchantID,
124 terminalID => $terminalID,
125 ($_->payby eq 'CARD') ? (
126 ccAccountNum => $_->payinfo,
127 ccExp => $_->expmmyy,
129 ecpCheckRT => ($_->payinfo =~ /@(\d+)/),
130 ecpCheckDDA => ($_->payinfo =~ /(\d+)@/),
131 ecpBankAcctType => $paytype{lc($_->cust_main->paytype)},
132 ecpDelvMethod => 'A',
134 avsZip => bytes_substr($_->zip, 0, 10),
135 avsAddress1 => bytes_substr($_->address1, 0, 30),
136 avsAddress2 => bytes_substr($_->address2, 0, 30),
137 avsCity => bytes_substr($_->city, 0, 20),
138 avsState => bytes_substr($_->state, 0, 2),
139 avsName => bytes_substr($_->first. ' '. $_->last, 0, 30),
140 ( $paymentech_countries{ $_->country }
141 ? ( avsCountryCode => $_->country )
144 orderID => $_->paybatchnum,
145 amount => $_->amount * 100,
147 # only do this if recurringInd is enabled in config,
148 # and the customer has at least one non-canceled recurring package
149 if ( $with_recurringInd and $status =~ /^active|suspended|ordered$/ ) {
150 # then send RF if this is the first payment on this payinfo,
152 $order{'recurringInd'} = $_->payinfo_used ? 'RS' : 'RF';
154 foreach my $key (keys %order) {
155 $xml->dataElement($key, $order{$key})
157 $xml->endTag('newOrder');
159 $xml->startTag('endOfDay', BatchRequestNo => $count);
160 $xml->dataElement(bin => $bin);
161 $xml->dataElement(merchantID => $merchantID);
162 $xml->dataElement(terminalID => $terminalID);
163 $xml->endTag('endOfDay');
164 $xml->endTag('transRequest');
170 # Including this means that there is a Business::BatchPayment module for
171 # this gateway and we want to upgrade it.
172 # Must return the name of the module, followed by a hash of options.
174 sub _upgrade_gateway {
175 my $conf = FS::Conf->new;
176 my @batchconfig = $conf->config('batchconfig-paymentech');
178 @options{ qw(bin terminalID merchantID login password ) } = @batchconfig;
179 $options{'industryType'} = 'EC';
180 ( 'Paymentech', %options );