1 package Business::OnlinePayment;
10 $VERSION = eval $VERSION; # modperlstyle: convert the string into a number
12 # Remember subclasses we have "wrapped" submit() with _pre_submit()
13 my %Presubmit_Added = ();
36 my($class,$processor,%data) = @_;
38 croak("unspecified processor") unless $processor;
40 my $subclass = "${class}::$processor";
42 croak("unknown processor $processor ($@)") if $@;
44 my $self = bless {processor => $processor}, $subclass;
45 $self->build_subs(@methods);
47 if($self->can("set_defaults")) {
48 $self->set_defaults(%data);
53 my $value = $data{$_};
55 $self->build_subs($key);
59 # "wrap" submit with _pre_submit only once
60 unless ( $Presubmit_Added{$subclass} ) {
61 my $real_submit = $subclass->can('submit');
63 no warnings 'redefine';
66 *{"${subclass}::submit"} = sub {
68 return unless $self->_pre_submit(@_);
69 return $real_submit->($self, @_);
77 my ($self, $risk_transaction) = @_;
79 my %parent_content = $self->content();
80 $parent_content{action} = 'Fraud Detect';
81 $risk_transaction->content( %parent_content );
82 $risk_transaction->submit();
83 if ($risk_transaction->is_success()) {
84 $self->fraud_score( $risk_transaction->fraud_score );
85 $self->fraud_transaction_id( $risk_transaction->fraud_transaction_id );
86 if ( $risk_transaction->fraud_score <= $self->maximum_fraud_score()) {
89 $self->error_message('Excessive risk from risk management');
92 $self->error_message('Error in risk detection stage: ' . $risk_transaction->error_message);
98 my @Fraud_Class_Path = qw(Business::OnlinePayment Business::FraudDetect);
102 my $fraud_detection = $self->fraud_detect();
104 # early return if user does not want optional risk mgt
105 return 1 unless $fraud_detection;
107 # Search for an appropriate FD module
108 foreach my $fraud_class ( @Fraud_Class_Path ) {
109 my $subclass = $fraud_class . "::" . $fraud_detection;
110 eval "use $subclass ()";
112 croak("error loading fraud_detection module ($@)")
113 unless ( $@ =~ m/^Can\'t locate/ );
115 my $risk_tx = bless( { processor => $fraud_detection }, $subclass );
116 $risk_tx->build_subs(@methods);
117 if ($risk_tx->can('set_defaults')) {
118 $risk_tx->set_defaults();
120 $risk_tx->_glean_parameters_from_parent($self);
121 return $self->_risk_detect($risk_tx);
124 croak("Unable to locate fraud_detection module $fraud_detection"
125 . " in \@INC under Fraud_Class_Path (\@Fraud_Class_Path"
126 . " contains: @Fraud_Class_Path) (\@INC contains: @INC)");
130 my($self,%params) = @_;
133 if($params{'type'}) { $self->transaction_type($params{'type'}); }
134 %{$self->{'_content'}} = %params;
136 return exists $self->{'_content'} ? %{$self->{'_content'}} : ();
139 sub required_fields {
140 my($self,@fields) = @_;
143 my %content = $self->content();
145 push(@missing, $_) unless exists $content{$_};
148 croak("missing required field(s): " . join(", ", @missing) . "\n")
153 my($self, @fields) = @_;
155 my %content = $self->content();
158 #foreach(@fields) { $new{$_} = $content{$_}; }
160 map { $_ => $content{$_} } grep defined $content{$_}, @fields;
166 my %content = $self->content();
167 foreach( keys %map ) {
168 $content{$map{$_}} = $content{$_};
170 $self->content(%content);
176 croak("Processor subclass did not override submit function");
182 my %content = $self->content();
184 foreach(sort keys %content) {
185 $dump .= "$_ = $content{$_}\n";
190 # didnt use AUTOLOAD because Net::SSLeay::AUTOLOAD passes right to
191 # AutoLoader::AUTOLOAD, instead of passing up the chain
196 next if($self->can($_));
197 eval "sub $_ { my \$self = shift; if(\@_) { \$self->{$_} = shift; } return \$self->{$_}; }";
204 my( $self, $value ) = @_;
205 return 1 if $value =~ /^[yt]/i;
206 return 0 if $value =~ /^[fn]/i;
207 #return 1 if $value == 1;
208 #return 0 if $value == 0;
218 Business::OnlinePayment - Perl extension for online payment processing
222 use Business::OnlinePayment;
224 my $transaction = new Business::OnlinePayment($processor, %processor_info);
225 $transaction->content(
228 card_number => '1234123412341238',
229 expiration => '0100',
230 name => 'John Q Doe',
232 $transaction->submit();
234 if($transaction->is_success()) {
235 print "Card processed successfully: ", $transaction->authorization(), "\n";
237 print "Card was rejected: ", $transaction->error_message(), "\n";
242 Business::OnlinePayment is a generic module for processing payments
243 through online credit card processors, electronic cash systems, etc.
247 =head2 new($processor, %processor_options)
249 Create a new Business::OnlinePayment object, $processor is required,
250 and defines the online processor to use. If necessary, processor
251 options can be specified, currently supported options are 'Server',
252 'Port', and 'Path', which specify how to find the online processor
253 (https://server:port/path), but individual processor modules should
254 supply reasonable defaults for this information, override the defaults
255 only if absolutely necessary (especially path), as the processor
256 module was probably written with a specific target script in mind.
258 =head1 TRANSACTION SETUP METHODS
260 =head2 content(%content)
262 The information necessary for the transaction, this tends to vary a
263 little depending on the processor, so we have chosen to use a system
264 which defines specific fields in the frontend which get mapped to the
265 correct fields in the backend. The currently defined fields are:
267 =head3 PROCESSOR FIELDS
273 Your login name to use for authentication to the online processor.
277 Your password to use for authentication to the online processor.
281 =head3 REQUIRED TRANSACTION FIELDS
287 Transaction type, supported types are: CC (credit card), ECHECK
288 (electronic check) and LEC (phone bill billing). Deprecated types
289 are: Visa, MasterCard, American Express, Discover, Check. Not all
290 processors support all transaction types.
294 What to do with the transaction (currently available are: Normal
295 Authorization, Authorization Only, Credit, Post Authorization,
296 Recurring Authorization, Modify Recurring Authorization,
297 Cancel Recurring Authorization)
301 The amount of the transaction. No dollar signs or currency identifiers,
302 just a whole or floating point number (i.e. 26, 26.1 or 26.13).
306 =head3 OPTIONAL TRANSACTION FIELDS
312 A description of the transaction (used by some processors to send
313 information to the client, normally not a required field).
317 An invoice number, for your use and not normally required, many
318 processors require this field to be a numeric only field.
322 Purchase order number (normally not required).
326 Tax amount (portion of amount field, not added to it).
330 Freight amount (portion of amount field, not added to it).
334 Duty amount (portion of amount field, not added to it).
338 Tax exempt flag (i.e. TRUE, FALSE, T, F, YES, NO, Y, N, 1, 0).
342 =head3 CUSTOMER INFO FIELDS
348 A customer identifier, again not normally required.
352 The customer's name, your processor may not require this.
358 The customer's first and last name as separate fields.
362 The customer's company name, not normally required.
366 The customer's address (your processor may not require this unless you
367 are requiring AVS Verification).
371 The customer's city (your processor may not require this unless you
372 are requiring AVS Verification).
376 The customer's state (your processor may not require this unless you
377 are requiring AVS Verification).
381 The customer's zip code (your processor may not require this unless
382 you are requiring AVS Verification).
388 =item ship_first_name
404 These shipping address fields may be accepted by your processor.
405 Refer to the description for the corresponding non-ship field for
406 general information on each field.
410 Customer's phone number.
414 Customer's fax number.
418 Customer's email address.
422 IP Address from which the transaction originated.
426 =head3 CREDIT CARD FIELDS
436 CVV2 number (also called CVC2 or CID) is a three- or four-digit
437 security code used to reduce credit card fraud.
441 Credit card expiration.
445 Track 1 on the magnetic stripe (Card present only)
449 Track 2 on the magnetic stripe (Card present only)
451 =item recurring billing
453 Recurring billing flag
457 =head3 ELECTRONIC CHECK FIELDS
463 Bank account number for electronic checks or electronic funds
468 Bank's routing code for electronic checks or electronic funds
473 Account type for electronic checks or electronic funds transfer. Can be
474 (case-insensitive): B<Personal Checking>, B<Personal Savings>,
475 B<Business Checking> or B<Business Savings>.
479 Account holder's name for electronic checks or electronic funds
484 Bank's name for electronic checks or electronic funds transfer.
488 Check type for electronic checks or electronic funds transfer.
492 Customer organization type.
496 Customer's social security number. Typically only required for
497 electronic checks or electronic funds transfer.
501 Customer's driver's license number. Typically only required for
502 electronic checks or electronic funds transfer.
506 Customer's date of birth. Typically only required for electronic
507 checks or electronic funds transfer.
511 =head3 RECURRING BILLING FIELDS
517 Interval expresses the amount of time between billings: digits, whitespace
518 and units (currently "days" or "months" in either singular or plural form).
522 The date of the first transaction (used for processors which allow delayed
523 start) expressed as YYYY-MM-DD.
527 The number of cycles of interval length for which billing should occur
528 (inclusive of 'trial periods' if the processor supports recurring billing
529 at more than one rate)
533 =head2 test_transaction()
535 Most processors provide a test mode, where submitted transactions will
536 not actually be charged or added to your batch, calling this function
537 with a true argument will turn that mode on if the processor supports
538 it, or generate a fatal error if the processor does not support a test
539 mode (which is probably better than accidentally making real charges).
543 Providing a true argument to this module will turn on address
544 verification (if the processor supports it).
546 =head1 TRANSACTION SUBMISSION METHOD
550 Submit the transaction to the processor for completion
552 =head1 TRANSACTION RESULT METHODS
556 Returns true if the transaction was submitted successfully, false if
557 it failed (or undef if it has not been submitted yet).
559 =head2 error_message()
561 If the transaction has been submitted but was not accepted, this
562 function will return the provided error message (if any) that the
565 =head2 failure_status()
567 If the transaction failed, it can optionally return a specific failure
568 status (normalized, not gateway-specific). Currently defined statuses
569 are: "expired", "nsf" (non-sufficient funds), "stolen", "pickup",
570 "blacklisted" and "declined" (card/transaction declines only, not
573 Note that (as of Aug 2006) this is only supported by some of the
574 newest processor modules, and that, even if supported, a failure
575 status is an entirely optional field that is only set for specific
578 =head2 authorization()
580 If the transaction has been submitted and accepted, this function will
581 provide you with the authorization code that the processor returned.
582 Store this if you would like to run inquiries or refunds on the transaction
585 =head2 order_number()
587 The unique order number for the transaction generated by the gateway. Store
588 this if you would like to run inquiries or refunds on the transaction later.
592 Retrieve or change the fraud score from any Business::FraudDetect plugin
594 =head2 fraud_transaction_id()
596 Retrieve or change the transaction id from any Business::FraudDetect plugin
600 Returns the precise result code that the processor returned, these are
601 normally one letter codes that don't mean much unless you understand
602 the protocol they speak, you probably don't need this, but it's there
605 =head1 MISCELLANEOUS INTERNAL METHODS
607 =head2 transaction_type()
609 Retrieve the transaction type (the 'type' argument to contents()).
610 Generally only used internally, but provided in case it is useful.
614 Retrieve or change the processor submission server address (CHANGE AT
619 Retrieve or change the processor submission port (CHANGE AT YOUR OWN
624 Retrieve or change the processor submission path (CHANGE AT YOUR OWN
627 =head1 HELPER METHODS FOR GATEWAY MODULE AUTHORS
629 =head2 build_subs( @sub_names )
631 Build setter/getter subroutines for new return values.
633 =head2 get_fields( @fields )
635 Get the named fields if they are defined.
637 =head2 remap_fields( %map )
639 Remap field content (and stuff it back into content).
641 =head2 required_fields( @fields )
643 Croaks if any of the required fields are not present.
647 =head2 silly_bool( $value )
649 Returns 0 if the value starts with y, Y, t or T.
650 Returns 1 if the value starts with n, N, f or F.
651 Otherwise returns the value itself.
653 Use this for handling boolean content like tax_exempt.
657 Jason Kohles, email@jasonkohles.com
659 (v3 rewrite) Ivan Kohler <ivan-business-onlinepayment@420.am>
661 Phil Lobbes E<lt>phil at perkpartners dot comE<gt>
665 Homepage: http://420.am/business-onlinepayment/
667 Development: http://420.am/business-onlinepayment/ng.html
671 Please direct current development questions, patches, etc. to the mailing list:
672 http://420.am/cgi-bin/mailman/listinfo/bop-devel/
676 The code is available from our public CVS repository:
678 export CVSROOT=":pserver:anonymous@cvs.freeside.biz:/home/cvs/cvsroot"
680 # The password for the user `anonymous' is `anonymous'.
681 cvs checkout Business-OnlinePayment
685 http://freeside.biz/cgi-bin/viewvc.cgi/Business-OnlinePayment/
687 Many (but by no means all!) processor plugins are also available in the same
690 http://freeside.biz/cgi-bin/viewvc.cgi/
694 THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
695 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
696 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
700 http://420.am/business-onlinepayment/
702 For verification of credit card checksums, see L<Business::CreditCard>.