1 package Business::OnlinePayment::PaymenTech;
5 use Business::OnlinePayment::HTTPS;
8 use vars qw($VERSION $DEBUG @ISA $me);
10 @ISA = qw(Business::OnlinePayment::HTTPS);
13 $VERSION = eval $VERSION; # modperlstyle: convert the string into a number
16 $me='Business::OnlinePayment::PaymenTech';
18 my %request_header = (
19 'MIME-VERSION' => '1.0',
20 'Content-Transfer-Encoding' => 'text',
21 'Request-Number' => 1,
22 'Document-Type' => 'Request',
23 'Interface-Version' => "$me $VERSION",
24 ); # Content-Type has to be passed separately
26 tie my %new_order, 'Tie::IxHash', (
27 OrbitalConnectionUsername => [ ':login', 32 ],
28 OrbitalConnectionPassword => [ ':password', 32 ],
29 IndustryType => [ 'EC', 2 ],
30 MessageType => [ ':message_type', 2 ],
32 MerchantID => [ ':merchant_id', 12 ],
33 TerminalID => [ ':terminal_id', 3 ],
34 CardBrand => [ '', 2 ],
35 AccountNum => [ ':card_number', 19 ],
36 Exp => [ ':expiration', 4 ],
37 CurrencyCode => [ ':currency_code', 3 ],
38 CurrencyExponent => [ ':currency_exp', 6 ],
39 CardSecValInd => [ ':cvvind', 1 ],
40 CardSecVal => [ ':cvv2', 4 ],
41 AVSzip => [ ':zip', 10 ],
42 AVSaddress1 => [ ':address', 30 ],
43 AVScity => [ ':city', 20 ],
44 AVSstate => [ ':state', 2 ],
45 AVScountryCode => [ ':country', 2 ],
46 OrderID => [ ':invoice_number', 22 ],
47 Amount => [ ':amount', 12 ],
48 Comments => [ ':email', 64 ],
49 TxRefNum => [ ':order_number', 40 ],# used only for Refund
52 tie my %mark_for_capture, 'Tie::IxHash', (
53 OrbitalConnectionUsername => [ ':login', 32 ],
54 OrbitalConnectionPassword => [ ':password', 32 ],
55 OrderID => [ ':invoice_number', 22 ],
56 Amount => [ ':amount', 12 ],
58 MerchantID => [ ':merchant_id', 12 ],
59 TerminalID => [ ':terminal_id', 3 ],
60 TxRefNum => [ ':order_number', 40 ],
63 tie my %reversal, 'Tie::IxHash', (
64 OrbitalConnectionUsername => [ ':login', 32 ],
65 OrbitalConnectionPassword => [ ':password', 32 ],
66 TxRefNum => [ ':order_number', 40 ],
67 TxRefIdx => [ '0', 4 ],
68 OrderID => [ ':invoice_number', 22 ],
70 MerchantID => [ ':merchant_id', 12 ],
71 TerminalID => [ ':terminal_id', 3 ],
72 OnlineReversalInd => [ 'Y', 1 ],
73 # Always attempt to reverse authorization.
94 # Per ISO 4217. Add to this as needed.
100 my %paymentech_countries = map { $_ => 1 } qw( US CA GB UK );
105 $self->server('orbitalvar1.paymentech.net') unless $self->server; # this is the test server.
106 $self->port('443') unless $self->port;
107 $self->path('/authorize') unless $self->path;
109 $self->build_subs(qw(
113 #leaking gateway-specific anmes? need to be mapped to B:OP standards :)
126 my %content = $self->content();
128 tie my %data, 'Tie::IxHash';
129 ref($skel) eq 'HASH' or die 'Tried to build non-hash';
130 foreach my $k (keys(%$skel)) {
133 ($v, $l) = @$v if(ref $v eq 'ARRAY');
135 # Get the content field with that name.
136 $data{$k} = $content{$1};
141 # Ruthlessly enforce field length.
142 $data{$k} = substr($data{$k}, 0, $l) if($data{$k} and $l);
150 my %content = $self->content();
151 foreach(qw(merchant_id terminal_id currency)) {
152 $content{$_} = $self->{$_} if exists($self->{$_});
155 $self->required_fields('action');
157 ('normal authorization' => 'AC',
158 'authorization only' => 'A',
161 'post authorization' => 'MFC', # for our use, doesn't go in the request
163 $content{'message_type'} = $message_type{lc($content{'action'})}
164 or die "unsupported action: '".$content{'action'}."'";
166 foreach (keys(%defaults) ) {
167 $content{$_} = $defaults{$_} if !defined($content{$_});
169 if(length($content{merchant_id}) == 12) {
170 $content{bin} = '000002' # PNS
172 elsif(length($content{merchant_id}) == 6) {
173 $content{bin} = '000001' # Salem
176 die "invalid merchant ID: '".$content{merchant_id}."'";
179 @content{qw(currency_code currency_exp)} = @{$currency_code{$content{currency}}}
180 if $content{currency};
182 if($content{card_number} =~ /^(4|6011)/) { # Matches Visa and Discover transactions
183 if(defined($content{cvv2})) {
184 $content{cvvind} = 1; # "Value is present"
187 $content{cvvind} = 9; # "Value is not available"
190 $content{amount} = int($content{amount}*100);
191 $content{name} = $content{first_name} . ' ' . $content{last_name};
192 # According to the spec, the first 8 characters of this have to be unique.
193 # The test server doesn't enforce this, but we comply anyway to the extent possible.
194 if(! $content{invoice_number}) {
195 # Choose one arbitrarily
196 $content{invoice_number} ||= sprintf("%04x%04x",time % 2**16,int(rand() * 2**16));
199 # Always send as MMYY
200 $content{expiration} =~ s/\D//g;
201 $content{expiration} = sprintf('%04d',$content{expiration});
203 $content{country} ||= 'US';
204 $content{country} = ( $paymentech_countries{ $content{country} }
209 $self->content(%content);
215 $DB::single = $DEBUG;
218 my %content = $self->content;
220 my @required_fields = @required;
223 if( $content{'message_type'} eq 'MFC' ) {
224 $request = { MarkForCapture => $self->build(\%mark_for_capture) };
225 push @required_fields, 'order_number';
227 elsif( $content{'message_type'} eq 'V' ) {
228 $request = { Reversal => $self->build(\%reversal) };
231 $request = { NewOrder => $self->build(\%new_order) };
232 push @required_fields, qw(
242 $self->required_fields(@required_fields);
244 my $post_data = XMLout({ Request => $request }, KeepRoot => 1, NoAttr => 1, NoSort => 1);
246 if (!$self->test_transaction()) {
247 $self->server('orbital1.paymentech.net');
250 warn $post_data if $DEBUG;
251 $DB::single = $DEBUG;
252 my($page,$server_response,%headers) =
253 $self->https_post( { 'Content-Type' => 'application/PTI47',
254 'headers' => \%request_header } ,
257 warn $page if $DEBUG;
259 my $response = XMLin($page, KeepRoot => 0);
260 #$self->Response($response);
263 #warn Dumper($response) if $DEBUG;
265 my ($r) = values(%$response);
266 #foreach(qw(ProcStatus RespCode AuthCode AVSRespCode CVV2RespCode)) {
267 # if(exists($r->{$_}) and
269 # $self->$_($r->{$_});
275 #turn empty hashrefs into the empty string
276 $r->{$_} = '' if ref($r->{$_}) && ! keys %{ $r->{$_} };
278 #turn hashrefs with content into scalars
279 $r->{$_} = $r->{$_}{'content'}
280 if ref($r->{$_}) && exists($r->{$_}{'content'});
283 if ($server_response !~ /^200/) {
285 #$self->is_success(0);
286 my $error = "Server error: '$server_response'";
287 $error .= " / Transaction error: '".
288 ($r->{'ProcStatusMsg'} || $r->{'StatusMsg'}) . "'"
289 if $r->{'ProcStatus'} != 0;
290 #$self->error_message($error);
291 #overzealous? are there "normal decline" transaction errors being returned?
296 die "Unable to parse response_page\n" if !exists($r->{'ProcStatus'});
298 if ( $r->{'ProcStatus'} != 0 or
299 # NewOrders get ApprovalStatus, Reversals don't.
300 ( exists($r->{'ApprovalStatus'}) ?
301 $r->{'ApprovalStatus'} != 1 :
302 $r->{'StatusMsg'} ne 'Approved' )
306 $self->is_success(0);
307 $self->error_message( "Transaction error: '".
308 ($r->{'ProcStatusMsg'} || $r->{'StatusMsg'}) . "'"
313 $self->is_success(1);
314 # For credits, AuthCode is empty and gets converted to a hashref.
315 $self->authorization($r->{'AuthCode'}) if !ref($r->{'AuthCode'});
316 $self->order_number($r->{'TxRefNum'});
328 Business::OnlinePayment::PaymenTech - Chase Paymentech backend for Business::OnlinePayment
332 $trans = new Business::OnlinePayment('PaymenTech',
333 merchant_id => "000111222333",
334 terminal_id => "001",
335 currency => "USD", # CAD, MXN
340 password => "password",
342 card_number => "5500000000000004",
343 expiration => "0211",
344 address => "123 Anystreet",
345 city => "Sacramento",
347 action => "Normal Authorization",
352 if($trans->is_approved) {
353 print "Approved: ".$trans->authorization;
355 print "Failed: ".$trans->error_message;
360 Electronic check processing and recurring billing are not yet supported.
364 Mark Wells, mark@freeside.biz
368 perl(1). L<Business::OnlinePayment>.