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->gateway_namespace eq
293 'Business::OnlineThirdPartyPayment'
295 warn "$me hiding $payby payment fields\n" if $DEBUG > 1;
299 push @{$signup_info->{'hide_payment_fields'}}, $hide;
302 warn "$me done setting agent-specific payment flag\n" if $DEBUG > 1;
304 warn "$me setting agent-specific package list\n" if $DEBUG > 1;
305 $signup_info->{'part_pkg'} = $signup_info->{'agentnum2part_pkg'}{$agentnum}
306 unless @{ $signup_info->{'part_pkg'} };
307 warn "$me done setting agent-specific package list\n" if $DEBUG > 1;
309 warn "$me setting agent-specific adv. source list\n" if $DEBUG > 1;
310 $signup_info->{'part_referral'} =
314 'table' => 'part_referral',
315 'hashref' => { 'disabled' => '' },
316 'extra_sql' => "AND ( agentnum = $agentnum ".
317 " OR agentnum IS NULL ) ",
321 warn "$me done setting agent-specific adv. source list\n" if $DEBUG > 1;
323 $signup_info->{'agent_name'} = $agent->agent;
325 $signup_info->{'company_name'} = $conf->config('company_name', $agentnum);
327 if ( $signup_info->{'agent_ship_address'} && $agent->agent_custnum ) {
328 my $cust_main = $agent->agent_cust_main;
329 my $prefix = length($cust_main->ship_last) ? 'ship_' : '';
330 $signup_info->{"ship_$_"} = $cust_main->get("$prefix$_")
331 foreach qw( address1 city county state zip country );
336 # delete $signup_info->{'part_pkg'};
339 warn "$me sorting package list\n" if $DEBUG > 1;
340 $signup_info->{'part_pkg'} = [ sort { $a->{pkg} cmp $b->{pkg} } # case?
341 @{ $signup_info->{'part_pkg'} }
343 warn "$me done sorting package list\n" if $DEBUG > 1;
345 if ( exists $packet->{'session_id'} ) {
346 my $agent_signup_info = { %$signup_info };
347 delete $agent_signup_info->{agentnum2part_pkg};
348 $agent_signup_info->{'agent'} = $session->{'agent'};
356 sub domain_select_hash {
361 if ($packet->{pkgpart}) {
362 my $part_pkg = qsearchs('part_pkg' => { 'pkgpart' => $packet->{pkgpart} } );
363 #$packet->{svcpart} = $part_pkg->svcpart('svc_acct')
364 $packet->{svcpart} = $part_pkg->svcpart
368 if ($packet->{svcpart}) {
369 my $part_svc = qsearchs('part_svc' => { 'svcpart' => $packet->{svcpart} } );
370 $response->{'domsvc'} = $part_svc->part_svc_column('domsvc')->columnvalue
371 if ($part_svc && $part_svc->part_svc_column('domsvc')->columnflag eq 'D');
374 $response->{'domains'}
375 = { domain_select_hash FS::svc_acct( map { $_ => $packet->{$_} }
385 my $conf = new FS::Conf;
386 my $svc_x = $conf->config('signup_server-service') || 'svc_acct';
388 if ( $svc_x eq 'svc_acct' ) {
390 #things that aren't necessary in base class, but are for signup server
391 #return "Passwords don't match"
392 # if $hashref->{'_password'} ne $hashref->{'_password2'}
393 return { 'error' => gettext('empty_password') }
394 unless length($packet->{'_password'});
395 # a bit inefficient for large numbers of pops
396 return { 'error' => gettext('no_access_number_selected') }
397 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
402 if ( exists $packet->{'session_id'} ) {
403 my $cache = new FS::ClientAPI_SessionCache( {
404 'namespace' => 'FS::ClientAPI::Agent',
406 my $session = $cache->get($packet->{'session_id'});
408 $agentnum = $session->{'agentnum'};
410 return { 'error' => "Can't resume session" }; #better error message
413 $agentnum = $packet->{agentnum}
414 || $conf->config('signup_server-default_agentnum');
417 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
418 # common that are still here and library them.
419 my $cust_main = new FS::cust_main ( {
421 'agentnum' => $agentnum,
422 'refnum' => $packet->{refnum}
423 || $conf->config('signup_server-default_refnum'),
425 map { $_ => $packet->{$_} } qw(
427 last first ss company address1 address2
428 city county state zip country
429 daytime night fax stateid stateid_state
431 ship_last ship_first ship_ss ship_company ship_address1 ship_address2
432 ship_city ship_county ship_state ship_zip ship_country
433 ship_daytime ship_night ship_fax
436 payinfo paycvv paydate payname paystate paytype
437 paystart_month paystart_year payissue
440 referral_custnum comments
445 my $agent = qsearchs('agent', { 'agentnum' => $agentnum } );
446 if ( $conf->exists('agent_ship_address') && $agent->agent_custnum ) {
447 my $agent_cust_main = $agent->agent_cust_main;
448 my $prefix = length($agent_cust_main->ship_last) ? 'ship_' : '';
449 $cust_main->set("ship_$_", $agent_cust_main->get("$prefix$_") )
450 foreach qw( address1 city county state zip country );
452 $cust_main->set("ship_$_", $cust_main->get($_))
453 foreach qw( last first );
458 return { 'error' => "Illegal payment type" }
459 unless grep { $_ eq $packet->{'payby'} }
460 $conf->config('signup_server-payby');
462 if (FS::payby->realtime($packet->{payby})) {
463 my $payby = $packet->{payby};
465 my $agent = qsearchs('agent', { 'agentnum' => $agentnum });
466 return { 'error' => "Unknown reseller" }
469 my $payment_gateway =
470 $agent->payment_gateway( 'method' => FS::payby->payby2bop($payby) );
472 if ($payment_gateway->gateway_namespace eq
473 'Business::OnlineThirdPartyPayment'
475 $cust_main->payby('BILL'); # MCRD better?
479 $cust_main->payinfo($cust_main->daytime)
480 if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
482 my @invoicing_list = $packet->{'invoicing_list'}
483 ? split( /\s*\,\s*/, $packet->{'invoicing_list'} )
486 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
488 return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
491 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
492 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
493 my $svcpart = $part_pkg->svcpart($svc_x);
496 if ( $packet->{'reg_code'} ) {
497 $reg_code = qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
498 'agentnum' => $agentnum, } )
499 or return { 'error' => 'Unknown registration code' };
502 my $cust_pkg = new FS::cust_pkg ( {
503 #later#'custnum' => $custnum,
504 'pkgpart' => $packet->{'pkgpart'},
505 'promo_code' => $packet->{'promo_code'},
506 'reg_code' => $packet->{'reg_code'},
508 #my $error = $cust_pkg->check;
509 #return { 'error' => $error } if $error;
511 #should be all auto-magic and shit
513 if ( $svc_x eq 'svc_acct' ) {
515 $svc = new FS::svc_acct ( {
516 'svcpart' => $svcpart,
517 map { $_ => $packet->{$_} }
518 qw( username _password sec_phrase popnum ),
523 while ( exists($packet->{"snarf_machine$snarfnum"})
524 && length($packet->{"snarf_machine$snarfnum"}) ) {
525 my $acct_snarf = new FS::acct_snarf ( {
526 'machine' => $packet->{"snarf_machine$snarfnum"},
527 'protocol' => $packet->{"snarf_protocol$snarfnum"},
528 'username' => $packet->{"snarf_username$snarfnum"},
529 '_password' => $packet->{"snarf_password$snarfnum"},
532 push @acct_snarf, $acct_snarf;
534 $svc->child_objects( \@acct_snarf );
536 } elsif ( $svc_x eq 'svc_phone' ) {
538 $svc = new FS::svc_phone ( {
539 'svcpart' => $svcpart,
540 map { $_ => $packet->{$_} }
541 qw( countrycode phonenum sip_password pin ),
545 die "unknown signup service $svc_x";
548 my $y = $svc->setdefault; # arguably should be in new method
549 return { 'error' => $y } if $y && !ref($y);
551 #$error = $svc->check;
552 #return { 'error' => $error } if $error;
554 #setup a job dependancy to delay provisioning
555 my $placeholder = new FS::queue ( {
556 'job' => 'FS::ClientAPI::Signup::__placeholder',
557 'status' => 'locked',
559 my $error = $placeholder->insert;
560 return { 'error' => $error } if $error;
563 tie my %hash, 'Tie::RefHash';
564 %hash = ( $cust_pkg => [ $svc ] );
566 $error = $cust_main->insert(
569 'depend_jobnum' => $placeholder->jobnum,
572 my $perror = $placeholder->delete;
573 $error .= " (Additionally, error removing placeholder: $perror)" if $perror;
574 return { 'error' => $error };
577 if ( $conf->exists('signup_server-realtime') ) {
579 #warn "[fs_signup_server] Billing customer...\n" if $Debug;
581 my $bill_error = $cust_main->bill;
582 #warn "[fs_signup_server] error billing new customer: $bill_error"
585 $bill_error = $cust_main->apply_payments_and_credits;
586 #warn "[fs_signup_server] error applying payments and credits for".
587 # " new customer: $bill_error"
590 if ($cust_main->_new_bop_required()) {
591 $bill_error = $cust_main->realtime_collect(
592 method => FS::payby->payby2bop( $packet->{payby} ),
593 depend_jobnum => $placeholder->jobnum,
596 $bill_error = $cust_main->collect('realtime' => 1);
598 #warn "[fs_signup_server] error collecting from new customer: $bill_error"
601 if ($bill_error && ref($bill_error) eq 'HASH') {
602 return { 'error' => '_collect',
603 ( map { $_ => $bill_error->{$_} }
604 qw(popup_url reference collectitems)
606 amount => $cust_main->balance,
610 if ( $cust_main->balance > 0 ) {
612 #this makes sense. credit is "un-doing" the invoice
613 $cust_main->credit( $cust_main->balance, 'signup server decline',
614 'reason_type' => $conf->config('signup_credit_type'),
616 $cust_main->apply_credits;
618 #should check list for errors...
619 #$cust_main->suspend;
620 local $FS::svc_Common::noexport_hack = 1;
621 $cust_main->cancel('quiet'=>1);
623 my $perror = $placeholder->depended_delete;
624 warn "error removing provisioning jobs after decline: $perror" if $perror;
626 $perror = $placeholder->delete;
627 warn "error removing placeholder after decline: $perror" if $perror;
630 return { 'error' => '_decline' };
636 $error = $reg_code->delete;
637 return { 'error' => $error } if $error;
640 $error = $placeholder->delete;
641 return { 'error' => $error } if $error;
643 my %return = ( 'error' => '',
644 'signup_service' => $svc_x,
647 if ( $svc_x eq 'svc_acct' ) {
648 $return{$_} = $svc->$_() for qw( username _password );
649 } elsif ( $svc_x eq 'svc_phone' ) {
650 $return{$_} = $svc->$_() for qw( countrycode phonenum sip_password pin );
652 die "unknown signup service $svc_x";
659 sub capture_payment {
662 warn "$me capture_payment called on $packet\n" if $DEBUG;
665 # identify processor/gateway from called back URL
668 my $conf = new FS::Conf;
670 my $url = $packet->{url};
671 my $payment_gateway =
672 qsearchs('payment_gateway', { 'gateway_callback_url' => popurl(0, $url) } );
674 unless ($payment_gateway) {
676 my ( $processor, $login, $password, $action, @bop_options ) =
677 $conf->config('business-onlinepayment');
678 $action ||= 'normal authorization';
679 pop @bop_options if scalar(@bop_options) % 2 && $bop_options[-1] =~ /^\s*$/;
680 die "No real-time processor is enabled - ".
681 "did you set the business-onlinepayment configuration value?\n"
684 $payment_gateway = new FS::payment_gateway( {
685 gateway_namespace => $conf->config('business-onlinepayment-namespace'),
686 gateway_module => $processor,
687 gateway_username => $login,
688 gateway_password => $password,
689 gateway_action => $action,
690 options => [ ( @bop_options ) ],
695 die "No real-time third party processor is enabled - ".
696 "did you set the business-onlinepayment configuration value?\n*"
697 unless $payment_gateway->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
700 # locate pending transaction
703 eval "use Business::OnlineThirdPartyPayment";
707 new Business::OnlineThirdPartyPayment( $payment_gateway->gateway_module,
708 @{ [ $payment_gateway->options ] },
711 my $paypendingnum = $transaction->reference($packet->{data});
713 my $cust_pay_pending =
714 qsearchs('cust_pay_pending', { paypendingnum => $paypendingnum } );
716 unless ($cust_pay_pending) {
717 my $bill_error = "No payment is being processed with id $paypendingnum".
718 "; Transaction aborted.";
719 return { error => '_decline', bill_error => $bill_error };
722 if ($cust_pay_pending->status ne 'pending') {
723 my $bill_error = "Payment with id $paypendingnum is not pending, but ".
724 $cust_pay_pending->status. "; Transaction aborted.";
725 return { error => '_decline', bill_error => $bill_error };
728 my $cust_main = $cust_pay_pending->cust_main;
730 $cust_main->realtime_botpp_capture( $cust_pay_pending, %{$packet->{data}} );
732 return { 'error' => ( $bill_error->{bill_error} ? '_decline' : '' ),