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,
127 $cache->set('signup_info_cache', $signup_info_cache);
131 my $signup_info = { %$signup_info_cache };
132 warn "$me signup info loaded\n" if $DEBUG > 1;
133 warn Dumper($signup_info). "\n" if $DEBUG > 2;
135 my @addl = qw( signup_server-classnum2 signup_server-classnum3 );
137 if ( grep { $conf->exists($_) } @addl ) {
139 $signup_info->{optional_packages} = [];
141 foreach my $addl ( @addl ) {
143 warn "$me adding optional package info\n" if $DEBUG > 1;
145 my $classnum = $conf->config($addl) or next;
148 'freq_pretty' => $_->freq_pretty,
149 'options' => { $_->options },
153 qsearch( 'part_pkg', { classnum => $classnum } );
155 push @{$signup_info->{optional_packages}}, \@pkgs;
157 warn "$me done adding opt. package info for $classnum\n" if $DEBUG > 1;
163 my $agentnum = $packet->{'agentnum'}
164 || $conf->config('signup_server-default_agentnum');
165 $agentnum =~ /^(\d*)$/ or die "illegal agentnum";
169 if ( exists $packet->{'session_id'} ) {
171 warn "$me loading agent session\n" if $DEBUG > 1;
172 my $cache = new FS::ClientAPI_SessionCache( {
173 'namespace' => 'FS::ClientAPI::Agent',
175 $session = $cache->get($packet->{'session_id'});
177 $agentnum = $session->{'agentnum'};
179 return { 'error' => "Can't resume session" }; #better error message
181 warn "$me done loading agent session\n" if $DEBUG > 1;
183 } elsif ( exists $packet->{'customer_session_id'} ) {
185 warn "$me loading customer session\n" if $DEBUG > 1;
186 my $cache = new FS::ClientAPI_SessionCache( {
187 'namespace' => 'FS::ClientAPI::MyAccount',
189 $session = $cache->get($packet->{'customer_session_id'});
191 my $custnum = $session->{'custnum'};
192 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum });
193 return { 'error' => "Can't find your customer record" } unless $cust_main;
194 $agentnum = $cust_main->agentnum;
196 return { 'error' => "Can't resume session" }; #better error message
198 warn "$me done loading customer session\n" if $DEBUG > 1;
202 $signup_info->{'part_pkg'} = [];
204 if ( $packet->{'reg_code'} ) {
206 warn "$me setting package list via reg_code\n" if $DEBUG > 1;
208 $signup_info->{'part_pkg'} =
209 [ map { { 'payby' => [ $_->payby ],
210 'freq_pretty' => $_->freq_pretty,
211 'options' => { $_->options },
215 grep { $_->svcpart($svc_x) }
217 qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
218 'agentnum' => $agentnum, } )
222 $signup_info->{'error'} = 'Unknown registration code'
223 unless @{ $signup_info->{'part_pkg'} };
225 warn "$me done setting package list via reg_code\n" if $DEBUG > 1;
227 } elsif ( $packet->{'promo_code'} ) {
229 warn "$me setting package list via promo_code\n" if $DEBUG > 1;
231 $signup_info->{'part_pkg'} =
232 [ map { { 'payby' => [ $_->payby ],
233 'freq_pretty' => $_->freq_pretty,
234 'options' => { $_->options },
237 grep { $_->svcpart($svc_x) }
238 qsearch( 'part_pkg', { 'promo_code' => {
240 value=>$packet->{'promo_code'}
242 'disabled' => '', } )
245 $signup_info->{'error'} = 'Unknown promotional code'
246 unless @{ $signup_info->{'part_pkg'} };
248 warn "$me done setting package list via promo_code\n" if $DEBUG > 1;
253 warn "$me setting agent-specific package list\n" if $DEBUG > 1;
254 $signup_info->{'part_pkg'} = $signup_info->{'agentnum2part_pkg'}{$agentnum}
255 unless @{ $signup_info->{'part_pkg'} };
256 warn "$me done setting agent-specific package list\n" if $DEBUG > 1;
258 warn "$me setting agent-specific adv. source list\n" if $DEBUG > 1;
259 $signup_info->{'part_referral'} =
263 'table' => 'part_referral',
264 'hashref' => { 'disabled' => '' },
265 'extra_sql' => "AND ( agentnum = $agentnum ".
266 " OR agentnum IS NULL ) ",
270 warn "$me done setting agent-specific adv. source list\n" if $DEBUG > 1;
274 # delete $signup_info->{'part_pkg'};
277 warn "$me sorting package list\n" if $DEBUG > 1;
278 $signup_info->{'part_pkg'} = [ sort { $a->{pkg} cmp $b->{pkg} } # case?
279 @{ $signup_info->{'part_pkg'} }
281 warn "$me done sorting package list\n" if $DEBUG > 1;
283 if ( exists $packet->{'session_id'} ) {
284 my $agent_signup_info = { %$signup_info };
285 delete $agent_signup_info->{agentnum2part_pkg};
286 $agent_signup_info->{'agent'} = $session->{'agent'};
294 sub domain_select_hash {
299 if ($packet->{pkgpart}) {
300 my $part_pkg = qsearchs('part_pkg' => { 'pkgpart' => $packet->{pkgpart} } );
301 #$packet->{svcpart} = $part_pkg->svcpart('svc_acct')
302 $packet->{svcpart} = $part_pkg->svcpart
306 if ($packet->{svcpart}) {
307 my $part_svc = qsearchs('part_svc' => { 'svcpart' => $packet->{svcpart} } );
308 $response->{'domsvc'} = $part_svc->part_svc_column('domsvc')->columnvalue
309 if ($part_svc && $part_svc->part_svc_column('domsvc')->columnflag eq 'D');
312 $response->{'domains'}
313 = { domain_select_hash FS::svc_acct( map { $_ => $packet->{$_} }
323 my $conf = new FS::Conf;
324 my $svc_x = $conf->config('signup_server-service') || 'svc_acct';
326 if ( $svc_x eq 'svc_acct' ) {
328 #things that aren't necessary in base class, but are for signup server
329 #return "Passwords don't match"
330 # if $hashref->{'_password'} ne $hashref->{'_password2'}
331 return { 'error' => gettext('empty_password') }
332 unless length($packet->{'_password'});
333 # a bit inefficient for large numbers of pops
334 return { 'error' => gettext('no_access_number_selected') }
335 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
340 if ( exists $packet->{'session_id'} ) {
341 my $cache = new FS::ClientAPI_SessionCache( {
342 'namespace' => 'FS::ClientAPI::Agent',
344 my $session = $cache->get($packet->{'session_id'});
346 $agentnum = $session->{'agentnum'};
348 return { 'error' => "Can't resume session" }; #better error message
351 $agentnum = $packet->{agentnum}
352 || $conf->config('signup_server-default_agentnum');
355 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
356 # common that are still here and library them.
357 my $cust_main = new FS::cust_main ( {
359 'agentnum' => $agentnum,
360 'refnum' => $packet->{refnum}
361 || $conf->config('signup_server-default_refnum'),
363 map { $_ => $packet->{$_} } qw(
365 last first ss company address1 address2
366 city county state zip country
367 daytime night fax stateid stateid_state
369 ship_last ship_first ship_ss ship_company ship_address1 ship_address2
370 ship_city ship_county ship_state ship_zip ship_country
371 ship_daytime ship_night ship_fax
374 payinfo paycvv paydate payname paystate paytype
375 paystart_month paystart_year payissue
378 referral_custnum comments
383 return { 'error' => "Illegal payment type" }
384 unless grep { $_ eq $packet->{'payby'} }
385 $conf->config('signup_server-payby');
387 $cust_main->payinfo($cust_main->daytime)
388 if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
390 my @invoicing_list = $packet->{'invoicing_list'}
391 ? split( /\s*\,\s*/, $packet->{'invoicing_list'} )
394 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
396 return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
399 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
400 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
401 my $svcpart = $part_pkg->svcpart($svc_x);
404 if ( $packet->{'reg_code'} ) {
405 $reg_code = qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
406 'agentnum' => $agentnum, } )
407 or return { 'error' => 'Unknown registration code' };
410 my $cust_pkg = new FS::cust_pkg ( {
411 #later#'custnum' => $custnum,
412 'pkgpart' => $packet->{'pkgpart'},
413 'promo_code' => $packet->{'promo_code'},
414 'reg_code' => $packet->{'reg_code'},
416 #my $error = $cust_pkg->check;
417 #return { 'error' => $error } if $error;
419 #should be all auto-magic and shit
421 if ( $svc_x eq 'svc_acct' ) {
423 my $svc = new FS::svc_acct ( {
424 'svcpart' => $svcpart,
425 map { $_ => $packet->{$_} }
426 qw( username _password sec_phrase popnum ),
431 while ( exists($packet->{"snarf_machine$snarfnum"})
432 && length($packet->{"snarf_machine$snarfnum"}) ) {
433 my $acct_snarf = new FS::acct_snarf ( {
434 'machine' => $packet->{"snarf_machine$snarfnum"},
435 'protocol' => $packet->{"snarf_protocol$snarfnum"},
436 'username' => $packet->{"snarf_username$snarfnum"},
437 '_password' => $packet->{"snarf_password$snarfnum"},
440 push @acct_snarf, $acct_snarf;
442 $svc->child_objects( \@acct_snarf );
444 } elsif ( $svc_x eq 'svc_phone' ) {
446 my $svc = new FS::svc_phone ( {
447 'svcpart' => $svcpart,
448 map { $_ => $packet->{$_} }
449 qw( countrycode phonenum pin ),
453 die "unknown signup service $svc_x";
456 my $y = $svc->setdefault; # arguably should be in new method
457 return { 'error' => $y } if $y && !ref($y);
459 #$error = $svc->check;
460 #return { 'error' => $error } if $error;
462 #setup a job dependancy to delay provisioning
463 my $placeholder = new FS::queue ( {
464 'job' => 'FS::ClientAPI::Signup::__placeholder',
465 'status' => 'locked',
467 my $error = $placeholder->insert;
468 return { 'error' => $error } if $error;
471 tie my %hash, 'Tie::RefHash';
472 %hash = ( $cust_pkg => [ $svc ] );
474 $error = $cust_main->insert(
477 'depend_jobnum' => $placeholder->jobnum,
480 my $perror = $placeholder->delete;
481 $error .= " (Additionally, error removing placeholder: $perror)" if $perror;
482 return { 'error' => $error };
485 if ( $conf->exists('signup_server-realtime') ) {
487 #warn "[fs_signup_server] Billing customer...\n" if $Debug;
489 my $bill_error = $cust_main->bill;
490 #warn "[fs_signup_server] error billing new customer: $bill_error"
493 $bill_error = $cust_main->apply_payments_and_credits;
494 #warn "[fs_signup_server] error applying payments and credits for".
495 # " new customer: $bill_error"
498 $bill_error = $cust_main->collect('realtime' => 1);
499 #warn "[fs_signup_server] error collecting from new customer: $bill_error"
502 if ( $cust_main->balance > 0 ) {
504 #this makes sense. credit is "un-doing" the invoice
505 $cust_main->credit( $cust_main->balance, 'signup server decline',
506 'reason_type' => $conf->config('signup_credit_type'),
508 $cust_main->apply_credits;
510 #should check list for errors...
511 #$cust_main->suspend;
512 local $FS::svc_Common::noexport_hack = 1;
513 $cust_main->cancel('quiet'=>1);
515 my $perror = $placeholder->depended_delete;
516 warn "error removing provisioning jobs after decline: $perror" if $perror;
518 $perror = $placeholder->delete;
519 warn "error removing placeholder after decline: $perror" if $perror;
522 return { 'error' => '_decline' };
528 $error = $reg_code->delete;
529 return { 'error' => $error } if $error;
532 $error = $placeholder->delete;
533 return { 'error' => $error } if $error;
535 return { error => '' };