1 package FS::ClientAPI::Signup;
6 use FS::Record qw(qsearch qsearchs dbdef);
7 use FS::Msgcat qw(gettext);
8 use FS::Misc qw(card_types);
9 use FS::ClientAPI_SessionCache;
11 use FS::cust_main_county;
24 my $conf = new FS::Conf;
26 use vars qw($signup_info_cache); #cache for performance;
27 $signup_info_cache ||= {
29 [ map { $_->hashref } qsearch('cust_main_county', {}) ],
34 qsearch('agent', { 'disabled' => '' } )
40 qsearch('part_referral', { 'disabled' => '' })
43 'agentnum2part_pkg' =>
46 my $href = $_->pkgpart_hashref;
49 map { { 'payby' => [ $_->payby ],
50 'freq_pretty' => $_->freq_pretty,
51 'options' => { $_->options },
54 grep { $_->svcpart('svc_acct') && $href->{ $_->pkgpart } }
55 qsearch( 'part_pkg', { 'disabled' => '' } )
57 } qsearch('agent', { 'disabled' => '' })
60 'svc_acct_pop' => [ map { $_->hashref } qsearch('svc_acct_pop',{} ) ],
62 'emailinvoiceonly' => $conf->exists('emailinvoiceonly'),
64 'security_phrase' => $conf->exists('security_phrase'),
66 'payby' => [ $conf->config('signup_server-payby') ],
68 'card_types' => card_types(),
70 'cvv_enabled' => defined dbdef->table('cust_main')->column('paycvv'), # 1,
72 'ship_enabled' => defined dbdef->table('cust_main')->column('ship_last'),#1,
74 'msgcat' => { map { $_=>gettext($_) } qw(
75 passwords_dont_match invalid_card unknown_card_type not_a empty_password illegal_or_empty_text
78 'statedefault' => $conf->config('statedefault') || 'CA',
80 'countrydefault' => $conf->config('countrydefault') || 'US',
82 'refnum' => $conf->config('signup_server-default_refnum'),
84 'default_pkgpart' => $conf->config('signup_server-default_pkgpart'),
88 my $signup_info = { %$signup_info_cache };
90 my @addl = qw( signup_server-classnum2 signup_server-classnum3 );
92 if ( grep { $conf->exists($_) } @addl ) {
94 $signup_info->{optional_packages} = [];
96 foreach my $addl ( @addl ) {
97 my $classnum = $conf->config($addl) or next;
100 'freq_pretty' => $_->freq_pretty,
101 'options' => { $_->options },
105 qsearch( 'part_pkg', { classnum => $classnum } );
107 push @{$signup_info->{optional_packages}}, \@pkgs;
113 my $agentnum = $packet->{'agentnum'}
114 || $conf->config('signup_server-default_agentnum');
115 $agentnum =~ /^(\d*)$/ or die "illegal agentnum";
119 if ( exists $packet->{'session_id'} ) {
120 my $cache = new FS::ClientAPI_SessionCache( {
121 'namespace' => 'FS::ClientAPI::Agent',
123 $session = $cache->get($packet->{'session_id'});
125 $agentnum = $session->{'agentnum'};
127 return { 'error' => "Can't resume session" }; #better error message
129 }elsif( exists $packet->{'customer_session_id'} ) {
130 my $cache = new FS::ClientAPI_SessionCache( {
131 'namespace' => 'FS::ClientAPI::MyAccount',
133 $session = $cache->get($packet->{'customer_session_id'});
135 my $custnum = $session->{'custnum'};
136 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum });
137 return { 'error' => "Can't find your customer record" } unless $cust_main;
138 $agentnum = $cust_main->agentnum;
140 return { 'error' => "Can't resume session" }; #better error message
144 $signup_info->{'part_pkg'} = [];
146 if ( $packet->{'reg_code'} ) {
147 $signup_info->{'part_pkg'} =
148 [ map { { 'payby' => [ $_->payby ],
149 'freq_pretty' => $_->freq_pretty,
150 'options' => { $_->options },
154 grep { $_->svcpart('svc_acct') }
156 qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
157 'agentnum' => $agentnum, } )
161 $signup_info->{'error'} = 'Unknown registration code'
162 unless @{ $signup_info->{'part_pkg'} };
164 } elsif ( $packet->{'promo_code'} ) {
166 $signup_info->{'part_pkg'} =
167 [ map { { 'payby' => [ $_->payby ],
168 'freq_pretty' => $_->freq_pretty,
169 'options' => { $_->options },
172 grep { $_->svcpart('svc_acct') }
173 qsearch( 'part_pkg', { 'promo_code' => {
175 value=>$packet->{'promo_code'}
177 'disabled' => '', } )
180 $signup_info->{'error'} = 'Unknown promotional code'
181 unless @{ $signup_info->{'part_pkg'} };
185 $signup_info->{'part_pkg'} = $signup_info->{'agentnum2part_pkg'}{$agentnum}
186 unless @{ $signup_info->{'part_pkg'} };
188 $signup_info->{'part_referral'} =
192 'table' => 'part_referral',
193 'hashref' => { 'disabled' => '' },
194 'extra_sql' => "AND ( agentnum = $agentnum ".
195 " OR agentnum IS NULL ) ",
202 # delete $signup_info->{'part_pkg'};
205 $signup_info->{'part_pkg'} = [ sort { $a->{pkg} cmp $b->{pkg} } # case?
206 @{ $signup_info->{'part_pkg'} }
209 if ( exists $packet->{'session_id'} ) {
210 my $agent_signup_info = { %$signup_info };
211 delete $agent_signup_info->{agentnum2part_pkg};
212 $agent_signup_info->{'agent'} = $session->{'agent'};
223 my $conf = new FS::Conf;
225 #things that aren't necessary in base class, but are for signup server
226 #return "Passwords don't match"
227 # if $hashref->{'_password'} ne $hashref->{'_password2'}
228 return { 'error' => gettext('empty_password') }
229 unless length($packet->{'_password'});
230 # a bit inefficient for large numbers of pops
231 return { 'error' => gettext('no_access_number_selected') }
232 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
235 if ( exists $packet->{'session_id'} ) {
236 my $cache = new FS::ClientAPI_SessionCache( {
237 'namespace' => 'FS::ClientAPI::Agent',
239 my $session = $cache->get($packet->{'session_id'});
241 $agentnum = $session->{'agentnum'};
243 return { 'error' => "Can't resume session" }; #better error message
246 $agentnum = $packet->{agentnum}
247 || $conf->config('signup_server-default_agentnum');
250 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
251 # common that are still here and library them.
252 my $cust_main = new FS::cust_main ( {
254 'agentnum' => $agentnum,
255 'refnum' => $packet->{refnum}
256 || $conf->config('signup_server-default_refnum'),
258 map { $_ => $packet->{$_} } qw(
260 last first ss company address1 address2
261 city county state zip country
264 ship_last ship_first ship_ss ship_company ship_address1 ship_address2
265 ship_city ship_county ship_state ship_zip ship_country
266 ship_daytime ship_night ship_fax
269 payinfo paycvv paydate payname
270 paystart_month paystart_year payissue
273 referral_custnum comments
278 return { 'error' => "Illegal payment type" }
279 unless grep { $_ eq $packet->{'payby'} }
280 $conf->config('signup_server-payby');
282 $cust_main->payinfo($cust_main->daytime)
283 if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
285 my @invoicing_list = $packet->{'invoicing_list'}
286 ? split( /\s*\,\s*/, $packet->{'invoicing_list'} )
289 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
291 return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
294 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
295 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
296 my $svcpart = $part_pkg->svcpart('svc_acct');
299 if ( $packet->{'reg_code'} ) {
300 $reg_code = qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
301 'agentnum' => $agentnum, } )
302 or return { 'error' => 'Unknown registration code' };
305 my $cust_pkg = new FS::cust_pkg ( {
306 #later#'custnum' => $custnum,
307 'pkgpart' => $packet->{'pkgpart'},
308 'promo_code' => $packet->{'promo_code'},
309 'reg_code' => $packet->{'reg_code'},
311 #my $error = $cust_pkg->check;
312 #return { 'error' => $error } if $error;
314 my $svc_acct = new FS::svc_acct ( {
315 'svcpart' => $svcpart,
316 map { $_ => $packet->{$_} }
317 qw( username _password sec_phrase popnum ),
322 while ( exists($packet->{"snarf_machine$snarfnum"})
323 && length($packet->{"snarf_machine$snarfnum"}) ) {
324 my $acct_snarf = new FS::acct_snarf ( {
325 'machine' => $packet->{"snarf_machine$snarfnum"},
326 'protocol' => $packet->{"snarf_protocol$snarfnum"},
327 'username' => $packet->{"snarf_username$snarfnum"},
328 '_password' => $packet->{"snarf_password$snarfnum"},
331 push @acct_snarf, $acct_snarf;
333 $svc_acct->child_objects( \@acct_snarf );
335 my $y = $svc_acct->setdefault; # arguably should be in new method
336 return { 'error' => $y } if $y && !ref($y);
338 #$error = $svc_acct->check;
339 #return { 'error' => $error } if $error;
341 #setup a job dependancy to delay provisioning
342 my $placeholder = new FS::queue ( {
343 'job' => 'FS::ClientAPI::Signup::__placeholder',
344 'status' => 'locked',
346 my $error = $placeholder->insert;
347 return { 'error' => $error } if $error;
350 tie my %hash, 'Tie::RefHash';
351 %hash = ( $cust_pkg => [ $svc_acct ] );
353 $error = $cust_main->insert(
356 'depend_jobnum' => $placeholder->jobnum,
359 my $perror = $placeholder->delete;
360 $error .= " (Additionally, error removing placeholder: $perror)" if $perror;
361 return { 'error' => $error };
364 if ( $conf->exists('signup_server-realtime') ) {
366 #warn "[fs_signup_server] Billing customer...\n" if $Debug;
368 my $bill_error = $cust_main->bill;
369 #warn "[fs_signup_server] error billing new customer: $bill_error"
372 $cust_main->apply_payments_and_credits;
374 $bill_error = $cust_main->collect('realtime' => 1);
375 #warn "[fs_signup_server] error collecting from new customer: $bill_error"
378 if ( $cust_main->balance > 0 ) {
380 #this makes sense. credit is "un-doing" the invoice
381 $cust_main->credit( $cust_main->balance, 'signup server decline' );
382 $cust_main->apply_credits;
384 #should check list for errors...
385 #$cust_main->suspend;
386 local $FS::svc_Common::noexport_hack = 1;
387 $cust_main->cancel('quiet'=>1);
389 my $perror = $placeholder->depended_delete;
390 warn "error removing provisioning jobs after decline: $perror" if $perror;
392 $perror = $placeholder->delete;
393 warn "error removing placeholder after decline: $perror" if $perror;
396 return { 'error' => '_decline' };
402 $error = $reg_code->delete;
403 return { 'error' => $error } if $error;
406 $error = $placeholder->delete;
407 return { 'error' => $error } if $error;
409 return { error => '' };