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 Customer has been sent to an off-site payment gateway to complete processing
105 Only used for two-stage transactions that require a separate capture step
109 Transaction completed with payment gateway (sucessfully), not yet recorded in
114 Transaction completed with payment gateway (declined), not yet recorded in
119 Transaction recorded in database
125 Additional status information.
129 L<FS::payment_gateway> id.
142 Creates a new pending payment. To add the pending payment to the database, see L<"insert">.
144 Note that this stores the hash reference, not a distinct copy of the hash it
145 points to. You can ask the object for a copy with the I<hash> method.
149 # the new method can be inherited from FS::Record, if a table method is defined
151 sub table { 'cust_pay_pending'; }
155 Adds this record to the database. If there is an error, returns the error,
156 otherwise returns false.
160 # the insert method can be inherited from FS::Record
164 Delete this record from the database.
168 # the delete method can be inherited from FS::Record
170 =item replace OLD_RECORD
172 Replaces the OLD_RECORD with this one in the database. If there is an error,
173 returns the error, otherwise returns false.
177 # the replace method can be inherited from FS::Record
181 Checks all fields to make sure this is a valid pending payment. If there is
182 an error, returns the error, otherwise returns false. Called by the insert
187 # the check method should currently be supplied - FS::Record contains some
188 # data checking routines
194 $self->ut_numbern('paypendingnum')
195 || $self->ut_foreign_key('custnum', 'cust_main', 'custnum')
196 || $self->ut_money('paid')
197 || $self->ut_numbern('_date')
198 || $self->ut_textn('payunique')
199 || $self->ut_text('status')
200 #|| $self->ut_textn('statustext')
201 || $self->ut_anything('statustext')
202 #|| $self->ut_money('cust_balance')
203 || $self->ut_hexn('session_id')
204 || $self->ut_foreign_keyn('paynum', 'cust_pay', 'paynum' )
205 || $self->ut_foreign_keyn('pkgnum', 'cust_pkg', 'pkgnum')
206 || $self->payinfo_check() #payby/payinfo/paymask/paydate
208 return $error if $error;
210 $self->_date(time) unless $self->_date;
212 # UNIQUE index should catch this too, without race conditions, but this
213 # should give a better error message the other 99.9% of the time...
214 if ( length($self->payunique) ) {
215 my $cust_pay_pending = qsearchs('cust_pay_pending', {
216 'payunique' => $self->payunique,
217 'paypendingnum' => { op=>'!=', value=>$self->paypendingnum },
219 if ( $cust_pay_pending ) {
220 #well, it *could* be a better error message
221 return "duplicate transaction - a payment with unique identifer ".
222 $self->payunique. " already exists";
231 Returns the associated L<FS::cust_main> record if any. Otherwise returns false.
237 qsearchs('cust_main', { custnum => $self->custnum } );
241 #these two are kind-of false laziness w/cust_main::realtime_bop
242 #(currently only used when resolving pending payments manually)
244 =item insert_cust_pay
246 Sets the status of this pending pament to "done" (with statustext
247 "captured (manual)"), and inserts a payment record (see L<FS::cust_pay>).
249 Currently only used when resolving pending payments manually.
253 sub insert_cust_pay {
256 my $cust_pay = new FS::cust_pay ( {
257 'custnum' => $self->custnum,
258 'paid' => $self->paid,
259 '_date' => $self->_date, #better than passing '' for now
260 'payby' => $self->payby,
261 'payinfo' => $self->payinfo,
262 'paybatch' => $self->paybatch,
263 'paydate' => $self->paydate,
266 my $oldAutoCommit = $FS::UID::AutoCommit;
267 local $FS::UID::AutoCommit = 0;
270 #start a transaction, insert the cust_pay and set cust_pay_pending.status to done in a single transction
272 my $error = $cust_pay->insert;#($options{'manual'} ? ( 'manual' => 1 ) : () );
276 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
280 $self->status('done');
281 $self->statustext('captured (manual)');
282 $self->paynum($cust_pay->paynum);
283 my $cpp_done_err = $self->replace;
285 if ( $cpp_done_err ) {
287 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
288 return $cpp_done_err;
292 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
299 =item decline [ STATUSTEXT ]
301 Sets the status of this pending payment to "done" (with statustext
302 "declined (manual)" unless otherwise specified).
304 Currently only used when resolving pending payments manually.
310 my $statustext = shift || "declined (manual)";
312 #could send decline email too? doesn't seem useful in manual resolution
314 $self->status('done');
315 $self->statustext($statustext);
321 # Used by FS::Upgrade to migrate to a new database.
323 sub _upgrade_data { #class method
324 my ($class, %opts) = @_;
327 "DELETE FROM cust_pay_pending WHERE status = 'new' AND _date < ".(time-600);
329 my $sth = dbh->prepare($sql) or die dbh->errstr;
330 $sth->execute or die $sth->errstr;
340 L<FS::Record>, schema.html from the base documentation.