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 =
52 my $href = $agent->pkgpart_hashref;
55 map { { 'payby' => [ $_->payby ],
56 'freq_pretty' => $_->freq_pretty,
57 'options' => { $_->options },
60 grep { $_->svcpart($svc_x)
61 && ( $href->{ $_->pkgpart }
62 || $_->agentnum == $agent->agentnum
65 qsearch( 'part_pkg', { 'disabled' => '' } )
67 } qsearch('agent', { 'disabled' => '' })
70 my $msgcat = { map { $_=>gettext($_) }
71 qw( passwords_dont_match invalid_card unknown_card_type
72 not_a empty_password illegal_or_empty_text )
74 warn "msgcat: ". Dumper($msgcat). "\n" if $DEBUG > 2;
76 my $label = { map { $_ => FS::Msgcat::_gettext($_) }
77 qw( stateid stateid_state )
79 warn "label: ". Dumper($label). "\n" if $DEBUG > 2;
81 my @agent_fields = qw( agentnum agent );
83 $signup_info_cache = {
84 'cust_main_county' => [ map $_->hashref,
85 qsearch('cust_main_county', {} )
88 'agent' => [ map { my $agent = $_;
89 map { $_ => $agent->get($_) } @agent_fields;
91 qsearch('agent', { 'disabled' => '' } )
94 'part_referral' => [ map $_->hashref,
95 qsearch('part_referral', { 'disabled' => '' } )
98 'agentnum2part_pkg' => $agentnum2part_pkg,
100 'svc_acct_pop' => [ map $_->hashref, qsearch('svc_acct_pop',{} ) ],
102 'emailinvoiceonly' => $conf->exists('emailinvoiceonly'),
104 'security_phrase' => $conf->exists('security_phrase'),
106 'payby' => [ $conf->config('signup_server-payby') ],
108 'card_types' => card_types(),
110 'paytypes' => [ @FS::cust_main::paytypes ],
114 'stateid_enabled' => $conf->exists('show_stateid'),
116 'paystate_enabled' => $conf->exists('show_bankstate'),
124 'statedefault' => scalar($conf->config('statedefault')) || 'CA',
126 'countrydefault' => scalar($conf->config('countrydefault')) || 'US',
128 'refnum' => scalar($conf->config('signup_server-default_refnum')),
130 'default_pkgpart' => scalar($conf->config('signup_server-default_pkgpart')),
132 'signup_service' => $svc_x,
133 'default_svcpart' => scalar($conf->config('signup_server-default_svcpart')),
135 'head' => join("\n", $conf->config('selfservice-head') ),
136 'body_header' => join("\n", $conf->config('selfservice-body_header') ),
137 'body_footer' => join("\n", $conf->config('selfservice-body_footer') ),
138 'body_bgcolor' => scalar( $conf->config('selfservice-body_bgcolor') ),
139 'box_bgcolor' => scalar( $conf->config('selfservice-box_bgcolor') ),
141 'company_name' => scalar($conf->config('company_name')),
144 'agent_ship_address' => scalar($conf->exists('agent-ship_address')),
146 'no_company' => scalar($conf->exists('signup-no_company')),
147 'require_phone' => scalar($conf->exists('cust_main-require_phone')),
148 'recommend_daytime' => scalar($conf->exists('signup-recommend_daytime')),
149 'recommend_email' => scalar($conf->exists('signup-recommend_email')),
153 $cache->set('signup_info_cache', $signup_info_cache);
157 my $signup_info = { %$signup_info_cache };
158 warn "$me signup info loaded\n" if $DEBUG > 1;
159 warn Dumper($signup_info). "\n" if $DEBUG > 2;
161 my @addl = qw( signup_server-classnum2 signup_server-classnum3 );
163 if ( grep { $conf->exists($_) } @addl ) {
165 $signup_info->{optional_packages} = [];
167 foreach my $addl ( @addl ) {
169 warn "$me adding optional package info\n" if $DEBUG > 1;
171 my $classnum = $conf->config($addl) or next;
174 'freq_pretty' => $_->freq_pretty,
175 'options' => { $_->options },
179 qsearch( 'part_pkg', { classnum => $classnum } );
181 push @{$signup_info->{optional_packages}}, \@pkgs;
183 warn "$me done adding opt. package info for $classnum\n" if $DEBUG > 1;
189 my $agentnum = $packet->{'agentnum'}
190 || $conf->config('signup_server-default_agentnum');
191 $agentnum =~ /^(\d*)$/ or die "illegal agentnum";
195 if ( exists $packet->{'session_id'} ) {
197 warn "$me loading agent session\n" if $DEBUG > 1;
198 my $cache = new FS::ClientAPI_SessionCache( {
199 'namespace' => 'FS::ClientAPI::Agent',
201 $session = $cache->get($packet->{'session_id'});
203 $agentnum = $session->{'agentnum'};
205 return { 'error' => "Can't resume session" }; #better error message
207 warn "$me done loading agent session\n" if $DEBUG > 1;
209 } elsif ( exists $packet->{'customer_session_id'} ) {
211 warn "$me loading customer session\n" if $DEBUG > 1;
212 my $cache = new FS::ClientAPI_SessionCache( {
213 'namespace' => 'FS::ClientAPI::MyAccount',
215 $session = $cache->get($packet->{'customer_session_id'});
217 my $custnum = $session->{'custnum'};
218 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum });
219 return { 'error' => "Can't find your customer record" } unless $cust_main;
220 $agentnum = $cust_main->agentnum;
222 return { 'error' => "Can't resume session" }; #better error message
224 warn "$me done loading customer session\n" if $DEBUG > 1;
228 $signup_info->{'part_pkg'} = [];
230 if ( $packet->{'reg_code'} ) {
232 warn "$me setting package list via reg_code\n" if $DEBUG > 1;
234 $signup_info->{'part_pkg'} =
235 [ map { { 'payby' => [ $_->payby ],
236 'freq_pretty' => $_->freq_pretty,
237 'options' => { $_->options },
241 grep { $_->svcpart($svc_x) }
243 qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
244 'agentnum' => $agentnum, } )
248 $signup_info->{'error'} = 'Unknown registration code'
249 unless @{ $signup_info->{'part_pkg'} };
251 warn "$me done setting package list via reg_code\n" if $DEBUG > 1;
253 } elsif ( $packet->{'promo_code'} ) {
255 warn "$me setting package list via promo_code\n" if $DEBUG > 1;
257 $signup_info->{'part_pkg'} =
258 [ map { { 'payby' => [ $_->payby ],
259 'freq_pretty' => $_->freq_pretty,
260 'options' => { $_->options },
263 grep { $_->svcpart($svc_x) }
264 qsearch( 'part_pkg', { 'promo_code' => {
266 value=>$packet->{'promo_code'}
268 'disabled' => '', } )
271 $signup_info->{'error'} = 'Unknown promotional code'
272 unless @{ $signup_info->{'part_pkg'} };
274 warn "$me done setting package list via promo_code\n" if $DEBUG > 1;
279 warn "$me setting agent-specific package list\n" if $DEBUG > 1;
280 $signup_info->{'part_pkg'} = $signup_info->{'agentnum2part_pkg'}{$agentnum}
281 unless @{ $signup_info->{'part_pkg'} };
282 warn "$me done setting agent-specific package list\n" if $DEBUG > 1;
284 warn "$me setting agent-specific adv. source list\n" if $DEBUG > 1;
285 $signup_info->{'part_referral'} =
289 'table' => 'part_referral',
290 'hashref' => { 'disabled' => '' },
291 'extra_sql' => "AND ( agentnum = $agentnum ".
292 " OR agentnum IS NULL ) ",
296 warn "$me done setting agent-specific adv. source list\n" if $DEBUG > 1;
298 my $agent = qsearchs('agent', { 'agentnum' => $agentnum } );
300 $signup_info->{'agent_name'} = $agent->agent;
302 $signup_info->{'company_name'} = $conf->config('company_name', $agentnum);
304 if ( $signup_info->{'agent_ship_address'} && $agent->agent_custnum ) {
305 my $cust_main = $agent->agent_cust_main;
306 my $prefix = length($cust_main->ship_last) ? 'ship_' : '';
307 $signup_info->{"ship_$_"} = $cust_main->get("$prefix$_")
308 foreach qw( address1 city county state zip country );
313 # delete $signup_info->{'part_pkg'};
316 warn "$me sorting package list\n" if $DEBUG > 1;
317 $signup_info->{'part_pkg'} = [ sort { $a->{pkg} cmp $b->{pkg} } # case?
318 @{ $signup_info->{'part_pkg'} }
320 warn "$me done sorting package list\n" if $DEBUG > 1;
322 if ( exists $packet->{'session_id'} ) {
323 my $agent_signup_info = { %$signup_info };
324 delete $agent_signup_info->{agentnum2part_pkg};
325 $agent_signup_info->{'agent'} = $session->{'agent'};
333 sub domain_select_hash {
338 if ($packet->{pkgpart}) {
339 my $part_pkg = qsearchs('part_pkg' => { 'pkgpart' => $packet->{pkgpart} } );
340 #$packet->{svcpart} = $part_pkg->svcpart('svc_acct')
341 $packet->{svcpart} = $part_pkg->svcpart
345 if ($packet->{svcpart}) {
346 my $part_svc = qsearchs('part_svc' => { 'svcpart' => $packet->{svcpart} } );
347 $response->{'domsvc'} = $part_svc->part_svc_column('domsvc')->columnvalue
348 if ($part_svc && $part_svc->part_svc_column('domsvc')->columnflag eq 'D');
351 $response->{'domains'}
352 = { domain_select_hash FS::svc_acct( map { $_ => $packet->{$_} }
362 my $conf = new FS::Conf;
363 my $svc_x = $conf->config('signup_server-service') || 'svc_acct';
365 if ( $svc_x eq 'svc_acct' ) {
367 #things that aren't necessary in base class, but are for signup server
368 #return "Passwords don't match"
369 # if $hashref->{'_password'} ne $hashref->{'_password2'}
370 return { 'error' => gettext('empty_password') }
371 unless length($packet->{'_password'});
372 # a bit inefficient for large numbers of pops
373 return { 'error' => gettext('no_access_number_selected') }
374 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
379 if ( exists $packet->{'session_id'} ) {
380 my $cache = new FS::ClientAPI_SessionCache( {
381 'namespace' => 'FS::ClientAPI::Agent',
383 my $session = $cache->get($packet->{'session_id'});
385 $agentnum = $session->{'agentnum'};
387 return { 'error' => "Can't resume session" }; #better error message
390 $agentnum = $packet->{agentnum}
391 || $conf->config('signup_server-default_agentnum');
394 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
395 # common that are still here and library them.
396 my $cust_main = new FS::cust_main ( {
398 'agentnum' => $agentnum,
399 'refnum' => $packet->{refnum}
400 || $conf->config('signup_server-default_refnum'),
402 map { $_ => $packet->{$_} } qw(
404 last first ss company address1 address2
405 city county state zip country
406 daytime night fax stateid stateid_state
408 ship_last ship_first ship_ss ship_company ship_address1 ship_address2
409 ship_city ship_county ship_state ship_zip ship_country
410 ship_daytime ship_night ship_fax
413 payinfo paycvv paydate payname paystate paytype
414 paystart_month paystart_year payissue
417 referral_custnum comments
422 my $agent = qsearchs('agent', { 'agentnum' => $agentnum } );
423 if ( $conf->exists('agent_ship_address') && $agent->agent_custnum ) {
424 my $agent_cust_main = $agent->agent_cust_main;
425 my $prefix = length($agent_cust_main->ship_last) ? 'ship_' : '';
426 $cust_main->set("ship_$_", $agent_cust_main->get("$prefix$_") )
427 foreach qw( address1 city county state zip country );
429 $cust_main->set("ship_$_", $cust_main->get($_))
430 foreach qw( last first );
435 return { 'error' => "Illegal payment type" }
436 unless grep { $_ eq $packet->{'payby'} }
437 $conf->config('signup_server-payby');
439 $cust_main->payinfo($cust_main->daytime)
440 if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
442 my @invoicing_list = $packet->{'invoicing_list'}
443 ? split( /\s*\,\s*/, $packet->{'invoicing_list'} )
446 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
448 return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
451 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
452 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
453 my $svcpart = $part_pkg->svcpart($svc_x);
456 if ( $packet->{'reg_code'} ) {
457 $reg_code = qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
458 'agentnum' => $agentnum, } )
459 or return { 'error' => 'Unknown registration code' };
462 my $cust_pkg = new FS::cust_pkg ( {
463 #later#'custnum' => $custnum,
464 'pkgpart' => $packet->{'pkgpart'},
465 'promo_code' => $packet->{'promo_code'},
466 'reg_code' => $packet->{'reg_code'},
468 #my $error = $cust_pkg->check;
469 #return { 'error' => $error } if $error;
471 #should be all auto-magic and shit
473 if ( $svc_x eq 'svc_acct' ) {
475 $svc = new FS::svc_acct ( {
476 'svcpart' => $svcpart,
477 map { $_ => $packet->{$_} }
478 qw( username _password sec_phrase popnum ),
483 while ( exists($packet->{"snarf_machine$snarfnum"})
484 && length($packet->{"snarf_machine$snarfnum"}) ) {
485 my $acct_snarf = new FS::acct_snarf ( {
486 'machine' => $packet->{"snarf_machine$snarfnum"},
487 'protocol' => $packet->{"snarf_protocol$snarfnum"},
488 'username' => $packet->{"snarf_username$snarfnum"},
489 '_password' => $packet->{"snarf_password$snarfnum"},
492 push @acct_snarf, $acct_snarf;
494 $svc->child_objects( \@acct_snarf );
496 } elsif ( $svc_x eq 'svc_phone' ) {
498 $svc = new FS::svc_phone ( {
499 'svcpart' => $svcpart,
500 map { $_ => $packet->{$_} }
501 qw( countrycode phonenum sip_password pin ),
505 die "unknown signup service $svc_x";
508 my $y = $svc->setdefault; # arguably should be in new method
509 return { 'error' => $y } if $y && !ref($y);
511 #$error = $svc->check;
512 #return { 'error' => $error } if $error;
514 #setup a job dependancy to delay provisioning
515 my $placeholder = new FS::queue ( {
516 'job' => 'FS::ClientAPI::Signup::__placeholder',
517 'status' => 'locked',
519 my $error = $placeholder->insert;
520 return { 'error' => $error } if $error;
523 tie my %hash, 'Tie::RefHash';
524 %hash = ( $cust_pkg => [ $svc ] );
526 $error = $cust_main->insert(
529 'depend_jobnum' => $placeholder->jobnum,
532 my $perror = $placeholder->delete;
533 $error .= " (Additionally, error removing placeholder: $perror)" if $perror;
534 return { 'error' => $error };
537 if ( $conf->exists('signup_server-realtime') ) {
539 #warn "[fs_signup_server] Billing customer...\n" if $Debug;
541 my $bill_error = $cust_main->bill;
542 #warn "[fs_signup_server] error billing new customer: $bill_error"
545 $bill_error = $cust_main->apply_payments_and_credits;
546 #warn "[fs_signup_server] error applying payments and credits for".
547 # " new customer: $bill_error"
550 $bill_error = $cust_main->collect('realtime' => 1);
551 #warn "[fs_signup_server] error collecting from new customer: $bill_error"
554 if ( $cust_main->balance > 0 ) {
556 #this makes sense. credit is "un-doing" the invoice
557 $cust_main->credit( $cust_main->balance, 'signup server decline',
558 'reason_type' => $conf->config('signup_credit_type'),
560 $cust_main->apply_credits;
562 #should check list for errors...
563 #$cust_main->suspend;
564 local $FS::svc_Common::noexport_hack = 1;
565 $cust_main->cancel('quiet'=>1);
567 my $perror = $placeholder->depended_delete;
568 warn "error removing provisioning jobs after decline: $perror" if $perror;
570 $perror = $placeholder->delete;
571 warn "error removing placeholder after decline: $perror" if $perror;
574 return { 'error' => '_decline' };
580 $error = $reg_code->delete;
581 return { 'error' => $error } if $error;
584 $error = $placeholder->delete;
585 return { 'error' => $error } if $error;
587 my %return = ( 'error' => '',
588 'signup_service' => $svc_x,
591 if ( $svc_x eq 'svc_acct' ) {
592 $return{$_} = $svc->$_() for qw( username _password );
593 } elsif ( $svc_x eq 'svc_phone' ) {
594 $return{$_} = $svc->$_() for qw( countrycode phonenum sip_password pin );
596 die "unknown signup service $svc_x";