This commit was manufactured by cvs2svn to create tag 'freeside_2_1_0'.
[freeside.git] / httemplate / misc / process / payment.cgi
index 6377b13..906c996 100644 (file)
@@ -1,13 +1,23 @@
-<% include( '/elements/header.html', ucfirst($type{$payby}). ' processing successful',
-             include('/elements/menubar.html'),
+% if ( $cgi->param('batch') ) {
 
-    )
-%>
-<% include( '/elements/small_custview.html', $cust_main, '', '', popurl(3). "view/cust_main.cgi" ) %>
+  <% include( '/elements/header.html', ucfirst($type{$payby}). ' processing successful',
+                 include('/elements/menubar.html'),
 
-<% include('/elements/footer.html') %>
+            )
+  %>
+
+  <% include( '/elements/small_custview.html', $cust_main, '', '', popurl(3). "view/cust_main.cgi" ) %>
+
+  <% include('/elements/footer.html') %>
+
+% } else {
+<% $cgi->redirect(popurl(3). "view/cust_pay.html?paynum=$paynum" ) %>
+% }
 <%init>
 
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('Process payment');
+
 #some false laziness w/MyAccount::process_payment
 
 $cgi->param('custnum') =~ /^(\d+)$/
@@ -22,6 +32,11 @@ $cgi->param('amount') =~ /^\s*(\d*(\.\d\d)?)\s*$/
 my $amount = $1;
 errorpage("amount <= 0") unless $amount > 0;
 
+if ( $cgi->param('fee') =~ /^\s*(\d*(\.\d\d)?)\s*$/ ) {
+  my $fee = $1;
+  $amount = sprintf('%.2f', $amount + $fee);
+}
+
 $cgi->param('year') =~ /^(\d+)$/
   or errorpage("illegal year ". $cgi->param('year'));
 my $year = $1;
@@ -34,7 +49,7 @@ $cgi->param('payby') =~ /^(CARD|CHEK)$/
   or errorpage("illegal payby ". $cgi->param('payby'));
 my $payby = $1;
 my %payby2fields = (
-  'CARD' => [ qw( address1 address2 city state zip ) ],
+  'CARD' => [ qw( address1 address2 city county state zip country ) ],
   'CHEK' => [ qw( ss paytype paystate stateid stateid_state ) ],
 );
 my %type = ( 'CARD' => 'credit card',
@@ -49,6 +64,10 @@ $cgi->param('payunique') =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
   or errorpage(gettext('illegal_text'). " payunique: ". $cgi->param('payunique'));
 my $payunique = $1;
 
+$cgi->param('balance') =~ /^\s*(\-?\s*\d*(\.\d\d)?)\s*$/
+  or errorpage("illegal balance");
+my $balance = $1;
+
 my $payinfo;
 my $paycvv = '';
 if ( $payby eq 'CHEK' ) {
@@ -78,7 +97,7 @@ if ( $payby eq 'CHEK' ) {
   validate($payinfo)
     or errorpage(gettext('invalid_card')); # . ": ". $self->payinfo;
   errorpage(gettext('unknown_card_type'))
-    if cardtype($payinfo) eq "Unknown";
+    if $payinfo !~ /^99\d{14}$/ && cardtype($payinfo) eq "Unknown";
 
   if ( defined $cust_main->dbdef_table->column('paycvv') ) {
     if ( length($cgi->param('paycvv') ) ) {
@@ -99,6 +118,7 @@ if ( $payby eq 'CHEK' ) {
 }
 
 my $error = '';
+my $paynum = '';
 if ( $cgi->param('batch') ) {
 
   $error = $cust_main->batch_card(
@@ -110,22 +130,40 @@ if ( $cgi->param('batch') ) {
                                    map { $_ => $cgi->param($_) } 
                                      @{$payby2fields{$payby}}
                                  );
-  errotpage($error) if $error;
+  errorpage($error) if $error;
 
 } else {
 
   $error = $cust_main->realtime_bop( $FS::payby::payby2bop{$payby}, $amount,
-    'quiet'     => 1,
-    'manual'    => 1,
-    'payinfo'   => $payinfo,
-    'paydate'   => "$year-$month-01",
-    'payname'   => $payname,
-    'payunique' => $payunique,
-    'paycvv'    => $paycvv,
+    'quiet'      => 1,
+    'manual'     => 1,
+    'balance'    => $balance,
+    'payinfo'    => $payinfo,
+    'paydate'    => "$year-$month-01",
+    'payname'    => $payname,
+    'payunique'  => $payunique,
+    'paycvv'     => $paycvv,
+    'paynum_ref' => \$paynum,
     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 $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;
 
 }
@@ -139,7 +177,7 @@ 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 );