1 package FS::ClientAPI::Signup;
4 use vars qw($DEBUG $me);
8 use FS::Record qw(qsearch qsearchs dbdef);
9 use FS::Msgcat qw(gettext);
10 use FS::Misc qw(card_types);
11 use FS::ClientAPI_SessionCache;
13 use FS::cust_main_county;
25 $me = '[FS::ClientAPI::Signup]';
30 warn "$me signup_info called on $packet\n" if $DEBUG;
32 my $conf = new FS::Conf;
33 my $svc_x = $conf->config('signup_server-service') || 'svc_acct';
35 my $cache = new FS::ClientAPI_SessionCache( {
36 'namespace' => 'FS::ClientAPI::Signup',
38 my $signup_info_cache = $cache->get('signup_info_cache');
40 if ( $signup_info_cache ) {
42 warn "$me loading cached signup info\n" if $DEBUG > 1;
46 warn "$me populating signup info cache\n" if $DEBUG > 1;
48 my $agentnum2part_pkg =
51 my $href = $_->pkgpart_hashref;
54 map { { 'payby' => [ $_->payby ],
55 'freq_pretty' => $_->freq_pretty,
56 'options' => { $_->options },
59 grep { $_->svcpart($svc_x) && $href->{ $_->pkgpart } }
60 qsearch( 'part_pkg', { 'disabled' => '' } )
62 } qsearch('agent', { 'disabled' => '' })
65 my $msgcat = { map { $_=>gettext($_) }
66 qw( passwords_dont_match invalid_card unknown_card_type
67 not_a empty_password illegal_or_empty_text )
69 warn "msgcat: ". Dumper($msgcat). "\n" if $DEBUG > 2;
71 my $label = { map { $_ => FS::Msgcat::_gettext($_) }
72 qw( stateid stateid_state )
74 warn "label: ". Dumper($label). "\n" if $DEBUG > 2;
76 $signup_info_cache = {
77 'cust_main_county' => [ map $_->hashref,
78 qsearch('cust_main_county', {} )
81 'agent' => [ map $_->hashref,
82 qsearch('agent', { 'disabled' => '' } )
85 'part_referral' => [ map $_->hashref,
86 qsearch('part_referral', { 'disabled' => '' } )
89 'agentnum2part_pkg' => $agentnum2part_pkg,
91 'svc_acct_pop' => [ map $_->hashref, qsearch('svc_acct_pop',{} ) ],
93 'emailinvoiceonly' => $conf->exists('emailinvoiceonly'),
95 'security_phrase' => $conf->exists('security_phrase'),
97 'payby' => [ $conf->config('signup_server-payby') ],
99 'card_types' => card_types(),
101 'paytypes' => [ @FS::cust_main::paytypes ],
105 'stateid_enabled' => $conf->exists('show_stateid'),
107 'paystate_enabled' => $conf->exists('show_bankstate'),
115 'statedefault' => scalar($conf->config('statedefault')) || 'CA',
117 'countrydefault' => scalar($conf->config('countrydefault')) || 'US',
119 'refnum' => scalar($conf->config('signup_server-default_refnum')),
121 'default_pkgpart' => scalar($conf->config('signup_server-default_pkgpart')),
123 'signup_service' => $svc_x,
124 'default_svcpart' => scalar($conf->config('signup_server-default_svcpart')),
128 $cache->set('signup_info_cache', $signup_info_cache);
132 my $signup_info = { %$signup_info_cache };
133 warn "$me signup info loaded\n" if $DEBUG > 1;
134 warn Dumper($signup_info). "\n" if $DEBUG > 2;
136 my @addl = qw( signup_server-classnum2 signup_server-classnum3 );
138 if ( grep { $conf->exists($_) } @addl ) {
140 $signup_info->{optional_packages} = [];
142 foreach my $addl ( @addl ) {
144 warn "$me adding optional package info\n" if $DEBUG > 1;
146 my $classnum = $conf->config($addl) or next;
149 'freq_pretty' => $_->freq_pretty,
150 'options' => { $_->options },
154 qsearch( 'part_pkg', { classnum => $classnum } );
156 push @{$signup_info->{optional_packages}}, \@pkgs;
158 warn "$me done adding opt. package info for $classnum\n" if $DEBUG > 1;
164 my $agentnum = $packet->{'agentnum'}
165 || $conf->config('signup_server-default_agentnum');
166 $agentnum =~ /^(\d*)$/ or die "illegal agentnum";
170 if ( exists $packet->{'session_id'} ) {
172 warn "$me loading agent session\n" if $DEBUG > 1;
173 my $cache = new FS::ClientAPI_SessionCache( {
174 'namespace' => 'FS::ClientAPI::Agent',
176 $session = $cache->get($packet->{'session_id'});
178 $agentnum = $session->{'agentnum'};
180 return { 'error' => "Can't resume session" }; #better error message
182 warn "$me done loading agent session\n" if $DEBUG > 1;
184 } elsif ( exists $packet->{'customer_session_id'} ) {
186 warn "$me loading customer session\n" if $DEBUG > 1;
187 my $cache = new FS::ClientAPI_SessionCache( {
188 'namespace' => 'FS::ClientAPI::MyAccount',
190 $session = $cache->get($packet->{'customer_session_id'});
192 my $custnum = $session->{'custnum'};
193 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum });
194 return { 'error' => "Can't find your customer record" } unless $cust_main;
195 $agentnum = $cust_main->agentnum;
197 return { 'error' => "Can't resume session" }; #better error message
199 warn "$me done loading customer session\n" if $DEBUG > 1;
203 $signup_info->{'part_pkg'} = [];
205 if ( $packet->{'reg_code'} ) {
207 warn "$me setting package list via reg_code\n" if $DEBUG > 1;
209 $signup_info->{'part_pkg'} =
210 [ map { { 'payby' => [ $_->payby ],
211 'freq_pretty' => $_->freq_pretty,
212 'options' => { $_->options },
216 grep { $_->svcpart($svc_x) }
218 qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
219 'agentnum' => $agentnum, } )
223 $signup_info->{'error'} = 'Unknown registration code'
224 unless @{ $signup_info->{'part_pkg'} };
226 warn "$me done setting package list via reg_code\n" if $DEBUG > 1;
228 } elsif ( $packet->{'promo_code'} ) {
230 warn "$me setting package list via promo_code\n" if $DEBUG > 1;
232 $signup_info->{'part_pkg'} =
233 [ map { { 'payby' => [ $_->payby ],
234 'freq_pretty' => $_->freq_pretty,
235 'options' => { $_->options },
238 grep { $_->svcpart($svc_x) }
239 qsearch( 'part_pkg', { 'promo_code' => {
241 value=>$packet->{'promo_code'}
243 'disabled' => '', } )
246 $signup_info->{'error'} = 'Unknown promotional code'
247 unless @{ $signup_info->{'part_pkg'} };
249 warn "$me done setting package list via promo_code\n" if $DEBUG > 1;
254 warn "$me setting agent-specific package list\n" if $DEBUG > 1;
255 $signup_info->{'part_pkg'} = $signup_info->{'agentnum2part_pkg'}{$agentnum}
256 unless @{ $signup_info->{'part_pkg'} };
257 warn "$me done setting agent-specific package list\n" if $DEBUG > 1;
259 warn "$me setting agent-specific adv. source list\n" if $DEBUG > 1;
260 $signup_info->{'part_referral'} =
264 'table' => 'part_referral',
265 'hashref' => { 'disabled' => '' },
266 'extra_sql' => "AND ( agentnum = $agentnum ".
267 " OR agentnum IS NULL ) ",
271 warn "$me done setting agent-specific adv. source list\n" if $DEBUG > 1;
275 # delete $signup_info->{'part_pkg'};
278 warn "$me sorting package list\n" if $DEBUG > 1;
279 $signup_info->{'part_pkg'} = [ sort { $a->{pkg} cmp $b->{pkg} } # case?
280 @{ $signup_info->{'part_pkg'} }
282 warn "$me done sorting package list\n" if $DEBUG > 1;
284 if ( exists $packet->{'session_id'} ) {
285 my $agent_signup_info = { %$signup_info };
286 delete $agent_signup_info->{agentnum2part_pkg};
287 $agent_signup_info->{'agent'} = $session->{'agent'};
295 sub domain_select_hash {
300 if ($packet->{pkgpart}) {
301 my $part_pkg = qsearchs('part_pkg' => { 'pkgpart' => $packet->{pkgpart} } );
302 #$packet->{svcpart} = $part_pkg->svcpart('svc_acct')
303 $packet->{svcpart} = $part_pkg->svcpart
307 if ($packet->{svcpart}) {
308 my $part_svc = qsearchs('part_svc' => { 'svcpart' => $packet->{svcpart} } );
309 $response->{'domsvc'} = $part_svc->part_svc_column('domsvc')->columnvalue
310 if ($part_svc && $part_svc->part_svc_column('domsvc')->columnflag eq 'D');
313 $response->{'domains'}
314 = { domain_select_hash FS::svc_acct( map { $_ => $packet->{$_} }
324 my $conf = new FS::Conf;
325 my $svc_x = $conf->config('signup_server-service') || 'svc_acct';
327 if ( $svc_x eq 'svc_acct' ) {
329 #things that aren't necessary in base class, but are for signup server
330 #return "Passwords don't match"
331 # if $hashref->{'_password'} ne $hashref->{'_password2'}
332 return { 'error' => gettext('empty_password') }
333 unless length($packet->{'_password'});
334 # a bit inefficient for large numbers of pops
335 return { 'error' => gettext('no_access_number_selected') }
336 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
341 if ( exists $packet->{'session_id'} ) {
342 my $cache = new FS::ClientAPI_SessionCache( {
343 'namespace' => 'FS::ClientAPI::Agent',
345 my $session = $cache->get($packet->{'session_id'});
347 $agentnum = $session->{'agentnum'};
349 return { 'error' => "Can't resume session" }; #better error message
352 $agentnum = $packet->{agentnum}
353 || $conf->config('signup_server-default_agentnum');
356 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
357 # common that are still here and library them.
358 my $cust_main = new FS::cust_main ( {
360 'agentnum' => $agentnum,
361 'refnum' => $packet->{refnum}
362 || $conf->config('signup_server-default_refnum'),
364 map { $_ => $packet->{$_} } qw(
366 last first ss company address1 address2
367 city county state zip country
368 daytime night fax stateid stateid_state
370 ship_last ship_first ship_ss ship_company ship_address1 ship_address2
371 ship_city ship_county ship_state ship_zip ship_country
372 ship_daytime ship_night ship_fax
375 payinfo paycvv paydate payname paystate paytype
376 paystart_month paystart_year payissue
379 referral_custnum comments
384 return { 'error' => "Illegal payment type" }
385 unless grep { $_ eq $packet->{'payby'} }
386 $conf->config('signup_server-payby');
388 $cust_main->payinfo($cust_main->daytime)
389 if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
391 my @invoicing_list = $packet->{'invoicing_list'}
392 ? split( /\s*\,\s*/, $packet->{'invoicing_list'} )
395 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
397 return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
400 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
401 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
402 my $svcpart = $part_pkg->svcpart($svc_x);
405 if ( $packet->{'reg_code'} ) {
406 $reg_code = qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
407 'agentnum' => $agentnum, } )
408 or return { 'error' => 'Unknown registration code' };
411 my $cust_pkg = new FS::cust_pkg ( {
412 #later#'custnum' => $custnum,
413 'pkgpart' => $packet->{'pkgpart'},
414 'promo_code' => $packet->{'promo_code'},
415 'reg_code' => $packet->{'reg_code'},
417 #my $error = $cust_pkg->check;
418 #return { 'error' => $error } if $error;
420 #should be all auto-magic and shit
422 if ( $svc_x eq 'svc_acct' ) {
424 $svc = new FS::svc_acct ( {
425 'svcpart' => $svcpart,
426 map { $_ => $packet->{$_} }
427 qw( username _password sec_phrase popnum ),
432 while ( exists($packet->{"snarf_machine$snarfnum"})
433 && length($packet->{"snarf_machine$snarfnum"}) ) {
434 my $acct_snarf = new FS::acct_snarf ( {
435 'machine' => $packet->{"snarf_machine$snarfnum"},
436 'protocol' => $packet->{"snarf_protocol$snarfnum"},
437 'username' => $packet->{"snarf_username$snarfnum"},
438 '_password' => $packet->{"snarf_password$snarfnum"},
441 push @acct_snarf, $acct_snarf;
443 $svc->child_objects( \@acct_snarf );
445 } elsif ( $svc_x eq 'svc_phone' ) {
447 $svc = new FS::svc_phone ( {
448 'svcpart' => $svcpart,
449 map { $_ => $packet->{$_} }
450 qw( countrycode phonenum sip_password pin ),
454 die "unknown signup service $svc_x";
457 my $y = $svc->setdefault; # arguably should be in new method
458 return { 'error' => $y } if $y && !ref($y);
460 #$error = $svc->check;
461 #return { 'error' => $error } if $error;
463 #setup a job dependancy to delay provisioning
464 my $placeholder = new FS::queue ( {
465 'job' => 'FS::ClientAPI::Signup::__placeholder',
466 'status' => 'locked',
468 my $error = $placeholder->insert;
469 return { 'error' => $error } if $error;
472 tie my %hash, 'Tie::RefHash';
473 %hash = ( $cust_pkg => [ $svc ] );
475 $error = $cust_main->insert(
478 'depend_jobnum' => $placeholder->jobnum,
481 my $perror = $placeholder->delete;
482 $error .= " (Additionally, error removing placeholder: $perror)" if $perror;
483 return { 'error' => $error };
486 if ( $conf->exists('signup_server-realtime') ) {
488 #warn "[fs_signup_server] Billing customer...\n" if $Debug;
490 my $bill_error = $cust_main->bill;
491 #warn "[fs_signup_server] error billing new customer: $bill_error"
494 $bill_error = $cust_main->apply_payments_and_credits;
495 #warn "[fs_signup_server] error applying payments and credits for".
496 # " new customer: $bill_error"
499 $bill_error = $cust_main->collect('realtime' => 1);
500 #warn "[fs_signup_server] error collecting from new customer: $bill_error"
503 if ( $cust_main->balance > 0 ) {
505 #this makes sense. credit is "un-doing" the invoice
506 $cust_main->credit( $cust_main->balance, 'signup server decline',
507 'reason_type' => $conf->config('signup_credit_type'),
509 $cust_main->apply_credits;
511 #should check list for errors...
512 #$cust_main->suspend;
513 local $FS::svc_Common::noexport_hack = 1;
514 $cust_main->cancel('quiet'=>1);
516 my $perror = $placeholder->depended_delete;
517 warn "error removing provisioning jobs after decline: $perror" if $perror;
519 $perror = $placeholder->delete;
520 warn "error removing placeholder after decline: $perror" if $perror;
523 return { 'error' => '_decline' };
529 $error = $reg_code->delete;
530 return { 'error' => $error } if $error;
533 $error = $placeholder->delete;
534 return { 'error' => $error } if $error;
536 my %return = ( 'error' => '',
537 'signup_service' => $svc_x,
540 if ( $svc_x eq 'svc_acct' ) {
541 $return{$_} = $svc->$_() for qw( username _password );
542 } elsif ( $svc_x eq 'svc_phone' ) {
543 $return{$_} = $svc->$_() for qw( countrycode phonenum sip_password pin );
545 die "unknown signup service $svc_x";