1 package FS::ClientAPI::Signup;
6 use FS::Record qw(qsearch qsearchs);
8 use FS::cust_main_county;
13 use FS::Msgcat qw(gettext);
15 use FS::ClientAPI; #hmm
16 FS::ClientAPI->register_handlers(
17 'Signup/signup_info' => \&signup_info,
18 'Signup/new_customer' => \&new_customer,
24 my $conf = new FS::Conf;
29 [ map { $_->hashref } qsearch('cust_main_county', {}) ],
31 'agentnum2part_pkg' =>
34 my $href = $_->pkgpart_hashref;
37 map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
38 grep { $_->svcpart('svc_acct') && $href->{ $_->pkgpart } }
39 qsearch( 'part_pkg', { 'disabled' => '' } )
41 } qsearch('agent', {} )
44 'svc_acct_pop' => [ map { $_->hashref } qsearch('svc_acct_pop',{} ) ],
46 'security_phrase' => $conf->exists('security_phrase'),
48 'payby' => [ $conf->config('signup_server-payby') ],
50 'msgcat' => { map { $_=>gettext($_) } qw(
51 passwords_dont_match invalid_card unknown_card_type not_a
54 'statedefault' => $conf->config('statedefault') || 'CA',
56 'countrydefault' => $conf->config('countrydefault') || 'US',
60 if ( $conf->config('signup_server-default_agentnum') ) {
61 my $agentnum = $conf->config('signup_server-default_agentnum');
62 my $agent = qsearchs( 'agent', { 'agentnum' => $agentnum } )
63 or die "fatal: signup_server-default_agentnum $agentnum not found\n";
64 my $pkgpart_href = $agent->pkgpart_hashref;
66 $signup_info->{'part_pkg'} = [
68 map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
69 grep { $_->svcpart('svc_acct') && $pkgpart_href->{ $_->pkgpart } }
70 qsearch( 'part_pkg', { 'disabled' => '' } )
81 my $conf = new FS::Conf;
84 #things that aren't necessary in base class, but are for signup server
85 #return "Passwords don't match"
86 # if $hashref->{'_password'} ne $hashref->{'_password2'}
87 $error ||= gettext('empty_password') unless $packet->{'_password'};
88 # a bit inefficient for large numbers of pops
89 $error ||= gettext('no_access_number_selected')
90 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
92 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
93 # common that are still here and library them.
94 my $cust_main = new FS::cust_main ( {
96 'agentnum' => $packet->{agentnum}
97 || $conf->config('signup_server-default_agentnum'),
98 'refnum' => $packet->{refnum}
99 || $conf->config('signup_server-default_refnum'),
101 map { $_ => $packet->{$_} } qw(
102 last first ss company address1 address2 city county state zip country
103 daytime night fax payby payinfo paydate payname referral_custnum comments
108 $error ||= "Illegal payment type"
109 unless grep { $_ eq $packet->{'payby'} }
110 $conf->config('signup_server-payby');
112 $cust_main->payinfo($cust_main->daytime)
113 if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
115 my @invoicing_list = split( /\s*\,\s*/, $packet->{'invoicing_list'} );
117 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
121 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
122 or $error ||= "WARNING: unknown pkgpart: $pkgpart";
123 my $svcpart = $part_pkg->svcpart('svc_acct') unless $error;
125 my $cust_pkg = new FS::cust_pkg ( {
126 #later#'custnum' => $custnum,
127 'pkgpart' => $packet->{'pkgpart'},
129 $error ||= $cust_pkg->check;
131 my $svc_acct = new FS::svc_acct ( {
132 'svcpart' => $svcpart,
133 map { $_ => $packet->{$_} }
134 qw( username _password sec_phrase popnum ),
137 my $y = $svc_acct->setdefault; # arguably should be in new method
138 $error ||= $y unless ref($y);
140 $error ||= $svc_acct->check;
143 tie my %hash, 'Tie::RefHash';
144 %hash = ( $cust_pkg => [ $svc_acct ] );
146 $error ||= $cust_main->insert( \%hash, \@invoicing_list, 'noexport' => 1 );
148 if ( ! $error && $conf->exists('signup_server-realtime') ) {
150 #warn "[fs_signup_server] Billing customer...\n" if $Debug;
152 my $bill_error = $cust_main->bill;
153 #warn "[fs_signup_server] error billing new customer: $bill_error"
156 $cust_main->apply_payments;
157 $cust_main->apply_credits;
159 $bill_error = $cust_main->collect;
160 #warn "[fs_signup_server] error collecting from new customer: $bill_error"
163 if ( $cust_main->balance > 0 ) {
165 #this makes sense. credit is "un-doing" the invoice
166 $cust_main->credit( $cust_main->balance, 'signup server decline' );
167 $cust_main->apply_credits;
169 #should check list for errors...
170 #$cust_main->suspend;
171 local $FS::svc_Common::noexport_hack = 1;
172 $cust_main->cancel('quiet'=>1);
178 $cust_main->reexport unless $error;
180 return { error => $error };