1 package FS::ClientAPI::Signup;
4 use vars qw( $DEBUG $me );
8 use FS::Record qw(qsearch qsearchs dbdef);
9 use FS::CGI qw(popurl);
10 use FS::Msgcat qw(gettext);
11 use FS::Misc qw(card_types);
12 use FS::ClientAPI_SessionCache;
14 use FS::cust_main_county;
27 $me = '[FS::ClientAPI::Signup]';
32 warn "$me signup_info called on $packet\n" if $DEBUG;
34 my $conf = new FS::Conf;
35 my $svc_x = $conf->config('signup_server-service') || 'svc_acct';
37 my $cache = new FS::ClientAPI_SessionCache( {
38 'namespace' => 'FS::ClientAPI::Signup',
40 my $signup_info_cache = $cache->get('signup_info_cache');
42 if ( $signup_info_cache ) {
44 warn "$me loading cached signup info\n" if $DEBUG > 1;
48 warn "$me populating signup info cache\n" if $DEBUG > 1;
50 my $agentnum2part_pkg =
54 my $href = $agent->pkgpart_hashref;
57 map { { 'payby' => [ $_->payby ],
58 'freq_pretty' => $_->freq_pretty,
59 'options' => { $_->options },
62 grep { $_->svcpart($svc_x)
63 && ( $href->{ $_->pkgpart }
65 && $_->agentnum == $agent->agentnum
69 qsearch( 'part_pkg', { 'disabled' => '' } )
71 } qsearch('agent', { 'disabled' => '' })
74 my $msgcat = { map { $_=>gettext($_) }
75 qw( passwords_dont_match invalid_card unknown_card_type
76 not_a empty_password illegal_or_empty_text )
78 warn "msgcat: ". Dumper($msgcat). "\n" if $DEBUG > 2;
80 my $label = { map { $_ => FS::Msgcat::_gettext($_) }
81 qw( stateid stateid_state )
83 warn "label: ". Dumper($label). "\n" if $DEBUG > 2;
85 my @agent_fields = qw( agentnum agent );
87 $signup_info_cache = {
88 'cust_main_county' => [ map $_->hashref,
89 qsearch('cust_main_county', {} )
92 'agent' => [ map { my $agent = $_;
93 +{ map { $_ => $agent->get($_) } @agent_fields }
95 qsearch('agent', { 'disabled' => '' } )
98 'part_referral' => [ map $_->hashref,
99 qsearch('part_referral', { 'disabled' => '' } )
102 'agentnum2part_pkg' => $agentnum2part_pkg,
104 'svc_acct_pop' => [ map $_->hashref, qsearch('svc_acct_pop',{} ) ],
106 'emailinvoiceonly' => $conf->exists('emailinvoiceonly'),
108 'security_phrase' => $conf->exists('security_phrase'),
110 'nomadix' => $conf->exists('signup_server-nomadix'),
112 'payby' => [ $conf->config('signup_server-payby') ],
114 'payby_longname' => [ map { FS::payby->longname($_) }
115 $conf->config('signup_server-payby') ],
117 'card_types' => card_types(),
119 'paytypes' => [ @FS::cust_main::paytypes ],
123 'stateid_enabled' => $conf->exists('show_stateid'),
125 'paystate_enabled' => $conf->exists('show_bankstate'),
133 'statedefault' => scalar($conf->config('statedefault')) || 'CA',
135 'countrydefault' => scalar($conf->config('countrydefault')) || 'US',
137 'refnum' => scalar($conf->config('signup_server-default_refnum')),
139 'default_pkgpart' => scalar($conf->config('signup_server-default_pkgpart')),
141 'signup_service' => $svc_x,
142 'default_svcpart' => scalar($conf->config('signup_server-default_svcpart')),
144 'head' => join("\n", $conf->config('selfservice-head') ),
145 'body_header' => join("\n", $conf->config('selfservice-body_header') ),
146 'body_footer' => join("\n", $conf->config('selfservice-body_footer') ),
147 'body_bgcolor' => scalar( $conf->config('selfservice-body_bgcolor') ),
148 'box_bgcolor' => scalar( $conf->config('selfservice-box_bgcolor') ),
150 'company_name' => scalar($conf->config('company_name')),
153 'agent_ship_address' => scalar($conf->exists('agent-ship_address')),
155 'no_company' => scalar($conf->exists('signup-no_company')),
156 'require_phone' => scalar($conf->exists('cust_main-require_phone')),
157 'recommend_daytime' => scalar($conf->exists('signup-recommend_daytime')),
158 'recommend_email' => scalar($conf->exists('signup-recommend_email')),
162 $cache->set('signup_info_cache', $signup_info_cache);
166 my $signup_info = { %$signup_info_cache };
167 warn "$me signup info loaded\n" if $DEBUG > 1;
168 warn Dumper($signup_info). "\n" if $DEBUG > 2;
170 my @addl = qw( signup_server-classnum2 signup_server-classnum3 );
172 if ( grep { $conf->exists($_) } @addl ) {
174 $signup_info->{optional_packages} = [];
176 foreach my $addl ( @addl ) {
178 warn "$me adding optional package info\n" if $DEBUG > 1;
180 my $classnum = $conf->config($addl) or next;
183 'freq_pretty' => $_->freq_pretty,
184 'options' => { $_->options },
188 qsearch( 'part_pkg', { classnum => $classnum } );
190 push @{$signup_info->{optional_packages}}, \@pkgs;
192 warn "$me done adding opt. package info for $classnum\n" if $DEBUG > 1;
198 my $agentnum = $packet->{'agentnum'}
199 || $conf->config('signup_server-default_agentnum');
200 $agentnum =~ /^(\d*)$/ or die "illegal agentnum";
204 if ( exists $packet->{'session_id'} ) {
206 warn "$me loading agent session\n" if $DEBUG > 1;
207 my $cache = new FS::ClientAPI_SessionCache( {
208 'namespace' => 'FS::ClientAPI::Agent',
210 $session = $cache->get($packet->{'session_id'});
212 $agentnum = $session->{'agentnum'};
214 return { 'error' => "Can't resume session" }; #better error message
216 warn "$me done loading agent session\n" if $DEBUG > 1;
218 } elsif ( exists $packet->{'customer_session_id'} ) {
220 warn "$me loading customer session\n" if $DEBUG > 1;
221 my $cache = new FS::ClientAPI_SessionCache( {
222 'namespace' => 'FS::ClientAPI::MyAccount',
224 $session = $cache->get($packet->{'customer_session_id'});
226 my $custnum = $session->{'custnum'};
227 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum });
228 return { 'error' => "Can't find your customer record" } unless $cust_main;
229 $agentnum = $cust_main->agentnum;
231 return { 'error' => "Can't resume session" }; #better error message
233 warn "$me done loading customer session\n" if $DEBUG > 1;
237 $signup_info->{'part_pkg'} = [];
239 if ( $packet->{'reg_code'} ) {
241 warn "$me setting package list via reg_code\n" if $DEBUG > 1;
243 $signup_info->{'part_pkg'} =
244 [ map { { 'payby' => [ $_->payby ],
245 'freq_pretty' => $_->freq_pretty,
246 'options' => { $_->options },
250 grep { $_->svcpart($svc_x) }
252 qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
253 'agentnum' => $agentnum, } )
257 $signup_info->{'error'} = 'Unknown registration code'
258 unless @{ $signup_info->{'part_pkg'} };
260 warn "$me done setting package list via reg_code\n" if $DEBUG > 1;
262 } elsif ( $packet->{'promo_code'} ) {
264 warn "$me setting package list via promo_code\n" if $DEBUG > 1;
266 $signup_info->{'part_pkg'} =
267 [ map { { 'payby' => [ $_->payby ],
268 'freq_pretty' => $_->freq_pretty,
269 'options' => { $_->options },
272 grep { $_->svcpart($svc_x) }
273 qsearch( 'part_pkg', { 'promo_code' => {
275 value=>$packet->{'promo_code'}
277 'disabled' => '', } )
280 $signup_info->{'error'} = 'Unknown promotional code'
281 unless @{ $signup_info->{'part_pkg'} };
283 warn "$me done setting package list via promo_code\n" if $DEBUG > 1;
288 warn "$me setting agent-specific payment flag\n" if $DEBUG > 1;
289 my $agent = qsearchs('agent', { 'agentnum' => $agentnum } );
290 warn "$me has agent $agent\n" if $DEBUG > 1;
291 if ( $agent ) { #else complain loudly?
292 $signup_info->{'hide_payment_fields'} = [];
293 foreach my $payby (@{$signup_info->{payby}}) {
294 warn "$me checking $payby payment fields\n" if $DEBUG > 1;
296 if ( FS::payby->realtime($payby) ) {
297 my $payment_gateway =
298 $agent->payment_gateway( 'method' => FS::payby->payby2bop($payby),
301 if ( $payment_gateway
302 && $payment_gateway->gateway_namespace
303 eq 'Business::OnlineThirdPartyPayment'
305 warn "$me hiding $payby payment fields\n" if $DEBUG > 1;
309 push @{$signup_info->{'hide_payment_fields'}}, $hide;
312 warn "$me done setting agent-specific payment flag\n" if $DEBUG > 1;
314 warn "$me setting agent-specific package list\n" if $DEBUG > 1;
315 $signup_info->{'part_pkg'} = $signup_info->{'agentnum2part_pkg'}{$agentnum}
316 unless @{ $signup_info->{'part_pkg'} };
317 warn "$me done setting agent-specific package list\n" if $DEBUG > 1;
319 warn "$me setting agent-specific adv. source list\n" if $DEBUG > 1;
320 $signup_info->{'part_referral'} =
324 'table' => 'part_referral',
325 'hashref' => { 'disabled' => '' },
326 'extra_sql' => "AND ( agentnum = $agentnum ".
327 " OR agentnum IS NULL ) ",
331 warn "$me done setting agent-specific adv. source list\n" if $DEBUG > 1;
333 $signup_info->{'agent_name'} = $agent->agent;
335 $signup_info->{'company_name'} = $conf->config('company_name', $agentnum);
337 if ( $signup_info->{'agent_ship_address'} && $agent->agent_custnum ) {
338 my $cust_main = $agent->agent_cust_main;
339 my $prefix = length($cust_main->ship_last) ? 'ship_' : '';
340 $signup_info->{"ship_$_"} = $cust_main->get("$prefix$_")
341 foreach qw( address1 city county state zip country );
344 #some of the above could probably be cached, too
346 my $signup_info_cache_agent = $cache->get("signup_info_cache_agent$agentnum");
348 if ( $signup_info_cache_agent ) {
350 warn "$me loading cached signup info for agentnum $agentnum\n"
355 warn "$me populating signup info cache for agentnum $agentnum\n"
358 $signup_info_cache_agent = {
359 #( map { $_ => scalar( $conf->config($_, $agentnum) ) }
360 # qw( company_name ) ),
361 ( map { $_ => scalar( $conf->config("selfservice-$_", $agentnum ) ) }
362 qw( body_bgcolor box_bgcolor) ),
363 ( map { $_ => join("\n", $conf->config("selfservice-$_", $agentnum ) ) }
364 qw( head body_header body_footer ) ),
367 $cache->set("signup_info_cache_agent$agentnum", $signup_info_cache_agent);
371 $signup_info->{$_} = $signup_info_cache_agent->{$_}
372 foreach keys %$signup_info_cache_agent;
376 # delete $signup_info->{'part_pkg'};
379 warn "$me sorting package list\n" if $DEBUG > 1;
380 $signup_info->{'part_pkg'} = [ sort { $a->{pkg} cmp $b->{pkg} } # case?
381 @{ $signup_info->{'part_pkg'} }
383 warn "$me done sorting package list\n" if $DEBUG > 1;
385 if ( exists $packet->{'session_id'} ) {
386 my $agent_signup_info = { %$signup_info };
387 delete $agent_signup_info->{agentnum2part_pkg};
388 $agent_signup_info->{'agent'} = $session->{'agent'};
396 sub domain_select_hash {
401 if ($packet->{pkgpart}) {
402 my $part_pkg = qsearchs('part_pkg' => { 'pkgpart' => $packet->{pkgpart} } );
403 #$packet->{svcpart} = $part_pkg->svcpart('svc_acct')
404 $packet->{svcpart} = $part_pkg->svcpart
408 if ($packet->{svcpart}) {
409 my $part_svc = qsearchs('part_svc' => { 'svcpart' => $packet->{svcpart} } );
410 $response->{'domsvc'} = $part_svc->part_svc_column('domsvc')->columnvalue
411 if ($part_svc && $part_svc->part_svc_column('domsvc')->columnflag eq 'D');
414 $response->{'domains'}
415 = { domain_select_hash FS::svc_acct( map { $_ => $packet->{$_} }
425 my $conf = new FS::Conf;
426 my $svc_x = $conf->config('signup_server-service') || 'svc_acct';
428 if ( $svc_x eq 'svc_acct' ) {
430 #things that aren't necessary in base class, but are for signup server
431 #return "Passwords don't match"
432 # if $hashref->{'_password'} ne $hashref->{'_password2'}
433 return { 'error' => gettext('empty_password') }
434 unless length($packet->{'_password'});
435 # a bit inefficient for large numbers of pops
436 return { 'error' => gettext('no_access_number_selected') }
437 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
442 if ( exists $packet->{'session_id'} ) {
443 my $cache = new FS::ClientAPI_SessionCache( {
444 'namespace' => 'FS::ClientAPI::Agent',
446 my $session = $cache->get($packet->{'session_id'});
448 $agentnum = $session->{'agentnum'};
450 return { 'error' => "Can't resume session" }; #better error message
453 $agentnum = $packet->{agentnum}
454 || $conf->config('signup_server-default_agentnum');
457 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
458 # common that are still here and library them.
459 my $cust_main = new FS::cust_main ( {
461 'agentnum' => $agentnum,
462 'refnum' => $packet->{refnum}
463 || $conf->config('signup_server-default_refnum'),
465 map { $_ => $packet->{$_} } qw(
467 last first ss company address1 address2
468 city county state zip country
469 daytime night fax stateid stateid_state
471 ship_last ship_first ship_ss ship_company ship_address1 ship_address2
472 ship_city ship_county ship_state ship_zip ship_country
473 ship_daytime ship_night ship_fax
476 payinfo paycvv paydate payname paystate paytype
477 paystart_month paystart_year payissue
480 referral_custnum comments
485 my $agent = qsearchs('agent', { 'agentnum' => $agentnum } );
486 if ( $conf->exists('agent_ship_address') && $agent->agent_custnum ) {
487 my $agent_cust_main = $agent->agent_cust_main;
488 my $prefix = length($agent_cust_main->ship_last) ? 'ship_' : '';
489 $cust_main->set("ship_$_", $agent_cust_main->get("$prefix$_") )
490 foreach qw( address1 city county state zip country );
492 $cust_main->set("ship_$_", $cust_main->get($_))
493 foreach qw( last first );
498 return { 'error' => "Illegal payment type" }
499 unless grep { $_ eq $packet->{'payby'} }
500 $conf->config('signup_server-payby');
502 if (FS::payby->realtime($packet->{payby})) {
503 my $payby = $packet->{payby};
505 my $agent = qsearchs('agent', { 'agentnum' => $agentnum });
506 return { 'error' => "Unknown reseller" }
509 my $gw = $agent->payment_gateway( 'method' => FS::payby->payby2bop($payby),
513 $cust_main->payby('BILL') # MCRD better?
514 if $gw && $gw->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
517 $cust_main->payinfo($cust_main->daytime)
518 if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
520 my @invoicing_list = $packet->{'invoicing_list'}
521 ? split( /\s*\,\s*/, $packet->{'invoicing_list'} )
524 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
526 return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
529 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
530 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
531 my $svcpart = $part_pkg->svcpart($svc_x);
534 if ( $packet->{'reg_code'} ) {
535 $reg_code = qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
536 'agentnum' => $agentnum, } )
537 or return { 'error' => 'Unknown registration code' };
540 my $cust_pkg = new FS::cust_pkg ( {
541 #later#'custnum' => $custnum,
542 'pkgpart' => $packet->{'pkgpart'},
543 'promo_code' => $packet->{'promo_code'},
544 'reg_code' => $packet->{'reg_code'},
546 #my $error = $cust_pkg->check;
547 #return { 'error' => $error } if $error;
549 #should be all auto-magic and shit
551 if ( $svc_x eq 'svc_acct' ) {
553 my $svc = new FS::svc_acct {
554 'svcpart' => $svcpart,
555 map { $_ => $packet->{$_} }
556 qw( username _password sec_phrase popnum ),
561 while ( exists($packet->{"snarf_machine$snarfnum"})
562 && length($packet->{"snarf_machine$snarfnum"}) ) {
563 my $acct_snarf = new FS::acct_snarf ( {
564 'machine' => $packet->{"snarf_machine$snarfnum"},
565 'protocol' => $packet->{"snarf_protocol$snarfnum"},
566 'username' => $packet->{"snarf_username$snarfnum"},
567 '_password' => $packet->{"snarf_password$snarfnum"},
570 push @acct_snarf, $acct_snarf;
572 $svc->child_objects( \@acct_snarf );
576 } elsif ( $svc_x eq 'svc_phone' ) {
578 my $svc = new FS::svc_phone ( {
579 'svcpart' => $svcpart,
580 map { $_ => $packet->{$_} }
581 qw( countrycode phonenum sip_password pin ),
587 die "unknown signup service $svc_x";
589 my $y = $svc[0]->setdefault; # arguably should be in new method
590 return { 'error' => $y } if $y && !ref($y);
592 if ($packet->{'mac_addr'} && $conf->exists('signup_server-mac_addr_svcparts'))
595 my %mac_addr_svcparts = map { $_ => 1 }
596 $conf->config('signup_server-mac_addr_svcparts');
597 my @pkg_svc = grep { $_->quantity && $mac_addr_svcparts{$_->svcpart} }
598 $cust_pkg->part_pkg->pkg_svc;
600 return { 'error' => 'No service defined to assign mac address' }
603 my $svc = new FS::svc_acct {
604 'svcpart' => $pkg_svc[0]->svcpart, #multiple matches? alas..
605 'username' => $packet->{'mac_addr'},
606 '_password' => '', #blank as requested (set passwordmin to 0)
609 my $y = $svc->setdefault; # arguably should be in new method
610 return { 'error' => $y } if $y && !ref($y);
616 #$error = $svc->check;
617 #return { 'error' => $error } if $error;
619 #setup a job dependancy to delay provisioning
620 my $placeholder = new FS::queue ( {
621 'job' => 'FS::ClientAPI::Signup::__placeholder',
622 'status' => 'locked',
624 my $error = $placeholder->insert;
625 return { 'error' => $error } if $error;
628 tie my %hash, 'Tie::RefHash';
629 %hash = ( $cust_pkg => \@svc );
631 $error = $cust_main->insert(
634 'depend_jobnum' => $placeholder->jobnum,
637 my $perror = $placeholder->delete;
638 $error .= " (Additionally, error removing placeholder: $perror)" if $perror;
639 return { 'error' => $error };
642 if ( $conf->exists('signup_server-realtime') ) {
644 #warn "[fs_signup_server] Billing customer...\n" if $Debug;
646 my $bill_error = $cust_main->bill;
647 #warn "[fs_signup_server] error billing new customer: $bill_error"
650 $bill_error = $cust_main->apply_payments_and_credits;
651 #warn "[fs_signup_server] error applying payments and credits for".
652 # " new customer: $bill_error"
655 $bill_error = $cust_main->realtime_collect(
656 method => FS::payby->payby2bop( $packet->{payby} ),
657 depend_jobnum => $placeholder->jobnum,
659 #warn "[fs_signup_server] error collecting from new customer: $bill_error"
662 if ($bill_error && ref($bill_error) eq 'HASH') {
663 return { 'error' => '_collect',
664 ( map { $_ => $bill_error->{$_} }
665 qw(popup_url reference collectitems)
667 amount => $cust_main->balance,
671 if ( $cust_main->balance > 0 ) {
673 #this makes sense. credit is "un-doing" the invoice
674 $cust_main->credit( $cust_main->balance, 'signup server decline',
675 'reason_type' => $conf->config('signup_credit_type'),
677 $cust_main->apply_credits;
679 #should check list for errors...
680 #$cust_main->suspend;
681 local $FS::svc_Common::noexport_hack = 1;
682 $cust_main->cancel('quiet'=>1);
684 my $perror = $placeholder->depended_delete;
685 warn "error removing provisioning jobs after decline: $perror" if $perror;
687 $perror = $placeholder->delete;
688 warn "error removing placeholder after decline: $perror" if $perror;
691 return { 'error' => '_decline' };
697 $error = $reg_code->delete;
698 return { 'error' => $error } if $error;
701 $error = $placeholder->delete;
702 return { 'error' => $error } if $error;
704 my %return = ( 'error' => '',
705 'signup_service' => $svc_x,
708 if ( $svc_x eq 'svc_acct' ) {
709 $return{$_} = $svc[0]->$_() for qw( username _password );
710 } elsif ( $svc_x eq 'svc_phone' ) {
711 $return{$_} = $svc[0]->$_() for qw( countrycode phonenum sip_password pin );
713 die "unknown signup service $svc_x";
720 sub capture_payment {
723 warn "$me capture_payment called on $packet\n" if $DEBUG;
726 # identify processor/gateway from called back URL
729 my $conf = new FS::Conf;
731 my $url = $packet->{url};
732 my $payment_gateway =
733 qsearchs('payment_gateway', { 'gateway_callback_url' => popurl(0, $url) } );
735 unless ($payment_gateway) {
737 my ( $processor, $login, $password, $action, @bop_options ) =
738 $conf->config('business-onlinepayment');
739 $action ||= 'normal authorization';
740 pop @bop_options if scalar(@bop_options) % 2 && $bop_options[-1] =~ /^\s*$/;
741 die "No real-time processor is enabled - ".
742 "did you set the business-onlinepayment configuration value?\n"
745 $payment_gateway = new FS::payment_gateway( {
746 gateway_namespace => $conf->config('business-onlinepayment-namespace'),
747 gateway_module => $processor,
748 gateway_username => $login,
749 gateway_password => $password,
750 gateway_action => $action,
751 options => [ ( @bop_options ) ],
756 die "No real-time third party processor is enabled - ".
757 "did you set the business-onlinepayment configuration value?\n*"
758 unless $payment_gateway->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
761 # locate pending transaction
764 eval "use Business::OnlineThirdPartyPayment";
768 new Business::OnlineThirdPartyPayment( $payment_gateway->gateway_module,
769 @{ [ $payment_gateway->options ] },
772 my $paypendingnum = $transaction->reference($packet->{data});
774 my $cust_pay_pending =
775 qsearchs('cust_pay_pending', { paypendingnum => $paypendingnum } );
777 unless ($cust_pay_pending) {
778 my $bill_error = "No payment is being processed with id $paypendingnum".
779 "; Transaction aborted.";
780 return { error => '_decline', bill_error => $bill_error };
783 if ($cust_pay_pending->status ne 'pending') {
784 my $bill_error = "Payment with id $paypendingnum is not pending, but ".
785 $cust_pay_pending->status. "; Transaction aborted.";
786 return { error => '_decline', bill_error => $bill_error };
789 my $cust_main = $cust_pay_pending->cust_main;
791 $cust_main->realtime_botpp_capture( $cust_pay_pending, %{$packet->{data}} );
793 return { 'error' => ( $bill_error->{bill_error} ? '_decline' : '' ),