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 risk_management => undef,
36 risk_management_params => undef,
37 server_response => undef,
38 maximum_risk => undef,
44 my($class,$processor,%data) = @_;
46 Carp::croak("unspecified processor") unless $processor;
48 my $subclass = "${class}::$processor";
49 if(!defined(&$subclass)) {
51 Carp::croak("unknown processor $processor ($@)") if $@;
54 my $self = bless {processor => $processor}, $subclass;
55 $self->build_subs(keys %fields);
57 if($self->can("set_defaults")) {
58 $self->set_defaults();
63 my $value = $data{$_};
65 $self->build_subs($key);
71 no warnings 'redefine';
72 my $submit = qualify_to_ref('submit', $subclass);
73 $self->{_child_submit} = \&$submit;
74 *{"${subclass}::submit"} = sub {
76 $self->_pre_submit(@_);
85 my ($self, $risk_transaction) = @_;
87 my %parent_content = $self->content();
88 $parent_content{action} = 'Fraud Detect';
89 $risk_transaction->content( %parent_content );
90 $risk_transaction->submit();
91 if ($risk_transaction->is_success()) {
92 if ( $risk_transaction->risk_level <= $self->maximum_risk()) {
93 $self->{_child_submit}->($self);
96 $self->error_message('Excessive risk from risk management');
99 $self->error_message('Error in risk detection stage: ' . $risk_transaction->error_message);
100 $self->is_success(0);
106 my $risk_detection = $self->risk_management();
109 # early return if user does not want optional risk mgt
112 return $self->{_child_submit}->($self,@_) unless $risk_detection && length $risk_detection;
116 # Search for an appropriate FD module
119 foreach my $subclass ( q(Business::OnlinePayment::) . $risk_detection,
120 q(Business::FraudDetect::).$risk_detection) {
122 if (!defined(&$subclass)) {
123 eval "use $subclass";
125 Carp::croak("serious problem loading risk_detection module ($@)") unless
126 $@ =~ m/^Can\'t locate/;
128 my $risk_tx = bless ( { processor => $risk_detection } , $subclass );
129 $risk_tx->build_subs(keys %fields);
130 if ($risk_tx->can('set_defaults')) {
131 $risk_tx->set_defaults();
133 my %risk_params = %{$self->risk_management_params()};
134 foreach my $key ( keys %risk_params ) {
135 my $value = $risk_params{$key};
138 $risk_tx->build_subs($key);
139 $risk_tx->$key($value);
141 return $self->_risk_detect($risk_tx);
148 my($self,%params) = @_;
151 if($params{'type'}) { $self->transaction_type($params{'type'}); }
152 %{$self->{'_content'}} = %params;
154 return %{$self->{'_content'}};
157 sub required_fields {
158 my($self,@fields) = @_;
160 my %content = $self->content();
162 Carp::croak("missing required field $_") unless exists $content{$_};
167 my($self, @fields) = @_;
169 my %content = $self->content();
172 #foreach(@fields) { $new{$_} = $content{$_}; }
174 map { $_ => $content{$_} } grep defined $content{$_}, @fields;
180 my %content = $self->content();
181 foreach( keys %map ) {
182 $content{$map{$_}} = $content{$_};
184 $self->content(%content);
190 Carp::croak("Processor subclass did not override submit function");
196 my %content = $self->content();
198 foreach(keys %content) {
199 $dump .= "$_ = $content{$_}\n";
204 # didnt use AUTOLOAD because Net::SSLeay::AUTOLOAD passes right to
205 # AutoLoader::AUTOLOAD, instead of passing up the chain
208 no warnings 'redefine';
210 eval "sub $_ { my \$self = shift; if(\@_) { \$self->{$_} = shift; } return \$self->{$_}; }";
220 Business::OnlinePayment - Perl extension for online payment processing
224 use Business::OnlinePayment;
226 my $transaction = new Business::OnlinePayment($processor, %processor_info);
227 $transaction->content(
230 cardnumber => '1234123412341238',
231 expiration => '0100',
232 name => 'John Q Doe',
234 $transaction->submit();
236 if($transaction->is_success()) {
237 print "Card processed successfully: ".$transaction->authorization()."\n";
239 print "Card was rejected: ".$transaction->error_message()."\n";
244 Business::OnlinePayment is a generic module for processing payments through
245 online credit card processors, electronic cash systems, etc.
247 =head1 METHODS AND FUNCTIONS
249 =head2 new($processor, %processor_options);
251 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.
253 =head2 content(%content);
255 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:
261 Transaction type, supported types are:
262 Visa, MasterCard, American Express, Discover, Check (not all processors support all these transaction types).
266 Your login name to use for authentication to the online processor.
270 Your password to use for authentication to the online processor.
274 What to do with the transaction (currently available are: Normal Authorization, Authorization Only, Credit, Post Authorization)
278 A description of the transaction (used by some processors to send information to the client, normally not a required field).
282 The amount of the transaction, most processors dont want dollar signs and the like, just a floating point number.
284 =item * invoice_number
286 An invoice number, for your use and not normally required, many processors require this field to be a numeric only field.
290 A customer identifier, again not normally required.
294 The customers name, your processor may not require this.
298 The customers address (your processor may not require this unless you are requiring AVS Verification).
302 The customers city (your processor may not require this unless you are requiring AVS Verification).
306 The customers state (your processor may not require this unless you are requiring AVS Verification).
310 The customers zip code (your processor may not require this unless you are requiring AVS Verification).
318 Customer's phone number.
322 Customer's fax number.
326 Customer's email address.
330 Credit card number (obviously not required for non-credit card transactions).
334 Credit card expiration (obviously not required for non-credit card transactions).
336 =item * account_number
338 Bank account number for electronic checks or electronic funds transfer.
342 Bank's routing code for electronic checks or electronic funds transfer.
346 Bank's name for electronic checks or electronic funds transfer.
352 Submit the transaction to the processor for completion
356 Returns true if the transaction was submitted successfully, false if it failed (or undef if it has not been submitted yet).
358 =head2 failure_status();
360 If the transactdion failed, it can optionally return a specific failure status
361 (normalized, not gateway-specific). Currently defined statuses are: "expired",
362 "nsf" (non-sufficient funds), "stolen", "pickup", "blacklisted" and
363 "declined" (card/transaction declines only, not other errors).
365 Note that (as of Aug 2006) this is only supported by some of the newest
366 processor modules, and that, even if supported, a failure status is an entirely
367 optional field that is only set for specific kinds of failures.
369 =head2 result_code();
371 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.
373 =head2 test_transaction();
375 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).
377 =head2 require_avs();
379 Providing a true argument to this module will turn on address verification (if the processor supports it).
381 =head2 transaction_type();
383 Retrieve the transaction type (the 'type' argument to contents();). Generally only used internally, but provided in case it is useful.
385 =head2 error_message();
387 If the transaction has been submitted but was not accepted, this function will return the provided error message (if any) that the processor returned.
389 =head2 authorization();
391 If the transaction has been submitted and accepted, this function will provide you with the authorization code that the processor returned.
395 Retrieve or change the processor submission server address (CHANGE AT YOUR OWN RISK).
399 Retrieve or change the processor submission port (CHANGE AT YOUR OWN RISK).
403 Retrieve or change the processor submission path (CHANGE AT YOUR OWN RISK).
407 Jason Kohles, email@jasonkohles.com
409 (v3 rewrite) Ivan Kohler <ivan-business-onlinepayment@420.am>
413 THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
414 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
415 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
420 http://420.am/business-onlinepayment/
422 For verification of credit card checksums, see L<Business::CreditCard>.