RT# 83091 - fixed selfserivce to insert payment account if one does not exits
[freeside.git] / FS / FS / ClientAPI / MyAccount.pm
index 99c5f74..e813016 100644 (file)
@@ -152,7 +152,6 @@ sub skin_info {
 
                  personal.php Profile
                  personal.php Personal Information
-                 payment_accounts.php Payment Accounts
                  password.php Change Password
 
                  payment.php Payments
@@ -624,6 +623,8 @@ sub customer_info_short {
     for (@cust_main_editable_fields) {
       $return{$_} = $cust_main->get($_);
     }
+    $return{$_} = $cust_main->masked($_) for qw/ss stateid/;
+
     #maybe a little more expensive, but it should be cached by now
     for (@location_editable_fields) {
       $return{$_} = $cust_main->bill_location->get($_)
@@ -744,8 +745,20 @@ sub edit_info {
     or return { 'error' => "unknown custnum $custnum" };
 
   my $conf = new FS::Conf;
-  if (($p->{payby} eq "CHEK" || $p->{payby} eq "DCHEK") && $conf->exists('selfservice-ACH_info_readonly')) {
-    return { 'error' => "You do not have authority to add a bank account" };
+
+  if ($p->{payby}) {
+    return { 'error' => "You do not have authority to add a bank account" }
+      if (($p->{payby} eq "CHEK" || $p->{payby} eq "DCHEK") && $conf->exists('selfservice-ACH_info_readonly'));
+
+    ## get default cust_payby and change it. For old v3 selfservice that upgraded to v4.  this is for v4 only
+    my ($cust_payby) = $cust_main->cust_payby();
+    if ($cust_payby) {
+      $p->{'custpaybynum'} = $cust_payby->custpaybynum;
+      update_payby($p);
+    }
+    else {
+      insert_payby($p);
+    }
   }
 
   my $new = new FS::cust_main { $cust_main->hash };
@@ -876,30 +889,34 @@ sub payment_info {
     for qw(address1 address2 city state zip);
 
   # look for stored cust_payby info
-  #   only if we've been given a clear payment_payby (to avoid payname conflicts)
-  if ($p->{'payment_payby'} =~ /^(CARD|CHEK)$/) {
-    my @search_payby = ($p->{'payment_payby'} eq 'CARD') ? ('CARD','DCRD') : ('CHEK','DCHK');
-    my ($cust_payby) = $cust_main->cust_payby(@search_payby);
-    if ($cust_payby) {
-      $return{payname} = $cust_payby->payname
+  #   v3 to v4 upgrade would break change_pay because change_pay does not send payment_payby
+  #   so for change_pay to work need to search for all allowed paybys and grab default payment account
+  my @search_payby = ();
+  @search_payby = ($p->{'payment_payby'} eq 'CARD') ? ('CARD','DCRD') : ('CHEK','DCHK')
+    if ($p->{'payment_payby'} =~ /^(CARD|CHEK)$/);
+
+  my ($cust_payby) = $cust_main->cust_payby(@search_payby);
+  if ($cust_payby) {
+    $return{payby} = $cust_payby->payby;
+    $return{payname} = $cust_payby->payname
                          || ( $cust_main->first. ' '. $cust_main->get('last') );
+    $return{custpaybynum} = $cust_payby->custpaybynum;
 
-      if ( $cust_payby->payby =~ /^(CARD|DCRD)$/ ) {
-        $return{card_type} = cardtype($cust_payby->payinfo);
-        $return{payinfo} = $cust_payby->paymask;
+    if ( $cust_payby->payby =~ /^(CARD|DCRD)$/ ) {
+      $return{card_type} = cardtype($cust_payby->payinfo);
+      $return{payinfo} = $cust_payby->paymask;
 
-        @return{'month', 'year'} = $cust_payby->paydate_monthyear;
+      @return{'month', 'year'} = $cust_payby->paydate_monthyear;
 
-      }
+    }
 
-      if ( $cust_payby->payby =~ /^(CHEK|DCHK)$/ ) {
-        my ($payinfo1, $payinfo2) = split '@', $cust_payby->paymask;
-        $return{payinfo1} = $payinfo1;
-        $return{payinfo2} = $payinfo2;
-        $return{paytype}  = $cust_payby->paytype;
-        $return{paystate} = $cust_payby->paystate;
-        $return{payname}  = $cust_payby->payname;      # override 'first/last name' default from above, if any.  Is instution-name here.  (#15819)
-      }
+    if ( $cust_payby->payby =~ /^(CHEK|DCHK)$/ ) {
+      my ($payinfo1, $payinfo2) = split '@', $cust_payby->paymask;
+      $return{payinfo1} = $payinfo1;
+      $return{payinfo2} = $payinfo2;
+      $return{paytype}  = $cust_payby->paytype;
+      $return{paystate} = $cust_payby->paystate;
+      $return{payname}  = $cust_payby->payname;        # override 'first/last name' default from above, if any.  Is instution-name here.  (#15819)
     }
   }
 
@@ -914,6 +931,13 @@ sub payment_info {
   $return{paybatch} = $return{payunique};  #back compat
 
   $return{credit_card_surcharge_percentage} = $conf->config('credit-card-surcharge-percentage', $cust_main->agentnum);
+  $return{credit_card_surcharge_flatfee} = $conf->config('credit-card-surcharge-flatfee', $cust_main->agentnum);
+
+  # A value for 'payby' must be defined in %return
+  $return{payby} = $return{paybys}->[0]
+    if !$return{payby}
+    && ref $return{paybys}
+    && scalar @{ $return{paybys} };
 
   return { 'error' => '',
            %return,
@@ -981,6 +1005,7 @@ sub validate_payment {
   #false laziness w/process/payment.cgi
   my $payinfo;
   my $paycvv = '';
+  my $replace_cust_payby;
   if ( $payby eq 'CHEK' || $payby eq 'DCHK' ) {
   
     $p->{'payinfo1'} =~ /^([\dx]+)$/
@@ -995,6 +1020,7 @@ sub validate_payment {
     foreach my $cust_payby ($cust_main->cust_payby('CHEK','DCHK')) {
       if ( $cust_payby->paymask eq $payinfo ) {
         $payinfo = $cust_payby->payinfo;
+        $replace_cust_payby = $cust_payby;
         $achonfile = 1;
         last;
       }
@@ -1015,6 +1041,7 @@ sub validate_payment {
     foreach my $cust_payby ($cust_main->cust_payby('CARD','DCRD')) {
       if ( $cust_payby->paymask eq $payinfo ) {
         $payinfo = $cust_payby->payinfo;
+        $replace_cust_payby = $cust_payby;
         $onfile = 1;
         last;
       }
@@ -1056,6 +1083,8 @@ sub validate_payment {
     'CHEK' => [ qw( ss paytype paystate stateid stateid_state payip ) ],
   );
 
+  my %replace = ( 'replace' => $replace_cust_payby, );
+
   my $card_type = '';
   $card_type = cardtype($payinfo) if $payby eq 'CARD';
 
@@ -1064,7 +1093,7 @@ sub validate_payment {
     'amount'         => sprintf('%.2f', $amount),
     'payby'          => $payby,
     'payinfo'        => $payinfo,
-    'paymask'        => $cust_main->mask_payinfo( $payby, $payinfo ),
+    'paymask'        => FS::payinfo_Mixin->mask_payinfo( $payby, $payinfo ),
     'card_type'      => $card_type,
     'paydate'        => $p->{'year'}. '-'. $p->{'month'}. '-01',
     'paydate_pretty' => $p->{'month'}. ' / '. $p->{'year'},
@@ -1077,6 +1106,7 @@ sub validate_payment {
     'payname'        => $payname,
     'discount_term'  => $discount_term,
     'pkgnum'         => $session->{'pkgnum'},
+    %replace,
     map { $_ => $p->{$_} } ( @{ $payby2fields{$payby} },
                              qw( save auto ),
                            )
@@ -1159,6 +1189,7 @@ sub do_process_payment {
 
     my $error = $cust_main->save_cust_payby(
       'payment_payby' => $payby,
+      'replace'       => $validate->{'replace'}, # cust_payby object to replace
       %saveopt
     );
 
@@ -1668,14 +1699,18 @@ sub insert_payby {
 
   #XXX payinfo1 + payinfo2 for CHEK?
   #or take the opportunity to use separate, more well- named fields?
-  # my $payinfo;
-  # $p->{'payinfo1'} =~ /^([\dx]+)$/
-  #   or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
-  # my $payinfo1 = $1;
-  #  $p->{'payinfo2'} =~ /^([\dx\.]+)$/ # . turned on by echeck-country CA ?
-  #   or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
-  # my $payinfo2 = $1;
-  # $payinfo = $payinfo1. '@'. $payinfo2;
+   if ($p->{'payby'} eq 'CHEK') {
+     $p->{'payinfo1'} =~ /^([\dx]+)$/
+       or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
+     my $payinfo1 = $1;
+      $p->{'payinfo2'} =~ /^([\dx\.]+)$/ # . turned on by echeck-country CA ?
+       or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
+     my $payinfo2 = $1;
+     $p->{'payinfo'} = $payinfo1. '@'. $payinfo2;
+   }
+   elsif ($p->{'payby'} eq 'CARD') {
+    $p->{paydate} = $p->{year} . '-' . $p->{month} . '-01' unless $p->{paydate};
+   }
 
   my $cust_payby = new FS::cust_payby {
     'custnum' => $custnum,
@@ -1699,26 +1734,53 @@ sub update_payby {
   my($context, $session, $custnum) = _custoragent_session_custnum($p);
   return { 'error' => $session } if $context eq 'error';
 
+  if ($p->{'payby'} eq 'CHEK') {
+     $p->{'payinfo1'} =~ /^([\dx]+)$/
+       or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
+     my $payinfo1 = $1;
+      $p->{'payinfo2'} =~ /^([\dx\.]+)$/ # . turned on by echeck-country CA ?
+       or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
+     my $payinfo2 = $1;
+     $p->{'payinfo'} = $payinfo1. '@'. $payinfo2;
+  }
+  elsif ($p->{'payby'} eq 'CARD') {
+    $p->{paydate} = $p->{year} . '-' . $p->{month} . '-01' unless $p->{paydate};
+  }
+
   my $cust_payby = qsearchs('cust_payby', {
                               'custnum'      => $custnum,
                               'custpaybynum' => $p->{'custpaybynum'},
                            })
     or return { 'error' => 'unknown custpaybynum '. $p->{'custpaybynum'} };
 
+  my $cust_main = qsearchs( 'cust_main', {custnum => $cust_payby->custnum} )
+    or return { 'error' => 'unknown custnum '.$cust_payby->custnum };
+
   foreach my $field (
     qw( weight payby payinfo paycvv paydate payname paystate paytype payip )
   ) {
     next unless exists($p->{$field});
     $cust_payby->set($field,$p->{$field});
   }
+  $cust_payby->set( 'paymask' => $cust_payby->mask_payinfo );
 
-  my $error = $cust_payby->replace;
-  if ( $error ) {
-    return { 'error' => $error };
-  } else {
-    return { 'custpaybynum' => $cust_payby->custpaybynum };
+  # Update column if given a value, and the given value wasn't
+  # the value generated by $cust_main->masked($column);
+  $cust_main->set( $_, $p->{$_} )
+    for grep{ $p->{$_} !~ /^x/i; }
+        grep{ exists $p->{$_} }
+        qw/ss stateid/;
+
+  # Perform updates within a transaction
+  local $FS::UID::AutoCommit = 0;
+
+  if ( my $error = $cust_payby->replace || $cust_main->replace ) {
+    dbh->rollback;
+    return { error => $error };
   }
-  
+
+  dbh->commit;
+  return { custpaybynum => $cust_payby->custpaybynum };
 }
 
 sub verify_payby {
@@ -3564,6 +3626,11 @@ sub list_tickets {
 
   # unavoidable false laziness w/ httemplate/view/cust_main/tickets.html
   if ( $FS::TicketSystem::system && FS::TicketSystem->selfservice_priority ) {
+
+    @tickets = grep { $_->{'_selfservice_priority'}
+                        !~ /^\s*(closed?|resolved?|done)\s*/i }
+                 @tickets;
+
     my $conf = new FS::Conf;
     my $dir = $conf->exists('ticket_system-priority_reverse') ? -1 : 1;
     +{ tickets => [ 
@@ -3869,4 +3936,3 @@ sub _custoragent_session_custnum {
 }
 
 1;
-