even more reliable multiple-payment/double-click/concurrent-payment-form protection
[freeside.git] / FS / FS / cust_pay.pm
index 85bd4db..f969460 100644 (file)
@@ -7,6 +7,7 @@ use Business::CreditCard;
 use Text::Template;
 use FS::Misc qw(send_email);
 use FS::Record qw( dbh qsearch qsearchs );
+use FS::payby;
 use FS::cust_main_Mixin;
 use FS::payinfo_Mixin;
 use FS::cust_bill;
@@ -69,7 +70,9 @@ L<Time::Local> and L<Date::Parse> for conversion functions.
 
 =item paymask - Masked payinfo (See L<FS::payinfo_Mixin> for how this works)
 
-=item paybatch - text field for tracking card processing
+=item paybatch - text field for tracking card processing or other batch grouping
+
+=item payunique - Optional unique identifer to prevent duplicate transactions.
 
 =item closed - books closed flag, empty or `Y'
 
@@ -162,17 +165,6 @@ sub insert {
     }
   }
 
-  if ( $self->paybatch =~ /^webui-/ ) {
-    my @cust_pay = qsearch('cust_pay', {
-      'custnum' => $self->custnum,
-      'paybatch' => $self->paybatch,
-    } );
-    if ( scalar(@cust_pay) > 1 ) {
-      $dbh->rollback if $oldAutoCommit;
-      return "a payment with webui token ". $self->paybatch. " already exists";
-    }
-  }
-
   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
 
   #false laziness w/ cust_credit::insert
@@ -192,8 +184,14 @@ sub insert {
   if ( $conf->exists('payment_receipt_email')
        && grep { $_ !~ /^(POST|FAX)$/ } $cust_main->invoicing_list
   ) {
+
+    $cust_bill ||= ($cust_main->cust_bill)[-1]; #rather inefficient though?
+
     my $error;
-    if ( exists($options{ 'manual' }) && $options{ 'manual' } ) {
+    if (    ( exists($options{'manual'}) && $options{'manual'} )
+         || ! $conf->exists('invoice_html_statement')
+         || ! $cust_bill
+       ) {
 
       my $receipt_template = new Text::Template (
         TYPE   => 'ARRAY',
@@ -226,21 +224,20 @@ sub insert {
                        'balance' => $cust_main->balance,
                      } ) ],
       );
-    }else{
-      unless($cust_bill){
-        $cust_bill = ($cust_main->cust_bill)[-1];
-      }
-      if ($cust_bill) {
-        my $queue = new FS::queue {
-           'paynum' => $self->paynum,
-           'job'    => 'FS::cust_bill::queueable_send',
-        };
-        $error = $queue->insert(
-          'invnum' => $cust_bill->invnum,
-          'template' => 'statement',
-        );
-      }
+
+    } else {
+
+      my $queue = new FS::queue {
+         'paynum' => $self->paynum,
+         'job'    => 'FS::cust_bill::queueable_email',
+      };
+      $error = $queue->insert(
+        'invnum' => $cust_bill->invnum,
+        'template' => 'statement',
+      );
+
     }
+
     if ( $error ) {
       warn "can't send payment receipt/statement: $error";
     }
@@ -398,6 +395,7 @@ sub check {
     || $self->ut_money('paid')
     || $self->ut_numbern('_date')
     || $self->ut_textn('paybatch')
+    || $self->ut_textn('payunique')
     || $self->ut_enum('closed', [ '', 'Y' ])
     || $self->payinfo_check()
   ;
@@ -411,6 +409,17 @@ sub check {
 
   $self->_date(time) unless $self->_date;
 
+#i guess not now, with cust_pay_pending, if we actually make it here, we _do_ want to record it
+#  # UNIQUE index should catch this too, without race conditions, but this
+#  # should give a better error message the other 99.9% of the time...
+#  if ( length($self->payunique)
+#       && qsearchs('cust_pay', { 'payunique' => $self->payunique } ) ) {
+#    #well, it *could* be a better error message
+#    return "duplicate transaction".
+#           " - a payment with unique identifer ". $self->payunique.
+#           " already exists";
+#  }
+
   $self->SUPER::check;
 }
 
@@ -541,6 +550,104 @@ sub cust_main {
   qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
 }
 
+=item payby_name
+
+Returns a name for the payby field.
+
+=cut
+
+sub payby_name {
+  my $self = shift;
+  FS::payby->shortname( $self->payby );
+}
+
+=item gatewaynum
+
+Returns a gatewaynum for the processing gateway.
+
+=item processor
+
+Returns a name for the processing gateway.
+
+=item authorization
+
+Returns a name for the processing gateway.
+
+=item order_number
+
+Returns a name for the processing gateway.
+
+=cut
+
+sub gatewaynum    { shift->_parse_paybatch->{'gatewaynum'}; }
+sub processor     { shift->_parse_paybatch->{'processor'}; }
+sub authorization { shift->_parse_paybatch->{'authorization'}; }
+sub order_number  { shift->_parse_paybatch->{'order_number'}; }
+
+#sucks that this stuff is in paybatch like this in the first place,
+#but at least other code can start to use new field names
+#(code nicked from FS::cust_main::realtime_refund_bop)
+sub _parse_paybatch {
+  my $self = shift;
+
+  $self->paybatch =~ /^((\d+)\-)?(\w+):\s*([\w\-\/ ]*)(:([\w\-]+))?$/
+    or return {};
+              #"Can't parse paybatch for paynum $options{'paynum'}: ".
+              #  $cust_pay->paybatch;
+
+  my( $gatewaynum, $processor, $auth, $order_number ) = ( $2, $3, $4, $6 );
+
+  if ( $gatewaynum ) { #gateway for the payment to be refunded
+
+    my $payment_gateway =
+      qsearchs('payment_gateway', { 'gatewaynum' => $gatewaynum } );
+
+    die "payment gateway $gatewaynum not found" #?
+      unless $payment_gateway;
+
+    $processor = $payment_gateway->gateway_module;
+
+  }
+
+  {
+    'gatewaynum'    => $gatewaynum,
+    'processor'     => $processor,
+    'authorization' => $auth,
+    'order_number'  => $order_number,
+  };
+
+}
+
+=back
+
+=head1 CLASS METHODS
+
+=over 4
+
+=item unapplied_sql
+
+Returns an SQL fragment to retreive the unapplied amount.
+
+=cut 
+
+sub unapplied_sql {
+  #my $class = shift;
+
+  "paid
+        - COALESCE( 
+                    ( SELECT SUM(amount) FROM cust_bill_pay
+                        WHERE cust_pay.paynum = cust_bill_pay.paynum )
+                    ,0
+                  )
+        - COALESCE(
+                    ( SELECT SUM(amount) FROM cust_pay_refund
+                        WHERE cust_pay.paynum = cust_pay_refund.paynum )
+                    ,0
+                  )
+  ";
+
+}
+
 =back
 
 =head1 BUGS
@@ -549,8 +656,8 @@ Delete and replace methods.
 
 =head1 SEE ALSO
 
-L<FS::cust_bill_pay>, L<FS::cust_bill>, L<FS::Record>, schema.html from the
-base documentation.
+L<FS::cust_pay_pending>, L<FS::cust_bill_pay>, L<FS::cust_bill>, L<FS::Record>,
+schema.html from the base documentation.
 
 =cut