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 = ();
16 authorization => undef,
17 error_message => undef,
18 failure_status => undef,
19 fraud_detect => undef,
21 maximum_risk => undef,
27 server_response => undef,
28 test_transaction => undef,
29 transaction_type => undef,
31 fraud_transaction_id => undef,
35 my($class,$processor,%data) = @_;
37 croak("unspecified processor") unless $processor;
39 my $subclass = "${class}::$processor";
41 croak("unknown processor $processor ($@)") if $@;
43 my $self = bless {processor => $processor}, $subclass;
44 $self->build_subs(keys %fields);
46 if($self->can("set_defaults")) {
47 $self->set_defaults(%data);
52 my $value = $data{$_};
54 $self->build_subs($key);
58 # "wrap" submit with _pre_submit only once
59 unless ( $Presubmit_Added{$subclass} ) {
60 my $real_submit = $subclass->can('submit');
62 no warnings 'redefine';
65 *{"${subclass}::submit"} = sub {
67 return unless $self->_pre_submit(@_);
68 return $real_submit->($self, @_);
76 my ($self, $risk_transaction) = @_;
78 my %parent_content = $self->content();
79 $parent_content{action} = 'Fraud Detect';
80 $risk_transaction->content( %parent_content );
81 $risk_transaction->submit();
82 if ($risk_transaction->is_success()) {
83 $self->fraud_score( $risk_transaction->fraud_score );
84 $self->fraud_transaction_id( $risk_transaction->fraud_transaction_id );
85 if ( $risk_transaction->fraud_score <= $self->maximum_fraud_score()) {
88 $self->error_message('Excessive risk from risk management');
91 $self->error_message('Error in risk detection stage: ' . $risk_transaction->error_message);
97 my @Fraud_Class_Path = qw(Business::OnlinePayment Business::FraudDetect);
101 my $fraud_detection = $self->fraud_detect();
103 # early return if user does not want optional risk mgt
104 return 1 unless $fraud_detection;
106 # Search for an appropriate FD module
107 foreach my $fraud_class ( @Fraud_Class_Path ) {
108 my $subclass = $fraud_class . "::" . $fraud_detection;
109 eval "use $subclass ()";
111 croak("error loading fraud_detection module ($@)")
112 unless ( $@ =~ m/^Can\'t locate/ );
114 my $risk_tx = bless( { processor => $fraud_detection }, $subclass );
115 $risk_tx->build_subs(keys %fields);
116 if ($risk_tx->can('set_defaults')) {
117 $risk_tx->set_defaults();
119 $risk_tx->_glean_parameters_from_parent($self);
120 return $self->_risk_detect($risk_tx);
123 croak("Unable to locate fraud_detection module $fraud_detection"
124 . " in \@INC under Fraud_Class_Path (\@Fraud_Class_Path"
125 . " contains: @Fraud_Class_Path) (\@INC contains: @INC)");
129 my($self,%params) = @_;
132 if($params{'type'}) { $self->transaction_type($params{'type'}); }
133 %{$self->{'_content'}} = %params;
135 return exists $self->{'_content'} ? %{$self->{'_content'}} : ();
138 sub required_fields {
139 my($self,@fields) = @_;
142 my %content = $self->content();
144 push(@missing, $_) unless exists $content{$_};
147 croak("missing required field(s): " . join(", ", @missing) . "\n")
152 my($self, @fields) = @_;
154 my %content = $self->content();
157 #foreach(@fields) { $new{$_} = $content{$_}; }
159 map { $_ => $content{$_} } grep defined $content{$_}, @fields;
165 my %content = $self->content();
166 foreach( keys %map ) {
167 $content{$map{$_}} = $content{$_};
169 $self->content(%content);
175 croak("Processor subclass did not override submit function");
181 my %content = $self->content();
183 foreach(sort keys %content) {
184 $dump .= "$_ = $content{$_}\n";
189 # didnt use AUTOLOAD because Net::SSLeay::AUTOLOAD passes right to
190 # AutoLoader::AUTOLOAD, instead of passing up the chain
195 next if($self->can($_));
196 eval "sub $_ { my \$self = shift; if(\@_) { \$self->{$_} = shift; } return \$self->{$_}; }";
206 Business::OnlinePayment - Perl extension for online payment processing
210 use Business::OnlinePayment;
212 my $transaction = new Business::OnlinePayment($processor, %processor_info);
213 $transaction->content(
216 card_number => '1234123412341238',
217 expiration => '0100',
218 name => 'John Q Doe',
220 $transaction->submit();
222 if($transaction->is_success()) {
223 print "Card processed successfully: ", $transaction->authorization(), "\n";
225 print "Card was rejected: ", $transaction->error_message(), "\n";
230 Business::OnlinePayment is a generic module for processing payments
231 through online credit card processors, electronic cash systems, etc.
233 =head1 METHODS AND FUNCTIONS
235 =head2 new($processor, %processor_options);
237 Create a new Business::OnlinePayment object, $processor is required,
238 and defines the online processor to use. If necessary, processor
239 options can be specified, currently supported options are 'Server',
240 'Port', and 'Path', which specify how to find the online processor
241 (https://server:port/path), but individual processor modules should
242 supply reasonable defaults for this information, override the defaults
243 only if absolutely necessary (especially path), as the processor
244 module was probably written with a specific target script in mind.
246 =head2 content(%content);
248 The information necessary for the transaction, this tends to vary a
249 little depending on the processor, so we have chosen to use a system
250 which defines specific fields in the frontend which get mapped to the
251 correct fields in the backend. The currently defined fields are:
253 =head3 PROCESSOR FIELDS
259 Your login name to use for authentication to the online processor.
263 Your password to use for authentication to the online processor.
267 =head3 GENERAL TRANSACTION FIELDS
273 Transaction type, supported types are: CC (credit card), ECHECK
274 (electronic check) and LEC (phone bill billing). Deprecated types
275 are: Visa, MasterCard, American Express, Discover, Check (not all
276 processors support all these transaction types).
280 What to do with the transaction (currently available are: Normal
281 Authorization, Authorization Only, Credit, Post Authorization,
282 Recurring Authorization, Modify Recurring Authorization,
283 Cancel Recurring Authorization)
287 A description of the transaction (used by some processors to send
288 information to the client, normally not a required field).
292 The amount of the transaction, most processors don't want dollar signs
293 and the like, just a floating point number.
295 =item * invoice_number
297 An invoice number, for your use and not normally required, many
298 processors require this field to be a numeric only field.
302 =head3 CUSTOMER INFO FIELDS
308 A customer identifier, again not normally required.
312 The customer's name, your processor may not require this.
318 The customer's first and last name as separate fields.
322 The customer's company name, not normally required.
326 The customer's address (your processor may not require this unless you
327 are requiring AVS Verification).
331 The customer's city (your processor may not require this unless you
332 are requiring AVS Verification).
336 The customer's state (your processor may not require this unless you
337 are requiring AVS Verification).
341 The customer's zip code (your processor may not require this unless
342 you are requiring AVS Verification).
348 =item * ship_first_name
350 =item * ship_last_name
364 These shipping address fields may be accepted by your processor.
365 Refer to the description for the corresponding non-ship field for
366 general information on each field.
370 Customer's phone number.
374 Customer's fax number.
378 Customer's email address.
382 IP Address from which the transaction originated.
386 =head3 CREDIT CARD FIELDS
396 CVV2 number (also called CVC2 or CID) is a three- or four-digit
397 security code used to reduce credit card fraud.
401 Credit card expiration.
405 Track 1 on the magnetic stripe (Card present only)
409 Track 2 on the magnetic stripe (Card present only)
411 =item * recurring billing
413 Recurring billing flag
417 =head3 ELECTRONIC CHECK FIELDS
421 =item * account_number
423 Bank account number for electronic checks or electronic funds
428 Bank's routing code for electronic checks or electronic funds
433 Account type for electronic checks or electronic funds transfer. Can be
434 (case-insensitive): B<Personal Checking>, B<Personal Savings>,
435 B<Business Checking> or B<Business Savings>.
439 Account holder's name for electronic checks or electronic funds
444 Bank's name for electronic checks or electronic funds transfer.
448 Check type for electronic checks or electronic funds transfer.
452 Customer organization type.
456 Customer's social security number. Typically only required for
457 electronic checks or electronic funds transfer.
461 Customer's driver's license number. Typically only required for
462 electronic checks or electronic funds transfer.
466 Customer's date of birth. Typically only required for electronic
467 checks or electronic funds transfer.
471 =head3 RECURRING BILLING FIELDS
477 Interval expresses the amount of time between billings: digits, whitespace
478 and units (currently "days" or "months" in either singular or plural form).
482 The date of the first transaction (used for processors which allow delayed
483 start) expressed as YYYY-MM-DD.
487 The number of cycles of interval length for which billing should occur
488 (inclusive of 'trial periods' if the processor supports recurring billing
489 at more than one rate)
495 Submit the transaction to the processor for completion
499 Returns true if the transaction was submitted successfully, false if
500 it failed (or undef if it has not been submitted yet).
502 =head2 failure_status();
504 If the transaction failed, it can optionally return a specific failure
505 status (normalized, not gateway-specific). Currently defined statuses
506 are: "expired", "nsf" (non-sufficient funds), "stolen", "pickup",
507 "blacklisted" and "declined" (card/transaction declines only, not
510 Note that (as of Aug 2006) this is only supported by some of the
511 newest processor modules, and that, even if supported, a failure
512 status is an entirely optional field that is only set for specific
515 =head2 result_code();
517 Returns the precise result code that the processor returned, these are
518 normally one letter codes that don't mean much unless you understand
519 the protocol they speak, you probably don't need this, but it's there
522 =head2 test_transaction();
524 Most processors provide a test mode, where submitted transactions will
525 not actually be charged or added to your batch, calling this function
526 with a true argument will turn that mode on if the processor supports
527 it, or generate a fatal error if the processor does not support a test
528 mode (which is probably better than accidentally making real charges).
530 =head2 require_avs();
532 Providing a true argument to this module will turn on address
533 verification (if the processor supports it).
535 =head2 transaction_type();
537 Retrieve the transaction type (the 'type' argument to contents()).
538 Generally only used internally, but provided in case it is useful.
540 =head2 error_message();
542 If the transaction has been submitted but was not accepted, this
543 function will return the provided error message (if any) that the
546 =head2 authorization();
548 If the transaction has been submitted and accepted, this function will
549 provide you with the authorization code that the processor returned.
553 Retrieve or change the processor submission server address (CHANGE AT
558 Retrieve or change the processor submission port (CHANGE AT YOUR OWN
563 Retrieve or change the processor submission path (CHANGE AT YOUR OWN
566 =head2 fraud_score();
568 Retrieve or change the fraud score from any Business::FraudDetect plugin
570 =head2 fraud_transaction_id();
572 Retrieve or change the transaction id from any Business::FraudDetect plugin
576 Jason Kohles, email@jasonkohles.com
578 (v3 rewrite) Ivan Kohler <ivan-business-onlinepayment@420.am>
580 Phil Lobbes E<lt>phil at perkpartners dot comE<gt>
584 Homepage: http://420.am/business-onlinepayment/
586 Development: http://420.am/business-onlinepayment/ng.html
590 Please direct current development questions, patches, etc. to the mailing list:
591 http://420.am/cgi-bin/mailman/listinfo/bop-devel/
595 The code is available from our public CVS repository:
597 export CVSROOT=":pserver:anonymous@cvs.freeside.biz:/home/cvs/cvsroot"
599 # The password for the user `anonymous' is `anonymous'.
600 cvs checkout Business-OnlinePayment
604 http://freeside.biz/cgi-bin/viewvc.cgi/Business-OnlinePayment/
606 Many (but by no means all!) processor plugins are also available in the same
609 http://freeside.biz/cgi-bin/viewvc.cgi/
613 THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
614 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
615 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
619 http://420.am/business-onlinepayment/
621 For verification of credit card checksums, see L<Business::CreditCard>.