assign a size to svc_phone.sms_account
[freeside.git] / FS / FS / payment_gateway.pm
1 package FS::payment_gateway;
2
3 use strict;
4 use vars qw( @ISA $me $DEBUG );
5 use FS::Record qw( qsearch qsearchs dbh );
6 use FS::option_Common;
7 use FS::agent_payment_gateway;
8
9 @ISA = qw( FS::option_Common );
10 $me = '[ FS::payment_gateway ]';
11 $DEBUG=0;
12
13 =head1 NAME
14
15 FS::payment_gateway - Object methods for payment_gateway records
16
17 =head1 SYNOPSIS
18
19   use FS::payment_gateway;
20
21   $record = new FS::payment_gateway \%hash;
22   $record = new FS::payment_gateway { 'column' => 'value' };
23
24   $error = $record->insert;
25
26   $error = $new_record->replace($old_record);
27
28   $error = $record->delete;
29
30   $error = $record->check;
31
32 =head1 DESCRIPTION
33
34 An FS::payment_gateway object represents an payment gateway.
35 FS::payment_gateway inherits from FS::Record.  The following fields are
36 currently supported:
37
38 =over 4
39
40 =item gatewaynum - primary key
41
42 =item gateway_namespace - Business::OnlinePayment, Business::OnlineThirdPartyPayment, or Business::BatchPayment
43
44 =item gateway_module - Business::OnlinePayment:: (or other) module name
45
46 =item gateway_username - payment gateway username
47
48 =item gateway_password - payment gateway password
49
50 =item gateway_action - optional action or actions (multiple actions are separated with `,': for example: `Authorization Only, Post Authorization').  Defaults to `Normal Authorization'.
51
52 =item disabled - Disabled flag, empty or 'Y'
53
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").
57
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.
61
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.
65
66 =item auto_resolve_days - For BatchPayment, the number of days to wait before 
67 auto-resolving the batch.
68
69 =back
70
71 =head1 METHODS
72
73 =over 4
74
75 =item new HASHREF
76
77 Creates a new payment gateway.  To add the payment gateway to the database, see
78 L<"insert">.
79
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.
82
83 =cut
84
85 # the new method can be inherited from FS::Record, if a table method is defined
86
87 sub table { 'payment_gateway'; }
88
89 =item insert
90
91 Adds this record to the database.  If there is an error, returns the error,
92 otherwise returns false.
93
94 =cut
95
96 # the insert method can be inherited from FS::Record
97
98 =item delete
99
100 Delete this record from the database.
101
102 =cut
103
104 # the delete method can be inherited from FS::Record
105
106 =item replace OLD_RECORD
107
108 Replaces the OLD_RECORD with this one in the database.  If there is an error,
109 returns the error, otherwise returns false.
110
111 =cut
112
113 # the replace method can be inherited from FS::Record
114
115 =item check
116
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
119 and replace methods.
120
121 =cut
122
123 # the check method should currently be supplied - FS::Record contains some
124 # data checking routines
125
126 sub check {
127   my $self = shift;
128
129   my $error = 
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',
135                                            ] )
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')
144   ;
145   return $error if $error;
146
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 $_";
154                        $1
155                      }
156                      @actions
157           )
158    );
159   } else {
160     $self->gateway_action('Normal Authorization');
161   }
162
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 !~ /\/$/ );
166
167   $self->SUPER::check;
168 }
169
170 =item agent_payment_gateway
171
172 Returns any agent overrides for this payment gateway.
173
174 =cut
175
176 sub agent_payment_gateway {
177   my $self = shift;
178   qsearch('agent_payment_gateway', { 'gatewaynum' => $self->gatewaynum } );
179 }
180
181 =item disable
182
183 Disables this payment gateway: deletes all associated agent_payment_gateway
184 overrides and sets the I<disabled> field to "B<Y>".
185
186 =cut
187
188 sub disable {
189   my $self = shift;
190
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';
197
198   my $oldAutoCommit = $FS::UID::AutoCommit;
199   local $FS::UID::AutoCommit = 0;
200   my $dbh = dbh;
201
202   foreach my $agent_payment_gateway ( $self->agent_payment_gateway ) {
203     my $error = $agent_payment_gateway->delete;
204     if ( $error ) {
205       $dbh->rollback if $oldAutoCommit;
206       return "error deleting agent_payment_gateway override: $error";
207     }
208   }
209
210   $self->disabled('Y');
211   my $error = $self->replace();
212   if ( $error ) {
213     $dbh->rollback if $oldAutoCommit;
214     return "error disabling payment_gateway: $error";
215   }
216
217   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
218   '';
219
220 }
221
222 =item label
223
224 Returns a semi-friendly label for the gateway.
225
226 =cut
227
228 sub label {
229   my $self = shift;
230   $self->gatewaynum . ': ' . 
231   ($self->gateway_username ? $self->gateway_username . '@' : '') . 
232   $self->gateway_module
233 }
234
235 =item namespace_description
236
237 returns a friendly name for the namespace
238
239 =cut
240
241 my %namespace2description = (
242   '' => 'Direct',
243   'Business::OnlinePayment' => 'Direct',
244   'Business::OnlineThirdPartyPayment' => 'Hosted',
245   'Business::BatchPayment' => 'Batch',
246 );
247
248 sub namespace_description {
249   $namespace2description{shift->gateway_namespace} || 'Unknown';
250 }
251
252 =item batch_processor OPTIONS
253
254 For BatchPayment gateways only.  Returns a 
255 L<Business::BatchPayment::Processor> object to communicate with the 
256 gateway.
257
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'.
262
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.
266
267 =cut
268
269 sub batch_processor {
270   local $@;
271   my $self = shift;
272   my %opt = @_;
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 $@;
279
280   my $conf = new FS::Conf;
281   my $test_mode = $conf->exists('business-batchpayment-test_transaction');
282   $opt{'test_mode'} = 1 if $test_mode;
283
284   my $module = $self->gateway_module;
285   my $processor = eval { 
286     Business::BatchPayment->create($module, $self->options, %opt)
287   };
288   die "failed to create Business::BatchPayment::$module object: $@"
289     if $@;
290
291   die "$module does not support test mode"
292     if $test_mode and not $processor->does('Business::BatchPayment::TestMode');
293
294   return $processor;
295 }
296
297 # _upgrade_data
298 #
299 # Used by FS::Upgrade to migrate to a new database.
300 #
301 #
302
303 sub _upgrade_data {
304   my ($class, %opts) = @_;
305   my $dbh = dbh;
306
307   warn "$me upgrading $class\n" if $DEBUG;
308
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;
313   }
314 }
315
316 =back
317
318 =head1 BUGS
319
320 =head1 SEE ALSO
321
322 L<FS::Record>, schema.html from the base documentation.
323
324 =cut
325
326 1;
327