show credit balance on invoices, #11564
[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 or Business::OnlineThirdPartyPayment
43
44 =item gateway_module - Business::OnlinePayment:: 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 =back
55
56 =head1 METHODS
57
58 =over 4
59
60 =item new HASHREF
61
62 Creates a new payment gateway.  To add the payment gateway to the database, see
63 L<"insert">.
64
65 Note that this stores the hash reference, not a distinct copy of the hash it
66 points to.  You can ask the object for a copy with the I<hash> method.
67
68 =cut
69
70 # the new method can be inherited from FS::Record, if a table method is defined
71
72 sub table { 'payment_gateway'; }
73
74 =item insert
75
76 Adds this record to the database.  If there is an error, returns the error,
77 otherwise returns false.
78
79 =cut
80
81 # the insert method can be inherited from FS::Record
82
83 =item delete
84
85 Delete this record from the database.
86
87 =cut
88
89 # the delete method can be inherited from FS::Record
90
91 =item replace OLD_RECORD
92
93 Replaces the OLD_RECORD with this one in the database.  If there is an error,
94 returns the error, otherwise returns false.
95
96 =cut
97
98 # the replace method can be inherited from FS::Record
99
100 =item check
101
102 Checks all fields to make sure this is a valid payment gateway.  If there is
103 an error, returns the error, otherwise returns false.  Called by the insert
104 and replace methods.
105
106 =cut
107
108 # the check method should currently be supplied - FS::Record contains some
109 # data checking routines
110
111 sub check {
112   my $self = shift;
113
114   my $error = 
115     $self->ut_numbern('gatewaynum')
116     || $self->ut_alpha('gateway_module')
117     || $self->ut_enum('gateway_namespace', ['Business::OnlinePayment',
118                                             'Business::OnlineThirdPartyPayment',
119                                            ] )
120     || $self->ut_textn('gateway_username')
121     || $self->ut_anything('gateway_password')
122     || $self->ut_textn('gateway_callback_url')  # a bit too permissive
123     || $self->ut_enum('disabled', [ '', 'Y' ] )
124     #|| $self->ut_textn('gateway_action')
125   ;
126   return $error if $error;
127
128   if ( $self->gateway_action ) {
129     my @actions = split(/,\s*/, $self->gateway_action);
130     $self->gateway_action(
131       join( ',', map { /^(Normal Authorization|Authorization Only|Credit|Post Authorization)$/
132                          or return "Unknown action $_";
133                        $1
134                      }
135                      @actions
136           )
137    );
138   } else {
139     $self->gateway_action('Normal Authorization');
140   }
141
142   # this little kludge mimics FS::CGI::popurl
143   $self->gateway_callback_url($self->gateway_callback_url. '/')
144     if ( $self->gateway_callback_url && $self->gateway_callback_url !~ /\/$/ );
145
146   $self->SUPER::check;
147 }
148
149 =item agent_payment_gateway
150
151 Returns any agent overrides for this payment gateway.
152
153 =cut
154
155 sub agent_payment_gateway {
156   my $self = shift;
157   qsearch('agent_payment_gateway', { 'gatewaynum' => $self->gatewaynum } );
158 }
159
160 =item disable
161
162 Disables this payment gateway: deletes all associated agent_payment_gateway
163 overrides and sets the I<disabled> field to "B<Y>".
164
165 =cut
166
167 sub disable {
168   my $self = shift;
169
170   local $SIG{HUP} = 'IGNORE';
171   local $SIG{INT} = 'IGNORE';
172   local $SIG{QUIT} = 'IGNORE';
173   local $SIG{TERM} = 'IGNORE';
174   local $SIG{TSTP} = 'IGNORE';
175   local $SIG{PIPE} = 'IGNORE';
176
177   my $oldAutoCommit = $FS::UID::AutoCommit;
178   local $FS::UID::AutoCommit = 0;
179   my $dbh = dbh;
180
181   foreach my $agent_payment_gateway ( $self->agent_payment_gateway ) {
182     my $error = $agent_payment_gateway->delete;
183     if ( $error ) {
184       $dbh->rollback if $oldAutoCommit;
185       return "error deleting agent_payment_gateway override: $error";
186     }
187   }
188
189   $self->disabled('Y');
190   my $error = $self->replace();
191   if ( $error ) {
192     $dbh->rollback if $oldAutoCommit;
193     return "error disabling payment_gateway: $error";
194   }
195
196   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
197   '';
198
199 }
200
201 =item namespace_description
202
203 returns a friendly name for the namespace
204
205 =cut
206
207 my %namespace2description = (
208   '' => 'Direct',
209   'Business::OnlinePayment' => 'Direct',
210   'Business::OnlineThirdPartyPayment' => 'Hosted',
211 );
212
213 sub namespace_description {
214   $namespace2description{shift->gateway_namespace} || 'Unknown';
215 }
216
217 # _upgrade_data
218 #
219 # Used by FS::Upgrade to migrate to a new database.
220 #
221 #
222
223 sub _upgrade_data {
224   my ($class, %opts) = @_;
225   my $dbh = dbh;
226
227   warn "$me upgrading $class\n" if $DEBUG;
228
229   foreach ( qsearch( 'payment_gateway', { 'gateway_namespace' => '' } ) ) {
230     $_->gateway_namespace('Business::OnlinePayment');  #defaulting
231     my $error = $_->replace;
232     die "$class had error during upgrade replacement: $error" if $error;
233   }
234 }
235
236 =back
237
238 =head1 BUGS
239
240 =head1 SEE ALSO
241
242 L<FS::Record>, schema.html from the base documentation.
243
244 =cut
245
246 1;
247