35b4f0835f1bf5c890e9b4ff0ee0392f565c4437
[freeside.git] / FS / FS / payment_gateway.pm
1 package FS::payment_gateway;
2
3 use strict;
4 use vars qw( @ISA );
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
11 =head1 NAME
12
13 FS::payment_gateway - Object methods for payment_gateway records
14
15 =head1 SYNOPSIS
16
17   use FS::payment_gateway;
18
19   $record = new FS::payment_gateway \%hash;
20   $record = new FS::payment_gateway { 'column' => 'value' };
21
22   $error = $record->insert;
23
24   $error = $new_record->replace($old_record);
25
26   $error = $record->delete;
27
28   $error = $record->check;
29
30 =head1 DESCRIPTION
31
32 An FS::payment_gateway object represents an payment gateway.
33 FS::payment_gateway inherits from FS::Record.  The following fields are
34 currently supported:
35
36 =over 4
37
38 =item gatewaynum - primary key
39
40 =item gateway_module - Business::OnlinePayment:: module name
41
42 =item gateway_username - payment gateway username
43
44 =item gateway_password - payment gateway password
45
46 =item gateway_action - optional action or actions (multiple actions are separated with `,': for example: `Authorization Only, Post Authorization').  Defaults to `Normal Authorization'.
47
48 =item disabled - Disabled flag, empty or 'Y'
49
50 =back
51
52 =head1 METHODS
53
54 =over 4
55
56 =item new HASHREF
57
58 Creates a new payment gateway.  To add the payment gateway to the database, see
59 L<"insert">.
60
61 Note that this stores the hash reference, not a distinct copy of the hash it
62 points to.  You can ask the object for a copy with the I<hash> method.
63
64 =cut
65
66 # the new method can be inherited from FS::Record, if a table method is defined
67
68 sub table { 'payment_gateway'; }
69
70 =item insert
71
72 Adds this record to the database.  If there is an error, returns the error,
73 otherwise returns false.
74
75 =cut
76
77 # the insert method can be inherited from FS::Record
78
79 =item delete
80
81 Delete this record from the database.
82
83 =cut
84
85 # the delete method can be inherited from FS::Record
86
87 =item replace OLD_RECORD
88
89 Replaces the OLD_RECORD with this one in the database.  If there is an error,
90 returns the error, otherwise returns false.
91
92 =cut
93
94 # the replace method can be inherited from FS::Record
95
96 =item check
97
98 Checks all fields to make sure this is a valid payment gateway.  If there is
99 an error, returns the error, otherwise returns false.  Called by the insert
100 and replace methods.
101
102 =cut
103
104 # the check method should currently be supplied - FS::Record contains some
105 # data checking routines
106
107 sub check {
108   my $self = shift;
109
110   my $error = 
111     $self->ut_numbern('gatewaynum')
112     || $self->ut_alpha('gateway_module')
113     || $self->ut_textn('gateway_username')
114     || $self->ut_anything('gateway_password')
115     || $self->ut_enum('disabled', [ '', 'Y' ] )
116     #|| $self->ut_textn('gateway_action')
117   ;
118   return $error if $error;
119
120   if ( $self->gateway_action ) {
121     my @actions = split(/,\s*/, $self->gateway_action);
122     $self->gateway_action(
123       join( ',', map { /^(Normal Authorization|Authorization Only|Credit|Post Authorization)$/
124                          or return "Unknown action $_";
125                        $1
126                      }
127                      @actions
128           )
129    );
130   } else {
131     $self->gateway_action('Normal Authorization');
132   }
133
134   $self->SUPER::check;
135 }
136
137 =item agent_payment_gateway
138
139 Returns any agent overrides for this payment gateway.
140
141 =cut
142
143 sub agent_payment_gateway {
144   my $self = shift;
145   qsearch('agent_payment_gateway', { 'gatewaynum' => $self->gatewaynum } );
146 }
147
148 =item disable
149
150 Disables this payment gateway: deletes all associated agent_payment_gateway
151 overrides and sets the I<disabled> field to "B<Y>".
152
153 =cut
154
155 sub disable {
156   my $self = shift;
157
158   local $SIG{HUP} = 'IGNORE';
159   local $SIG{INT} = 'IGNORE';
160   local $SIG{QUIT} = 'IGNORE';
161   local $SIG{TERM} = 'IGNORE';
162   local $SIG{TSTP} = 'IGNORE';
163   local $SIG{PIPE} = 'IGNORE';
164
165   my $oldAutoCommit = $FS::UID::AutoCommit;
166   local $FS::UID::AutoCommit = 0;
167   my $dbh = dbh;
168
169   foreach my $agent_payment_gateway ( $self->agent_payment_gateway ) {
170     my $error = $agent_payment_gateway->delete;
171     if ( $error ) {
172       $dbh->rollback if $oldAutoCommit;
173       return "error deleting agent_payment_gateway override: $error";
174     }
175   }
176
177   $self->disabled('Y');
178   my $error = $self->replace();
179   if ( $error ) {
180     $dbh->rollback if $oldAutoCommit;
181     return "error disabling payment_gateway: $error";
182   }
183
184   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
185   '';
186
187 }
188
189 =back
190
191 =head1 BUGS
192
193 =head1 SEE ALSO
194
195 L<FS::Record>, schema.html from the base documentation.
196
197 =cut
198
199 1;
200