1 package FS::cust_pay_pending;
4 use vars qw( @ISA @encrypted_fields );
5 use FS::Record qw( qsearch qsearchs dbh ); #dbh for _upgrade_data
6 use FS::payinfo_transaction_Mixin;
7 use FS::cust_main_Mixin;
12 @ISA = qw( FS::payinfo_transaction_Mixin FS::cust_main_Mixin FS::Record );
14 @encrypted_fields = ('payinfo');
18 FS::cust_pay_pending - Object methods for cust_pay_pending records
22 use FS::cust_pay_pending;
24 $record = new FS::cust_pay_pending \%hash;
25 $record = new FS::cust_pay_pending { 'column' => 'value' };
27 $error = $record->insert;
29 $error = $new_record->replace($old_record);
31 $error = $record->delete;
33 $error = $record->check;
37 An FS::cust_pay_pending object represents an pending payment. It reflects
38 local state through the multiple stages of processing a real-time transaction
39 with an external gateway. FS::cust_pay_pending inherits from FS::Record. The
40 following fields are currently supported:
50 Customer (see L<FS::cust_main>)
54 Amount of this payment
58 Specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
59 L<Time::Local> and L<Date::Parse> for conversion functions.
63 Payment Type (See L<FS::payinfo_Mixin> for valid payby values)
67 Payment Information (See L<FS::payinfo_Mixin> for data format)
71 Masked payinfo (See L<FS::payinfo_Mixin> for how this works)
79 Unique identifer to prevent duplicate transactions.
83 Desired pkgnum when using experimental package balances.
87 Pending transaction status, one of the following:
93 Aquires basic lock on payunique
97 Transaction is pending with the gateway
101 Only used for two-stage transactions that require a separate capture step
105 Transaction completed with payment gateway (sucessfully), not yet recorded in
110 Transaction completed with payment gateway (declined), not yet recorded in
115 Transaction recorded in database
121 Additional status information.
125 L<FS::payment_gateway> id.
138 Creates a new pending payment. To add the pending payment to the database, see L<"insert">.
140 Note that this stores the hash reference, not a distinct copy of the hash it
141 points to. You can ask the object for a copy with the I<hash> method.
145 # the new method can be inherited from FS::Record, if a table method is defined
147 sub table { 'cust_pay_pending'; }
151 Adds this record to the database. If there is an error, returns the error,
152 otherwise returns false.
156 # the insert method can be inherited from FS::Record
160 Delete this record from the database.
164 # the delete method can be inherited from FS::Record
166 =item replace OLD_RECORD
168 Replaces the OLD_RECORD with this one in the database. If there is an error,
169 returns the error, otherwise returns false.
173 # the replace method can be inherited from FS::Record
177 Checks all fields to make sure this is a valid pending payment. If there is
178 an error, returns the error, otherwise returns false. Called by the insert
183 # the check method should currently be supplied - FS::Record contains some
184 # data checking routines
190 $self->ut_numbern('paypendingnum')
191 || $self->ut_foreign_key('custnum', 'cust_main', 'custnum')
192 || $self->ut_money('paid')
193 || $self->ut_numbern('_date')
194 || $self->ut_textn('payunique')
195 || $self->ut_text('status')
196 #|| $self->ut_textn('statustext')
197 || $self->ut_anything('statustext')
198 #|| $self->ut_money('cust_balance')
199 || $self->ut_hexn('session_id')
200 || $self->ut_foreign_keyn('paynum', 'cust_pay', 'paynum' )
201 || $self->ut_foreign_keyn('pkgnum', 'cust_pkg', 'pkgnum')
202 || $self->payinfo_check() #payby/payinfo/paymask/paydate
204 return $error if $error;
206 $self->_date(time) unless $self->_date;
208 # UNIQUE index should catch this too, without race conditions, but this
209 # should give a better error message the other 99.9% of the time...
210 if ( length($self->payunique) ) {
211 my $cust_pay_pending = qsearchs('cust_pay_pending', {
212 'payunique' => $self->payunique,
213 'paypendingnum' => { op=>'!=', value=>$self->paypendingnum },
215 if ( $cust_pay_pending ) {
216 #well, it *could* be a better error message
217 return "duplicate transaction - a payment with unique identifer ".
218 $self->payunique. " already exists";
227 Returns the associated L<FS::cust_main> record if any. Otherwise returns false.
233 qsearchs('cust_main', { custnum => $self->custnum } );
237 #these two are kind-of false laziness w/cust_main::realtime_bop
238 #(currently only used when resolving pending payments manually)
240 =item insert_cust_pay
242 Sets the status of this pending pament to "done" (with statustext
243 "captured (manual)"), and inserts a payment record (see L<FS::cust_pay>).
245 Currently only used when resolving pending payments manually.
249 sub insert_cust_pay {
252 my $cust_pay = new FS::cust_pay ( {
253 'custnum' => $self->custnum,
254 'paid' => $self->paid,
255 '_date' => $self->_date, #better than passing '' for now
256 'payby' => $self->payby,
257 'payinfo' => $self->payinfo,
258 'paybatch' => $self->paybatch,
259 'paydate' => $self->paydate,
262 my $oldAutoCommit = $FS::UID::AutoCommit;
263 local $FS::UID::AutoCommit = 0;
266 #start a transaction, insert the cust_pay and set cust_pay_pending.status to done in a single transction
268 my $error = $cust_pay->insert;#($options{'manual'} ? ( 'manual' => 1 ) : () );
272 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
276 $self->status('done');
277 $self->statustext('captured (manual)');
278 $self->paynum($cust_pay->paynum);
279 my $cpp_done_err = $self->replace;
281 if ( $cpp_done_err ) {
283 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
284 return $cpp_done_err;
288 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
295 =item decline [ STATUSTEXT ]
297 Sets the status of this pending payment to "done" (with statustext
298 "declined (manual)" unless otherwise specified).
300 Currently only used when resolving pending payments manually.
306 my $statustext = shift || "declined (manual)";
308 #could send decline email too? doesn't seem useful in manual resolution
310 $self->status('done');
311 $self->statustext($statustext);
317 # Used by FS::Upgrade to migrate to a new database.
319 sub _upgrade_data { #class method
320 my ($class, %opts) = @_;
323 "DELETE FROM cust_pay_pending WHERE status = 'new' AND _date < ".(time-600);
325 my $sth = dbh->prepare($sql) or die dbh->errstr;
326 $sth->execute or die $sth->errstr;
336 L<FS::Record>, schema.html from the base documentation.