1 package Business::OnlinePayment;
4 use vars qw($VERSION); # @ISA @EXPORT @EXPORT_OK $AUTOLOAD);
13 #@ISA = (); #qw(Exporter AutoLoader);
18 sub VERSION { #Argument "3.00_01" isn't numeric in subroutine entry
20 UNIVERSAL::VERSION(@_);
25 failure_status => undef,
27 test_transaction => undef,
29 transaction_type => undef,
30 error_message => undef,
31 authorization => undef,
35 fraud_detect => undef,
36 server_response => undef,
37 maximum_risk => undef,
43 my($class,$processor,%data) = @_;
45 Carp::croak("unspecified processor") unless $processor;
47 my $subclass = "${class}::$processor";
48 if(!defined(&$subclass)) {
50 Carp::croak("unknown processor $processor ($@)") if $@;
53 my $self = bless {processor => $processor}, $subclass;
54 $self->build_subs(keys %fields);
56 if($self->can("set_defaults")) {
57 $self->set_defaults();
62 my $value = $data{$_};
64 $self->build_subs($key);
70 no warnings 'redefine';
71 my $submit = qualify_to_ref('submit', $subclass);
72 $self->{_child_submit} = \&$submit;
73 *{"${subclass}::submit"} = sub {
84 my ($self, $risk_transaction) = @_;
86 my %parent_content = $self->content();
87 $parent_content{action} = 'Fraud Detect';
88 $risk_transaction->content( %parent_content );
89 $risk_transaction->submit();
90 if ($risk_transaction->is_success()) {
91 if ( $risk_transaction->fraud_score <= $self->maximum_fraud_score()) {
92 $self->{_child_submit}->($self);
95 $self->error_message('Excessive risk from risk management');
98 $self->error_message('Error in risk detection stage: ' . $risk_transaction->error_message);
105 my $fraud_detection = $self->fraud_detect();
108 # early return if user does not want optional risk mgt
111 return $self->{_child_submit}->($self,@_) unless $fraud_detection && length $fraud_detection;
115 # Search for an appropriate FD module
118 foreach my $subclass ( q(Business::OnlinePayment::) . $fraud_detection,
119 q(Business::FraudDetect::).$fraud_detection) {
121 if (!defined(&$subclass)) {
122 eval "use $subclass";
124 Carp::croak("serious problem loading fraud_detection module ($@)") unless
125 $@ =~ m/^Can\'t locate/;
127 my $risk_tx = bless ( { processor => $fraud_detection } , $subclass );
128 $risk_tx->build_subs(keys %fields);
129 if ($risk_tx->can('set_defaults')) {
130 $risk_tx->set_defaults();
132 $risk_tx->_glean_parameters_from_parent($self);
133 return $self->_risk_detect($risk_tx);
140 my($self,%params) = @_;
143 if($params{'type'}) { $self->transaction_type($params{'type'}); }
144 %{$self->{'_content'}} = %params;
146 return %{$self->{'_content'}};
149 sub required_fields {
150 my($self,@fields) = @_;
152 my %content = $self->content();
154 Carp::croak("missing required field $_") unless exists $content{$_};
159 my($self, @fields) = @_;
161 my %content = $self->content();
164 #foreach(@fields) { $new{$_} = $content{$_}; }
166 map { $_ => $content{$_} } grep defined $content{$_}, @fields;
172 my %content = $self->content();
173 foreach( keys %map ) {
174 $content{$map{$_}} = $content{$_};
176 $self->content(%content);
182 Carp::croak("Processor subclass did not override submit function");
188 my %content = $self->content();
190 foreach(keys %content) {
191 $dump .= "$_ = $content{$_}\n";
196 # didnt use AUTOLOAD because Net::SSLeay::AUTOLOAD passes right to
197 # AutoLoader::AUTOLOAD, instead of passing up the chain
200 no warnings 'redefine';
202 eval "sub $_ { my \$self = shift; if(\@_) { \$self->{$_} = shift; } return \$self->{$_}; }";
212 Business::OnlinePayment - Perl extension for online payment processing
216 use Business::OnlinePayment;
218 my $transaction = new Business::OnlinePayment($processor, %processor_info);
219 $transaction->content(
222 cardnumber => '1234123412341238',
223 expiration => '0100',
224 name => 'John Q Doe',
226 $transaction->submit();
228 if($transaction->is_success()) {
229 print "Card processed successfully: ".$transaction->authorization()."\n";
231 print "Card was rejected: ".$transaction->error_message()."\n";
236 Business::OnlinePayment is a generic module for processing payments through
237 online credit card processors, electronic cash systems, etc.
239 =head1 METHODS AND FUNCTIONS
241 =head2 new($processor, %processor_options);
243 Create a new Business::OnlinePayment object, $processor is required, and defines the online processor to use. If necessary, processor options can be specified, currently supported options are 'Server', 'Port', and 'Path', which specify how to find the online processor (https://server:port/path), but individual processor modules should supply reasonable defaults for this information, override the defaults only if absolutely necessary (especially path), as the processor module was probably written with a specific target script in mind.
245 =head2 content(%content);
247 The information necessary for the transaction, this tends to vary a little depending on the processor, so we have chosen to use a system which defines specific fields in the frontend which get mapped to the correct fields in the backend. The currently defined fields are:
253 Transaction type, supported types are:
254 Visa, MasterCard, American Express, Discover, Check (not all processors support all these transaction types).
258 Your login name to use for authentication to the online processor.
262 Your password to use for authentication to the online processor.
266 What to do with the transaction (currently available are: Normal Authorization, Authorization Only, Credit, Post Authorization)
270 A description of the transaction (used by some processors to send information to the client, normally not a required field).
274 The amount of the transaction, most processors dont want dollar signs and the like, just a floating point number.
276 =item * invoice_number
278 An invoice number, for your use and not normally required, many processors require this field to be a numeric only field.
282 A customer identifier, again not normally required.
286 The customers name, your processor may not require this.
290 The customers address (your processor may not require this unless you are requiring AVS Verification).
294 The customers city (your processor may not require this unless you are requiring AVS Verification).
298 The customers state (your processor may not require this unless you are requiring AVS Verification).
302 The customers zip code (your processor may not require this unless you are requiring AVS Verification).
310 Customer's phone number.
314 Customer's fax number.
318 Customer's email address.
322 Credit card number (obviously not required for non-credit card transactions).
326 Credit card expiration (obviously not required for non-credit card transactions).
328 =item * account_number
330 Bank account number for electronic checks or electronic funds transfer.
334 Bank's routing code for electronic checks or electronic funds transfer.
338 Bank's name for electronic checks or electronic funds transfer.
344 Submit the transaction to the processor for completion
348 Returns true if the transaction was submitted successfully, false if it failed (or undef if it has not been submitted yet).
350 =head2 failure_status();
352 If the transactdion failed, it can optionally return a specific failure status
353 (normalized, not gateway-specific). Currently defined statuses are: "expired",
354 "nsf" (non-sufficient funds), "stolen", "pickup", "blacklisted" and
355 "declined" (card/transaction declines only, not other errors).
357 Note that (as of Aug 2006) this is only supported by some of the newest
358 processor modules, and that, even if supported, a failure status is an entirely
359 optional field that is only set for specific kinds of failures.
361 =head2 result_code();
363 Returns the precise result code that the processor returned, these are normally one letter codes that don't mean much unless you understand the protocol they speak, you probably don't need this, but it's there just in case.
365 =head2 test_transaction();
367 Most processors provide a test mode, where submitted transactions will not actually be charged or added to your batch, calling this function with a true argument will turn that mode on if the processor supports it, or generate a fatal error if the processor does not support a test mode (which is probably better than accidentally making real charges).
369 =head2 require_avs();
371 Providing a true argument to this module will turn on address verification (if the processor supports it).
373 =head2 transaction_type();
375 Retrieve the transaction type (the 'type' argument to contents();). Generally only used internally, but provided in case it is useful.
377 =head2 error_message();
379 If the transaction has been submitted but was not accepted, this function will return the provided error message (if any) that the processor returned.
381 =head2 authorization();
383 If the transaction has been submitted and accepted, this function will provide you with the authorization code that the processor returned.
387 Retrieve or change the processor submission server address (CHANGE AT YOUR OWN RISK).
391 Retrieve or change the processor submission port (CHANGE AT YOUR OWN RISK).
395 Retrieve or change the processor submission path (CHANGE AT YOUR OWN RISK).
399 Jason Kohles, email@jasonkohles.com
401 (v3 rewrite) Ivan Kohler <ivan-business-onlinepayment@420.am>
405 THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
406 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
407 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
412 http://420.am/business-onlinepayment/
414 For verification of credit card checksums, see L<Business::CreditCard>.