1 package FS::cust_pay_pending;
2 use base qw( FS::payinfo_transaction_Mixin FS::cust_main_Mixin FS::Record );
5 use vars qw( @encrypted_fields );
6 use FS::Record qw( qsearchs dbh ); #dbh for _upgrade_data
9 @encrypted_fields = ('payinfo');
10 sub nohistory_fields { ('payinfo'); }
14 FS::cust_pay_pending - Object methods for cust_pay_pending records
18 use FS::cust_pay_pending;
20 $record = new FS::cust_pay_pending \%hash;
21 $record = new FS::cust_pay_pending { 'column' => 'value' };
23 $error = $record->insert;
25 $error = $new_record->replace($old_record);
27 $error = $record->delete;
29 $error = $record->check;
33 An FS::cust_pay_pending object represents an pending payment. It reflects
34 local state through the multiple stages of processing a real-time transaction
35 with an external gateway. FS::cust_pay_pending inherits from FS::Record. The
36 following fields are currently supported:
46 Customer (see L<FS::cust_main>)
50 Amount of this payment
54 Specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
55 L<Time::Local> and L<Date::Parse> for conversion functions.
59 Payment Type (See L<FS::payinfo_Mixin> for valid payby values)
63 Payment Information (See L<FS::payinfo_Mixin> for data format)
67 Masked payinfo (See L<FS::payinfo_Mixin> for how this works)
75 Unique identifer to prevent duplicate transactions.
79 Desired pkgnum when using experimental package balances.
83 Pending transaction status, one of the following:
89 Aquires basic lock on payunique
93 Transaction is pending with the gateway
97 Customer has been sent to an off-site payment gateway to complete processing
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 One of the standard failure status strings defined in
126 L<Business::OnlinePayment>: "expired", "nsf", "stolen", "pickup",
127 "blacklisted", "declined". If the transaction status is not "declined",
132 L<FS::payment_gateway> id.
136 Payment number (L<FS::cust_pay>) of the completed payment.
140 Payment number of the payment if it's been voided.
144 Invoice number (L<FS::cust_bill>) to try to apply this payment to.
148 Flag for whether this is a "manual" payment (i.e. initiated through
149 self-service or the back-office web interface, rather than from an event
150 or a payment batch). "Manual" payments will cause the customer to be
151 sent a payment receipt rather than a statement.
155 Number of months the customer tried to prepay for.
165 Creates a new pending payment. To add the pending payment to the database, see L<"insert">.
167 Note that this stores the hash reference, not a distinct copy of the hash it
168 points to. You can ask the object for a copy with the I<hash> method.
172 # the new method can be inherited from FS::Record, if a table method is defined
174 sub table { 'cust_pay_pending'; }
178 Adds this record to the database. If there is an error, returns the error,
179 otherwise returns false.
183 # the insert method can be inherited from FS::Record
187 Delete this record from the database.
191 # the delete method can be inherited from FS::Record
193 =item replace OLD_RECORD
195 Replaces the OLD_RECORD with this one in the database. If there is an error,
196 returns the error, otherwise returns false.
200 # the replace method can be inherited from FS::Record
204 Checks all fields to make sure this is a valid pending payment. If there is
205 an error, returns the error, otherwise returns false. Called by the insert
210 # the check method should currently be supplied - FS::Record contains some
211 # data checking routines
217 $self->ut_numbern('paypendingnum')
218 || $self->ut_foreign_key('custnum', 'cust_main', 'custnum')
219 || $self->ut_money('paid')
220 || $self->ut_numbern('_date')
221 || $self->ut_textn('payunique')
222 || $self->ut_text('status')
223 #|| $self->ut_textn('statustext')
224 || $self->ut_anything('statustext')
225 || $self->ut_textn('failure_status')
226 #|| $self->ut_money('cust_balance')
227 || $self->ut_hexn('session_id')
228 || $self->ut_foreign_keyn('paynum', 'cust_pay', 'paynum' )
229 || $self->ut_foreign_keyn('pkgnum', 'cust_pkg', 'pkgnum')
230 || $self->ut_foreign_keyn('invnum', 'cust_bill', 'invnum')
231 || $self->ut_foreign_keyn('void_paynum', 'cust_pay_void', 'paynum' )
232 || $self->ut_flag('manual')
233 || $self->ut_numbern('discount_term')
234 || $self->payinfo_check() #payby/payinfo/paymask/paydate
236 return $error if $error;
238 $self->_date(time) unless $self->_date;
240 # UNIQUE index should catch this too, without race conditions, but this
241 # should give a better error message the other 99.9% of the time...
242 if ( length($self->payunique) ) {
243 my $cust_pay_pending = qsearchs('cust_pay_pending', {
244 'payunique' => $self->payunique,
245 'paypendingnum' => { op=>'!=', value=>$self->paypendingnum },
247 if ( $cust_pay_pending ) {
248 #well, it *could* be a better error message
249 return "duplicate transaction - a payment with unique identifer ".
250 $self->payunique. " already exists";
259 Returns the associated L<FS::cust_main> record if any. Otherwise returns false.
263 #these two are kind-of false laziness w/cust_main::realtime_bop
264 #(currently only used when resolving pending payments manually)
266 =item insert_cust_pay
268 Sets the status of this pending pament to "done" (with statustext
269 "captured (manual)"), and inserts a payment record (see L<FS::cust_pay>).
271 Currently only used when resolving pending payments manually.
275 sub insert_cust_pay {
278 my $cust_pay = new FS::cust_pay ( {
279 'custnum' => $self->custnum,
280 'paid' => $self->paid,
281 '_date' => $self->_date, #better than passing '' for now
282 'payby' => $self->payby,
283 'payinfo' => $self->payinfo,
284 'paybatch' => $self->paybatch,
285 'paydate' => $self->paydate,
288 my $oldAutoCommit = $FS::UID::AutoCommit;
289 local $FS::UID::AutoCommit = 0;
292 #start a transaction, insert the cust_pay and set cust_pay_pending.status to done in a single transction
294 my $error = $cust_pay->insert;#($options{'manual'} ? ( 'manual' => 1 ) : () );
298 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
302 $self->status('done');
303 $self->statustext('captured (manual)');
304 $self->paynum($cust_pay->paynum);
305 my $cpp_done_err = $self->replace;
307 if ( $cpp_done_err ) {
309 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
310 return $cpp_done_err;
314 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
321 =item approve OPTIONS
323 Sets the status of this pending payment to "done" and creates a completed
324 payment (L<FS::cust_pay>). This should be called when a realtime or
325 third-party payment has been approved.
327 OPTIONS may include any of 'processor', 'payinfo', 'discount_term', 'auth',
328 and 'order_number' to set those fields on the completed payment, as well as
329 'apply' to apply payments for this customer after inserting the new payment.
338 my $oldAutoCommit = $FS::UID::AutoCommit;
339 local $FS::UID::AutoCommit = 0;
341 my $cust_pay = FS::cust_pay->new({
342 'custnum' => $self->custnum,
343 'invnum' => $self->invnum,
344 'pkgnum' => $self->pkgnum,
345 'paid' => $self->paid,
347 'payby' => $self->payby,
348 'payinfo' => $self->payinfo,
349 'gatewaynum' => $self->gatewaynum,
351 foreach my $opt_field (qw(processor payinfo auth order_number))
353 $cust_pay->set($opt_field, $opt{$opt_field}) if exists $opt{$opt_field};
357 'manual' => $self->manual,
358 'discount_term' => $self->discount_term,
360 my $error = $cust_pay->insert( %insert_opt );
362 # try it again without invnum or discount
363 # (both of those can make payments fail to insert, and at this point
364 # the payment is a done deal and MUST be recorded)
366 my $error2 = $cust_pay->insert('manual' => $self->manual);
368 # attempt to void the payment?
369 # no, we'll just stop digging at this point.
370 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
371 my $e = "WARNING: payment captured but not recorded - error inserting ".
372 "payment (". ($opt{processor} || $self->payby) .
373 ": $error2\n(previously tried insert with invnum#".$self->invnum.
374 ": $error)\npending payment saved as paypendingnum#".
375 $self->paypendingnum."\n\n";
380 if ( my $jobnum = $self->jobnum ) {
381 my $placeholder = FS::queue->by_key($jobnum);
384 $error = "not found";
386 $error = $placeholder->delete;
390 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
391 my $e = "WARNING: payment captured but could not delete job $jobnum ".
392 "for paypendingnum #" . $self->paypendingnum . ": $error\n\n";
398 if ( $opt{'paynum_ref'} ) {
399 ${ $opt{'paynum_ref'} } = $cust_pay->paynum;
402 $self->status('done');
403 $self->statustext('captured');
404 $self->paynum($cust_pay->paynum);
405 my $cpp_done_err = $self->replace;
407 if ( $cpp_done_err ) {
409 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
410 my $e = "WARNING: payment captured but could not update pending status ".
411 "for paypendingnum ".$self->paypendingnum.": $cpp_done_err \n\n";
417 # commit at this stage--we don't want to roll back if applying
419 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
421 if ( $opt{'apply'} ) {
422 my $apply_error = $self->apply_payments_and_credits;
423 if ( $apply_error ) {
424 warn "WARNING: error applying payment: $apply_error\n\n";
431 =item decline [ STATUSTEXT [ STATUS ] ]
433 Sets the status of this pending payment to "done" (with statustext
434 "declined (manual)" unless otherwise specified). The optional STATUS can be
435 used to set the failure_status field.
437 Currently only used when resolving pending payments manually.
443 my $statustext = shift || "declined (manual)";
444 my $failure_status = shift || '';
446 #could send decline email too? doesn't seem useful in manual resolution
447 # this is also used for thirdparty payment execution failures, but a decline
448 # email isn't useful there either, and will just confuse people.
450 $self->status('done');
451 $self->statustext($statustext);
452 $self->failure_status($failure_status);
458 # Used by FS::Upgrade to migrate to a new database.
460 sub _upgrade_data { #class method
461 my ($class, %opts) = @_;
464 "DELETE FROM cust_pay_pending WHERE status = 'new' AND _date < ".(time-600);
466 my $sth = dbh->prepare($sql) or die dbh->errstr;
467 $sth->execute or die $sth->errstr;
477 L<FS::Record>, schema.html from the base documentation.