[freeside-commits] freeside/FS/FS/cust_main Billing_Realtime.pm, 1.11, 1.12

Erik Levinson levinse at wavetail.420.am
Fri Jan 21 14:17:32 PST 2011


Update of /home/cvs/cvsroot/freeside/FS/FS/cust_main
In directory wavetail.420.am:/tmp/cvs-serv20947/FS/FS/cust_main

Modified Files:
	Billing_Realtime.pm 
Log Message:
add (unfinished) credit card surcharge, part 1

Index: Billing_Realtime.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/cust_main/Billing_Realtime.pm,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -w -d -r1.11 -r1.12
--- Billing_Realtime.pm	21 Dec 2010 09:12:45 -0000	1.11
+++ Billing_Realtime.pm	21 Jan 2011 22:17:30 -0000	1.12
@@ -19,7 +19,7 @@
 # 1 is mostly method/subroutine entry and options
 # 2 traces progress of some operations
 # 3 is even more information including possibly sensitive data
-$DEBUG = 0;
+$DEBUG = 1;
 $me = '[FS::cust_main::Billing_Realtime]';
 
 install_callback FS::UID sub { 
@@ -186,6 +186,13 @@
     }
   }
 
+  if ( $options->{'fake_gatewaynum'} ) {
+	$options->{payment_gateway} =
+	    qsearchs('payment_gateway',
+		      { 'gatewaynum' => $options->{'fake_gatewaynum'}, }
+		    );
+  }
+
   $options->{payment_gateway} = $self->agent->payment_gateway( %$options )
     unless exists($options->{payment_gateway});
 
@@ -309,12 +316,38 @@
     $options{amount} = $amount;
   }
   
+
+  ### 
+  # optional credit card surcharge
+  ###
+
+  my $cc_surcharge = 0;
+  my $cc_surcharge_pct = $conf->config('credit-card-surcharge-percentage');
+  
+  # always add cc surcharge if called from event 
+  if($options{'cc_surcharge_from_event'} && $cc_surcharge_pct > 0) {
+      $cc_surcharge = $options{'amount'} * $cc_surcharge_pct / 100;
+      $options{'amount'} += $cc_surcharge;
+      $options{'amount'} = sprintf("%.2f", $options{'amount'}); # round (again)?
+  }
+  elsif($cc_surcharge_pct > 0) { # we're called not from event (i.e. from a 
+                                 # payment screen), so consider the given 
+				 # amount as post-surcharge
+    $cc_surcharge = $options{'amount'} - ($options{'amount'} / ( 1 + $cc_surcharge_pct/100 ));
+  }
+  if ( $cc_surcharge > 0) {
+      $cc_surcharge = sprintf("%.2f",$cc_surcharge);
+      $options{'cc_surcharge'} = $cc_surcharge;
+  }
+ 
+
   if ( $DEBUG ) {
     warn "$me realtime_bop (new): $options{method} $options{amount}\n";
+    warn " cc_surcharge = $cc_surcharge\n";
     warn "  $_ => $options{$_}\n" foreach keys %options;
   }
 
-  return $self->fake_bop(%options) if $options{'fake'};
+  return $self->fake_bop(\%options) if $options{'fake'};
 
   $self->_bop_defaults(\%options);
 
@@ -709,6 +742,11 @@
   } );
   $cust_pay->payunique( $options{payunique} ) if length($options{payunique});
 
+  if ( $DEBUG ) {
+      warn "fake_bop\n cust_pay: ". Dumper($cust_pay) . "\n options: ";
+      warn "  $_ => $options{$_}\n" foreach keys %options;
+  }
+
   my $error = $cust_pay->insert($options{'manual'} ? ( 'manual' => 1 ) : () );
 
   if ( $error ) {
@@ -872,6 +910,60 @@
         }
       }
 
+      # have a CC surcharge portion --> one-time charge
+      if ( $options{'cc_surcharge'} > 0 ) { 
+	  my $invnum;
+	  $invnum = $options{'invnum'} if $options{'invnum'};
+	  unless ( $invnum ) { # probably from a payment screen
+	     # do we have any open invoices? pick earliest
+	     # uses the fact that cust_main->cust_bill sorts by date ascending
+	     my @open = $self->open_cust_bill;
+	     $invnum = $open[0]->invnum if scalar(@open);
+	  }
+	    
+	  unless ( $invnum ) {  # still nothing? pick last closed invoice
+	     # again uses fact that cust_main->cust_bill sorts by date ascending
+	     my @closed = $self->cust_bill;
+	     $invnum = $closed[$#closed]->invnum if scalar(@closed);
+	  }
+
+	  unless ( $invnum ) {
+	    # XXX: unlikely case - pre-paying before any invoices generated
+	    # what it should do is create a new invoice and pick it
+		warn 'CC SURCHARGE AND NO INVOICES PICKED TO APPLY IT!';
+		return '';
+	  }
+
+	  my $cust_pkg;
+	  my $charge_error = $self->charge({
+				    'amount' 	=> $options{'cc_surcharge'},
+				    'pkg' 	=> 'Credit Card Surcharge',
+				    'setuptax'  => 'Y',
+				    'cust_pkg_ref' => \$cust_pkg,
+				});
+	  if($charge_error) {
+		warn 'Unable to add CC surcharge';
+		return '';
+	  }
+				    
+	  my $cust_bill_pkg = new FS::cust_bill_pkg({
+	    'invnum' => $invnum,
+	    'pkgnum' => $cust_pkg->pkgnum,
+	    'setup' => $options{'cc_surcharge'},
+	  });
+	  my $cbp_error = $cust_bill_pkg->insert;
+
+	  if ( $cbp_error) {
+		warn 'Cannot add CC surcharge line item to invoice #'.$invnum;
+		return '';
+	  } else {
+	        my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum });
+		warn 'invoice for cc surcharge: ' . Dumper($cust_bill) if $DEBUG;
+		$cust_bill->apply_payments_and_credits;
+	  }
+
+      }
+
       return ''; #no error
 
     }



More information about the freeside-commits mailing list