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 "callback_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. PayPal
60 requires this; other gateways ignore it.
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 my $conf = new FS::Conf;
281 my $test_mode = $conf->exists('business-batchpayment-test_transaction');
282 $opt{'test_mode'} = 1 if $test_mode;
284 my $module = $self->gateway_module;
285 my $processor = eval {
286 Business::BatchPayment->create($module, $self->options, %opt)
288 die "failed to create Business::BatchPayment::$module object: $@"
291 die "$module does not support test mode"
292 if $test_mode and not $processor->does('Business::BatchPayment::TestMode');
299 # Used by FS::Upgrade to migrate to a new database.
304 my ($class, %opts) = @_;
307 warn "$me upgrading $class\n" if $DEBUG;
309 foreach ( qsearch( 'payment_gateway', { 'gateway_namespace' => '' } ) ) {
310 $_->gateway_namespace('Business::OnlinePayment'); #defaulting
311 my $error = $_->replace;
312 die "$class had error during upgrade replacement: $error" if $error;
322 L<FS::Record>, schema.html from the base documentation.