1 package FS::ClientAPI::Signup;
6 use FS::Record qw(qsearch qsearchs dbdef);
8 use FS::cust_main_county;
15 use FS::Msgcat qw(gettext);
17 use FS::ClientAPI; #hmm
18 FS::ClientAPI->register_handlers(
19 'Signup/signup_info' => \&signup_info,
20 'Signup/new_customer' => \&new_customer,
26 my $conf = new FS::Conf;
28 use vars qw($signup_info); #cache for performance;
32 [ map { $_->hashref } qsearch('cust_main_county', {}) ],
37 qsearch('agent', dbdef->table('agent')->column('disabled')
38 ? { 'disabled' => '' }
46 qsearch('part_referral',
47 dbdef->table('part_referral')->column('disabled')
48 ? { 'disabled' => '' }
53 'agentnum2part_pkg' =>
56 my $href = $_->pkgpart_hashref;
59 map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
60 grep { $_->svcpart('svc_acct') && $href->{ $_->pkgpart } }
61 qsearch( 'part_pkg', { 'disabled' => '' } )
63 } qsearch('agent', dbdef->table('agent')->column('disabled')
64 ? { 'disabled' => '' }
69 'svc_acct_pop' => [ map { $_->hashref } qsearch('svc_acct_pop',{} ) ],
71 'security_phrase' => $conf->exists('security_phrase'),
73 'payby' => [ $conf->config('signup_server-payby') ],
75 'cvv_enabled' => defined dbdef->table('cust_main')->column('paycvv'),
77 'msgcat' => { map { $_=>gettext($_) } qw(
78 passwords_dont_match invalid_card unknown_card_type not_a
81 'statedefault' => $conf->config('statedefault') || 'CA',
83 'countrydefault' => $conf->config('countrydefault') || 'US',
85 'refnum' => $conf->config('signup_server-default_refnum'),
90 $conf->config('signup_server-default_agentnum')
91 && !exists $signup_info->{'part_pkg'} #cache for performance
93 my $agentnum = $conf->config('signup_server-default_agentnum');
94 my $agent = qsearchs( 'agent', { 'agentnum' => $agentnum } )
95 or die "fatal: signup_server-default_agentnum $agentnum not found\n";
96 my $pkgpart_href = $agent->pkgpart_hashref;
98 $signup_info->{'part_pkg'} = [
100 map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
101 grep { $_->svcpart('svc_acct') && $pkgpart_href->{ $_->pkgpart } }
102 qsearch( 'part_pkg', { 'disabled' => '' } )
113 my $conf = new FS::Conf;
115 #things that aren't necessary in base class, but are for signup server
116 #return "Passwords don't match"
117 # if $hashref->{'_password'} ne $hashref->{'_password2'}
118 return { 'error' => gettext('empty_password') }
119 unless $packet->{'_password'};
120 # a bit inefficient for large numbers of pops
121 return { 'error' => gettext('no_access_number_selected') }
122 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
124 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
125 # common that are still here and library them.
126 my $cust_main = new FS::cust_main ( {
128 'agentnum' => $packet->{agentnum}
129 || $conf->config('signup_server-default_agentnum'),
130 'refnum' => $packet->{refnum}
131 || $conf->config('signup_server-default_refnum'),
133 map { $_ => $packet->{$_} } qw(
134 last first ss company address1 address2 city county state zip country
135 daytime night fax payby payinfo paycvv paydate payname referral_custnum
141 return { 'error' => "Illegal payment type" }
142 unless grep { $_ eq $packet->{'payby'} }
143 $conf->config('signup_server-payby');
145 $cust_main->payinfo($cust_main->daytime)
146 if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
148 my @invoicing_list = split( /\s*\,\s*/, $packet->{'invoicing_list'} );
150 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
152 return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
155 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
156 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
157 my $svcpart = $part_pkg->svcpart('svc_acct');
159 my $cust_pkg = new FS::cust_pkg ( {
160 #later#'custnum' => $custnum,
161 'pkgpart' => $packet->{'pkgpart'},
163 my $error = $cust_pkg->check;
164 return { 'error' => $error } if $error;
166 my $svc_acct = new FS::svc_acct ( {
167 'svcpart' => $svcpart,
168 map { $_ => $packet->{$_} }
169 qw( username _password sec_phrase popnum ),
174 while ( length($packet->{"snarf_machine$snarfnum"}) ) {
175 my $acct_snarf = new FS::acct_snarf ( {
176 'machine' => $packet->{"snarf_machine$snarfnum"},
177 'protocol' => $packet->{"snarf_protocol$snarfnum"},
178 'username' => $packet->{"snarf_username$snarfnum"},
179 '_password' => $packet->{"snarf_password$snarfnum"},
182 push @acct_snarf, $acct_snarf;
184 $svc_acct->child_objects( \@acct_snarf );
186 my $y = $svc_acct->setdefault; # arguably should be in new method
187 return { 'error' => $y } if $y && !ref($y);
189 $error = $svc_acct->check;
190 return { 'error' => $error } if $error;
193 tie my %hash, 'Tie::RefHash';
194 %hash = ( $cust_pkg => [ $svc_acct ] );
196 $error = $cust_main->insert( \%hash, \@invoicing_list, 'noexport' => 1 );
197 return { 'error' => $error } if $error;
199 if ( $conf->exists('signup_server-realtime') ) {
201 #warn "[fs_signup_server] Billing customer...\n" if $Debug;
203 my $bill_error = $cust_main->bill;
204 #warn "[fs_signup_server] error billing new customer: $bill_error"
207 $cust_main->apply_payments;
208 $cust_main->apply_credits;
210 $bill_error = $cust_main->collect;
211 #warn "[fs_signup_server] error collecting from new customer: $bill_error"
214 if ( $cust_main->balance > 0 ) {
216 #this makes sense. credit is "un-doing" the invoice
217 $cust_main->credit( $cust_main->balance, 'signup server decline' );
218 $cust_main->apply_credits;
220 #should check list for errors...
221 #$cust_main->suspend;
222 local $FS::svc_Common::noexport_hack = 1;
223 $cust_main->cancel('quiet'=>1);
225 return { 'error' => '_decline' };
229 $cust_main->reexport;
231 return { error => '' };