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 OrderID => [ ':invoice_number', 22 ],
46 Amount => [ ':amount', 12 ],
47 Comments => [ ':email', 64 ],
48 TxRefNum => [ ':order_number', 40 ],# used only for Refund
51 tie my %mark_for_capture, 'Tie::IxHash', (
52 OrbitalConnectionUsername => [ ':login', 32 ],
53 OrbitalConnectionPassword => [ ':password', 32 ],
54 OrderID => [ ':invoice_number', 22 ],
55 Amount => [ ':amount', 12 ],
57 MerchantID => [ ':merchant_id', 12 ],
58 TerminalID => [ ':terminal_id', 3 ],
59 TxRefNum => [ ':order_number', 40 ],
62 tie my %reversal, 'Tie::IxHash', (
63 OrbitalConnectionUsername => [ ':login', 32 ],
64 OrbitalConnectionPassword => [ ':password', 32 ],
65 TxRefNum => [ ':order_number', 40 ],
66 TxRefIdx => [ '0', 4 ],
67 OrderID => [ ':invoice_number', 22 ],
69 MerchantID => [ ':merchant_id', 12 ],
70 TerminalID => [ ':terminal_id', 3 ],
71 OnlineReversalInd => [ 'Y', 1 ],
72 # Always attempt to reverse authorization.
93 # Per ISO 4217. Add to this as needed.
102 $self->server('orbitalvar1.paymentech.net') unless $self->server; # this is the test server.
103 $self->port('443') unless $self->port;
104 $self->path('/authorize') unless $self->path;
106 $self->build_subs(qw(
110 #leaking gateway-specific anmes? need to be mapped to B:OP standards :)
123 my %content = $self->content();
125 tie my %data, 'Tie::IxHash';
126 ref($skel) eq 'HASH' or die 'Tried to build non-hash';
127 foreach my $k (keys(%$skel)) {
130 ($v, $l) = @$v if(ref $v eq 'ARRAY');
132 # Get the content field with that name.
133 $data{$k} = $content{$1};
138 # Ruthlessly enforce field length.
139 $data{$k} = substr($data{$k}, 0, $l) if($data{$k} and $l);
147 my %content = $self->content();
148 foreach(qw(merchant_id terminal_id currency)) {
149 $content{$_} = $self->{$_} if exists($self->{$_});
152 $self->required_fields('action');
154 ('normal authorization' => 'AC',
155 'authorization only' => 'A',
158 'post authorization' => 'MFC', # for our use, doesn't go in the request
160 $content{'message_type'} = $message_type{lc($content{'action'})}
161 or die "unsupported action: '".$content{'action'}."'";
163 foreach (keys(%defaults) ) {
164 $content{$_} = $defaults{$_} if !defined($content{$_});
166 if(length($content{merchant_id}) == 12) {
167 $content{bin} = '000002' # PNS
169 elsif(length($content{merchant_id}) == 6) {
170 $content{bin} = '000001' # Salem
173 die "invalid merchant ID: '".$content{merchant_id}."'";
176 @content{qw(currency_code currency_exp)} = @{$currency_code{$content{currency}}}
177 if $content{currency};
179 if($content{card_number} =~ /^(4|6011)/) { # Matches Visa and Discover transactions
180 if(defined($content{cvv2})) {
181 $content{cvvind} = 1; # "Value is present"
184 $content{cvvind} = 9; # "Value is not available"
187 $content{amount} = int($content{amount}*100);
188 $content{name} = $content{first_name} . ' ' . $content{last_name};
189 # According to the spec, the first 8 characters of this have to be unique.
190 # The test server doesn't enforce this, but we comply anyway to the extent possible.
191 if(! $content{invoice_number}) {
192 # Choose one arbitrarily
193 $content{invoice_number} ||= sprintf("%04x%04x",time % 2**16,int(rand() * 2**16));
196 # Always send as MMYY
197 $content{expiration} =~ s/\D//g;
198 $content{expiration} = sprintf('%04d',$content{expiration});
200 $self->content(%content);
206 $DB::single = $DEBUG;
209 my %content = $self->content;
211 my @required_fields = @required;
214 if( $content{'message_type'} eq 'MFC' ) {
215 $request = { MarkForCapture => $self->build(\%mark_for_capture) };
216 push @required_fields, 'order_number';
218 elsif( $content{'message_type'} eq 'V' ) {
219 $request = { Reversal => $self->build(\%reversal) };
222 $request = { NewOrder => $self->build(\%new_order) };
223 push @required_fields, qw(
233 $self->required_fields(@required_fields);
235 my $post_data = XMLout({ Request => $request }, KeepRoot => 1, NoAttr => 1, NoSort => 1);
237 if (!$self->test_transaction()) {
238 $self->server('orbital1.paymentech.net');
241 warn $post_data if $DEBUG;
242 $DB::single = $DEBUG;
243 my($page,$server_response,%headers) =
244 $self->https_post( { 'Content-Type' => 'application/PTI47',
245 'headers' => \%request_header } ,
248 warn $page if $DEBUG;
250 my $response = XMLin($page, KeepRoot => 0);
251 #$self->Response($response);
254 #warn Dumper($response) if $DEBUG;
256 my ($r) = values(%$response);
257 #foreach(qw(ProcStatus RespCode AuthCode AVSRespCode CVV2RespCode)) {
258 # if(exists($r->{$_}) and
260 # $self->$_($r->{$_});
266 #turn empty hashrefs into the empty string
267 $r->{$_} = '' if ref($r->{$_}) && ! keys %{ $r->{$_} };
269 #turn hashrefs with content into scalars
270 $r->{$_} = $r->{$_}{'content'}
271 if ref($r->{$_}) && exists($r->{$_}{'content'});
274 if ($server_response !~ /^200/) {
276 $self->is_success(0);
277 my $error = "Server error: '$server_response'";
278 $error .= " / Transaction error: '".
279 ($r->{'ProcStatusMsg'} || $r->{'StatusMsg'}) . "'"
280 if $r->{'ProcStatus'} != 0;
281 $self->error_message($error);
285 if ( !exists($r->{'ProcStatus'}) ) {
287 $self->is_success(0);
288 $self->error_message( "Malformed response: '$page'" );
290 } elsif ( $r->{'ProcStatus'} != 0 or
291 # NewOrders get ApprovalStatus, Reversals don't.
292 ( exists($r->{'ApprovalStatus'}) ?
293 $r->{'ApprovalStatus'} != 1 :
294 $r->{'StatusMsg'} ne 'Approved' )
298 $self->is_success(0);
299 $self->error_message( "Transaction error: '".
300 ($r->{'ProcStatusMsg'} || $r->{'StatusMsg'}) . "'"
305 $self->is_success(1);
306 # For credits, AuthCode is empty and gets converted to a hashref.
307 $self->authorization($r->{'AuthCode'}) if !ref($r->{'AuthCode'});
308 $self->order_number($r->{'TxRefNum'});
320 Business::OnlinePayment::PaymenTech - Chase Paymentech backend for Business::OnlinePayment
324 $trans = new Business::OnlinePayment('PaymenTech',
325 merchant_id => "000111222333",
326 terminal_id => "001",
327 currency => "USD", # CAD, MXN
332 password => "password",
334 card_number => "5500000000000004",
335 expiration => "0211",
336 address => "123 Anystreet",
337 city => "Sacramento",
339 action => "Normal Authorization",
344 if($trans->is_approved) {
345 print "Approved: ".$trans->authorization;
347 print "Failed: ".$trans->error_message;
352 Electronic check processing and recurring billing are not yet supported.
356 Mark Wells, mark@freeside.biz
360 perl(1). L<Business::OnlinePayment>.