eWay self-signup fixes
[freeside.git] / FS / FS / ClientAPI / MyAccount.pm
index f6a71cd..9a20285 100644 (file)
@@ -237,6 +237,28 @@ sub logout {
   }
 }
 
+sub payment_gateway {
+  # internal use only
+  # takes a cust_main and a cust_payby entry, returns the payment_gateway
+  my $conf = new FS::Conf;
+  my $cust_main = shift;
+  my $cust_payby = shift;
+  my $gatewaynum = $conf->config('selfservice-payment_gateway');
+  if ( $gatewaynum ) {
+    my $pg = qsearchs('payment_gateway', { gatewaynum => $gatewaynum });
+    die "configured gatewaynum $gatewaynum not found!" if !$pg;
+    return $pg;
+  }
+  else {
+    return '' if ! FS::payby->realtime($cust_payby);
+    my $pg = $cust_main->agent->payment_gateway(
+      'method'  => FS::payby->payby2bop($cust_payby),
+      'nofatal' => 1
+    );
+    return $pg;
+  }
+}
+
 sub access_info {
   my $p = shift;
 
@@ -262,18 +284,11 @@ sub access_info {
   my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
     or return { 'error' => "unknown custnum $custnum" };
 
-  $info->{hide_payment_fields} =
-  [
-    map { my $pg = '';
-          if ( FS::payby->realtime($_) ) {
-            $pg = $cust_main->agent->payment_gateway(
-              'method'  => FS::payby->payby2bop($_),
-              'nofatal' => 1,
-            );
-          }
-          $pg && $pg->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
-        }
-    @{ $info->{cust_paybys} }
+  $info->{'hide_payment_fields'} = [ 
+    map { 
+      my $pg = payment_gateway($cust_main, $_);
+      $pg && $pg->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
+    } @{ $info->{cust_paybys} }
   ];
 
   $info->{'self_suspend_reason'} = 
@@ -532,18 +547,11 @@ sub payment_info {
   my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
     or return { 'error' => "unknown custnum $custnum" };
 
-  $return{hide_payment_fields} =
-  [
-    map { my $pg = '';
-          if ( FS::payby->realtime($_) ) {
-            $pg = $cust_main->agent->payment_gateway(
-              'method'  => FS::payby->payby2bop($_),
-              'nofatal' => 1,
-            );
-          }
-          $pg && $pg->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
-        }
-    @{ $return{cust_paybys} }
+  $return{'hide_payment_fields'} = [
+    map { 
+      my $pg = payment_gateway($cust_main, $_);
+      $pg && $pg->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
+    } @{ $return{cust_paybys} }
   ];
 
   $return{balance} = $cust_main->balance; #XXX pkg-balances?
@@ -692,6 +700,7 @@ sub process_payment {
     'paycvv'   => $paycvv,
     'pkgnum'   => $session->{'pkgnum'},
     'discount_term' => $discount_term,
+    'selfservice' => 1,
     map { $_ => $p->{$_} } @{ $payby2fields{$payby} }
   );
   return { 'error' => $error } if $error;
@@ -746,18 +755,27 @@ sub realtime_collect {
   my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
     or return { 'error' => "unknown custnum $custnum" };
 
+  my $amount;
+  if ( $p->{'amount'} ) {
+    $amount = $p->{'amount'};
+  }
+  elsif ( $session->{'pkgnum'} ) {
+    $amount = $cust_main->balance_pkgnum( $session->{'pkgnum'} );
+  }
+  else {
+    $amount = $cust_main->balance;
+  }
+
   my $error = $cust_main->realtime_collect(
     'method'     => $p->{'method'},
+    'amount'     => $amount,
     'pkgnum'     => $session->{'pkgnum'},
     'session_id' => $p->{'session_id'},
     'apply'      => 1,
+    'selfservice'=> 1,
   );
   return { 'error' => $error } unless ref( $error );
 
-  my $amount = $session->{'pkgnum'}
-                 ? $cust_main->balance_pkgnum( $session->{'pkgnum'} )
-                 : $cust_main->balance;
-
   return { 'error' => '', amount => $amount, %$error };
 }
 
@@ -1410,7 +1428,7 @@ sub _do_bop_realtime {
 
     my $bill_error =    $cust_main->bill
                      || $cust_main->apply_payments_and_credits
-                     || $cust_main->realtime_collect;
+                     || $cust_main->realtime_collect('selfservice' => 1);
 
     if (    $cust_main->balance > $old_balance
          && $cust_main->balance > 0
@@ -1548,6 +1566,38 @@ sub cancel_pkg {
 
 }
 
+sub provision_phone {
+ my $p = shift;
+ my @bulkdid = @{$p->{'bulkdid'}};
+ unless (scalar(@bulkdid)) {
+    return _provision( 'FS::svc_phone',
+                 [qw(phonenum countrycode)],
+                 [qw(phonenum countrycode)],
+                 $p,
+                 @_
+               );
+ }
+
+# bulk case
+  my $error;
+  foreach my $did ( @bulkdid ) {
+    $did =~ s/[^0-9]//g;
+    $error = _provision( 'FS::svc_phone',
+             [qw(phonenum countrycode)],
+             [qw(phonenum countrycode)],
+             {
+               'pkgnum' => $p->{'pkgnum'},
+               'svcpart' => $p->{'svcpart'},
+               'phonenum' => $did,
+               'countrycode' => $p->{'countrycode'},
+               'session_id' => $p->{'session_id'},
+             }
+           );
+    return $error if ($error->{'error'} && length($error->{'error'}) > 1);
+  }
+  { 'bulkdid' => [ @bulkdid ], 'svc' => $error->{'svc'} }
+}
+
 sub provision_acct {
   my $p = shift;
   warn "provision_acct called\n"
@@ -1907,16 +1957,24 @@ sub get_ticket {
 # the requested ticket was actually linked to this customer
     my @custs = @{$err_or_ticket->{'custs'}};
     my @txns = @{$err_or_ticket->{'txns'}};
+    my @filtered_txns;
 
     return { 'error' => 'no customer' } unless ( $custnum && scalar(@custs) );
 
     return { 'error' => 'invalid ticket requested' } 
        unless grep($_ eq $custnum, @custs);
 
+    foreach my $txn ( @txns ) {
+       push @filtered_txns, $txn 
+           if ($txn->{'type'} eq 'EmailRecord' 
+               || $txn->{'type'} eq 'Correspond'
+               || $txn->{'type'} eq 'Create');
+    }
+
     warn "$me get_ticket: sucessful: \n"
       if $DEBUG;
     return { 'error'     => '',
-             'transactions' => \@txns,
+             'transactions' => \@filtered_txns,
             'ticket_id' => $p->{'ticket_id'},
            };
   } else {