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 }
64 || $_->agentnum == $agent->agentnum
67 qsearch( 'part_pkg', { 'disabled' => '' } )
69 } qsearch('agent', { 'disabled' => '' })
72 my $msgcat = { map { $_=>gettext($_) }
73 qw( passwords_dont_match invalid_card unknown_card_type
74 not_a empty_password illegal_or_empty_text )
76 warn "msgcat: ". Dumper($msgcat). "\n" if $DEBUG > 2;
78 my $label = { map { $_ => FS::Msgcat::_gettext($_) }
79 qw( stateid stateid_state )
81 warn "label: ". Dumper($label). "\n" if $DEBUG > 2;
83 my @agent_fields = qw( agentnum agent );
85 $signup_info_cache = {
86 'cust_main_county' => [ map $_->hashref,
87 qsearch('cust_main_county', {} )
90 'agent' => [ map { my $agent = $_;
91 map { $_ => $agent->get($_) } @agent_fields;
93 qsearch('agent', { 'disabled' => '' } )
96 'part_referral' => [ map $_->hashref,
97 qsearch('part_referral', { 'disabled' => '' } )
100 'agentnum2part_pkg' => $agentnum2part_pkg,
102 'svc_acct_pop' => [ map $_->hashref, qsearch('svc_acct_pop',{} ) ],
104 'emailinvoiceonly' => $conf->exists('emailinvoiceonly'),
106 'security_phrase' => $conf->exists('security_phrase'),
108 'payby' => [ $conf->config('signup_server-payby') ],
110 'card_types' => card_types(),
112 'paytypes' => [ @FS::cust_main::paytypes ],
116 'stateid_enabled' => $conf->exists('show_stateid'),
118 'paystate_enabled' => $conf->exists('show_bankstate'),
126 'statedefault' => scalar($conf->config('statedefault')) || 'CA',
128 'countrydefault' => scalar($conf->config('countrydefault')) || 'US',
130 'refnum' => scalar($conf->config('signup_server-default_refnum')),
132 'default_pkgpart' => scalar($conf->config('signup_server-default_pkgpart')),
134 'signup_service' => $svc_x,
135 'default_svcpart' => scalar($conf->config('signup_server-default_svcpart')),
137 'head' => join("\n", $conf->config('selfservice-head') ),
138 'body_header' => join("\n", $conf->config('selfservice-body_header') ),
139 'body_footer' => join("\n", $conf->config('selfservice-body_footer') ),
140 'body_bgcolor' => scalar( $conf->config('selfservice-body_bgcolor') ),
141 'box_bgcolor' => scalar( $conf->config('selfservice-box_bgcolor') ),
143 'company_name' => scalar($conf->config('company_name')),
146 'agent_ship_address' => scalar($conf->exists('agent-ship_address')),
148 'no_company' => scalar($conf->exists('signup-no_company')),
149 'require_phone' => scalar($conf->exists('cust_main-require_phone')),
150 'recommend_daytime' => scalar($conf->exists('signup-recommend_daytime')),
151 'recommend_email' => scalar($conf->exists('signup-recommend_email')),
155 $cache->set('signup_info_cache', $signup_info_cache);
159 my $signup_info = { %$signup_info_cache };
160 warn "$me signup info loaded\n" if $DEBUG > 1;
161 warn Dumper($signup_info). "\n" if $DEBUG > 2;
163 my @addl = qw( signup_server-classnum2 signup_server-classnum3 );
165 if ( grep { $conf->exists($_) } @addl ) {
167 $signup_info->{optional_packages} = [];
169 foreach my $addl ( @addl ) {
171 warn "$me adding optional package info\n" if $DEBUG > 1;
173 my $classnum = $conf->config($addl) or next;
176 'freq_pretty' => $_->freq_pretty,
177 'options' => { $_->options },
181 qsearch( 'part_pkg', { classnum => $classnum } );
183 push @{$signup_info->{optional_packages}}, \@pkgs;
185 warn "$me done adding opt. package info for $classnum\n" if $DEBUG > 1;
191 my $agentnum = $packet->{'agentnum'}
192 || $conf->config('signup_server-default_agentnum');
193 $agentnum =~ /^(\d*)$/ or die "illegal agentnum";
197 if ( exists $packet->{'session_id'} ) {
199 warn "$me loading agent session\n" if $DEBUG > 1;
200 my $cache = new FS::ClientAPI_SessionCache( {
201 'namespace' => 'FS::ClientAPI::Agent',
203 $session = $cache->get($packet->{'session_id'});
205 $agentnum = $session->{'agentnum'};
207 return { 'error' => "Can't resume session" }; #better error message
209 warn "$me done loading agent session\n" if $DEBUG > 1;
211 } elsif ( exists $packet->{'customer_session_id'} ) {
213 warn "$me loading customer session\n" if $DEBUG > 1;
214 my $cache = new FS::ClientAPI_SessionCache( {
215 'namespace' => 'FS::ClientAPI::MyAccount',
217 $session = $cache->get($packet->{'customer_session_id'});
219 my $custnum = $session->{'custnum'};
220 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum });
221 return { 'error' => "Can't find your customer record" } unless $cust_main;
222 $agentnum = $cust_main->agentnum;
224 return { 'error' => "Can't resume session" }; #better error message
226 warn "$me done loading customer session\n" if $DEBUG > 1;
230 $signup_info->{'part_pkg'} = [];
232 if ( $packet->{'reg_code'} ) {
234 warn "$me setting package list via reg_code\n" if $DEBUG > 1;
236 $signup_info->{'part_pkg'} =
237 [ map { { 'payby' => [ $_->payby ],
238 'freq_pretty' => $_->freq_pretty,
239 'options' => { $_->options },
243 grep { $_->svcpart($svc_x) }
245 qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
246 'agentnum' => $agentnum, } )
250 $signup_info->{'error'} = 'Unknown registration code'
251 unless @{ $signup_info->{'part_pkg'} };
253 warn "$me done setting package list via reg_code\n" if $DEBUG > 1;
255 } elsif ( $packet->{'promo_code'} ) {
257 warn "$me setting package list via promo_code\n" if $DEBUG > 1;
259 $signup_info->{'part_pkg'} =
260 [ map { { 'payby' => [ $_->payby ],
261 'freq_pretty' => $_->freq_pretty,
262 'options' => { $_->options },
265 grep { $_->svcpart($svc_x) }
266 qsearch( 'part_pkg', { 'promo_code' => {
268 value=>$packet->{'promo_code'}
270 'disabled' => '', } )
273 $signup_info->{'error'} = 'Unknown promotional code'
274 unless @{ $signup_info->{'part_pkg'} };
276 warn "$me done setting package list via promo_code\n" if $DEBUG > 1;
281 warn "$me setting agent-specific payment flag\n" if $DEBUG > 1;
282 my $agent = qsearchs('agent', { 'agentnum' => $agentnum } );
283 warn "$me has agent $agent\n" if $DEBUG > 1;
284 if ( $agent ) { #else complain loudly?
285 $signup_info->{'hide_payment_fields'} = [];
286 foreach my $payby (@{$signup_info->{payby}}) {
287 warn "$me checking $payby payment fields\n" if $DEBUG > 1;
289 if ( FS::payby->realtime($payby) ) {
290 my $payment_gateway =
291 $agent->payment_gateway( 'method' => FS::payby->payby2bop($payby) );
292 if ( $payment_gateway
293 && $payment_gateway->gateway_namespace
294 eq 'Business::OnlineThirdPartyPayment'
296 warn "$me hiding $payby payment fields\n" if $DEBUG > 1;
300 push @{$signup_info->{'hide_payment_fields'}}, $hide;
303 warn "$me done setting agent-specific payment flag\n" if $DEBUG > 1;
305 warn "$me setting agent-specific package list\n" if $DEBUG > 1;
306 $signup_info->{'part_pkg'} = $signup_info->{'agentnum2part_pkg'}{$agentnum}
307 unless @{ $signup_info->{'part_pkg'} };
308 warn "$me done setting agent-specific package list\n" if $DEBUG > 1;
310 warn "$me setting agent-specific adv. source list\n" if $DEBUG > 1;
311 $signup_info->{'part_referral'} =
315 'table' => 'part_referral',
316 'hashref' => { 'disabled' => '' },
317 'extra_sql' => "AND ( agentnum = $agentnum ".
318 " OR agentnum IS NULL ) ",
322 warn "$me done setting agent-specific adv. source list\n" if $DEBUG > 1;
324 $signup_info->{'agent_name'} = $agent->agent;
326 $signup_info->{'company_name'} = $conf->config('company_name', $agentnum);
328 if ( $signup_info->{'agent_ship_address'} && $agent->agent_custnum ) {
329 my $cust_main = $agent->agent_cust_main;
330 my $prefix = length($cust_main->ship_last) ? 'ship_' : '';
331 $signup_info->{"ship_$_"} = $cust_main->get("$prefix$_")
332 foreach qw( address1 city county state zip country );
337 # delete $signup_info->{'part_pkg'};
340 warn "$me sorting package list\n" if $DEBUG > 1;
341 $signup_info->{'part_pkg'} = [ sort { $a->{pkg} cmp $b->{pkg} } # case?
342 @{ $signup_info->{'part_pkg'} }
344 warn "$me done sorting package list\n" if $DEBUG > 1;
346 if ( exists $packet->{'session_id'} ) {
347 my $agent_signup_info = { %$signup_info };
348 delete $agent_signup_info->{agentnum2part_pkg};
349 $agent_signup_info->{'agent'} = $session->{'agent'};
357 sub domain_select_hash {
362 if ($packet->{pkgpart}) {
363 my $part_pkg = qsearchs('part_pkg' => { 'pkgpart' => $packet->{pkgpart} } );
364 #$packet->{svcpart} = $part_pkg->svcpart('svc_acct')
365 $packet->{svcpart} = $part_pkg->svcpart
369 if ($packet->{svcpart}) {
370 my $part_svc = qsearchs('part_svc' => { 'svcpart' => $packet->{svcpart} } );
371 $response->{'domsvc'} = $part_svc->part_svc_column('domsvc')->columnvalue
372 if ($part_svc && $part_svc->part_svc_column('domsvc')->columnflag eq 'D');
375 $response->{'domains'}
376 = { domain_select_hash FS::svc_acct( map { $_ => $packet->{$_} }
386 my $conf = new FS::Conf;
387 my $svc_x = $conf->config('signup_server-service') || 'svc_acct';
389 if ( $svc_x eq 'svc_acct' ) {
391 #things that aren't necessary in base class, but are for signup server
392 #return "Passwords don't match"
393 # if $hashref->{'_password'} ne $hashref->{'_password2'}
394 return { 'error' => gettext('empty_password') }
395 unless length($packet->{'_password'});
396 # a bit inefficient for large numbers of pops
397 return { 'error' => gettext('no_access_number_selected') }
398 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
403 if ( exists $packet->{'session_id'} ) {
404 my $cache = new FS::ClientAPI_SessionCache( {
405 'namespace' => 'FS::ClientAPI::Agent',
407 my $session = $cache->get($packet->{'session_id'});
409 $agentnum = $session->{'agentnum'};
411 return { 'error' => "Can't resume session" }; #better error message
414 $agentnum = $packet->{agentnum}
415 || $conf->config('signup_server-default_agentnum');
418 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
419 # common that are still here and library them.
420 my $cust_main = new FS::cust_main ( {
422 'agentnum' => $agentnum,
423 'refnum' => $packet->{refnum}
424 || $conf->config('signup_server-default_refnum'),
426 map { $_ => $packet->{$_} } qw(
428 last first ss company address1 address2
429 city county state zip country
430 daytime night fax stateid stateid_state
432 ship_last ship_first ship_ss ship_company ship_address1 ship_address2
433 ship_city ship_county ship_state ship_zip ship_country
434 ship_daytime ship_night ship_fax
437 payinfo paycvv paydate payname paystate paytype
438 paystart_month paystart_year payissue
441 referral_custnum comments
446 my $agent = qsearchs('agent', { 'agentnum' => $agentnum } );
447 if ( $conf->exists('agent_ship_address') && $agent->agent_custnum ) {
448 my $agent_cust_main = $agent->agent_cust_main;
449 my $prefix = length($agent_cust_main->ship_last) ? 'ship_' : '';
450 $cust_main->set("ship_$_", $agent_cust_main->get("$prefix$_") )
451 foreach qw( address1 city county state zip country );
453 $cust_main->set("ship_$_", $cust_main->get($_))
454 foreach qw( last first );
459 return { 'error' => "Illegal payment type" }
460 unless grep { $_ eq $packet->{'payby'} }
461 $conf->config('signup_server-payby');
463 if (FS::payby->realtime($packet->{payby})) {
464 my $payby = $packet->{payby};
466 my $agent = qsearchs('agent', { 'agentnum' => $agentnum });
467 return { 'error' => "Unknown reseller" }
470 my $payment_gateway =
471 $agent->payment_gateway( 'method' => FS::payby->payby2bop($payby) );
473 if ($payment_gateway->gateway_namespace eq
474 'Business::OnlineThirdPartyPayment'
476 $cust_main->payby('BILL'); # MCRD better?
480 $cust_main->payinfo($cust_main->daytime)
481 if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
483 my @invoicing_list = $packet->{'invoicing_list'}
484 ? split( /\s*\,\s*/, $packet->{'invoicing_list'} )
487 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
489 return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
492 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
493 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
494 my $svcpart = $part_pkg->svcpart($svc_x);
497 if ( $packet->{'reg_code'} ) {
498 $reg_code = qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
499 'agentnum' => $agentnum, } )
500 or return { 'error' => 'Unknown registration code' };
503 my $cust_pkg = new FS::cust_pkg ( {
504 #later#'custnum' => $custnum,
505 'pkgpart' => $packet->{'pkgpart'},
506 'promo_code' => $packet->{'promo_code'},
507 'reg_code' => $packet->{'reg_code'},
509 #my $error = $cust_pkg->check;
510 #return { 'error' => $error } if $error;
512 #should be all auto-magic and shit
514 if ( $svc_x eq 'svc_acct' ) {
516 $svc = new FS::svc_acct ( {
517 'svcpart' => $svcpart,
518 map { $_ => $packet->{$_} }
519 qw( username _password sec_phrase popnum ),
524 while ( exists($packet->{"snarf_machine$snarfnum"})
525 && length($packet->{"snarf_machine$snarfnum"}) ) {
526 my $acct_snarf = new FS::acct_snarf ( {
527 'machine' => $packet->{"snarf_machine$snarfnum"},
528 'protocol' => $packet->{"snarf_protocol$snarfnum"},
529 'username' => $packet->{"snarf_username$snarfnum"},
530 '_password' => $packet->{"snarf_password$snarfnum"},
533 push @acct_snarf, $acct_snarf;
535 $svc->child_objects( \@acct_snarf );
537 } elsif ( $svc_x eq 'svc_phone' ) {
539 $svc = new FS::svc_phone ( {
540 'svcpart' => $svcpart,
541 map { $_ => $packet->{$_} }
542 qw( countrycode phonenum sip_password pin ),
546 die "unknown signup service $svc_x";
549 my $y = $svc->setdefault; # arguably should be in new method
550 return { 'error' => $y } if $y && !ref($y);
552 #$error = $svc->check;
553 #return { 'error' => $error } if $error;
555 #setup a job dependancy to delay provisioning
556 my $placeholder = new FS::queue ( {
557 'job' => 'FS::ClientAPI::Signup::__placeholder',
558 'status' => 'locked',
560 my $error = $placeholder->insert;
561 return { 'error' => $error } if $error;
564 tie my %hash, 'Tie::RefHash';
565 %hash = ( $cust_pkg => [ $svc ] );
567 $error = $cust_main->insert(
570 'depend_jobnum' => $placeholder->jobnum,
573 my $perror = $placeholder->delete;
574 $error .= " (Additionally, error removing placeholder: $perror)" if $perror;
575 return { 'error' => $error };
578 if ( $conf->exists('signup_server-realtime') ) {
580 #warn "[fs_signup_server] Billing customer...\n" if $Debug;
582 my $bill_error = $cust_main->bill;
583 #warn "[fs_signup_server] error billing new customer: $bill_error"
586 $bill_error = $cust_main->apply_payments_and_credits;
587 #warn "[fs_signup_server] error applying payments and credits for".
588 # " new customer: $bill_error"
591 if ($cust_main->_new_bop_required()) {
592 $bill_error = $cust_main->realtime_collect(
593 method => FS::payby->payby2bop( $packet->{payby} ),
594 depend_jobnum => $placeholder->jobnum,
597 $bill_error = $cust_main->collect('realtime' => 1);
599 #warn "[fs_signup_server] error collecting from new customer: $bill_error"
602 if ($bill_error && ref($bill_error) eq 'HASH') {
603 return { 'error' => '_collect',
604 ( map { $_ => $bill_error->{$_} }
605 qw(popup_url reference collectitems)
607 amount => $cust_main->balance,
611 if ( $cust_main->balance > 0 ) {
613 #this makes sense. credit is "un-doing" the invoice
614 $cust_main->credit( $cust_main->balance, 'signup server decline',
615 'reason_type' => $conf->config('signup_credit_type'),
617 $cust_main->apply_credits;
619 #should check list for errors...
620 #$cust_main->suspend;
621 local $FS::svc_Common::noexport_hack = 1;
622 $cust_main->cancel('quiet'=>1);
624 my $perror = $placeholder->depended_delete;
625 warn "error removing provisioning jobs after decline: $perror" if $perror;
627 $perror = $placeholder->delete;
628 warn "error removing placeholder after decline: $perror" if $perror;
631 return { 'error' => '_decline' };
637 $error = $reg_code->delete;
638 return { 'error' => $error } if $error;
641 $error = $placeholder->delete;
642 return { 'error' => $error } if $error;
644 my %return = ( 'error' => '',
645 'signup_service' => $svc_x,
648 if ( $svc_x eq 'svc_acct' ) {
649 $return{$_} = $svc->$_() for qw( username _password );
650 } elsif ( $svc_x eq 'svc_phone' ) {
651 $return{$_} = $svc->$_() for qw( countrycode phonenum sip_password pin );
653 die "unknown signup service $svc_x";
660 sub capture_payment {
663 warn "$me capture_payment called on $packet\n" if $DEBUG;
666 # identify processor/gateway from called back URL
669 my $conf = new FS::Conf;
671 my $url = $packet->{url};
672 my $payment_gateway =
673 qsearchs('payment_gateway', { 'gateway_callback_url' => popurl(0, $url) } );
675 unless ($payment_gateway) {
677 my ( $processor, $login, $password, $action, @bop_options ) =
678 $conf->config('business-onlinepayment');
679 $action ||= 'normal authorization';
680 pop @bop_options if scalar(@bop_options) % 2 && $bop_options[-1] =~ /^\s*$/;
681 die "No real-time processor is enabled - ".
682 "did you set the business-onlinepayment configuration value?\n"
685 $payment_gateway = new FS::payment_gateway( {
686 gateway_namespace => $conf->config('business-onlinepayment-namespace'),
687 gateway_module => $processor,
688 gateway_username => $login,
689 gateway_password => $password,
690 gateway_action => $action,
691 options => [ ( @bop_options ) ],
696 die "No real-time third party processor is enabled - ".
697 "did you set the business-onlinepayment configuration value?\n*"
698 unless $payment_gateway->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
701 # locate pending transaction
704 eval "use Business::OnlineThirdPartyPayment";
708 new Business::OnlineThirdPartyPayment( $payment_gateway->gateway_module,
709 @{ [ $payment_gateway->options ] },
712 my $paypendingnum = $transaction->reference($packet->{data});
714 my $cust_pay_pending =
715 qsearchs('cust_pay_pending', { paypendingnum => $paypendingnum } );
717 unless ($cust_pay_pending) {
718 my $bill_error = "No payment is being processed with id $paypendingnum".
719 "; Transaction aborted.";
720 return { error => '_decline', bill_error => $bill_error };
723 if ($cust_pay_pending->status ne 'pending') {
724 my $bill_error = "Payment with id $paypendingnum is not pending, but ".
725 $cust_pay_pending->status. "; Transaction aborted.";
726 return { error => '_decline', bill_error => $bill_error };
729 my $cust_main = $cust_pay_pending->cust_main;
731 $cust_main->realtime_botpp_capture( $cust_pay_pending, %{$packet->{data}} );
733 return { 'error' => ( $bill_error->{bill_error} ? '_decline' : '' ),