RT#38363: use cust_payby when saving cards during payments [editing from cust_main]
[freeside.git] / FS / FS / cust_payby.pm
index a65a171..83b951e 100644 (file)
@@ -2,6 +2,8 @@ package FS::cust_payby;
 use base qw( FS::payinfo_Mixin FS::cust_main_Mixin FS::Record );
 
 use strict;
+use Scalar::Util qw( blessed );
+use Digest::SHA qw( sha512_base64 );
 use Business::CreditCard qw( validate cardtype );
 use FS::UID qw( dbh );
 use FS::Msgcat qw( gettext );
@@ -201,8 +203,7 @@ sub replace {
           )
      )
   {
-warn $self->payinfo;
-warn $old->payinfo;
+
     $self->payinfo($old->payinfo);
 
   } elsif ( $self->payby =~ /^(CHEK|DCHK)$/ && $self->payinfo =~ /xx/ ) {
@@ -427,12 +428,16 @@ sub check {
 
   }
 
-  if ( $self->paydate eq '' || $self->paydate eq '-' ) {
-    return "Expiration date required"
-      # shouldn't payinfo_check do this?
-      unless $self->payby =~ /^(CHEK|DCHK)$/;
+  if ( $self->payby =~ /^(CHEK|DCHK)$/ ) {
+
     $self->paydate('');
-  } else {
+
+  } elsif ( $self->payby =~ /^(CARD|DCRD)$/ ) {
+
+    # shouldn't payinfo_check do this?
+    return "Expiration date required"
+      if $self->paydate eq '' || $self->paydate eq '-';
+
     my( $m, $y );
     if ( $self->paydate =~ /^(\d{1,2})[\/\-](\d{2}(\d{2})?)$/ ) {
       ( $m, $y ) = ( $1, length($2) == 4 ? $2 : "20$2" );
@@ -451,6 +456,7 @@ sub check {
       #&&
          !$ignore_expired_card 
       && ( $y<$nowy || ( $y==$nowy && $1<$nowm ) );
+
   }
 
   if ( $self->payname eq '' && $self->payby !~ /^(CHEK|DCHK)$/ &&
@@ -494,6 +500,14 @@ sub _banned_pay_hashref {
   };
 }
 
+sub _new_banned_pay_hashref {
+  my $self = shift;
+  my $hr = $self->_banned_pay_hashref;
+  $hr->{payinfo_hash} = 'SHA512';
+  $hr->{payinfo} = sha512_base64($hr->{payinfo});
+  $hr;
+}
+
 =item paydate_mon_year
 
 Returns a two element list consisting of the paydate month and year.
@@ -560,18 +574,23 @@ Returns the field names used in the web interface (including some pseudo-fields)
 sub cgi_cust_payby_fields {
   #my $class = shift;
   [qw( payby payinfo paydate_month paydate_year paycvv payname weight
-       payinfo1 payinfo2 payinfo3 paytype paystate )];
+       payinfo1 payinfo2 payinfo3 paytype paystate payname_CHEK )];
 }
 
-=item cgi_hash_callback HASHREF
+=item cgi_hash_callback HASHREF OLD
 
 Subroutine (not a class or object method).  Processes a hash reference
 of web interface contet (transfers the data from pseudo-fields to real fields).
 
+If OLD object is passed, also preserves locationnum, paystart_month, paystart_year,
+payissue and payip.  If the new field is blank but the old is not, the old field 
+will be preserved.
+
 =cut
 
 sub cgi_hash_callback {
   my $hashref = shift;
+  my $old = shift;
 
   my %noauto = (
     'CARD' => 'DCRD',
@@ -582,7 +601,7 @@ sub cgi_hash_callback {
 
   if ( $hashref->{payby} =~ /^(CHEK|DCHK)$/ ) {
 
-    unless ( grep $hashref->{$_}, qw( payinfo1 payinfo2 payinfo3 payname ) ) {
+    unless ( grep $hashref->{$_}, qw(payinfo1 payinfo2 payinfo3 payname_CHEK)) {
       %$hashref = ();
       return;
     }
@@ -592,7 +611,7 @@ sub cgi_hash_callback {
       if $conf->config('echeck-country') eq 'CA';
     $hashref->{payinfo} .= $hashref->{'payinfo2'};
 
-    $hashref->{payname} .= $hashref->{'payname_CHEK'};
+    $hashref->{payname} = $hashref->{'payname_CHEK'};
 
   } elsif ( $hashref->{payby} =~ /^(CARD|DCRD)$/ ) {
 
@@ -605,6 +624,14 @@ sub cgi_hash_callback {
 
   $hashref->{paydate}= $hashref->{paydate_month}. '-'. $hashref->{paydate_year};
 
+  if ($old) {
+    foreach my $field ( qw(locationnum paystart_month paystart_year payissue payip) ) {
+      next if $hashref->{$field};
+      next unless $old->get($field);
+      $hashref->{$field} = $old->get($field);
+    }
+  }
+
 }
 
 =item search_sql