fix payinfo_used on batch payments with encrypted payinfo, related to #19571
authorMark Wells <mark@freeside.biz>
Wed, 21 Dec 2016 23:25:35 +0000 (15:25 -0800)
committerMark Wells <mark@freeside.biz>
Wed, 21 Dec 2016 23:39:50 +0000 (15:39 -0800)
FS/FS/cust_pay.pm
FS/FS/cust_pay_batch.pm
FS/FS/payinfo_Mixin.pm

index b15920b..64ce7ec 100644 (file)
@@ -1216,6 +1216,30 @@ sub _upgrade_data {  #class method
   ###
   $class->upgrade_set_cardtype;
 
+  # for batch payments, make sure paymask is set
+  do {
+    local $FS::payinfo_Mixin::allow_closed_replace = 1;
+    local $FS::payinfo_Mixin::ignore_masked_payinfo = 1;
+
+    my $cursor = FS::Cursor->new({
+      table => 'cust_pay',
+      extra_sql => ' WHERE paymask IS NULL AND payinfo IS NOT NULL
+                    AND payby IN(\'CARD\', \'CHEK\')
+                    AND batchnum IS NOT NULL',
+    });
+
+    # records from cursors for some reason don't decrypt payinfo, so
+    # call replace_old to fetch the record "normally"
+    while (my $cust_pay = $cursor->fetch) {
+      $cust_pay = $cust_pay->replace_old;
+      $cust_pay->set('paymask', $cust_pay->mask_payinfo);
+      my $error = $cust_pay->replace;
+      if ($error) {
+        die "$error (setting masked payinfo on payment#". $cust_pay->paynum.
+          ")\n"
+      }
+    }
+  };
 }
 
 sub process_upgrade_paybatch {
index 8127c6a..d29c6d0 100644 (file)
@@ -297,6 +297,7 @@ sub approve {
       'custnum'   => $new->custnum,
       'payby'     => $new->payby,
       'payinfo'   => $new->payinfo || $old->payinfo,
+      'paymask'   => $new->mask_payinfo,
       'paid'      => $new->paid,
       '_date'     => $new->_date,
       'usernum'   => $new->usernum,
index 3a51022..1c45720 100644 (file)
@@ -307,13 +307,12 @@ sub payinfo_used {
   my $payinfo = shift || $self->payinfo;
   my %hash = (
     'custnum' => $self->custnum,
-    'payby'   => 'CARD',
+    'payby'   => $self->payby,
   );
 
   return 1
   if qsearch('cust_pay', { %hash, 'payinfo' => $payinfo } )
-  || qsearch('cust_pay', 
-    { %hash, 'paymask' => $self->mask_payinfo('CARD', $payinfo) }  )
+  || qsearch('cust_pay', { %hash, 'paymask' => $self->mask_payinfo } )
   ;
 
   return 0;