changes to support eWay third-party payment, #10208
[freeside.git] / FS / FS / agent.pm
index e471e04..3794d3f 100644 (file)
@@ -206,7 +206,10 @@ sub ticketing_queue {
 
 Returns a payment gateway object (see L<FS::payment_gateway>) for this agent.
 
-Currently available options are I<invnum>, I<method>, and I<payinfo>.
+Currently available options are I<nofatal>, I<invnum>, I<method>, and I<payinfo>.
+
+If I<nofatal> is set, and no gateway is available, then the empty string
+will be returned instead of throwing a fatal exception.
 
 If I<invnum> is set to the number of an invoice (see L<FS::cust_bill>) then
 an attempt will be made to select a gateway suited for the taxes paid on 
@@ -225,21 +228,26 @@ sub payment_gateway {
 
   my $taxclass = '';
   if ( $options{invnum} ) {
+
     my $cust_bill = qsearchs('cust_bill', { 'invnum' => $options{invnum} } );
     die "invnum ". $options{'invnum'}. " not found" unless $cust_bill;
-    my @taxclasses =
-      map  { $_->part_pkg->taxclass }
+
+    my @part_pkg =
+      map  { $_->part_pkg }
       grep { $_ }
       map  { $_->cust_pkg }
       $cust_bill->cust_bill_pkg;
-    unless ( grep { $taxclasses[0] ne $_ } @taxclasses ) { #unless there are
-                                                           #different taxclasses      $taxclass = $taxclasses[0];
-    }
+
+    my @taxclasses = map $_->taxclass, @part_pkg;
+
+    $taxclass = $taxclasses[0]
+      unless grep { $taxclasses[0] ne $_ } @taxclasses; #unless there are
+                                                        #different taxclasses
   }
 
   #look for an agent gateway override first
   my $cardtype;
-  if ( $options{method} && $options{method} eq 'CC' ) {
+  if ( $options{method} && $options{method} eq 'CC' && $options{payinfo} ) {
     $cardtype = cardtype($options{payinfo});
   } elsif ( $options{method} && $options{method} eq 'ECHECK' ) {
     $cardtype = 'ACH';
@@ -261,18 +269,27 @@ sub payment_gateway {
                                            cardtype => '',
                                            taxclass => '',              } );
 
-  my $payment_gateway = new FS::payment_gateway;
+  my $payment_gateway;
+  my $conf = new FS::Conf;
   if ( $override ) { #use a payment gateway override
 
     $payment_gateway = $override->payment_gateway;
 
+    $payment_gateway->gateway_namespace('Business::OnlinePayment')
+      unless $payment_gateway->gateway_namespace;
+
   } else { #use the standard settings from the config
+
     # the standard settings from the config could be moved to a null agent
     # agent_payment_gateway referenced payment_gateway
 
-    my $conf = new FS::Conf;
-    die "Real-time processing not enabled\n"
-      unless $conf->exists('business-onlinepayment');
+    unless ( $conf->exists('business-onlinepayment') ) {
+      if ( $options{'nofatal'} ) {
+        return '';
+      } else {
+        die "Real-time processing not enabled\n";
+      }
+    }
 
     #load up config
     my $bop_config = 'business-onlinepayment';
@@ -289,6 +306,8 @@ sub payment_gateway {
         "did you set the business-onlinepayment configuration value?\n"
       unless $processor;
 
+    $payment_gateway = new FS::payment_gateway;
+
     $payment_gateway->gateway_namespace( $conf->config('business-onlinepayment-namespace') ||
                                  'Business::OnlinePayment');
     $payment_gateway->gateway_module($processor);
@@ -299,6 +318,13 @@ sub payment_gateway {
 
   }
 
+  unless ( $payment_gateway->gateway_namespace ) {
+    $payment_gateway->gateway_namespace(
+      scalar($conf->config('business-onlinepayment-namespace'))
+      || 'Business::OnlinePayment'
+    );
+  }
+
   $payment_gateway;
 }