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 'card_types' => card_types(),
116 'paytypes' => [ @FS::cust_main::paytypes ],
120 'stateid_enabled' => $conf->exists('show_stateid'),
122 'paystate_enabled' => $conf->exists('show_bankstate'),
130 'statedefault' => scalar($conf->config('statedefault')) || 'CA',
132 'countrydefault' => scalar($conf->config('countrydefault')) || 'US',
134 'refnum' => scalar($conf->config('signup_server-default_refnum')),
136 'default_pkgpart' => scalar($conf->config('signup_server-default_pkgpart')),
138 'signup_service' => $svc_x,
139 'default_svcpart' => scalar($conf->config('signup_server-default_svcpart')),
141 'head' => join("\n", $conf->config('selfservice-head') ),
142 'body_header' => join("\n", $conf->config('selfservice-body_header') ),
143 'body_footer' => join("\n", $conf->config('selfservice-body_footer') ),
144 'body_bgcolor' => scalar( $conf->config('selfservice-body_bgcolor') ),
145 'box_bgcolor' => scalar( $conf->config('selfservice-box_bgcolor') ),
147 'company_name' => scalar($conf->config('company_name')),
150 'agent_ship_address' => scalar($conf->exists('agent-ship_address')),
152 'no_company' => scalar($conf->exists('signup-no_company')),
153 'require_phone' => scalar($conf->exists('cust_main-require_phone')),
154 'recommend_daytime' => scalar($conf->exists('signup-recommend_daytime')),
155 'recommend_email' => scalar($conf->exists('signup-recommend_email')),
159 $cache->set('signup_info_cache', $signup_info_cache);
163 my $signup_info = { %$signup_info_cache };
164 warn "$me signup info loaded\n" if $DEBUG > 1;
165 warn Dumper($signup_info). "\n" if $DEBUG > 2;
167 my @addl = qw( signup_server-classnum2 signup_server-classnum3 );
169 if ( grep { $conf->exists($_) } @addl ) {
171 $signup_info->{optional_packages} = [];
173 foreach my $addl ( @addl ) {
175 warn "$me adding optional package info\n" if $DEBUG > 1;
177 my $classnum = $conf->config($addl) or next;
180 'freq_pretty' => $_->freq_pretty,
181 'options' => { $_->options },
185 qsearch( 'part_pkg', { classnum => $classnum } );
187 push @{$signup_info->{optional_packages}}, \@pkgs;
189 warn "$me done adding opt. package info for $classnum\n" if $DEBUG > 1;
195 my $agentnum = $packet->{'agentnum'}
196 || $conf->config('signup_server-default_agentnum');
197 $agentnum =~ /^(\d*)$/ or die "illegal agentnum";
201 if ( exists $packet->{'session_id'} ) {
203 warn "$me loading agent session\n" if $DEBUG > 1;
204 my $cache = new FS::ClientAPI_SessionCache( {
205 'namespace' => 'FS::ClientAPI::Agent',
207 $session = $cache->get($packet->{'session_id'});
209 $agentnum = $session->{'agentnum'};
211 return { 'error' => "Can't resume session" }; #better error message
213 warn "$me done loading agent session\n" if $DEBUG > 1;
215 } elsif ( exists $packet->{'customer_session_id'} ) {
217 warn "$me loading customer session\n" if $DEBUG > 1;
218 my $cache = new FS::ClientAPI_SessionCache( {
219 'namespace' => 'FS::ClientAPI::MyAccount',
221 $session = $cache->get($packet->{'customer_session_id'});
223 my $custnum = $session->{'custnum'};
224 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum });
225 return { 'error' => "Can't find your customer record" } unless $cust_main;
226 $agentnum = $cust_main->agentnum;
228 return { 'error' => "Can't resume session" }; #better error message
230 warn "$me done loading customer session\n" if $DEBUG > 1;
234 $signup_info->{'part_pkg'} = [];
236 if ( $packet->{'reg_code'} ) {
238 warn "$me setting package list via reg_code\n" if $DEBUG > 1;
240 $signup_info->{'part_pkg'} =
241 [ map { { 'payby' => [ $_->payby ],
242 'freq_pretty' => $_->freq_pretty,
243 'options' => { $_->options },
247 grep { $_->svcpart($svc_x) }
249 qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
250 'agentnum' => $agentnum, } )
254 $signup_info->{'error'} = 'Unknown registration code'
255 unless @{ $signup_info->{'part_pkg'} };
257 warn "$me done setting package list via reg_code\n" if $DEBUG > 1;
259 } elsif ( $packet->{'promo_code'} ) {
261 warn "$me setting package list via promo_code\n" if $DEBUG > 1;
263 $signup_info->{'part_pkg'} =
264 [ map { { 'payby' => [ $_->payby ],
265 'freq_pretty' => $_->freq_pretty,
266 'options' => { $_->options },
269 grep { $_->svcpart($svc_x) }
270 qsearch( 'part_pkg', { 'promo_code' => {
272 value=>$packet->{'promo_code'}
274 'disabled' => '', } )
277 $signup_info->{'error'} = 'Unknown promotional code'
278 unless @{ $signup_info->{'part_pkg'} };
280 warn "$me done setting package list via promo_code\n" if $DEBUG > 1;
285 warn "$me setting agent-specific payment flag\n" if $DEBUG > 1;
286 my $agent = qsearchs('agent', { 'agentnum' => $agentnum } );
287 warn "$me has agent $agent\n" if $DEBUG > 1;
288 if ( $agent ) { #else complain loudly?
289 $signup_info->{'hide_payment_fields'} = [];
290 foreach my $payby (@{$signup_info->{payby}}) {
291 warn "$me checking $payby payment fields\n" if $DEBUG > 1;
293 if ( FS::payby->realtime($payby) ) {
294 my $payment_gateway =
295 $agent->payment_gateway( 'method' => FS::payby->payby2bop($payby),
298 if ( $payment_gateway
299 && $payment_gateway->gateway_namespace
300 eq 'Business::OnlineThirdPartyPayment'
302 warn "$me hiding $payby payment fields\n" if $DEBUG > 1;
306 push @{$signup_info->{'hide_payment_fields'}}, $hide;
309 warn "$me done setting agent-specific payment flag\n" if $DEBUG > 1;
311 warn "$me setting agent-specific package list\n" if $DEBUG > 1;
312 $signup_info->{'part_pkg'} = $signup_info->{'agentnum2part_pkg'}{$agentnum}
313 unless @{ $signup_info->{'part_pkg'} };
314 warn "$me done setting agent-specific package list\n" if $DEBUG > 1;
316 warn "$me setting agent-specific adv. source list\n" if $DEBUG > 1;
317 $signup_info->{'part_referral'} =
321 'table' => 'part_referral',
322 'hashref' => { 'disabled' => '' },
323 'extra_sql' => "AND ( agentnum = $agentnum ".
324 " OR agentnum IS NULL ) ",
328 warn "$me done setting agent-specific adv. source list\n" if $DEBUG > 1;
330 $signup_info->{'agent_name'} = $agent->agent;
332 $signup_info->{'company_name'} = $conf->config('company_name', $agentnum);
334 if ( $signup_info->{'agent_ship_address'} && $agent->agent_custnum ) {
335 my $cust_main = $agent->agent_cust_main;
336 my $prefix = length($cust_main->ship_last) ? 'ship_' : '';
337 $signup_info->{"ship_$_"} = $cust_main->get("$prefix$_")
338 foreach qw( address1 city county state zip country );
343 # delete $signup_info->{'part_pkg'};
346 warn "$me sorting package list\n" if $DEBUG > 1;
347 $signup_info->{'part_pkg'} = [ sort { $a->{pkg} cmp $b->{pkg} } # case?
348 @{ $signup_info->{'part_pkg'} }
350 warn "$me done sorting package list\n" if $DEBUG > 1;
352 if ( exists $packet->{'session_id'} ) {
353 my $agent_signup_info = { %$signup_info };
354 delete $agent_signup_info->{agentnum2part_pkg};
355 $agent_signup_info->{'agent'} = $session->{'agent'};
363 sub domain_select_hash {
368 if ($packet->{pkgpart}) {
369 my $part_pkg = qsearchs('part_pkg' => { 'pkgpart' => $packet->{pkgpart} } );
370 #$packet->{svcpart} = $part_pkg->svcpart('svc_acct')
371 $packet->{svcpart} = $part_pkg->svcpart
375 if ($packet->{svcpart}) {
376 my $part_svc = qsearchs('part_svc' => { 'svcpart' => $packet->{svcpart} } );
377 $response->{'domsvc'} = $part_svc->part_svc_column('domsvc')->columnvalue
378 if ($part_svc && $part_svc->part_svc_column('domsvc')->columnflag eq 'D');
381 $response->{'domains'}
382 = { domain_select_hash FS::svc_acct( map { $_ => $packet->{$_} }
392 my $conf = new FS::Conf;
393 my $svc_x = $conf->config('signup_server-service') || 'svc_acct';
395 if ( $svc_x eq 'svc_acct' ) {
397 #things that aren't necessary in base class, but are for signup server
398 #return "Passwords don't match"
399 # if $hashref->{'_password'} ne $hashref->{'_password2'}
400 return { 'error' => gettext('empty_password') }
401 unless length($packet->{'_password'});
402 # a bit inefficient for large numbers of pops
403 return { 'error' => gettext('no_access_number_selected') }
404 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
409 if ( exists $packet->{'session_id'} ) {
410 my $cache = new FS::ClientAPI_SessionCache( {
411 'namespace' => 'FS::ClientAPI::Agent',
413 my $session = $cache->get($packet->{'session_id'});
415 $agentnum = $session->{'agentnum'};
417 return { 'error' => "Can't resume session" }; #better error message
420 $agentnum = $packet->{agentnum}
421 || $conf->config('signup_server-default_agentnum');
424 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
425 # common that are still here and library them.
426 my $cust_main = new FS::cust_main ( {
428 'agentnum' => $agentnum,
429 'refnum' => $packet->{refnum}
430 || $conf->config('signup_server-default_refnum'),
432 map { $_ => $packet->{$_} } qw(
434 last first ss company address1 address2
435 city county state zip country
436 daytime night fax stateid stateid_state
438 ship_last ship_first ship_ss ship_company ship_address1 ship_address2
439 ship_city ship_county ship_state ship_zip ship_country
440 ship_daytime ship_night ship_fax
443 payinfo paycvv paydate payname paystate paytype
444 paystart_month paystart_year payissue
447 referral_custnum comments
452 my $agent = qsearchs('agent', { 'agentnum' => $agentnum } );
453 if ( $conf->exists('agent_ship_address') && $agent->agent_custnum ) {
454 my $agent_cust_main = $agent->agent_cust_main;
455 my $prefix = length($agent_cust_main->ship_last) ? 'ship_' : '';
456 $cust_main->set("ship_$_", $agent_cust_main->get("$prefix$_") )
457 foreach qw( address1 city county state zip country );
459 $cust_main->set("ship_$_", $cust_main->get($_))
460 foreach qw( last first );
465 return { 'error' => "Illegal payment type" }
466 unless grep { $_ eq $packet->{'payby'} }
467 $conf->config('signup_server-payby');
469 if (FS::payby->realtime($packet->{payby})) {
470 my $payby = $packet->{payby};
472 my $agent = qsearchs('agent', { 'agentnum' => $agentnum });
473 return { 'error' => "Unknown reseller" }
476 my $gw = $agent->payment_gateway( 'method' => FS::payby->payby2bop($payby),
480 $cust_main->payby('BILL') # MCRD better?
481 if $gw && $gw->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
484 $cust_main->payinfo($cust_main->daytime)
485 if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
487 my @invoicing_list = $packet->{'invoicing_list'}
488 ? split( /\s*\,\s*/, $packet->{'invoicing_list'} )
491 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
493 return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
496 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
497 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
498 my $svcpart = $part_pkg->svcpart($svc_x);
501 if ( $packet->{'reg_code'} ) {
502 $reg_code = qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
503 'agentnum' => $agentnum, } )
504 or return { 'error' => 'Unknown registration code' };
507 my $cust_pkg = new FS::cust_pkg ( {
508 #later#'custnum' => $custnum,
509 'pkgpart' => $packet->{'pkgpart'},
510 'promo_code' => $packet->{'promo_code'},
511 'reg_code' => $packet->{'reg_code'},
513 #my $error = $cust_pkg->check;
514 #return { 'error' => $error } if $error;
516 #should be all auto-magic and shit
518 if ( $svc_x eq 'svc_acct' ) {
520 my $svc = new FS::svc_acct {
521 'svcpart' => $svcpart,
522 map { $_ => $packet->{$_} }
523 qw( username _password sec_phrase popnum ),
528 while ( exists($packet->{"snarf_machine$snarfnum"})
529 && length($packet->{"snarf_machine$snarfnum"}) ) {
530 my $acct_snarf = new FS::acct_snarf ( {
531 'machine' => $packet->{"snarf_machine$snarfnum"},
532 'protocol' => $packet->{"snarf_protocol$snarfnum"},
533 'username' => $packet->{"snarf_username$snarfnum"},
534 '_password' => $packet->{"snarf_password$snarfnum"},
537 push @acct_snarf, $acct_snarf;
539 $svc->child_objects( \@acct_snarf );
543 } elsif ( $svc_x eq 'svc_phone' ) {
545 my $svc = new FS::svc_phone ( {
546 'svcpart' => $svcpart,
547 map { $_ => $packet->{$_} }
548 qw( countrycode phonenum sip_password pin ),
554 die "unknown signup service $svc_x";
556 my $y = $svc[0]->setdefault; # arguably should be in new method
557 return { 'error' => $y } if $y && !ref($y);
559 if ($packet->{'mac_addr'} && $conf->exists('signup_server-mac_addr_svcparts'))
562 my %mac_addr_svcparts = map { $_ => 1 }
563 $conf->config('signup_server-mac_addr_svcparts');
564 my @pkg_svc = grep { $_->quantity && $mac_addr_svcparts{$_->svcpart} }
565 $cust_pkg->part_pkg->pkg_svc;
567 return { 'error' => 'No service defined to assign mac address' }
570 my $svc = new FS::svc_acct {
571 'svcpart' => $pkg_svc[0]->svcpart, #multiple matches? alas..
572 'username' => $packet->{'mac_addr'},
573 '_password' => '', #blank as requested (set passwordmin to 0)
576 my $y = $svc->setdefault; # arguably should be in new method
577 return { 'error' => $y } if $y && !ref($y);
583 #$error = $svc->check;
584 #return { 'error' => $error } if $error;
586 #setup a job dependancy to delay provisioning
587 my $placeholder = new FS::queue ( {
588 'job' => 'FS::ClientAPI::Signup::__placeholder',
589 'status' => 'locked',
591 my $error = $placeholder->insert;
592 return { 'error' => $error } if $error;
595 tie my %hash, 'Tie::RefHash';
596 %hash = ( $cust_pkg => \@svc );
598 $error = $cust_main->insert(
601 'depend_jobnum' => $placeholder->jobnum,
604 my $perror = $placeholder->delete;
605 $error .= " (Additionally, error removing placeholder: $perror)" if $perror;
606 return { 'error' => $error };
609 if ( $conf->exists('signup_server-realtime') ) {
611 #warn "[fs_signup_server] Billing customer...\n" if $Debug;
613 my $bill_error = $cust_main->bill;
614 #warn "[fs_signup_server] error billing new customer: $bill_error"
617 $bill_error = $cust_main->apply_payments_and_credits;
618 #warn "[fs_signup_server] error applying payments and credits for".
619 # " new customer: $bill_error"
622 if ($cust_main->_new_bop_required()) {
623 $bill_error = $cust_main->realtime_collect(
624 method => FS::payby->payby2bop( $packet->{payby} ),
625 depend_jobnum => $placeholder->jobnum,
628 $bill_error = $cust_main->collect('realtime' => 1);
630 #warn "[fs_signup_server] error collecting from new customer: $bill_error"
633 if ($bill_error && ref($bill_error) eq 'HASH') {
634 return { 'error' => '_collect',
635 ( map { $_ => $bill_error->{$_} }
636 qw(popup_url reference collectitems)
638 amount => $cust_main->balance,
642 if ( $cust_main->balance > 0 ) {
644 #this makes sense. credit is "un-doing" the invoice
645 $cust_main->credit( $cust_main->balance, 'signup server decline',
646 'reason_type' => $conf->config('signup_credit_type'),
648 $cust_main->apply_credits;
650 #should check list for errors...
651 #$cust_main->suspend;
652 local $FS::svc_Common::noexport_hack = 1;
653 $cust_main->cancel('quiet'=>1);
655 my $perror = $placeholder->depended_delete;
656 warn "error removing provisioning jobs after decline: $perror" if $perror;
658 $perror = $placeholder->delete;
659 warn "error removing placeholder after decline: $perror" if $perror;
662 return { 'error' => '_decline' };
668 $error = $reg_code->delete;
669 return { 'error' => $error } if $error;
672 $error = $placeholder->delete;
673 return { 'error' => $error } if $error;
675 my %return = ( 'error' => '',
676 'signup_service' => $svc_x,
679 if ( $svc_x eq 'svc_acct' ) {
680 $return{$_} = $svc[0]->$_() for qw( username _password );
681 } elsif ( $svc_x eq 'svc_phone' ) {
682 $return{$_} = $svc[0]->$_() for qw( countrycode phonenum sip_password pin );
684 die "unknown signup service $svc_x";
691 sub capture_payment {
694 warn "$me capture_payment called on $packet\n" if $DEBUG;
697 # identify processor/gateway from called back URL
700 my $conf = new FS::Conf;
702 my $url = $packet->{url};
703 my $payment_gateway =
704 qsearchs('payment_gateway', { 'gateway_callback_url' => popurl(0, $url) } );
706 unless ($payment_gateway) {
708 my ( $processor, $login, $password, $action, @bop_options ) =
709 $conf->config('business-onlinepayment');
710 $action ||= 'normal authorization';
711 pop @bop_options if scalar(@bop_options) % 2 && $bop_options[-1] =~ /^\s*$/;
712 die "No real-time processor is enabled - ".
713 "did you set the business-onlinepayment configuration value?\n"
716 $payment_gateway = new FS::payment_gateway( {
717 gateway_namespace => $conf->config('business-onlinepayment-namespace'),
718 gateway_module => $processor,
719 gateway_username => $login,
720 gateway_password => $password,
721 gateway_action => $action,
722 options => [ ( @bop_options ) ],
727 die "No real-time third party processor is enabled - ".
728 "did you set the business-onlinepayment configuration value?\n*"
729 unless $payment_gateway->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
732 # locate pending transaction
735 eval "use Business::OnlineThirdPartyPayment";
739 new Business::OnlineThirdPartyPayment( $payment_gateway->gateway_module,
740 @{ [ $payment_gateway->options ] },
743 my $paypendingnum = $transaction->reference($packet->{data});
745 my $cust_pay_pending =
746 qsearchs('cust_pay_pending', { paypendingnum => $paypendingnum } );
748 unless ($cust_pay_pending) {
749 my $bill_error = "No payment is being processed with id $paypendingnum".
750 "; Transaction aborted.";
751 return { error => '_decline', bill_error => $bill_error };
754 if ($cust_pay_pending->status ne 'pending') {
755 my $bill_error = "Payment with id $paypendingnum is not pending, but ".
756 $cust_pay_pending->status. "; Transaction aborted.";
757 return { error => '_decline', bill_error => $bill_error };
760 my $cust_main = $cust_pay_pending->cust_main;
762 $cust_main->realtime_botpp_capture( $cust_pay_pending, %{$packet->{data}} );
764 return { 'error' => ( $bill_error->{bill_error} ? '_decline' : '' ),