1 package Business::OnlinePayment::IPPay;
9 use Business::OnlinePayment;
10 use Business::OnlinePayment::HTTPS;
11 use vars qw($VERSION $DEBUG @ISA $me);
13 @ISA = qw(Business::OnlinePayment::HTTPS);
15 $VERSION = eval $VERSION; # modperlstyle: convert the string into a number
18 $me = 'Business::OnlinePayment::IPPay';
22 'info_version' => '0.01',
23 'module_version' => $VERSION,
24 'supported_types' => [ qw( CC ECHECK ) ],
25 'supported_actions' => { 'CC' => [
26 'Normal Authorization',
31 'Reverse Authorization',
34 'Normal Authorization',
39 'CC_void_requires_card' => 1,
40 'ECHECK_void_requires_account' => 1,
48 # standard B::OP methods/data
49 $self->server('gtwy.ippay.com') unless $self->server;
50 $self->port('443') unless $self->port;
51 $self->path('/ippay') unless $self->path;
53 $self->build_subs(qw( order_number avs_code cvv2_response
54 response_page response_code response_headers
57 # module specific data
59 $self->debug( $opts{debug} );
64 foreach my $key (keys %opts) {
65 $key =~ /^default_(\w*)$/ or next;
66 $_defaults{$1} = $opts{$key};
69 $self->{_defaults} = \%_defaults;
75 my %content = $self->content();
78 my %types = ( 'visa' => 'CC',
80 'american express' => 'CC',
84 $content{'type'} = $types{lc($content{'type'})} || $content{'type'};
85 $self->transaction_type($content{'type'});
88 my $action = lc($content{'action'});
90 ( 'normal authorization' => 'SALE',
91 'authorization only' => 'AUTHONLY',
92 'post authorization' => 'CAPT',
93 'reverse authorization' => 'REVERSEAUTH',
98 ( 'normal authorization' => 'CHECK',
100 'credit' => 'REVERSAL',
103 if ($self->transaction_type eq 'CC') {
104 $content{'TransactionType'} = $actions{$action} || $action;
105 } elsif ($self->transaction_type eq 'ECHECK') {
107 $content{'TransactionType'} = $check_actions{$action} || $action;
110 my %account_types = ('personal checking' => 'CHECKING',
111 'personal savings' => 'SAVINGS',
112 'business checking' => 'CHECKING',
113 'business savings' => 'SAVINGS',
114 #not technically B:OP valid i guess?
115 'checking' => 'CHECKING',
116 'savings' => 'SAVINGS',
118 $content{'account_type'} = $account_types{lc($content{'account_type'})}
119 || $content{'account_type'};
122 $content{Origin} = 'RECURRING'
123 if ($content{recurring_billing} &&$content{recurring_billing} eq 'YES' );
125 # stuff it back into %content
126 $self->content(%content);
131 my ($self, $exp) = (shift, shift);
133 if ( defined($exp) and $exp =~ /^(\d+)\D+\d*\d{2}$/ ) {
134 $month = sprintf( "%02d", $1 );
135 }elsif ( defined($exp) and $exp =~ /^(\d{2})\d{2}$/ ) {
136 $month = sprintf( "%02d", $1 );
142 my ($self, $exp) = (shift, shift);
144 if ( defined($exp) and $exp =~ /^\d+\D+\d*(\d{2})$/ ) {
145 $year = sprintf( "%02d", $1 );
146 }elsif ( defined($exp) and $exp =~ /^\d{2}(\d{2})$/ ) {
147 $year = sprintf( "%02d", $1 );
154 tie my(%map), 'Tie::IxHash', @_;
155 my %content = $self->content();
158 if ( ref( $map{$_} ) eq 'HASH' ) {
159 $value = $map{$_} if ( keys %{ $map{$_} } );
160 }elsif( ref( $map{$_} ) ) {
161 $value = ${ $map{$_} };
162 }elsif( exists( $content{ $map{$_} } ) ) {
163 $value = $content{ $map{$_} };
166 if (defined($value)) {
177 $self->is_success(0);
180 my @required_fields = qw(action login type);
182 my $action = lc($self->{_content}->{action});
183 my $type = $self->transaction_type();
184 if ( $action eq 'normal authorization'
185 || $action eq 'credit'
186 || $action eq 'authorization only' && $type eq 'CC')
188 push @required_fields, qw( amount );
190 push @required_fields, qw( card_number expiration )
193 push @required_fields,
194 qw( routing_code account_number name ) # account_type
195 if ($type eq "ECHECK");
197 }elsif ( $action eq 'post authorization' && $type eq 'CC') {
198 push @required_fields, qw( order_number );
199 }elsif ( $action eq 'reverse authorization' && $type eq 'CC') {
200 push @required_fields, qw( order_number card_number expiration amount );
201 }elsif ( $action eq 'void') {
202 push @required_fields, qw( order_number amount );
204 push @required_fields, qw( authorization card_number )
207 push @required_fields,
208 qw( routing_code account_number name ) # account_type
209 if ($type eq "ECHECK");
212 croak "$me can't handle transaction type: ".
213 $self->{_content}->{action}. " for ".
214 $self->transaction_type();
217 my %content = $self->content();
218 foreach ( keys ( %{($self->{_defaults})} ) ) {
219 $content{$_} = $self->{_defaults}->{$_} unless exists($content{$_});
221 if ($self->test_transaction()) {
222 $content{'login'} = 'TESTTERMINAL';
224 $self->content(%content);
226 $self->required_fields(@required_fields);
228 #quick validation because ippay dumps an error indecipherable to the end user
229 if (grep { /^routing_code$/ } @required_fields) {
230 unless( $content{routing_code} =~ /^\d{9}$/ ) {
231 $self->_error_response('Invalid routing code');
236 my $transaction_id = $content{'order_number'};
237 unless ($transaction_id) {
238 my ($page, $server_response, %headers) = $self->https_get('dummy' => 1);
239 warn "fetched transaction id: (HTTPS response: $server_response) ".
241 join(", ", map { "$_ => ". $headers{$_} } keys %headers ). ") ".
242 "(Raw HTTPS content: $page)"
244 return unless $server_response=~ /^200/;
245 $transaction_id = $page;
248 my $cardexpmonth = $self->expdate_month($content{expiration});
249 my $cardexpyear = $self->expdate_year($content{expiration});
250 my $cardstartmonth = $self->expdate_month($content{card_start});
251 my $cardstartyear = $self->expdate_year($content{card_start});
254 if (defined($content{amount})) {
255 $amount = sprintf("%.2f", $content{amount});
259 my $check_number = $content{check_number} || "100" # make one up
260 if($content{account_number});
262 my $terminalid = $content{login} if $type eq 'CC';
263 my $merchantid = $content{login} if $type eq 'ECHECK';
265 my $country = country2code( $content{country}, LOCALE_CODE_ALPHA_3 );
266 $country = country_code2code( $content{country},
271 $country = $content{country}
273 $country = uc($country) if $country;
276 country2code( $content{ship_country}, LOCALE_CODE_ALPHA_3 );
277 $ship_country = country_code2code( $content{ship_country},
281 unless $ship_country;
282 $ship_country = $content{ship_country}
283 unless $ship_country;
284 $ship_country = uc($ship_country) if $ship_country;
286 tie my %ach, 'Tie::IxHash',
287 $self->revmap_fields(
288 #wtf, this is a "Type"" attribute of the ACH element,
289 # not a child element like the others
290 #AccountType => 'account_type',
291 AccountNumber => 'account_number',
292 ABA => 'routing_code',
293 CheckNumber => \$check_number,
296 tie my %industryinfo, 'Tie::IxHash',
297 $self->revmap_fields(
298 Type => 'IndustryInfo',
301 tie my %shippingaddr, 'Tie::IxHash',
302 $self->revmap_fields(
303 Address => 'ship_address',
305 StateProv => 'ship_state',
306 Country => \$ship_country,
307 Phone => 'ship_phone',
310 unless ( $type ne 'CC' || keys %shippingaddr ) {
311 tie %shippingaddr, 'Tie::IxHash',
312 $self->revmap_fields(
313 Address => 'address',
315 StateProv => 'state',
316 Country => \$country,
320 delete $shippingaddr{Country} unless $shippingaddr{Country};
322 tie my %shippinginfo, 'Tie::IxHash',
323 $self->revmap_fields(
324 CustomerPO => 'CustomerPO',
325 ShippingMethod => 'ShippingMethod',
326 ShippingName => 'ship_name',
327 ShippingAddr => \%shippingaddr,
330 tie my %req, 'Tie::IxHash',
331 $self->revmap_fields(
332 TransactionType => 'TransactionType',
333 TerminalID => 'login',
334 # TerminalID => \$terminalid,
335 # MerchantID => \$merchantid,
336 TransactionID => \$transaction_id,
337 RoutingCode => 'RoutingCode',
338 Approval => 'authorization',
339 BatchID => 'BatchID',
341 Password => 'password',
342 OrderNumber => 'invoice_number',
343 CardNum => 'card_number',
345 Issue => 'issue_number',
346 CardExpMonth => \$cardexpmonth,
347 CardExpYear => \$cardexpyear,
348 CardStartMonth => \$cardstartmonth,
349 CardStartYear => \$cardstartyear,
354 DispositionType => 'DispositionType',
355 TotalAmount => \$amount,
356 FeeAmount => 'FeeAmount',
357 TaxAmount => 'TaxAmount',
358 BillingAddress => 'address',
359 BillingCity => 'city',
360 BillingStateProv => 'state',
361 BillingPostalCode => 'zip',
362 BillingCountry => \$country,
363 BillingPhone => 'phone',
365 UserIPAddr => 'customer_ip',
366 UserHost => 'UserHost',
367 UDField1 => 'UDField1',
368 UDField2 => 'UDField2',
369 UDField3 => 'UDField3',
370 ActionCode => 'ActionCode',
371 IndustryInfo => \%industryinfo,
372 ShippingInfo => \%shippinginfo,
374 delete $req{BillingCountry} unless $req{BillingCountry};
377 my $writer = new XML::Writer( OUTPUT => \$post_data,
380 ENCODING => 'us-ascii',
383 $writer->startTag('JetPay');
384 foreach ( keys ( %req ) ) {
385 $self->_xmlwrite($writer, $_, $req{$_});
387 $writer->endTag('JetPay');
390 warn "$post_data\n" if $DEBUG;
392 my ($page,$server_response,%headers) = $self->https_post($post_data);
394 warn "$page\n" if $DEBUG;
397 if ($server_response =~ /^200/){
398 $response = XMLin($page);
399 if ( exists($response->{ActionCode}) && !exists($response->{ErrMsg})) {
400 $self->error_message($response->{ResponseText});
402 $self->error_message($response->{ErrMsg});
405 # $self->error_message("Server Failed");
408 $self->result_code($response->{ActionCode} || '');
409 $self->order_number($response->{TransactionID} || '');
410 $self->authorization($response->{Approval} || '');
411 $self->cvv2_response($response->{CVV2} || '');
412 $self->avs_code($response->{AVS} || '');
414 $self->is_success($self->result_code() eq '000' ? 1 : 0);
416 unless ($self->is_success()) {
417 unless ( $self->error_message() ) { #additional logging information
418 $self->error_message(
419 "(HTTPS response: $server_response) ".
421 join(", ", map { "$_ => ". $headers{$_} } keys %headers ). ") ".
422 "(Raw HTTPS content: $page)"
429 sub _error_response {
430 my ($self, $error_message) = (shift, shift);
431 $self->result_code('');
432 $self->order_number('');
433 $self->authorization('');
434 $self->cvv2_response('');
436 $self->is_success( 0);
437 $self->error_message($error_message);
441 my ($self, $writer, $item, $value) = @_;
444 if ( $item eq 'ACH' ) {
445 $att{'Type'} = $self->{_content}->{'account_type'}
446 if $self->{_content}->{'account_type'}; #necessary so we don't pass empty?
450 $writer->startTag($item, %att);
452 if ( ref( $value ) eq 'HASH' ) {
453 foreach ( keys ( %$value ) ) {
454 $self->_xmlwrite($writer, $_, $value->{$_});
457 $writer->characters($value);
460 $writer->endTag($item);
469 Business::OnlinePayment::IPPay - IPPay backend for Business::OnlinePayment
473 use Business::OnlinePayment;
476 new Business::OnlinePayment( "IPPay",
477 'default_Origin' => 'PHONE ORDER',
481 login => 'testdrive',
482 password => '', #password
483 action => 'Normal Authorization',
484 description => 'Business::OnlinePayment test',
486 customer_id => 'tfb',
487 name => 'Tofu Beast',
488 address => '123 Anystreet',
492 card_number => '4007000000027',
493 expiration => '09/02',
494 cvv2 => '1234', #optional
498 if($tx->is_success()) {
499 print "Card processed successfully: ".$tx->authorization."\n";
501 print "Card was rejected: ".$tx->error_message."\n";
504 =head1 SUPPORTED TRANSACTION TYPES
506 =head2 CC, Visa, MasterCard, American Express, Discover
508 Content required: type, login, action, amount, card_number, expiration.
512 Content required: type, login, action, amount, name, account_number, routing_code.
516 For detailed information see L<Business::OnlinePayment>.
518 =head1 METHODS AND FUNCTIONS
520 See L<Business::OnlinePayment> for the complete list. The following methods either override the methods in L<Business::OnlinePayment> or provide additional functions.
524 Returns the response error code.
528 Returns the response error description text.
530 =head2 server_response
532 Returns the complete response from the server.
534 =head1 Handling of content(%content) data:
538 The following actions are valid
542 reverse authorization
547 =head1 Setting IPPay parameters from content(%content)
549 The following rules are applied to map data to IPPay parameters
550 from content(%content):
552 # param => $content{<key>}
553 TransactionType => 'TransactionType',
554 TerminalID => 'login',
555 TransactionID => 'order_number',
556 RoutingCode => 'RoutingCode',
557 Approval => 'authorization',
558 BatchID => 'BatchID',
560 Password => 'password',
561 OrderNumber => 'invoice_number',
562 CardNum => 'card_number',
564 Issue => 'issue_number',
565 CardExpMonth => \( $month ), # MM from MM(-)YY(YY) of 'expiration'
566 CardExpYear => \( $year ), # YY from MM(-)YY(YY) of 'expiration'
567 CardStartMonth => \( $month ), # MM from MM(-)YY(YY) of 'card_start'
568 CardStartYear => \( $year ), # YY from MM(-)YY(YY) of 'card_start'
572 AccountNumber => 'account_number',
573 ABA => 'routing_code',
574 CheckNumber => 'check_number',
576 DispositionType => 'DispositionType',
577 TotalAmount => 'amount' reformatted into cents
578 FeeAmount => 'FeeAmount',
579 TaxAmount => 'TaxAmount',
580 BillingAddress => 'address',
581 BillingCity => 'city',
582 BillingStateProv => 'state',
583 BillingPostalCode => 'zip',
584 BillingCountry => 'country', # forced to ISO-3166-alpha-3
585 BillingPhone => 'phone',
587 UserIPAddr => 'customer_ip',
588 UserHost => 'UserHost',
589 UDField1 => 'UDField1',
590 UDField2 => 'UDField2',
591 UDField3 => 'UDField3',
592 ActionCode => 'ActionCode',
594 Type => 'IndustryInfo',
596 CustomerPO => 'CustomerPO',
597 ShippingMethod => 'ShippingMethod',
598 ShippingName => 'ship_name',
600 Address => 'ship_address',
602 StateProv => 'ship_state',
603 Country => 'ship_country', # forced to ISO-3166-alpha-3
604 Phone => 'ship_phone',
610 Version 0.07 changes the server name and path for IPPay's late 2012 update.
612 Business::OnlinePayment::IPPay uses IPPay XML Product Specifications version
615 See http://www.ippay.com/ for more information.
619 Original author: Jeff Finucane
621 Current maintainer: Ivan Kohler <ivan-ippay@freeside.biz>
623 Reverse Authorization patch from dougforpres
627 perl(1). L<Business::OnlinePayment>.