1 package FS::payment_gateway;
4 use vars qw( @ISA $me $DEBUG );
5 use FS::Record qw( qsearch qsearchs dbh );
7 use FS::agent_payment_gateway;
9 @ISA = qw( FS::option_Common );
10 $me = '[ FS::payment_gateway ]';
15 FS::payment_gateway - Object methods for payment_gateway records
19 use FS::payment_gateway;
21 $record = new FS::payment_gateway \%hash;
22 $record = new FS::payment_gateway { 'column' => 'value' };
24 $error = $record->insert;
26 $error = $new_record->replace($old_record);
28 $error = $record->delete;
30 $error = $record->check;
34 An FS::payment_gateway object represents an payment gateway.
35 FS::payment_gateway inherits from FS::Record. The following fields are
40 =item gatewaynum - primary key
42 =item gateway_namespace - Business::OnlinePayment, Business::OnlineThirdPartyPayment, or Business::BatchPayment
44 =item gateway_module - Business::OnlinePayment:: (or other) module name
46 =item gateway_username - payment gateway username
48 =item gateway_password - payment gateway password
50 =item gateway_action - optional action or actions (multiple actions are separated with `,': for example: `Authorization Only, Post Authorization'). Defaults to `Normal Authorization'.
52 =item disabled - Disabled flag, empty or 'Y'
54 =item gateway_callback_url - For ThirdPartyPayment only, set to the URL that
55 the user should be redirected to on a successful payment. This will be sent
56 as a transaction parameter named "return_url".
58 =item gateway_cancel_url - For ThirdPartyPayment only, set to the URL that
59 the user should be redirected to if they cancel the transaction. This will
60 be sent as a transaction parameter named "cancel_url".
62 =item auto_resolve_status - For BatchPayment only, set to 'approve' to
63 auto-approve unresolved payments after some number of days, 'reject' to
64 auto-decline them, or null to do nothing.
66 =item auto_resolve_days - For BatchPayment, the number of days to wait before
67 auto-resolving the batch.
77 Creates a new payment gateway. To add the payment gateway to the database, see
80 Note that this stores the hash reference, not a distinct copy of the hash it
81 points to. You can ask the object for a copy with the I<hash> method.
85 # the new method can be inherited from FS::Record, if a table method is defined
87 sub table { 'payment_gateway'; }
91 Adds this record to the database. If there is an error, returns the error,
92 otherwise returns false.
96 # the insert method can be inherited from FS::Record
100 Delete this record from the database.
104 # the delete method can be inherited from FS::Record
106 =item replace OLD_RECORD
108 Replaces the OLD_RECORD with this one in the database. If there is an error,
109 returns the error, otherwise returns false.
113 # the replace method can be inherited from FS::Record
117 Checks all fields to make sure this is a valid payment gateway. If there is
118 an error, returns the error, otherwise returns false. Called by the insert
123 # the check method should currently be supplied - FS::Record contains some
124 # data checking routines
130 $self->ut_numbern('gatewaynum')
131 || $self->ut_alpha('gateway_module')
132 || $self->ut_enum('gateway_namespace', ['Business::OnlinePayment',
133 'Business::OnlineThirdPartyPayment',
134 'Business::BatchPayment',
136 || $self->ut_textn('gateway_username')
137 || $self->ut_anything('gateway_password')
138 || $self->ut_textn('gateway_callback_url') # a bit too permissive
139 || $self->ut_textn('gateway_cancel_url')
140 || $self->ut_enum('disabled', [ '', 'Y' ] )
141 || $self->ut_enum('auto_resolve_status', [ '', 'approve', 'reject' ])
142 || $self->ut_numbern('auto_resolve_days')
143 #|| $self->ut_textn('gateway_action')
145 return $error if $error;
147 if ( $self->gateway_namespace eq 'Business::BatchPayment' ) {
148 $self->gateway_action('Payment');
149 } elsif ( $self->gateway_action ) {
150 my @actions = split(/,\s*/, $self->gateway_action);
151 $self->gateway_action(
152 join( ',', map { /^(Normal Authorization|Authorization Only|Credit|Post Authorization)$/
153 or return "Unknown action $_";
160 $self->gateway_action('Normal Authorization');
163 # this little kludge mimics FS::CGI::popurl
164 #$self->gateway_callback_url($self->gateway_callback_url. '/')
165 # if ( $self->gateway_callback_url && $self->gateway_callback_url !~ /\/$/ );
170 =item agent_payment_gateway
172 Returns any agent overrides for this payment gateway.
176 sub agent_payment_gateway {
178 qsearch('agent_payment_gateway', { 'gatewaynum' => $self->gatewaynum } );
183 Disables this payment gateway: deletes all associated agent_payment_gateway
184 overrides and sets the I<disabled> field to "B<Y>".
191 local $SIG{HUP} = 'IGNORE';
192 local $SIG{INT} = 'IGNORE';
193 local $SIG{QUIT} = 'IGNORE';
194 local $SIG{TERM} = 'IGNORE';
195 local $SIG{TSTP} = 'IGNORE';
196 local $SIG{PIPE} = 'IGNORE';
198 my $oldAutoCommit = $FS::UID::AutoCommit;
199 local $FS::UID::AutoCommit = 0;
202 foreach my $agent_payment_gateway ( $self->agent_payment_gateway ) {
203 my $error = $agent_payment_gateway->delete;
205 $dbh->rollback if $oldAutoCommit;
206 return "error deleting agent_payment_gateway override: $error";
210 $self->disabled('Y');
211 my $error = $self->replace();
213 $dbh->rollback if $oldAutoCommit;
214 return "error disabling payment_gateway: $error";
217 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
224 Returns a semi-friendly label for the gateway.
230 $self->gatewaynum . ': ' .
231 ($self->gateway_username ? $self->gateway_username . '@' : '') .
232 $self->gateway_module
235 =item namespace_description
237 returns a friendly name for the namespace
241 my %namespace2description = (
243 'Business::OnlinePayment' => 'Direct',
244 'Business::OnlineThirdPartyPayment' => 'Hosted',
245 'Business::BatchPayment' => 'Batch',
248 sub namespace_description {
249 $namespace2description{shift->gateway_namespace} || 'Unknown';
252 =item batch_processor OPTIONS
254 For BatchPayment gateways only. Returns a
255 L<Business::BatchPayment::Processor> object to communicate with the
258 OPTIONS will be passed to the constructor, along with any gateway
259 options in the database for this L<FS::payment_gateway>. Useful things
260 to include there may include 'input' and 'output' (to direct transport
261 to files), 'debug', and 'test_mode'.
263 If the global 'business-batchpayment-test_transaction' flag is set,
264 'test_mode' will be forced on, and gateways that don't support test
265 mode will be disabled.
269 sub batch_processor {
273 my $batch = $opt{batch};
274 my $output = $opt{output};
275 die 'gateway '.$self->gatewaynum.' is not a Business::BatchPayment gateway'
276 unless $self->gateway_namespace eq 'Business::BatchPayment';
277 eval "use Business::BatchPayment;";
278 die "couldn't load Business::BatchPayment: $@" if $@;
280 #false laziness with processor
281 foreach (qw(username password)) {
282 if (length($self->get("gateway_$_"))) {
283 $opt{$_} = $self->get("gateway_$_");
287 my $module = $self->gateway_module;
288 my $processor = eval {
289 Business::BatchPayment->create($module, $self->options, %opt)
291 die "failed to create Business::BatchPayment::$module object: $@"
294 die "$module does not support test mode"
296 and not $processor->does('Business::BatchPayment::TestMode');
301 =item processor OPTIONS
303 Loads the module for the processor and returns an instance of it.
311 foreach (qw(action username password)) {
312 if (length($self->get("gateway_$_"))) {
313 $opt{$_} = $self->get("gateway_$_");
316 $opt{'return_url'} = $self->gateway_callback_url;
317 $opt{'cancel_url'} = $self->gateway_cancel_url;
319 my $conf = new FS::Conf;
320 my $test_mode = $conf->exists('business-batchpayment-test_transaction');
321 $opt{'test_mode'} = 1 if $test_mode;
323 my $namespace = $self->gateway_namespace;
324 eval "use $namespace";
325 die "couldn't load $namespace: $@" if $@;
327 if ( $namespace eq 'Business::BatchPayment' ) {
328 # at some point we can merge these, but there's enough special behavior...
329 return $self->batch_processor(%opt);
331 return $namespace->new( $self->gateway_module, $self->options, %opt );
335 =item default_gateway OPTIONS
339 Returns default gateway (from business-onlinepayment conf) as a payment_gateway object.
343 conf - existing conf object
345 nofatal - return blank instead of dying if no default gateway is configured
347 method - if set to CHEK or ECHECK, returns object for business-onlinepayment-ach if available
349 Before using this, be sure you wouldn't rather be using L</by_key_or_default> or,
350 more likely, L<FS::agent/payment_gateway>.
354 # the standard settings from the config could be moved to a null agent
355 # agent_payment_gateway referenced payment_gateway
357 sub default_gateway {
358 my ($self,%options) = @_;
360 $options{'conf'} ||= new FS::Conf;
361 my $conf = $options{'conf'};
363 unless ( $conf->exists('business-onlinepayment') ) {
364 if ( $options{'nofatal'} ) {
367 die "Real-time processing not enabled\n";
372 my $bop_config = 'business-onlinepayment';
373 $bop_config .= '-ach'
374 if ( $options{method}
375 && $options{method} =~ /^(ECHECK|CHEK)$/
376 && $conf->exists($bop_config. '-ach')
378 my ( $processor, $login, $password, $action, @bop_options ) =
379 $conf->config($bop_config);
380 $action ||= 'normal authorization';
381 pop @bop_options if scalar(@bop_options) % 2 && $bop_options[-1] =~ /^\s*$/;
382 die "No real-time processor is enabled - ".
383 "did you set the business-onlinepayment configuration value?\n"
386 my $payment_gateway = new FS::payment_gateway;
387 $payment_gateway->gateway_namespace( $conf->config('business-onlinepayment-namespace') ||
388 'Business::OnlinePayment');
389 $payment_gateway->gateway_module($processor);
390 $payment_gateway->gateway_username($login);
391 $payment_gateway->gateway_password($password);
392 $payment_gateway->gateway_action($action);
393 $payment_gateway->set('options', [ @bop_options ]);
394 return $payment_gateway;
397 =item by_key_with_namespace GATEWAYNUM
399 Like usual by_key, but makes sure namespace is set,
400 and dies if not found.
404 sub by_key_with_namespace {
406 my $payment_gateway = $self->by_key(@_);
407 die "payment_gateway not found"
408 unless $payment_gateway;
409 $payment_gateway->gateway_namespace('Business::OnlinePayment')
410 unless $payment_gateway->gateway_namespace;
411 return $payment_gateway;
414 =item by_key_or_default OPTIONS
416 Either returns the gateway specified by option gatewaynum, or the default gateway.
418 Accepts the same options as L</default_gateway>.
420 Also ensures that the gateway_namespace has been set.
424 sub by_key_or_default {
425 my ($self,%options) = @_;
427 if ($options{'gatewaynum'}) {
428 return $self->by_key_with_namespace($options{'gatewaynum'});
430 return $self->default_gateway(%options);
434 # if it weren't for the way gateway_namespace default is set, this method would not be necessary
435 # that should really go in check() with an accompanying upgrade, so we could just use qsearch safely,
436 # but currently short on time to test deeper changes...
438 # if no default gateway is set and nofatal is passed, first value returned is blank string
440 my ($self,%options) = @_;
442 foreach my $gatewaynum ('',( map {$_->gatewaynum} qsearch('payment_gateway') )) {
443 push @out, $self->by_key_or_default( %options, gatewaynum => $gatewaynum );
450 # Used by FS::Upgrade to migrate to a new database.
455 my ($class, %opts) = @_;
458 warn "$me upgrading $class\n" if $DEBUG;
460 foreach ( qsearch( 'payment_gateway', { 'gateway_namespace' => '' } ) ) {
461 $_->gateway_namespace('Business::OnlinePayment'); #defaulting
462 my $error = $_->replace;
463 die "$class had error during upgrade replacement: $error" if $error;
473 L<FS::Record>, schema.html from the base documentation.