improve echeck validation for canada, deprecate echeck-nonus and cust_main-require...
[freeside.git] / httemplate / misc / process / payment.cgi
index d41bc0f..506e266 100644 (file)
 
   <% include('/elements/footer.html') %>
 
-% } else {
+% #2.5/2.7?# } elsif ( $curuser->access_right('View payments') ) {
+% } elsif ( $curuser->access_right(['View invoices', 'View payments']) ) {
 <% $cgi->redirect(popurl(3). "view/cust_pay.html?paynum=$paynum" ) %>
+% } else {
+<% $cgi->redirect(popurl(3). "view/cust_main.html?custnum=$custnum" ) %>
 % }
 <%init>
 
-die "access denied"
-  unless $FS::CurrentUser::CurrentUser->access_right('Process payment');
+my $curuser = $FS::CurrentUser::CurrentUser;
+die "access denied" unless $curuser->access_right('Process payment');
+
+my $conf = new FS::Conf;
 
 #some false laziness w/MyAccount::process_payment
 
@@ -76,11 +81,16 @@ if ( $payby eq 'CHEK' ) {
     $payinfo = $cust_main->payinfo;
   } else {
     $cgi->param('payinfo1') =~ /^(\d+)$/
-      or errorpage("illegal account number ". $cgi->param('payinfo1'));
+      or errorpage("Illegal account number ". $cgi->param('payinfo1'));
     my $payinfo1 = $1;
     $cgi->param('payinfo2') =~ /^(\d+)$/
-      or errorpage("illegal ABA/routing number ". $cgi->param('payinfo2'));
+      or errorpage("Illegal ABA/routing number ". $cgi->param('payinfo2'));
     my $payinfo2 = $1;
+    if ( $conf->config('echeck-country') eq 'CA' ) {
+      $cgi->param('payinfo3') =~ /^(\d{5})$/
+        or errorpage("Illegal branch number ". $cgi->param('payinfo2'));
+      $payinfo2 = "$1.$payinfo2";
+    }
     $payinfo = $payinfo1. '@'. $payinfo2;
   }
 
@@ -91,13 +101,15 @@ if ( $payby eq 'CHEK' ) {
     $payinfo = $cust_main->payinfo;
   }
   $payinfo =~ s/\D//g;
-  $payinfo =~ /^(\d{13,16})$/
+  $payinfo =~ /^(\d{13,16}|\d{8,9})$/
     or errorpage(gettext('invalid_card')); # . ": ". $self->payinfo;
   $payinfo = $1;
   validate($payinfo)
     or errorpage(gettext('invalid_card')); # . ": ". $self->payinfo;
+
   errorpage(gettext('unknown_card_type'))
-    if cardtype($payinfo) eq "Unknown";
+    if $payinfo !~ /^99\d{14}$/ #token
+    && cardtype($payinfo) eq "Unknown";
 
   if ( defined $cust_main->dbdef_table->column('paycvv') ) {
     if ( length($cgi->param('paycvv') ) ) {
@@ -117,19 +129,26 @@ if ( $payby eq 'CHEK' ) {
   die "unknown payby $payby";
 }
 
+$cgi->param('discount_term') =~ /^(\d*)$/
+  or errorpage("illegal discount_term");
+my $discount_term = $1;
+
 my $error = '';
 my $paynum = '';
 if ( $cgi->param('batch') ) {
 
-  $error = $cust_main->batch_card(
-                                   'payby'    => $payby,
-                                   'amount'   => $amount,
-                                   'payinfo'  => $payinfo,
-                                   'paydate'  => "$year-$month-01",
-                                   'payname'  => $payname,
-                                   map { $_ => $cgi->param($_) } 
-                                     @{$payby2fields{$payby}}
-                                 );
+  $error = 'Prepayment discounts not supported with batched payments' 
+    if $discount_term;
+
+  $error ||= $cust_main->batch_card(
+                                     'payby'    => $payby,
+                                     'amount'   => $amount,
+                                     'payinfo'  => $payinfo,
+                                     'paydate'  => "$year-$month-01",
+                                     'payname'  => $payname,
+                                     map { $_ => $cgi->param($_) } 
+                                       @{$payby2fields{$payby}}
+                                   );
   errorpage($error) if $error;
 
 } else {
@@ -144,17 +163,25 @@ if ( $cgi->param('batch') ) {
     'payunique'  => $payunique,
     'paycvv'     => $paycvv,
     'paynum_ref' => \$paynum,
+    'discount_term' => $discount_term,
     map { $_ => $cgi->param($_) } @{$payby2fields{$payby}}
   );
   errorpage($error) if $error;
 
   #no error, so order the fee package if applicable...
   if ( $cgi->param('fee_pkgpart') =~ /^(\d+)$/ ) {
-    my $error = $cust_main->order_pkg(
-      'cust_pkg' => new FS::cust_pkg { 'pkgpart' => $1 }
-    );
+
+    my $cust_pkg = new FS::cust_pkg { 'pkgpart' => $1 };
+
+    my $error = $cust_main->order_pkg( 'cust_pkg' => $cust_pkg );
     errorpage("payment processed successfully, but error ordering fee: $error")
       if $error;
+
+    #and generate an invoice for it now too
+    $error = $cust_main->bill( 'pkg_list' => [ $cust_pkg ] );
+    errorpage("payment processed and fee ordered sucessfully, but error billing fee: $error")
+      if $error;
+
   }
 
   $cust_main->apply_payments;
@@ -170,13 +197,12 @@ if ( $cgi->param('save') ) {
   } else {
     die "unknown payby $payby";
   }
-  $new->set( 'payinfo' => $payinfo );
+  $new->set( 'payinfo' => $cust_main->card_token || $payinfo );
   $new->set( 'paydate' => "$year-$month-01" );
   $new->set( 'payname' => $payname );
 
   #false laziness w/FS:;cust_main::realtime_bop - check both to make sure
   # working correctly
-  my $conf = new FS::Conf;
   if ( $payby eq 'CARD' &&
        grep { $_ eq cardtype($payinfo) } $conf->config('cvv-save') ) {
     $new->set( 'paycvv' => $paycvv );