1 package FS::ClientAPI::Signup;
4 use vars qw( $DEBUG $me );
5 use subs qw( _myaccount_cache );
8 use Digest::SHA qw(sha512_hex);
10 use FS::Record qw(qsearch qsearchs dbdef dbh);
11 use FS::CGI qw(popurl);
12 use FS::Msgcat qw(gettext);
13 use FS::Misc qw(card_types);
14 use FS::ClientAPI_SessionCache;
16 use FS::cust_main_county;
32 $me = '[FS::ClientAPI::Signup]';
36 FS::ClientAPI::Signup - Front-end API for signing up customers
40 This module provides the ClientAPI functions for talking to a signup
41 server. The signup server is open to the public, i.e. does not require a
42 login. The back-end Freeside server creates customers, orders packages and
43 services, and processes initial payments.
51 # document the rest of this as we work on it
54 warn "$me clear_cache called\n" if $DEBUG;
55 my $cache = new FS::ClientAPI_SessionCache( {
56 'namespace' => 'FS::ClientAPI::Signup',
65 warn "$me signup_info called on $packet\n" if $DEBUG;
67 my $conf = new FS::Conf;
68 my $svc_x = $conf->config('signup_server-service') || 'svc_acct';
70 my $cache = new FS::ClientAPI_SessionCache( {
71 'namespace' => 'FS::ClientAPI::Signup',
73 my $signup_info_cache = $cache->get('signup_info_cache');
75 if ( $signup_info_cache ) {
77 warn "$me loading cached signup info\n" if $DEBUG > 1;
81 warn "$me populating signup info cache\n" if $DEBUG > 1;
83 my $agentnum2part_pkg =
87 my $href = $agent->pkgpart_hashref;
90 map { { 'payby' => [ $_->payby ],
91 'freq_pretty' => $_->freq_pretty,
92 'options' => { $_->options },
95 grep { $_->svcpart($svc_x)
96 && ( $href->{ $_->pkgpart }
98 && $_->agentnum == $agent->agentnum
102 qsearch( 'part_pkg', { 'disabled' => '' } )
104 } qsearch('agent', { 'disabled' => '' })
107 my $msgcat = { map { $_=>gettext($_) }
108 qw( passwords_dont_match invalid_card unknown_card_type
109 not_a empty_password illegal_or_empty_text )
111 warn "msgcat: ". Dumper($msgcat). "\n" if $DEBUG > 2;
113 my $label = { map { $_ => FS::Msgcat::_gettext($_) }
114 qw( stateid stateid_state )
116 warn "label: ". Dumper($label). "\n" if $DEBUG > 2;
118 my @agent_fields = qw( agentnum agent );
120 my @bools = qw( emailinvoiceonly security_phrase );
122 my @signup_bools = qw( no_company recommend_daytime recommend_email );
124 my @signup_server_scalars = qw( default_pkgpart default_svcpart default_domsvc );
126 my @selfservice_textareas = qw( head body_header body_footer );
128 my @selfservice_scalars = qw(
129 body_bgcolor box_bgcolor
130 text_color link_color vlink_color hlink_color alink_color
131 font title_color title_align title_size menu_bgcolor menu_fontsize
134 #XXX my @selfservice_bools = qw(
135 # menu_skipblanks menu_skipheadings menu_nounderline
138 #my $selfservice_binaries = qw(
139 # title_left_image title_right_image
140 # menu_top_image menu_body_image menu_bottom_image
143 $signup_info_cache = {
145 'cust_main_county' => [ map $_->hashref,
146 qsearch('cust_main_county', {} )
149 'agent' => [ map { my $agent = $_;
150 +{ map { $_ => $agent->get($_) } @agent_fields }
152 qsearch('agent', { 'disabled' => '' } )
155 'part_referral' => [ map $_->hashref,
156 qsearch('part_referral', { 'disabled' => '' } )
159 'agentnum2part_pkg' => $agentnum2part_pkg,
161 'svc_acct_pop' => [ map $_->hashref, qsearch('svc_acct_pop',{} ) ],
163 'emailinvoiceonly' => $conf->exists('emailinvoiceonly'),
165 'security_phrase' => $conf->exists('security_phrase'),
167 'payby' => [ $conf->config('signup_server-payby') ],
169 'payby_longname' => [ map { FS::payby->longname($_) }
170 $conf->config('signup_server-payby') ],
172 'card_types' => card_types(),
174 ( map { $_ => $conf->exists("signup-$_") } @signup_bools ),
176 ( map { $_ => scalar($conf->config("signup_server-$_")) }
177 @signup_server_scalars
180 ( map { $_ => join("\n", $conf->config("selfservice-$_")) }
181 @selfservice_textareas
183 ( map { $_ => scalar($conf->config("selfservice-$_")) }
187 #( map { $_ => scalar($conf->config_binary("selfservice-$_")) }
188 # @selfservice_binaries
191 'agentnum2part_pkg' => $agentnum2part_pkg,
192 'svc_acct_pop' => [ map $_->hashref, qsearch('svc_acct_pop',{} ) ],
193 'payby' => [ $conf->config('signup_server-payby') ],
194 'card_types' => card_types(),
195 'paytypes' => [ FS::cust_payby->paytypes ],
197 'require_cvv' => $conf->exists('signup-require_cvv'),
198 'stateid_enabled' => $conf->exists('show_stateid'),
199 'paystate_enabled' => $conf->exists('show_bankstate'),
200 'exempt_groups' => [ grep /\S/, $conf->config('tax-cust_exempt-groups') ],
204 'statedefault' => scalar($conf->config('statedefault')) || 'CA',
205 'countrydefault' => scalar($conf->config('countrydefault')) || 'US',
206 'refnum' => scalar($conf->config('signup_server-default_refnum')),
207 'signup_service' => $svc_x,
208 'company_name' => scalar($conf->config('company_name')),
210 'logo' => scalar($conf->config_binary('logo.png')),
211 'prepaid_template_custnum' => $conf->exists('signup_server-prepaid-template-custnum'),
214 $cache->set('signup_info_cache', $signup_info_cache);
218 my $signup_info = { %$signup_info_cache };
219 warn "$me signup info loaded\n" if $DEBUG > 1;
220 warn Dumper($signup_info). "\n" if $DEBUG > 2;
222 my @addl = qw( signup_server-classnum2 signup_server-classnum3 );
224 if ( grep { $conf->exists($_) } @addl ) {
226 $signup_info->{optional_packages} = [];
228 foreach my $addl ( @addl ) {
230 warn "$me adding optional package info\n" if $DEBUG > 1;
232 my $classnum = $conf->config($addl) or next;
235 'freq_pretty' => $_->freq_pretty,
236 'options' => { $_->options },
240 qsearch( 'part_pkg', { classnum => $classnum } );
242 push @{$signup_info->{optional_packages}}, \@pkgs;
244 warn "$me done adding opt. package info for $classnum\n" if $DEBUG > 1;
250 my $agentnum = $packet->{'agentnum'}
251 || $conf->config('signup_server-default_agentnum');
252 $agentnum =~ /^(\d*)$/ or die "illegal agentnum";
256 if ( exists $packet->{'session_id'} ) {
258 warn "$me loading agent session\n" if $DEBUG > 1;
259 my $cache = new FS::ClientAPI_SessionCache( {
260 'namespace' => 'FS::ClientAPI::Agent',
262 $session = $cache->get($packet->{'session_id'});
264 $agentnum = $session->{'agentnum'};
266 return { 'error' => "Can't resume session" }; #better error message
268 warn "$me done loading agent session\n" if $DEBUG > 1;
270 } elsif ( exists $packet->{'customer_session_id'} ) {
272 warn "$me loading customer session\n" if $DEBUG > 1;
273 my $cache = new FS::ClientAPI_SessionCache( {
274 'namespace' => 'FS::ClientAPI::MyAccount',
276 $session = $cache->get($packet->{'customer_session_id'});
278 my $custnum = $session->{'custnum'};
279 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum });
280 return { 'error' => "Can't find your customer record" } unless $cust_main;
281 $agentnum = $cust_main->agentnum;
283 return { 'error' => "Can't resume session" }; #better error message
285 warn "$me done loading customer session\n" if $DEBUG > 1;
289 $signup_info->{'part_pkg'} = [];
291 if ( $packet->{'reg_code'} ) {
293 warn "$me setting package list via reg_code\n" if $DEBUG > 1;
295 $signup_info->{'part_pkg'} =
296 [ map { { 'payby' => [ $_->payby ],
297 'freq_pretty' => $_->freq_pretty,
298 'options' => { $_->options },
302 grep { $_->svcpart($svc_x) }
304 qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
305 'agentnum' => $agentnum, } )
309 $signup_info->{'error'} = 'Unknown registration code'
310 unless @{ $signup_info->{'part_pkg'} };
312 warn "$me done setting package list via reg_code\n" if $DEBUG > 1;
314 } elsif ( $packet->{'promo_code'} ) {
316 warn "$me setting package list via promo_code\n" if $DEBUG > 1;
318 $signup_info->{'part_pkg'} =
319 [ map { { 'payby' => [ $_->payby ],
320 'freq_pretty' => $_->freq_pretty,
321 'options' => { $_->options },
324 grep { $_->svcpart($svc_x) }
325 qsearch( 'part_pkg', { 'promo_code' => {
327 value=>$packet->{'promo_code'}
329 'disabled' => '', } )
332 $signup_info->{'error'} = 'Unknown promotional code'
333 unless @{ $signup_info->{'part_pkg'} };
335 warn "$me done setting package list via promo_code\n" if $DEBUG > 1;
340 warn "$me setting agent-specific payment flag\n" if $DEBUG > 1;
341 my $agent = qsearchs('agent', { 'agentnum' => $agentnum } )
342 or return { 'error' => "Self-service agent #$agentnum does not exist" };
343 warn "$me has agent $agent\n" if $DEBUG > 1;
344 my @paybys = @{ $signup_info->{'payby'} };
345 $signup_info->{'hide_payment_fields'} = [];
347 my $gatewaynum = $conf->config('selfservice-payment_gateway');
350 $force_gateway = qsearchs('payment_gateway', { gatewaynum => $gatewaynum });
351 warn "using forced gateway #$gatewaynum - " .
352 $force_gateway->gateway_username . '@' . $force_gateway->gateway_module
354 die "configured gatewaynum $gatewaynum not found!" if !$force_gateway;
356 foreach my $payby (@paybys) {
357 warn "$me checking $payby payment fields\n" if $DEBUG > 1;
359 if ( FS::payby->realtime($payby) ) {
360 my $gateway = $force_gateway ||
361 $agent->payment_gateway( 'method' => FS::payby->payby2bop($payby),
364 if ( $gateway && $gateway->gateway_namespace
365 eq 'Business::OnlineThirdPartyPayment'
367 warn "$me hiding $payby payment fields\n" if $DEBUG > 1;
371 push @{$signup_info->{'hide_payment_fields'}}, $hide;
373 warn "$me done setting agent-specific payment flag\n" if $DEBUG > 1;
375 warn "$me setting agent-specific package list\n" if $DEBUG > 1;
376 $signup_info->{'part_pkg'} = $signup_info->{'agentnum2part_pkg'}{$agentnum}
377 unless @{ $signup_info->{'part_pkg'} };
378 warn "$me done setting agent-specific package list\n" if $DEBUG > 1;
380 warn "$me setting agent-specific adv. source list\n" if $DEBUG > 1;
381 $signup_info->{'part_referral'} =
385 'table' => 'part_referral',
386 'hashref' => { 'disabled' => '' },
387 'extra_sql' => "AND ( agentnum = $agentnum ".
388 " OR agentnum IS NULL ) ",
392 warn "$me done setting agent-specific adv. source list\n" if $DEBUG > 1;
394 $signup_info->{'agent_name'} = $agent->agent;
396 $signup_info->{'company_name'} = $conf->config('company_name', $agentnum);
398 #some of the above could probably be cached, too
400 my $signup_info_cache_agent = $cache->get("signup_info_cache_agent$agentnum");
402 if ( $signup_info_cache_agent ) {
404 warn "$me loading cached signup info for agentnum $agentnum\n"
409 warn "$me populating signup info cache for agentnum $agentnum\n"
412 $signup_info_cache_agent = {
413 #( map { $_ => scalar( $conf->config($_, $agentnum) ) }
414 # qw( company_name ) ),
415 ( map { $_ => scalar( $conf->config("selfservice-$_", $agentnum ) ) }
416 qw( body_bgcolor box_bgcolor menu_bgcolor ) ),
417 ( map { $_ => join("\n", $conf->config("selfservice-$_", $agentnum ) ) }
418 qw( head body_header body_footer ) ),
419 ( map { $_ => join("\n", $conf->config("signup_server-$_", $agentnum ) ) }
420 qw( terms_of_service ) ),
422 ( map { $_ => scalar($conf->exists($_, $agentnum)) }
423 qw(cust_main-require_phone agent-ship_address) ),
426 if ( $signup_info_cache_agent->{'agent-ship_address'}
427 && $agent->agent_cust_main ) {
429 my $cust_main = $agent->agent_cust_main;
430 my $location = $cust_main->ship_location;
431 $signup_info_cache_agent->{"ship_$_"} = $location->get($_)
432 foreach qw( address1 city county state zip country );
436 $cache->set("signup_info_cache_agent$agentnum", $signup_info_cache_agent);
440 $signup_info->{$_} = $signup_info_cache_agent->{$_}
441 foreach keys %$signup_info_cache_agent;
445 # delete $signup_info->{'part_pkg'};
448 warn "$me sorting package list\n" if $DEBUG > 1;
449 $signup_info->{'part_pkg'} = [ sort { $a->{pkg} cmp $b->{pkg} } # case?
450 @{ $signup_info->{'part_pkg'} }
452 warn "$me done sorting package list\n" if $DEBUG > 1;
454 if ( exists $packet->{'session_id'} ) {
455 my $agent_signup_info = { %$signup_info };
456 delete $agent_signup_info->{agentnum2part_pkg};
457 $agent_signup_info->{'agent'} = $session->{'agent'};
458 return $agent_signup_info;
460 elsif ( exists $packet->{'keys'} ) {
461 my @keys = @{ $packet->{'keys'} };
462 return { map { $_ => $signup_info->{$_} } @keys };
470 sub domain_select_hash {
475 if ($packet->{pkgpart}) {
476 my $part_pkg = qsearchs('part_pkg' => { 'pkgpart' => $packet->{pkgpart} } );
477 #$packet->{svcpart} = $part_pkg->svcpart('svc_acct')
478 $packet->{svcpart} = $part_pkg->svcpart
482 if ($packet->{svcpart}) {
483 my $part_svc = qsearchs('part_svc' => { 'svcpart' => $packet->{svcpart} } );
484 $response->{'domsvc'} = $part_svc->part_svc_column('domsvc')->columnvalue
485 if ($part_svc && $part_svc->part_svc_column('domsvc')->columnflag eq 'D');
488 $response->{'domains'}
489 = { domain_select_hash FS::svc_acct( map { $_ => $packet->{$_} }
499 my $conf = new FS::Conf;
500 my $svc_x = $conf->config('signup_server-service') || 'svc_acct';
502 if ( $svc_x eq 'svc_acct' ) {
504 #things that aren't necessary in base class, but are for signup server
505 #return "Passwords don't match"
506 # if $hashref->{'_password'} ne $hashref->{'_password2'}
507 return { 'error' => gettext('empty_password') }
508 unless length($packet->{'_password'});
509 # a bit inefficient for large numbers of pops
510 return { 'error' => gettext('no_access_number_selected') }
511 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
514 elsif ( $svc_x eq 'svc_pbx' ) {
515 #possibly some validation will be needed
518 my $agentnum = get_agentnum($packet);
519 return $agentnum if ref($agentnum);
521 my ($bill_hash, $ship_hash);
522 foreach my $f (FS::cust_main->location_fields) {
523 # avoid having to change this in front-end code
524 $bill_hash->{$f} = $packet->{"bill_$f"} || $packet->{$f};
525 $ship_hash->{$f} = $packet->{"ship_$f"};
528 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
529 # common that are still here and library them.
532 'agentnum' => $agentnum,
533 'refnum' => $packet->{refnum}
534 || $conf->config('signup_server-default_refnum'),
535 'tagnum' => [ FS::part_tag->default_tags ],
537 ( map { $_ => $packet->{$_} } qw(
539 ss stateid stateid_state
541 referral_custnum comments
547 my %insert_options = ();
548 if ( $packet->{payby} =~ /^(CARD|DCRD|CHEK|DCHK)$/ ) {
549 $insert_options{cust_payby} = [
551 map { $_ => $packet->{$_} } qw(
553 payinfo paycvv paydate payname paystate paytype
554 paystart_month paystart_year payissue
561 my $template_custnum = $conf->config('signup_server-prepaid-template-custnum');
563 if ( $template_custnum && $packet->{prepaid_shortform} ) {
565 my $template_cust = qsearchs('cust_main', { 'custnum' => $template_custnum } );
566 return { 'error' => 'Configuration error' } unless $template_cust;
567 $cust_main = new FS::cust_main ( {
569 map { $_ => $template_cust->$_ } qw(
570 last first company daytime night fax mobile
574 $bill_hash = { $template_cust->bill_location->location_hash };
575 $ship_hash = { $template_cust->ship_location->location_hash };
579 $cust_main = new FS::cust_main ( {
581 map { $_ => $packet->{$_} } qw(
582 last first company daytime night fax mobile
588 my $bill_location = FS::cust_location->new($bill_hash);
590 my $agent = qsearchs('agent', { 'agentnum' => $agentnum } );
591 if ( $conf->exists('agent-ship_address', $agentnum)
592 && $agent->agent_custnum ) {
594 my $agent_cust_main = $agent->agent_cust_main;
595 my $prefix = length($agent_cust_main->ship_last) ? 'ship_' : '';
596 $ship_location = FS::cust_location->new({
597 $agent_cust_main->ship_location->location_hash
601 # we don't have an equivalent of the "same" checkbox in selfservice
602 # so is there a ship address, and if so, is it different from the billing
604 elsif ( length($ship_hash->{address1}) > 0 and
605 grep { $bill_hash->{$_} ne $ship_hash->{$_} } keys(%$ship_hash)
608 $ship_location = FS::cust_location->new( $ship_hash );
612 $ship_location = $bill_location;
615 $cust_main->set('bill_location' => $bill_location);
616 $cust_main->set('ship_location' => $ship_location);
618 return { 'error' => "Illegal payment type" }
619 unless grep { $_ eq $packet->{'payby'} }
620 $conf->config('signup_server-payby');
622 if (FS::payby->realtime($packet->{payby})
623 and not $conf->exists('signup_server-third_party_as_card')) {
624 my $payby = $packet->{payby};
626 my $agent = qsearchs('agent', { 'agentnum' => $agentnum });
627 return { 'error' => "Unknown reseller" }
631 my $gatewaynum = $conf->config('selfservice-payment_gateway');
633 $gw = qsearchs('payment_gateway', { gatewaynum => $gatewaynum });
634 die "configured gatewaynum $gatewaynum not found!" if !$gw;
637 $gw = $agent->payment_gateway( 'method' => FS::payby->payby2bop($payby),
642 $cust_main->payby('BILL') # MCRD better? no, that's for something else
643 if $gw && $gw->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
646 return { 'error' => "CVV2 is required" }
647 if $cust_main->payby =~ /^(CARD|DCRD)$/
648 && ! $cust_main->paycvv
649 && $conf->exists('signup-require_cvv');
651 my @invoicing_list = $packet->{'invoicing_list'}
652 ? split( /\s*\,\s*/, $packet->{'invoicing_list'} )
655 my @exempt_groups = grep /\S/, $conf->config('tax-cust_exempt-groups');
656 my @tax_exempt = grep { $packet->{"tax_$_"} eq 'Y' } @exempt_groups;
657 $insert_options{'tax_exemption'} = {
658 map { $_ => $packet->{"tax_$_".'_num'} } @tax_exempt
661 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
663 return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
666 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
667 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
670 if ( $packet->{'reg_code'} ) {
671 $reg_code = qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
672 'agentnum' => $agentnum, } )
673 or return { 'error' => 'Unknown registration code' };
676 my $cust_pkg = new FS::cust_pkg ( {
677 #later#'custnum' => $custnum,
678 'pkgpart' => $packet->{'pkgpart'},
679 'promo_code' => $packet->{'promo_code'},
680 'reg_code' => $packet->{'reg_code'},
682 #my $error = $cust_pkg->check;
683 #return { 'error' => $error } if $error;
686 unless ( $svc_x eq 'none' ) {
688 my $svcpart = $part_pkg->svcpart($svc_x);
689 #should be all auto-magic and shit
690 if ( $svc_x eq 'svc_acct' ) {
692 my $svc = new FS::svc_acct {
693 'svcpart' => $svcpart,
694 map { $_ => $packet->{$_} }
695 qw( username _password sec_phrase popnum domsvc ),
698 my $error = $svc->is_password_allowed($packet->{_password});
699 return { error => $error } if $error;
703 while ( exists($packet->{"snarf_machine$snarfnum"})
704 && length($packet->{"snarf_machine$snarfnum"}) ) {
705 my $acct_snarf = new FS::acct_snarf ( {
706 'machine' => $packet->{"snarf_machine$snarfnum"},
707 'protocol' => $packet->{"snarf_protocol$snarfnum"},
708 'username' => $packet->{"snarf_username$snarfnum"},
709 '_password' => $packet->{"snarf_password$snarfnum"},
712 push @acct_snarf, $acct_snarf;
714 $svc->child_objects( \@acct_snarf );
717 } elsif ( $svc_x eq 'svc_phone' ) {
719 push @svc, new FS::svc_phone ( {
720 'svcpart' => $svcpart,
721 map { $_ => $packet->{$_} }
722 qw( countrycode phonenum sip_password pin ),
725 } elsif ( $svc_x eq 'svc_pbx' ) {
727 push @svc, new FS::svc_pbx ( {
728 'svcpart' => $svcpart,
729 map { $_ => $packet->{$_} }
734 die "unknown signup service $svc_x";
739 if ($packet->{'mac_addr'} && $conf->exists('signup_server-mac_addr_svcparts'))
742 my %mac_addr_svcparts = map { $_ => 1 }
743 $conf->config('signup_server-mac_addr_svcparts');
744 my @pkg_svc = grep { $_->quantity && $mac_addr_svcparts{$_->svcpart} }
745 $cust_pkg->part_pkg->pkg_svc;
747 return { 'error' => 'No service defined to assign mac address' }
750 my $svc = new FS::svc_acct {
751 'svcpart' => $pkg_svc[0]->svcpart, #multiple matches? alas..
752 'username' => $packet->{'mac_addr'},
753 '_password' => '', #blank as requested (set passwordmin to 0)
760 foreach my $svc ( @svc ) {
761 my $y = $svc->setdefault; # arguably should be in new method
762 return { 'error' => $y } if $y && !ref($y);
763 #$error = $svc->check;
764 #return { 'error' => $error } if $error;
767 #setup a job dependancy to delay provisioning
768 my $placeholder = new FS::queue ( {
769 'job' => 'FS::ClientAPI::Signup::__placeholder',
770 'status' => 'locked',
772 my $error = $placeholder->insert;
773 return { 'error' => $error } if $error;
776 tie my %hash, 'Tie::RefHash';
777 %hash = ( $cust_pkg => \@svc );
779 $error = $cust_main->insert(
782 'depend_jobnum' => $placeholder->jobnum,
786 my $perror = $placeholder->delete;
787 $error .= " (Additionally, error removing placeholder: $perror)" if $perror;
788 return { 'error' => $error };
791 if ( $conf->exists('signup_server-realtime') ) {
793 #warn "$me Billing customer...\n" if $Debug;
796 my $bill_error = $cust_main->bill(
797 'depend_jobnum' => $placeholder->jobnum,
798 'return_bill' => \@cust_bill,
800 #warn "$me error billing new customer: $bill_error"
803 $bill_error = $cust_main->apply_payments_and_credits;
804 #warn "$me error applying payments and credits for".
805 # " new customer: $bill_error"
808 unless ( $packet->{payby} eq 'PREPAY' ) {
809 $bill_error = $cust_main->realtime_collect(
810 method => FS::payby->payby2bop( $packet->{payby} ),
811 depend_jobnum => $placeholder->jobnum,
814 #warn "$me error collecting from new customer: $bill_error"
818 if ($bill_error && ref($bill_error) eq 'HASH') {
819 return { 'error' => '_collect',
820 ( map { $_ => $bill_error->{$_} }
821 qw(popup_url reference collectitems)
823 amount => $cust_main->balance,
827 $bill_error = $cust_main->apply_payments_and_credits;
828 #warn "$me error applying payments and credits for".
829 # " new customer: $bill_error"
832 if ( $cust_main->balance > 0 ) {
834 #this used to apply a credit, but now we can void invoices...
835 foreach my $cust_bill (@cust_bill) {
836 my $voiderror = $cust_bill->void('automatic payment failed');
837 warn "Error voiding cust bill after decline: $voiderror" if $voiderror;
840 #should check list for errors...
841 #$cust_main->suspend;
842 local $FS::svc_Common::noexport_hack = 1;
843 $cust_main->cancel('quiet'=>1);
845 my $perror = $placeholder->depended_delete;
846 warn "error removing provisioning jobs after decline: $perror" if $perror;
848 $perror = $placeholder->delete;
849 warn "error removing placeholder after decline: $perror" if $perror;
852 return { 'error' => '_decline' };
858 $error = $reg_code->delete;
859 return { 'error' => $error } if $error;
862 $error = $placeholder->delete;
863 return { 'error' => $error } if $error;
865 if ( $conf->exists('signup-duplicate_cc-warn_hours') ) {
866 my $hours = $conf->config('signup-duplicate_cc-warn_hours');
867 my $ban = new FS::banned_pay $cust_main->_new_banned_pay_hashref;
868 $ban->end_date( int( time + $hours*3600 ) );
869 $ban->bantype('warn');
870 $ban->reason('signup-duplicate_cc-warn_hours');
871 $error = $ban->insert;
872 warn "WARNING: error inserting temporary banned_pay for ".
873 " signup-duplicate_cc-warn_hours (proceeding anyway): $error"
877 my %return = ( 'error' => '',
878 'signup_service' => $svc_x,
879 'custnum' => $cust_main->custnum,
884 $return{'svcnum'} = $svc[0]->svcnum;
886 if ( $svc_x eq 'svc_acct' ) {
887 $return{$_} = $svc[0]->$_() for qw( username _password );
888 } elsif ( $svc_x eq 'svc_phone' ) {
889 $return{$_} = $svc[0]->$_() for qw(countrycode phonenum sip_password pin);
890 } elsif ( $svc_x eq 'svc_pbx' ) {
891 #$return{$_} = $svc[0]->$_() for qw( ) #nothing yet
893 return {'error' => "configuration error: unknown signup service $svc_x"};
894 #die "unknown signup service $svc_x";
895 # return an error that's visible to someone somewhere
904 #false laziness w/ above
905 # fresh restart to support "free account" portals with 3.x/4.x-style
906 # addressless accounts
907 # and a contact (for self-service login)
908 sub new_customer_minimal {
911 my $conf = new FS::Conf;
912 my $svc_x = $conf->config('signup_server-service') || 'svc_acct';
914 if ( $svc_x eq 'svc_acct' ) {
916 #things that aren't necessary in base class, but are for signup server
917 #return "Passwords don't match"
918 # if $hashref->{'_password'} ne $hashref->{'_password2'}
919 return { 'error' => gettext('empty_password') }
920 unless length($packet->{'_password'});
921 # a bit inefficient for large numbers of pops
922 return { 'error' => gettext('no_access_number_selected') }
923 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
926 elsif ( $svc_x eq 'svc_pbx' ) {
927 #possibly some validation will be needed
930 my $agentnum = get_agentnum($packet);
931 return $agentnum if ref($agentnum);
933 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
934 # common that are still here and library them.
936 my $cust_main = new FS::cust_main ( {
937 'agentnum' => $agentnum,
938 'refnum' => $packet->{refnum}
939 || $conf->config('signup_server-default_refnum'),
940 'tagnum' => [ FS::part_tag->default_tags ],
942 map { $_ => $packet->{$_} } qw(
944 last first company daytime night fax mobile
945 ss stateid stateid_state
953 if ( $packet->{payby} =~ /^(CARD|DCRD|CHEK|DCHK)$/ ) {
956 map { $_ => $packet->{$_} } qw(
958 payinfo paycvv paydate payname paystate paytype
959 paystart_month paystart_year payissue
966 if ( grep length($packet->{$_}), FS::cust_main->location_fields ) {
968 foreach my $f (FS::cust_main->location_fields) {
969 $bill_hash->{$f} = $packet->{$f};
971 my $bill_location = FS::cust_location->new($bill_hash);
972 $cust_main->set('bill_location' => $bill_location);
973 $cust_main->set('ship_location' => $bill_location);
976 my @invoicing_list = $packet->{'invoicing_list'}
977 ? split( /\s*\,\s*/, $packet->{'invoicing_list'} )
981 tie my %hash, 'Tie::RefHash', ();
984 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
990 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
991 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
993 my $cust_pkg = new FS::cust_pkg ( {
994 #later#'custnum' => $custnum,
995 'pkgpart' => $packet->{'pkgpart'},
997 #my $error = $cust_pkg->check;
998 #return { 'error' => $error } if $error;
1000 unless ( $svc_x eq 'none' ) {
1002 my $svcpart = $part_pkg->svcpart($svc_x);
1003 #should be all auto-magic and shit
1004 if ( $svc_x eq 'svc_acct' ) {
1006 my $svc = new FS::svc_acct {
1007 'svcpart' => $svcpart,
1008 map { $_ => $packet->{$_} }
1009 qw( username _password sec_phrase popnum domsvc ),
1014 } elsif ( $svc_x eq 'svc_phone' ) {
1016 push @svc, new FS::svc_phone ( {
1017 'svcpart' => $svcpart,
1018 map { $_ => $packet->{$_} }
1019 qw( countrycode phonenum sip_password pin ),
1022 } elsif ( $svc_x eq 'svc_pbx' ) {
1024 push @svc, new FS::svc_pbx ( {
1025 'svcpart' => $svcpart,
1026 map { $_ => $packet->{$_} }
1031 die "unknown signup service $svc_x";
1036 foreach my $svc ( @svc ) {
1037 my $y = $svc->setdefault; # arguably should be in new method
1038 return { 'error' => $y } if $y && !ref($y);
1039 #$error = $svc->check;
1040 #return { 'error' => $error } if $error;
1044 tie my %hash, 'Tie::RefHash';
1045 $hash{ $cust_pkg } = \@svc;
1049 if ( $invoicing_list[0] && $packet->{'_password'} ) {
1051 new FS::contact { 'first' => $cust_main->first,
1052 'last' => $cust_main->get('last'),
1053 '_password' => $packet->{'_password'},
1054 'emailaddress' => $invoicing_list[0],
1055 'selfservice_access' => 'Y',
1060 my $error = $cust_main->insert(
1065 return { 'error' => $error } if $error;
1067 my $session = { 'custnum' => $cust_main->custnum };
1071 $session_id = sha512_hex(time(). {}. rand(). $$)
1072 } until ( ! defined _myaccount_cache->get($session_id) ); #just in case
1074 _myaccount_cache->set( $session_id, $session, '1 hour' ); # 1 hour?
1076 my %return = ( 'error' => '',
1077 'signup_service' => $svc_x,
1078 'custnum' => $cust_main->custnum,
1079 'session_id' => $session_id,
1080 map { $_ => $cust_main->$_ } qw( first last company ),
1085 $return{'svcnum'} = $svc[0]->svcnum;
1087 if ( $svc_x eq 'svc_acct' ) {
1088 $return{$_} = $svc[0]->$_() for qw( username _password );
1089 } elsif ( $svc_x eq 'svc_phone' ) {
1090 $return{$_} = $svc[0]->$_() for qw(countrycode phonenum sip_password pin);
1091 } elsif ( $svc_x eq 'svc_pbx' ) {
1092 #$return{$_} = $svc[0]->$_() for qw( ) #nothing yet
1094 return {'error' => "configuration error: unknown signup service $svc_x"};
1095 #die "unknown signup service $svc_x";
1096 # return an error that's visible to someone somewhere
1105 use vars qw( $myaccount_cache );
1106 sub _myaccount_cache {
1107 $myaccount_cache ||= new FS::ClientAPI_SessionCache( {
1108 'namespace' => 'FS::ClientAPI::MyAccount',
1112 sub capture_payment {
1115 warn "$me capture_payment called on $packet\n" if $DEBUG;
1118 # identify processor/gateway from called back URL
1121 my $conf = new FS::Conf;
1123 my $payment_gateway;
1124 if ( my $gwnum = $conf->config('selfservice-payment_gateway') ) {
1125 $payment_gateway = qsearchs('payment_gateway', { 'gatewaynum' => $gwnum })
1126 or die "configured gatewaynum $gwnum not found!";
1129 my $url = $packet->{url};
1131 $payment_gateway = qsearchs('payment_gateway',
1132 { 'gateway_callback_url' => popurl(0, $url) }
1134 if (!$payment_gateway) {
1136 my ( $processor, $login, $password, $action, @bop_options ) =
1137 $conf->config('business-onlinepayment');
1138 $action ||= 'normal authorization';
1139 pop @bop_options if scalar(@bop_options) % 2 && $bop_options[-1] =~ /^\s*$/;
1140 die "No real-time processor is enabled - ".
1141 "did you set the business-onlinepayment configuration value?\n"
1144 $payment_gateway = new FS::payment_gateway( {
1145 gateway_namespace => $conf->config('business-onlinepayment-namespace'),
1146 gateway_module => $processor,
1147 gateway_username => $login,
1148 gateway_password => $password,
1149 gateway_action => $action,
1150 options => [ ( @bop_options ) ],
1155 die "No real-time third party processor is enabled - ".
1156 "did you set the business-onlinepayment configuration value?\n*"
1157 unless $payment_gateway->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
1160 # locate pending transaction
1163 eval "use Business::OnlineThirdPartyPayment";
1167 new Business::OnlineThirdPartyPayment( $payment_gateway->gateway_module,
1168 @{ [ $payment_gateway->options ] },
1171 my $paypendingnum = $transaction->reference($packet->{data});
1173 my $cust_pay_pending =
1174 qsearchs('cust_pay_pending', { paypendingnum => $paypendingnum } );
1176 unless ($cust_pay_pending) {
1177 my $bill_error = "No payment is being processed with id $paypendingnum".
1178 "; Transaction aborted.";
1179 return { error => '_decline', bill_error => $bill_error };
1182 if ($cust_pay_pending->status ne 'thirdparty') {
1183 my $bill_error = "Payment with id $paypendingnum is not thirdparty, but ".
1184 $cust_pay_pending->status. "; Transaction aborted.";
1185 return { error => '_decline', bill_error => $bill_error };
1188 my $cust_main = $cust_pay_pending->cust_main;
1189 if ( $packet->{cancel} ) {
1190 # the user has chosen not to make this payment
1191 # (probably should be a separate API call, but I don't want to duplicate
1192 # all of the above...which should eventually go away)
1193 my $error = $cust_pay_pending->delete;
1194 # don't show any errors related to this; they're not meaningful
1195 warn "error canceling pending payment $paypendingnum: $error\n" if $error;
1196 return { 'error' => '_cancel',
1197 'session_id' => $cust_pay_pending->session_id };
1199 # create the payment
1201 $cust_main->realtime_botpp_capture( $cust_pay_pending,
1206 return { 'error' => ( $bill_error->{bill_error} ? '_decline' : '' ),
1213 =item get_agentnum PACKET
1215 Given a PACKET from the signup server, looks up the agentnum to use for signing
1216 up a customer. This will use 'session_id' if the agent is authenticated,
1217 otherwise 'agentnum', otherwise the 'signup_server-default_agentnum' config. If
1218 the agent can't be found, returns an error packet.
1224 my $conf = new FS::Conf;
1226 if ( exists $packet->{'session_id'} ) {
1227 my $cache = new FS::ClientAPI_SessionCache( {
1228 'namespace' => 'FS::ClientAPI::Agent',
1230 my $session = $cache->get($packet->{'session_id'});
1232 $agentnum = $session->{'agentnum'};
1234 return { 'error' => "Can't resume session" }; #better error message
1237 $agentnum = $packet->{agentnum}
1238 || $conf->config('signup_server-default_agentnum');
1240 if ( $agentnum and FS::agent->count('agentnum = ?', $agentnum) ) {
1243 return { 'error' => 'Signup is not configured' };
1246 =item new_prospect PACKET
1248 Creates a new L<FS::prospect_main> entry. PACKET must contain:
1250 - either agentnum or session_id; if not, signup_server-default_agentnum will
1251 be used and must not be empty
1253 - either refnum or referral_title; if not, signup_server-default_refnum will
1254 be used and must not be empty
1256 - last and first (names), and optionally company and title
1258 - address1, city, state, country, zip, and optionally address2
1262 and can also contain:
1264 - one or more of phone_daytime, phone_night, phone_mobile, and phone_fax
1266 - a 'comment' (will be attached to the contact)
1268 State and country will be normalized to Freeside state/country codes if
1276 warn "$me new_prospect called\n".Dumper($packet) if $DEBUG;
1278 my $oldAutoCommit = $FS::UID::AutoCommit;
1279 local $FS::UID::AutoCommit = 0;
1281 my $conf = FS::Conf->new;
1285 my $agentnum = get_agentnum($packet);
1286 return $agentnum if ref $agentnum;
1288 if ( my $title = $packet->{referral_title} ) {
1289 my $part_referral = qsearchs('part_referral', {
1290 'agentnum' => $agentnum,
1293 $part_referral ||= qsearchs('part_referral', {
1297 if (!$part_referral) {
1298 return { error => "Unknown referral type: '$title'" };
1300 $refnum = $part_referral->refnum;
1301 } elsif ( $packet->{refnum} ) {
1302 $refnum = $packet->{refnum};
1304 $refnum ||= $conf->config('signup_server-default_refnum');
1305 return { error => "Signup referral type is not configured" } if !$refnum;
1307 my $prospect = FS::prospect_main->new({
1308 'agentnum' => $agentnum,
1309 'refnum' => $refnum,
1310 'company' => $packet->{company},
1313 my $location = FS::cust_location->new;
1314 foreach ( qw(address1 address2 city county zip ) ) {
1315 $location->set($_, $packet->{$_});
1317 # normalize country and state if they're not already ISO codes
1318 # easier than doing it on the client side--we already have the tables here
1319 my $country = $packet->{country};
1320 my $state = $packet->{state};
1321 if (length($country) > 2) {
1322 # it likes title case
1323 $country = join(' ', map ucfirst, split(/\s+/, $country));
1324 my $lsc = Locale::SubCountry->new($country);
1326 $country = uc($lsc->country_code);
1328 if ($lsc->has_sub_countries) {
1329 if ( $lsc->full_name($state) eq 'unknown' ) {
1330 # then we were probably given a full name, so resolve it
1331 $state = $lsc->code($state);
1332 if ( $state eq 'unknown' ) {
1333 # doesn't resolve as a full name either, return an error
1334 $error = "Unknown state: ".$packet->{state};
1336 $state = uc($state);
1339 } # else state doesn't matter
1341 # couldn't find the country in LSC
1342 $error = "Unknown country: $country";
1345 $location->set('country', $country);
1346 $location->set('state', $state);
1347 $prospect->set('cust_location', $location);
1349 $error ||= $prospect->insert; # also does location
1350 return { error => $error } if $error;
1352 my $contact = FS::contact->new({
1353 prospectnum => $prospect->prospectnum,
1354 locationnum => $location->locationnum,
1355 invoice_dest => 'Y',
1357 # use emailaddress pseudo-field behavior here
1358 foreach (qw(last first title emailaddress comment)) {
1359 $contact->set($_, $packet->{$_});
1361 $error = $contact->insert;
1363 $dbh->rollback if $oldAutoCommit;
1364 return { error => $error };
1367 foreach my $phone_type (qsearch('phone_type', {})) {
1368 my $key = 'phone_' . lc($phone_type->typename);
1369 my $phonenum = $packet->{$key};
1371 # just to not have to supply country code from the other end
1372 my $number = Number::Phone->new($location->country, $phonenum);
1374 $error = 'invalid phone number';
1376 my $phone = FS::contact_phone->new({
1377 contactnum => $contact->contactnum,
1378 phonenum => $phonenum,
1379 countrycode => $number->country_code,
1380 phonetypenum => $phone_type->phonetypenum,
1382 $error = $phone->insert;
1385 $dbh->rollback if $oldAutoCommit;
1386 return { error => $phone_type->typename . ' phone: ' . $error };
1389 } # foreach $phone_type
1391 $dbh->commit if $oldAutoCommit;
1392 return { prospectnum => $prospect->prospectnum };