1 package Business::OnlinePayment;
11 $VERSION = eval $VERSION; # modperlstyle: convert the string into a number
15 failure_status => undef,
17 test_transaction => undef,
19 transaction_type => undef,
20 error_message => undef,
21 authorization => undef,
25 fraud_detect => undef,
26 server_response => undef,
27 maximum_risk => undef,
32 my($class,$processor,%data) = @_;
34 Carp::croak("unspecified processor") unless $processor;
36 my $subclass = "${class}::$processor";
37 if(!defined(&$subclass)) {
39 Carp::croak("unknown processor $processor ($@)") if $@;
42 my $self = bless {processor => $processor}, $subclass;
43 $self->build_subs(keys %fields);
45 if($self->can("set_defaults")) {
46 $self->set_defaults();
51 my $value = $data{$_};
53 $self->build_subs($key);
59 no warnings 'redefine';
60 my $submit = qualify_to_ref('submit', $subclass);
61 $self->{_child_submit} = \&$submit;
62 *{"${subclass}::submit"} = sub {
73 my ($self, $risk_transaction) = @_;
75 my %parent_content = $self->content();
76 $parent_content{action} = 'Fraud Detect';
77 $risk_transaction->content( %parent_content );
78 $risk_transaction->submit();
79 if ($risk_transaction->is_success()) {
80 if ( $risk_transaction->fraud_score <= $self->maximum_fraud_score()) {
81 $self->{_child_submit}->($self);
84 $self->error_message('Excessive risk from risk management');
87 $self->error_message('Error in risk detection stage: ' . $risk_transaction->error_message);
94 my $fraud_detection = $self->fraud_detect();
97 # early return if user does not want optional risk mgt
100 return $self->{_child_submit}->($self,@_) unless $fraud_detection && length $fraud_detection;
104 # Search for an appropriate FD module
107 foreach my $subclass ( q(Business::OnlinePayment::) . $fraud_detection,
108 q(Business::FraudDetect::).$fraud_detection) {
110 if (!defined(&$subclass)) {
111 eval "use $subclass";
113 Carp::croak("serious problem loading fraud_detection module ($@)") unless
114 $@ =~ m/^Can\'t locate/;
116 my $risk_tx = bless ( { processor => $fraud_detection } , $subclass );
117 $risk_tx->build_subs(keys %fields);
118 if ($risk_tx->can('set_defaults')) {
119 $risk_tx->set_defaults();
121 $risk_tx->_glean_parameters_from_parent($self);
122 return $self->_risk_detect($risk_tx);
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 Carp::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 Carp::croak("Processor subclass did not override submit function");
181 my %content = $self->content();
183 foreach(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 through
231 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, 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.
239 =head2 content(%content);
241 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:
247 Transaction type, supported types are:
248 Visa, MasterCard, American Express, Discover, Check (not all processors support all these transaction types).
252 Your login name to use for authentication to the online processor.
256 Your password to use for authentication to the online processor.
260 What to do with the transaction (currently available are: Normal Authorization, Authorization Only, Credit, Post Authorization)
264 A description of the transaction (used by some processors to send information to the client, normally not a required field).
268 The amount of the transaction, most processors dont want dollar signs and the like, just a floating point number.
270 =item * invoice_number
272 An invoice number, for your use and not normally required, many processors require this field to be a numeric only field.
276 A customer identifier, again not normally required.
280 The customers name, your processor may not require this.
284 The customers address (your processor may not require this unless you are requiring AVS Verification).
288 The customers city (your processor may not require this unless you are requiring AVS Verification).
292 The customers state (your processor may not require this unless you are requiring AVS Verification).
296 The customers zip code (your processor may not require this unless you are requiring AVS Verification).
304 Customer's phone number.
308 Customer's fax number.
312 Customer's email address.
316 Credit card number (obviously not required for non-credit card transactions).
320 Credit card expiration (obviously not required for non-credit card transactions).
322 =item * account_number
324 Bank account number for electronic checks or electronic funds transfer.
328 Bank's routing code for electronic checks or electronic funds transfer.
332 Bank's name for electronic checks or electronic funds transfer.
338 Submit the transaction to the processor for completion
342 Returns true if the transaction was submitted successfully, false if it failed (or undef if it has not been submitted yet).
344 =head2 failure_status();
346 If the transactdion failed, it can optionally return a specific failure status
347 (normalized, not gateway-specific). Currently defined statuses are: "expired",
348 "nsf" (non-sufficient funds), "stolen", "pickup", "blacklisted" and
349 "declined" (card/transaction declines only, not other errors).
351 Note that (as of Aug 2006) this is only supported by some of the newest
352 processor modules, and that, even if supported, a failure status is an entirely
353 optional field that is only set for specific kinds of failures.
355 =head2 result_code();
357 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.
359 =head2 test_transaction();
361 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).
363 =head2 require_avs();
365 Providing a true argument to this module will turn on address verification (if the processor supports it).
367 =head2 transaction_type();
369 Retrieve the transaction type (the 'type' argument to contents();). Generally only used internally, but provided in case it is useful.
371 =head2 error_message();
373 If the transaction has been submitted but was not accepted, this function will return the provided error message (if any) that the processor returned.
375 =head2 authorization();
377 If the transaction has been submitted and accepted, this function will provide you with the authorization code that the processor returned.
381 Retrieve or change the processor submission server address (CHANGE AT YOUR OWN RISK).
385 Retrieve or change the processor submission port (CHANGE AT YOUR OWN RISK).
389 Retrieve or change the processor submission path (CHANGE AT YOUR OWN RISK).
393 Jason Kohles, email@jasonkohles.com
395 (v3 rewrite) Ivan Kohler <ivan-business-onlinepayment@420.am>
399 THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
400 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
401 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
406 http://420.am/business-onlinepayment/
408 For verification of credit card checksums, see L<Business::CreditCard>.