even more reliable multiple-payment/double-click/concurrent-payment-form protection
[freeside.git] / FS / FS / cust_pay.pm
index 5d31d2c..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;
@@ -408,15 +409,16 @@ sub check {
 
   $self->_date(time) unless $self->_date;
 
-  # 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";
-  }
+#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;
 }
@@ -548,6 +550,74 @@ 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
@@ -586,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