71513: Card tokenization [bug fix to selfservice-payment_gateway removal]
[freeside.git] / FS / FS / ClientAPI / Signup.pm
index 94c9c5e..5ced42b 100644 (file)
@@ -164,8 +164,6 @@ sub signup_info {
 
       'security_phrase' => $conf->exists('security_phrase'),
 
-      'nomadix' => $conf->exists('signup_server-nomadix'),
-
       'payby' => [ $conf->config('signup_server-payby') ],
 
       'payby_longname' => [ map { FS::payby->longname($_) } 
@@ -192,7 +190,6 @@ sub signup_info {
 
       'agentnum2part_pkg'  => $agentnum2part_pkg,
       'svc_acct_pop'       => [ map $_->hashref, qsearch('svc_acct_pop',{} ) ],
-      'nomadix'            => $conf->exists('signup_server-nomadix'),
       'payby'              => [ $conf->config('signup_server-payby') ],
       'card_types'         => card_types(),
       'paytypes'           => [ FS::cust_payby->paytypes ],
@@ -347,20 +344,11 @@ sub signup_info {
     my @paybys = @{ $signup_info->{'payby'} };
     $signup_info->{'hide_payment_fields'} = [];
 
-    my $gatewaynum = $conf->config('selfservice-payment_gateway');
-    my $force_gateway;
-    if ( $gatewaynum ) {
-      $force_gateway = qsearchs('payment_gateway', { gatewaynum => $gatewaynum });
-      warn "using forced gateway #$gatewaynum - " .
-        $force_gateway->gateway_username . '@' . $force_gateway->gateway_module
-        if $DEBUG > 1;
-      die "configured gatewaynum $gatewaynum not found!" if !$force_gateway;
-    }
     foreach my $payby (@paybys) {
       warn "$me checking $payby payment fields\n" if $DEBUG > 1;
       my $hide = 0;
       if ( FS::payby->realtime($payby) ) {
-        my $gateway = $force_gateway || 
+        my $gateway = 
           $agent->payment_gateway( 'method'  => FS::payby->payby2bop($payby),
                                    'nofatal' => 1,
                                  );
@@ -630,17 +618,9 @@ sub new_customer {
     return { 'error' => "Unknown reseller" }
       unless $agent;
 
-    my $gw;
-    my $gatewaynum = $conf->config('selfservice-payment_gateway');
-    if ( $gatewaynum ) {
-      $gw = qsearchs('payment_gateway', { gatewaynum => $gatewaynum });
-      die "configured gatewaynum $gatewaynum not found!" if !$gw;
-    }
-    else {
-      $gw = $agent->payment_gateway( 'method'  => FS::payby->payby2bop($payby),
-                                     'nofatal' => 1,
+    my $gw = $agent->payment_gateway( 'method'  => FS::payby->payby2bop($payby),
+                                      'nofatal' => 1,
                                     );
-    }
 
     $cust_main->payby('BILL')   # MCRD better?  no, that's for something else
       if $gw && $gw->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
@@ -1123,36 +1103,28 @@ sub capture_payment {
 
   my $conf = new FS::Conf;
 
-  my $payment_gateway;
-  if ( my $gwnum = $conf->config('selfservice-payment_gateway') ) {
-    $payment_gateway = qsearchs('payment_gateway', { 'gatewaynum' => $gwnum })
-      or die "configured gatewaynum $gwnum not found!";
-  }
-  else {
-    my $url = $packet->{url};
-
-    $payment_gateway = qsearchs('payment_gateway', 
+  my $url = $packet->{url};
+  my $payment_gateway = qsearchs('payment_gateway', 
         { 'gateway_callback_url' => popurl(0, $url) } 
       );
-    if (!$payment_gateway) { 
-
-      my ( $processor, $login, $password, $action, @bop_options ) =
-        $conf->config('business-onlinepayment');
-      $action ||= 'normal authorization';
-      pop @bop_options if scalar(@bop_options) % 2 && $bop_options[-1] =~ /^\s*$/;
-      die "No real-time processor is enabled - ".
-          "did you set the business-onlinepayment configuration value?\n"
-        unless $processor;
-
-      $payment_gateway = new FS::payment_gateway( {
-        gateway_namespace => $conf->config('business-onlinepayment-namespace'),
-        gateway_module    => $processor,
-        gateway_username  => $login,
-        gateway_password  => $password,
-        gateway_action    => $action,
-        options   => [ ( @bop_options ) ],
-      });
-    }
+  if (!$payment_gateway) { 
+
+    my ( $processor, $login, $password, $action, @bop_options ) =
+      $conf->config('business-onlinepayment');
+    $action ||= 'normal authorization';
+    pop @bop_options if scalar(@bop_options) % 2 && $bop_options[-1] =~ /^\s*$/;
+    die "No real-time processor is enabled - ".
+        "did you set the business-onlinepayment configuration value?\n"
+      unless $processor;
+
+    $payment_gateway = new FS::payment_gateway( {
+      gateway_namespace => $conf->config('business-onlinepayment-namespace'),
+      gateway_module    => $processor,
+      gateway_username  => $login,
+      gateway_password  => $password,
+      gateway_action    => $action,
+      options   => [ ( @bop_options ) ],
+    });
   }
  
   die "No real-time third party processor is enabled - ".
@@ -1250,9 +1222,11 @@ sub get_agentnum {
 
 Creates a new L<FS::prospect_main> entry. PACKET must contain:
 
-- either agentnum or session_id (unless signup_server-default_agentnum exists)
+- either agentnum or session_id; if not, signup_server-default_agentnum will
+be used and must not be empty
 
-- refnum (unless signup_server-default_refnum exists)
+- either refnum or referral_title; if not, signup_server-default_refnum will
+be used and must not be empty
 
 - last and first (names), and optionally company and title
 
@@ -1260,8 +1234,15 @@ Creates a new L<FS::prospect_main> entry. PACKET must contain:
 
 - emailaddress
 
+and can also contain:
+
 - one or more of phone_daytime, phone_night, phone_mobile, and phone_fax
 
+- a 'comment' (will be attached to the contact)
+
+State and country will be normalized to Freeside state/country codes if
+necessary.
+
 =cut
 
 sub new_prospect {
@@ -1274,10 +1255,37 @@ sub new_prospect {
   my $dbh = dbh;
   my $conf = FS::Conf->new;
 
+  my $error;
+
   my $agentnum = get_agentnum($packet);
   return $agentnum if ref $agentnum;
-  my $refnum = $packet->{refnum}
-               || $conf->config('signup_server-default_refnum');
+  my $refnum;
+  if ( my $title = $packet->{referral_title} ) {
+    my $part_referral = qsearchs('part_referral', {
+        'agentnum'  => $agentnum,
+        'title'     => $title,
+    });
+    $part_referral ||= qsearchs('part_referral', {
+        'agentnum'  => '',
+        'title'     => $title,
+    });
+    if (!$part_referral) {
+      $part_referral = FS::part_referral->new({
+        'agentnum' => $agentnum,
+        'title'    => $title,
+        'referral' => $title,
+      });
+      $error = $part_referral->insert;
+      if ( $error ) {
+        warn "ERROR: could not create referral type '$title': $error\n";
+      }
+    }
+    $refnum = $part_referral->refnum;
+  } elsif ( $packet->{refnum} ) {
+    $refnum = $packet->{refnum};
+  }
+  $refnum ||= $conf->config('signup_server-default_refnum');
+  return { error => "Signup referral type is not configured" } if !$refnum;
 
   my $prospect = FS::prospect_main->new({
       'agentnum' => $agentnum,
@@ -1286,12 +1294,41 @@ sub new_prospect {
   });
 
   my $location = FS::cust_location->new;
-  foreach ( qw(address1 address2 city county state zip country ) ) {
+  foreach ( qw(address1 address2 city county zip ) ) {
     $location->set($_, $packet->{$_});
   }
-  $prospect->set('cust_location', $location);
-  
-  my $error = $prospect->insert; # also does location
+  # normalize country and state if they're not already ISO codes
+  # easier than doing it on the client side--we already have the tables here
+  my $country = $packet->{country};
+  my $state = $packet->{state};
+  if (length($country) > 2) {
+    # it likes title case
+    $country = join(' ', map ucfirst, split(/\s+/, $country));
+    my $lsc = Locale::SubCountry->new($country);
+    if ($lsc) {
+      $country = uc($lsc->country_code);
+
+      if ($lsc->has_sub_countries) {
+        if ( $lsc->full_name($state) eq 'unknown' ) {
+          # then we were probably given a full name, so resolve it
+          $state = $lsc->code($state);
+          if ( $state eq 'unknown' ) {
+            # doesn't resolve as a full name either, return an error
+            $error = "Unknown state: ".$packet->{state};
+          } else {
+            $state = uc($state);
+          }
+        }
+      } # else state doesn't matter
+    } else {
+      # couldn't find the country in LSC
+      $error = "Unknown country: $country";
+    }
+  }
+  $location->set('country', $country);
+  $location->set('state', $state);
+
+  $error ||= $prospect->insert( cust_location => $location );
   return { error => $error } if $error;
 
   my $contact = FS::contact->new({
@@ -1300,7 +1337,7 @@ sub new_prospect {
       invoice_dest  => 'Y',
   });
   # use emailaddress pseudo-field behavior here
-  foreach (qw(last first title emailaddress)) {
+  foreach (qw(last first title emailaddress comment)) {
     $contact->set($_, $packet->{$_});
   }
   $error = $contact->insert;