1 package FS::ClientAPI::Signup;
4 use vars qw( $DEBUG $me );
7 use Digest::SHA qw(sha512_hex);
9 use FS::Record qw(qsearch qsearchs dbdef);
10 use FS::CGI qw(popurl);
11 use FS::Msgcat qw(gettext);
12 use FS::Misc qw(card_types);
13 use FS::ClientAPI_SessionCache;
15 use FS::cust_main_county;
30 $me = '[FS::ClientAPI::Signup]';
33 warn "$me clear_cache called\n" if $DEBUG;
34 my $cache = new FS::ClientAPI_SessionCache( {
35 'namespace' => 'FS::ClientAPI::Signup',
44 warn "$me signup_info called on $packet\n" if $DEBUG;
46 my $conf = new FS::Conf;
47 my $svc_x = $conf->config('signup_server-service') || 'svc_acct';
49 my $cache = new FS::ClientAPI_SessionCache( {
50 'namespace' => 'FS::ClientAPI::Signup',
52 my $signup_info_cache = $cache->get('signup_info_cache');
54 if ( $signup_info_cache ) {
56 warn "$me loading cached signup info\n" if $DEBUG > 1;
60 warn "$me populating signup info cache\n" if $DEBUG > 1;
62 my $agentnum2part_pkg =
66 my $href = $agent->pkgpart_hashref;
69 map { { 'payby' => [ $_->payby ],
70 'freq_pretty' => $_->freq_pretty,
71 'options' => { $_->options },
74 grep { $_->svcpart($svc_x)
75 && ( $href->{ $_->pkgpart }
77 && $_->agentnum == $agent->agentnum
81 qsearch( 'part_pkg', { 'disabled' => '' } )
83 } qsearch('agent', { 'disabled' => '' })
86 my $msgcat = { map { $_=>gettext($_) }
87 qw( passwords_dont_match invalid_card unknown_card_type
88 not_a empty_password illegal_or_empty_text )
90 warn "msgcat: ". Dumper($msgcat). "\n" if $DEBUG > 2;
92 my $label = { map { $_ => FS::Msgcat::_gettext($_) }
93 qw( stateid stateid_state )
95 warn "label: ". Dumper($label). "\n" if $DEBUG > 2;
97 my @agent_fields = qw( agentnum agent );
99 my @bools = qw( emailinvoiceonly security_phrase );
101 my @signup_bools = qw( no_company recommend_daytime recommend_email );
103 my @signup_server_scalars = qw( default_pkgpart default_svcpart default_domsvc );
105 my @selfservice_textareas = qw( head body_header body_footer );
107 my @selfservice_scalars = qw(
108 body_bgcolor box_bgcolor
109 text_color link_color vlink_color hlink_color alink_color
110 font title_color title_align title_size menu_bgcolor menu_fontsize
113 #XXX my @selfservice_bools = qw(
114 # menu_skipblanks menu_skipheadings menu_nounderline
117 #my $selfservice_binaries = qw(
118 # title_left_image title_right_image
119 # menu_top_image menu_body_image menu_bottom_image
122 $signup_info_cache = {
124 'cust_main_county' => [ map $_->hashref,
125 qsearch('cust_main_county', {} )
128 'agent' => [ map { my $agent = $_;
129 +{ map { $_ => $agent->get($_) } @agent_fields }
131 qsearch('agent', { 'disabled' => '' } )
134 'part_referral' => [ map $_->hashref,
135 qsearch('part_referral', { 'disabled' => '' } )
138 'agentnum2part_pkg' => $agentnum2part_pkg,
140 'svc_acct_pop' => [ map $_->hashref, qsearch('svc_acct_pop',{} ) ],
142 'emailinvoiceonly' => $conf->exists('emailinvoiceonly'),
144 'security_phrase' => $conf->exists('security_phrase'),
146 'nomadix' => $conf->exists('signup_server-nomadix'),
148 'payby' => [ $conf->config('signup_server-payby') ],
150 'payby_longname' => [ map { FS::payby->longname($_) }
151 $conf->config('signup_server-payby') ],
153 'card_types' => card_types(),
155 ( map { $_ => $conf->exists("signup-$_") } @signup_bools ),
157 ( map { $_ => scalar($conf->config("signup_server-$_")) }
158 @signup_server_scalars
161 ( map { $_ => join("\n", $conf->config("selfservice-$_")) }
162 @selfservice_textareas
164 ( map { $_ => scalar($conf->config("selfservice-$_")) }
168 #( map { $_ => scalar($conf->config_binary("selfservice-$_")) }
169 # @selfservice_binaries
172 'agentnum2part_pkg' => $agentnum2part_pkg,
173 'svc_acct_pop' => [ map $_->hashref, qsearch('svc_acct_pop',{} ) ],
174 'nomadix' => $conf->exists('signup_server-nomadix'),
175 'payby' => [ $conf->config('signup_server-payby') ],
176 'card_types' => card_types(),
177 'paytypes' => [ @FS::cust_main::paytypes ],
179 'require_cvv' => $conf->exists('signup-require_cvv'),
180 'stateid_enabled' => $conf->exists('show_stateid'),
181 'paystate_enabled' => $conf->exists('show_bankstate'),
182 'exempt_groups' => [ grep /\S/, $conf->config('tax-cust_exempt-groups') ],
186 'statedefault' => scalar($conf->config('statedefault')) || 'CA',
187 'countrydefault' => scalar($conf->config('countrydefault')) || 'US',
188 'refnum' => scalar($conf->config('signup_server-default_refnum')),
189 'signup_service' => $svc_x,
190 'company_name' => scalar($conf->config('company_name')),
192 'logo' => scalar($conf->config_binary('logo.png')),
193 'prepaid_template_custnum' => $conf->exists('signup_server-prepaid-template-custnum'),
196 $cache->set('signup_info_cache', $signup_info_cache);
200 my $signup_info = { %$signup_info_cache };
201 warn "$me signup info loaded\n" if $DEBUG > 1;
202 warn Dumper($signup_info). "\n" if $DEBUG > 2;
204 my @addl = qw( signup_server-classnum2 signup_server-classnum3 );
206 if ( grep { $conf->exists($_) } @addl ) {
208 $signup_info->{optional_packages} = [];
210 foreach my $addl ( @addl ) {
212 warn "$me adding optional package info\n" if $DEBUG > 1;
214 my $classnum = $conf->config($addl) or next;
217 'freq_pretty' => $_->freq_pretty,
218 'options' => { $_->options },
222 qsearch( 'part_pkg', { classnum => $classnum } );
224 push @{$signup_info->{optional_packages}}, \@pkgs;
226 warn "$me done adding opt. package info for $classnum\n" if $DEBUG > 1;
232 my $agentnum = $packet->{'agentnum'}
233 || $conf->config('signup_server-default_agentnum');
234 $agentnum =~ /^(\d*)$/ or die "illegal agentnum";
238 if ( exists $packet->{'session_id'} ) {
240 warn "$me loading agent session\n" if $DEBUG > 1;
241 my $cache = new FS::ClientAPI_SessionCache( {
242 'namespace' => 'FS::ClientAPI::Agent',
244 $session = $cache->get($packet->{'session_id'});
246 $agentnum = $session->{'agentnum'};
248 return { 'error' => "Can't resume session" }; #better error message
250 warn "$me done loading agent session\n" if $DEBUG > 1;
252 } elsif ( exists $packet->{'customer_session_id'} ) {
254 warn "$me loading customer session\n" if $DEBUG > 1;
255 my $cache = new FS::ClientAPI_SessionCache( {
256 'namespace' => 'FS::ClientAPI::MyAccount',
258 $session = $cache->get($packet->{'customer_session_id'});
260 my $custnum = $session->{'custnum'};
261 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum });
262 return { 'error' => "Can't find your customer record" } unless $cust_main;
263 $agentnum = $cust_main->agentnum;
265 return { 'error' => "Can't resume session" }; #better error message
267 warn "$me done loading customer session\n" if $DEBUG > 1;
271 $signup_info->{'part_pkg'} = [];
273 if ( $packet->{'reg_code'} ) {
275 warn "$me setting package list via reg_code\n" if $DEBUG > 1;
277 $signup_info->{'part_pkg'} =
278 [ map { { 'payby' => [ $_->payby ],
279 'freq_pretty' => $_->freq_pretty,
280 'options' => { $_->options },
284 grep { $_->svcpart($svc_x) }
286 qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
287 'agentnum' => $agentnum, } )
291 $signup_info->{'error'} = 'Unknown registration code'
292 unless @{ $signup_info->{'part_pkg'} };
294 warn "$me done setting package list via reg_code\n" if $DEBUG > 1;
296 } elsif ( $packet->{'promo_code'} ) {
298 warn "$me setting package list via promo_code\n" if $DEBUG > 1;
300 $signup_info->{'part_pkg'} =
301 [ map { { 'payby' => [ $_->payby ],
302 'freq_pretty' => $_->freq_pretty,
303 'options' => { $_->options },
306 grep { $_->svcpart($svc_x) }
307 qsearch( 'part_pkg', { 'promo_code' => {
309 value=>$packet->{'promo_code'}
311 'disabled' => '', } )
314 $signup_info->{'error'} = 'Unknown promotional code'
315 unless @{ $signup_info->{'part_pkg'} };
317 warn "$me done setting package list via promo_code\n" if $DEBUG > 1;
322 warn "$me setting agent-specific payment flag\n" if $DEBUG > 1;
323 my $agent = qsearchs('agent', { 'agentnum' => $agentnum } )
324 or return { 'error' => "Self-service agent #$agentnum does not exist" };
325 warn "$me has agent $agent\n" if $DEBUG > 1;
326 my @paybys = @{ $signup_info->{'payby'} };
327 $signup_info->{'hide_payment_fields'} = [];
329 my $gatewaynum = $conf->config('selfservice-payment_gateway');
332 $force_gateway = qsearchs('payment_gateway', { gatewaynum => $gatewaynum });
333 warn "using forced gateway #$gatewaynum - " .
334 $force_gateway->gateway_username . '@' . $force_gateway->gateway_module
336 die "configured gatewaynum $gatewaynum not found!" if !$force_gateway;
338 foreach my $payby (@paybys) {
339 warn "$me checking $payby payment fields\n" if $DEBUG > 1;
341 if ( FS::payby->realtime($payby) ) {
342 my $gateway = $force_gateway ||
343 $agent->payment_gateway( 'method' => FS::payby->payby2bop($payby),
346 if ( $gateway && $gateway->gateway_namespace
347 eq 'Business::OnlineThirdPartyPayment'
349 warn "$me hiding $payby payment fields\n" if $DEBUG > 1;
353 push @{$signup_info->{'hide_payment_fields'}}, $hide;
355 warn "$me done setting agent-specific payment flag\n" if $DEBUG > 1;
357 warn "$me setting agent-specific package list\n" if $DEBUG > 1;
358 $signup_info->{'part_pkg'} = $signup_info->{'agentnum2part_pkg'}{$agentnum}
359 unless @{ $signup_info->{'part_pkg'} };
360 warn "$me done setting agent-specific package list\n" if $DEBUG > 1;
362 warn "$me setting agent-specific adv. source list\n" if $DEBUG > 1;
363 $signup_info->{'part_referral'} =
367 'table' => 'part_referral',
368 'hashref' => { 'disabled' => '' },
369 'extra_sql' => "AND ( agentnum = $agentnum ".
370 " OR agentnum IS NULL ) ",
374 warn "$me done setting agent-specific adv. source list\n" if $DEBUG > 1;
376 $signup_info->{'agent_name'} = $agent->agent;
378 $signup_info->{'company_name'} = $conf->config('company_name', $agentnum);
380 #some of the above could probably be cached, too
382 my $signup_info_cache_agent = $cache->get("signup_info_cache_agent$agentnum");
384 if ( $signup_info_cache_agent ) {
386 warn "$me loading cached signup info for agentnum $agentnum\n"
391 warn "$me populating signup info cache for agentnum $agentnum\n"
394 $signup_info_cache_agent = {
395 #( map { $_ => scalar( $conf->config($_, $agentnum) ) }
396 # qw( company_name ) ),
397 ( map { $_ => scalar( $conf->config("selfservice-$_", $agentnum ) ) }
398 qw( body_bgcolor box_bgcolor menu_bgcolor ) ),
399 ( map { $_ => join("\n", $conf->config("selfservice-$_", $agentnum ) ) }
400 qw( head body_header body_footer ) ),
401 ( map { $_ => join("\n", $conf->config("signup_server-$_", $agentnum ) ) }
402 qw( terms_of_service ) ),
404 ( map { $_ => scalar($conf->exists($_, $agentnum)) }
405 qw(cust_main-require_phone agent-ship_address) ),
408 if ( $signup_info_cache_agent->{'agent-ship_address'}
409 && $agent->agent_cust_main ) {
411 my $cust_main = $agent->agent_cust_main;
412 my $location = $cust_main->ship_location;
413 $signup_info_cache_agent->{"ship_$_"} = $location->get($_)
414 foreach qw( address1 city county state zip country );
418 $cache->set("signup_info_cache_agent$agentnum", $signup_info_cache_agent);
422 $signup_info->{$_} = $signup_info_cache_agent->{$_}
423 foreach keys %$signup_info_cache_agent;
427 # delete $signup_info->{'part_pkg'};
430 warn "$me sorting package list\n" if $DEBUG > 1;
431 $signup_info->{'part_pkg'} = [ sort { $a->{pkg} cmp $b->{pkg} } # case?
432 @{ $signup_info->{'part_pkg'} }
434 warn "$me done sorting package list\n" if $DEBUG > 1;
436 if ( exists $packet->{'session_id'} ) {
437 my $agent_signup_info = { %$signup_info };
438 delete $agent_signup_info->{agentnum2part_pkg};
439 $agent_signup_info->{'agent'} = $session->{'agent'};
440 return $agent_signup_info;
442 elsif ( exists $packet->{'keys'} ) {
443 my @keys = @{ $packet->{'keys'} };
444 return { map { $_ => $signup_info->{$_} } @keys };
452 sub domain_select_hash {
457 if ($packet->{pkgpart}) {
458 my $part_pkg = qsearchs('part_pkg' => { 'pkgpart' => $packet->{pkgpart} } );
459 #$packet->{svcpart} = $part_pkg->svcpart('svc_acct')
460 $packet->{svcpart} = $part_pkg->svcpart
464 if ($packet->{svcpart}) {
465 my $part_svc = qsearchs('part_svc' => { 'svcpart' => $packet->{svcpart} } );
466 $response->{'domsvc'} = $part_svc->part_svc_column('domsvc')->columnvalue
467 if ($part_svc && $part_svc->part_svc_column('domsvc')->columnflag eq 'D');
470 $response->{'domains'}
471 = { domain_select_hash FS::svc_acct( map { $_ => $packet->{$_} }
481 my $conf = new FS::Conf;
482 my $svc_x = $conf->config('signup_server-service') || 'svc_acct';
484 if ( $svc_x eq 'svc_acct' ) {
486 #things that aren't necessary in base class, but are for signup server
487 #return "Passwords don't match"
488 # if $hashref->{'_password'} ne $hashref->{'_password2'}
489 return { 'error' => gettext('empty_password') }
490 unless length($packet->{'_password'});
491 # a bit inefficient for large numbers of pops
492 return { 'error' => gettext('no_access_number_selected') }
493 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
496 elsif ( $svc_x eq 'svc_pbx' ) {
497 #possibly some validation will be needed
501 if ( exists $packet->{'session_id'} ) {
502 my $cache = new FS::ClientAPI_SessionCache( {
503 'namespace' => 'FS::ClientAPI::Agent',
505 my $session = $cache->get($packet->{'session_id'});
507 $agentnum = $session->{'agentnum'};
509 return { 'error' => "Can't resume session" }; #better error message
512 $agentnum = $packet->{agentnum}
513 || $conf->config('signup_server-default_agentnum');
516 my ($bill_hash, $ship_hash);
517 foreach my $f (FS::cust_main->location_fields) {
518 # avoid having to change this in front-end code
519 $bill_hash->{$f} = $packet->{"bill_$f"} || $packet->{$f};
520 $ship_hash->{$f} = $packet->{"ship_$f"};
523 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
524 # common that are still here and library them.
527 'agentnum' => $agentnum,
528 'refnum' => $packet->{refnum}
529 || $conf->config('signup_server-default_refnum'),
530 'tagnum' => [ FS::part_tag->default_tags ],
532 ( map { $_ => $packet->{$_} } qw(
534 ss stateid stateid_state
537 payinfo paycvv paydate payname paystate paytype
538 paystart_month paystart_year payissue
541 referral_custnum comments
547 my $template_custnum = $conf->config('signup_server-prepaid-template-custnum');
549 if ( $template_custnum && $packet->{prepaid_shortform} ) {
551 my $template_cust = qsearchs('cust_main', { 'custnum' => $template_custnum } );
552 return { 'error' => 'Configuration error' } unless $template_cust;
553 $cust_main = new FS::cust_main ( {
555 map { $_ => $template_cust->$_ } qw(
556 last first company daytime night fax mobile
560 $bill_hash = { $template_cust->bill_location->location_hash };
561 $ship_hash = { $template_cust->ship_location->location_hash };
565 $cust_main = new FS::cust_main ( {
567 map { $_ => $packet->{$_} } qw(
568 last first company daytime night fax mobile
574 my $bill_location = FS::cust_location->new($bill_hash);
576 my $agent = qsearchs('agent', { 'agentnum' => $agentnum } );
577 if ( $conf->exists('agent-ship_address', $agentnum)
578 && $agent->agent_custnum ) {
580 my $agent_cust_main = $agent->agent_cust_main;
581 my $prefix = length($agent_cust_main->ship_last) ? 'ship_' : '';
582 $ship_location = FS::cust_location->new({
583 $agent_cust_main->ship_location->location_hash
587 # we don't have an equivalent of the "same" checkbox in selfservice
588 # so is there a ship address, and if so, is it different from the billing
590 elsif ( length($ship_hash->{address1}) > 0 and
591 grep { $bill_hash->{$_} ne $ship_hash->{$_} } keys(%$ship_hash)
594 $ship_location = FS::cust_location->new( $ship_hash );
598 $ship_location = $bill_location;
601 $cust_main->set('bill_location' => $bill_location);
602 $cust_main->set('ship_location' => $ship_location);
604 return { 'error' => "Illegal payment type" }
605 unless grep { $_ eq $packet->{'payby'} }
606 $conf->config('signup_server-payby');
608 if (FS::payby->realtime($packet->{payby})
609 and not $conf->exists('signup_server-third_party_as_card')) {
610 my $payby = $packet->{payby};
612 my $agent = qsearchs('agent', { 'agentnum' => $agentnum });
613 return { 'error' => "Unknown reseller" }
617 my $gatewaynum = $conf->config('selfservice-payment_gateway');
619 $gw = qsearchs('payment_gateway', { gatewaynum => $gatewaynum });
620 die "configured gatewaynum $gatewaynum not found!" if !$gw;
623 $gw = $agent->payment_gateway( 'method' => FS::payby->payby2bop($payby),
628 $cust_main->payby('BILL') # MCRD better? no, that's for something else
629 if $gw && $gw->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
632 return { 'error' => "CVV2 is required" }
633 if $cust_main->payby =~ /^(CARD|DCRD)$/
634 && ! $cust_main->paycvv
635 && $conf->exists('signup-require_cvv');
637 $cust_main->payinfo($cust_main->daytime)
638 if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
640 my @invoicing_list = $packet->{'invoicing_list'}
641 ? split( /\s*\,\s*/, $packet->{'invoicing_list'} )
644 my %insert_options = ();
646 my @exempt_groups = grep /\S/, $conf->config('tax-cust_exempt-groups');
647 my @tax_exempt = grep { $packet->{"tax_$_"} eq 'Y' } @exempt_groups;
648 $insert_options{'tax_exemption'} = {
649 map { $_ => $packet->{"tax_$_".'_num'} } @tax_exempt
652 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
654 return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
657 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
658 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
659 my $svcpart = $part_pkg->svcpart($svc_x);
662 if ( $packet->{'reg_code'} ) {
663 $reg_code = qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
664 'agentnum' => $agentnum, } )
665 or return { 'error' => 'Unknown registration code' };
668 my $cust_pkg = new FS::cust_pkg ( {
669 #later#'custnum' => $custnum,
670 'pkgpart' => $packet->{'pkgpart'},
671 'promo_code' => $packet->{'promo_code'},
672 'reg_code' => $packet->{'reg_code'},
674 #my $error = $cust_pkg->check;
675 #return { 'error' => $error } if $error;
677 #should be all auto-magic and shit
679 if ( $svc_x eq 'svc_acct' ) {
681 my $svc = new FS::svc_acct {
682 'svcpart' => $svcpart,
683 map { $_ => $packet->{$_} }
684 qw( username _password sec_phrase popnum domsvc ),
689 while ( exists($packet->{"snarf_machine$snarfnum"})
690 && length($packet->{"snarf_machine$snarfnum"}) ) {
691 my $acct_snarf = new FS::acct_snarf ( {
692 'machine' => $packet->{"snarf_machine$snarfnum"},
693 'protocol' => $packet->{"snarf_protocol$snarfnum"},
694 'username' => $packet->{"snarf_username$snarfnum"},
695 '_password' => $packet->{"snarf_password$snarfnum"},
698 push @acct_snarf, $acct_snarf;
700 $svc->child_objects( \@acct_snarf );
703 } elsif ( $svc_x eq 'svc_phone' ) {
705 push @svc, new FS::svc_phone ( {
706 'svcpart' => $svcpart,
707 map { $_ => $packet->{$_} }
708 qw( countrycode phonenum sip_password pin ),
711 } elsif ( $svc_x eq 'svc_pbx' ) {
713 push @svc, new FS::svc_pbx ( {
714 'svcpart' => $svcpart,
715 map { $_ => $packet->{$_} }
720 die "unknown signup service $svc_x";
723 if ($packet->{'mac_addr'} && $conf->exists('signup_server-mac_addr_svcparts'))
726 my %mac_addr_svcparts = map { $_ => 1 }
727 $conf->config('signup_server-mac_addr_svcparts');
728 my @pkg_svc = grep { $_->quantity && $mac_addr_svcparts{$_->svcpart} }
729 $cust_pkg->part_pkg->pkg_svc;
731 return { 'error' => 'No service defined to assign mac address' }
734 my $svc = new FS::svc_acct {
735 'svcpart' => $pkg_svc[0]->svcpart, #multiple matches? alas..
736 'username' => $packet->{'mac_addr'},
737 '_password' => '', #blank as requested (set passwordmin to 0)
744 foreach my $svc ( @svc ) {
745 my $y = $svc->setdefault; # arguably should be in new method
746 return { 'error' => $y } if $y && !ref($y);
747 #$error = $svc->check;
748 #return { 'error' => $error } if $error;
751 #setup a job dependancy to delay provisioning
752 my $placeholder = new FS::queue ( {
753 'job' => 'FS::ClientAPI::Signup::__placeholder',
754 'status' => 'locked',
756 my $error = $placeholder->insert;
757 return { 'error' => $error } if $error;
760 tie my %hash, 'Tie::RefHash';
761 %hash = ( $cust_pkg => \@svc );
763 $error = $cust_main->insert(
766 'depend_jobnum' => $placeholder->jobnum,
770 my $perror = $placeholder->delete;
771 $error .= " (Additionally, error removing placeholder: $perror)" if $perror;
772 return { 'error' => $error };
775 if ( $conf->exists('signup_server-realtime') ) {
777 #warn "$me Billing customer...\n" if $Debug;
779 my $bill_error = $cust_main->bill( 'depend_jobnum'=>$placeholder->jobnum );
780 #warn "$me error billing new customer: $bill_error"
783 $bill_error = $cust_main->apply_payments_and_credits;
784 #warn "$me error applying payments and credits for".
785 # " new customer: $bill_error"
788 unless ( $packet->{payby} eq 'PREPAY' ) {
789 $bill_error = $cust_main->realtime_collect(
790 method => FS::payby->payby2bop( $packet->{payby} ),
791 depend_jobnum => $placeholder->jobnum,
794 #warn "$me error collecting from new customer: $bill_error"
798 if ($bill_error && ref($bill_error) eq 'HASH') {
799 return { 'error' => '_collect',
800 ( map { $_ => $bill_error->{$_} }
801 qw(popup_url reference collectitems)
803 amount => $cust_main->balance,
807 $bill_error = $cust_main->apply_payments_and_credits;
808 #warn "$me error applying payments and credits for".
809 # " new customer: $bill_error"
812 if ( $cust_main->balance > 0 ) {
814 #this makes sense. credit is "un-doing" the invoice
815 $cust_main->credit( $cust_main->balance, 'signup server decline',
816 'reason_type' => $conf->config('signup_credit_type'),
818 $cust_main->apply_credits;
820 #should check list for errors...
821 #$cust_main->suspend;
822 local $FS::svc_Common::noexport_hack = 1;
823 $cust_main->cancel('quiet'=>1);
825 my $perror = $placeholder->depended_delete;
826 warn "error removing provisioning jobs after decline: $perror" if $perror;
828 $perror = $placeholder->delete;
829 warn "error removing placeholder after decline: $perror" if $perror;
832 return { 'error' => '_decline' };
838 $error = $reg_code->delete;
839 return { 'error' => $error } if $error;
842 $error = $placeholder->delete;
843 return { 'error' => $error } if $error;
845 if ( $conf->exists('signup-duplicate_cc-warn_hours') ) {
846 my $hours = $conf->config('signup-duplicate_cc-warn_hours');
847 my $ban = new FS::banned_pay $cust_main->_new_banned_pay_hashref;
848 $ban->end_date( int( time + $hours*3600 ) );
849 $ban->bantype('warn');
850 $ban->reason('signup-duplicate_cc-warn_hours');
851 $error = $ban->insert;
852 warn "WARNING: error inserting temporary banned_pay for ".
853 " signup-duplicate_cc-warn_hours (proceeding anyway): $error"
857 my %return = ( 'error' => '',
858 'signup_service' => $svc_x,
859 'custnum' => $cust_main->custnum,
864 $return{'svcnum'} = $svc[0]->svcnum;
866 if ( $svc_x eq 'svc_acct' ) {
867 $return{$_} = $svc[0]->$_() for qw( username _password );
868 } elsif ( $svc_x eq 'svc_phone' ) {
869 $return{$_} = $svc[0]->$_() for qw(countrycode phonenum sip_password pin);
870 } elsif ( $svc_x eq 'svc_pbx' ) {
871 #$return{$_} = $svc[0]->$_() for qw( ) #nothing yet
873 return {'error' => "configuration error: unknown signup service $svc_x"};
874 #die "unknown signup service $svc_x";
875 # return an error that's visible to someone somewhere
884 #false laziness w/ above
885 # fresh restart to support "free account" portals with 3.x/4.x-style
886 # addressless accounts
887 # and a contact (for self-service login)
888 sub new_customer_minimal {
891 my $conf = new FS::Conf;
892 my $svc_x = $conf->config('signup_server-service') || 'svc_acct';
894 if ( $svc_x eq 'svc_acct' ) {
896 #things that aren't necessary in base class, but are for signup server
897 #return "Passwords don't match"
898 # if $hashref->{'_password'} ne $hashref->{'_password2'}
899 return { 'error' => gettext('empty_password') }
900 unless length($packet->{'_password'});
901 # a bit inefficient for large numbers of pops
902 return { 'error' => gettext('no_access_number_selected') }
903 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
906 elsif ( $svc_x eq 'svc_pbx' ) {
907 #possibly some validation will be needed
911 if ( exists $packet->{'session_id'} ) {
912 my $cache = new FS::ClientAPI_SessionCache( {
913 'namespace' => 'FS::ClientAPI::Agent',
915 my $session = $cache->get($packet->{'session_id'});
917 $agentnum = $session->{'agentnum'};
919 return { 'error' => "Can't resume session" }; #better error message
922 $agentnum = $packet->{agentnum}
923 || $conf->config('signup_server-default_agentnum');
926 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
927 # common that are still here and library them.
929 my $cust_main = new FS::cust_main ( {
930 'agentnum' => $agentnum,
931 'refnum' => $packet->{refnum}
932 || $conf->config('signup_server-default_refnum'),
933 'tagnum' => [ FS::part_tag->default_tags ],
936 map { $_ => $packet->{$_} } qw(
938 last first company daytime night fax mobile
944 my @invoicing_list = $packet->{'invoicing_list'}
945 ? split( /\s*\,\s*/, $packet->{'invoicing_list'} )
949 tie my %hash, 'Tie::RefHash', ();
952 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
958 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
959 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
960 my $svcpart = $part_pkg->svcpart($svc_x);
962 my $cust_pkg = new FS::cust_pkg ( {
963 #later#'custnum' => $custnum,
964 'pkgpart' => $packet->{'pkgpart'},
966 #my $error = $cust_pkg->check;
967 #return { 'error' => $error } if $error;
969 #should be all auto-magic and shit
970 if ( $svc_x eq 'svc_acct' ) {
972 my $svc = new FS::svc_acct {
973 'svcpart' => $svcpart,
974 map { $_ => $packet->{$_} }
975 qw( username _password sec_phrase popnum domsvc ),
980 } elsif ( $svc_x eq 'svc_phone' ) {
982 push @svc, new FS::svc_phone ( {
983 'svcpart' => $svcpart,
984 map { $_ => $packet->{$_} }
985 qw( countrycode phonenum sip_password pin ),
988 } elsif ( $svc_x eq 'svc_pbx' ) {
990 push @svc, new FS::svc_pbx ( {
991 'svcpart' => $svcpart,
992 map { $_ => $packet->{$_} }
997 die "unknown signup service $svc_x";
1000 foreach my $svc ( @svc ) {
1001 my $y = $svc->setdefault; # arguably should be in new method
1002 return { 'error' => $y } if $y && !ref($y);
1003 #$error = $svc->check;
1004 #return { 'error' => $error } if $error;
1008 tie my %hash, 'Tie::RefHash';
1009 $hash{ $cust_pkg } = \@svc;
1014 if ( $invoicing_list[0] && $packet->{'_password'} ) {
1016 new FS::contact { 'first' => $cust_main->first,
1017 'last' => $cust_main->get('last'),
1018 '_password' => $packet->{'_password'},
1019 'emailaddress' => $invoicing_list[0],
1020 'selfservice_access' => 'Y',
1025 my $error = $cust_main->insert(
1030 return { 'error' => $error } if $error;
1032 my $session = { 'custnum' => $cust_main->custnum };
1036 $session_id = sha1_hex(time(). {}. rand(). $$)
1037 } until ( ! defined _myaccount_cache->get($session_id) ); #just in case
1039 _cache->set( $session_id, $session, '1 hour' ); # 1 hour?
1041 my %return = ( 'error' => '',
1042 'signup_service' => $svc_x,
1043 'custnum' => $cust_main->custnum,
1044 'session_id' => $session_id,
1049 $return{'svcnum'} = $svc[0]->svcnum;
1051 if ( $svc_x eq 'svc_acct' ) {
1052 $return{$_} = $svc[0]->$_() for qw( username _password );
1053 } elsif ( $svc_x eq 'svc_phone' ) {
1054 $return{$_} = $svc[0]->$_() for qw(countrycode phonenum sip_password pin);
1055 } elsif ( $svc_x eq 'svc_pbx' ) {
1056 #$return{$_} = $svc[0]->$_() for qw( ) #nothing yet
1058 return {'error' => "configuration error: unknown signup service $svc_x"};
1059 #die "unknown signup service $svc_x";
1060 # return an error that's visible to someone somewhere
1069 use vars qw( $myaccount_cache );
1070 sub _myaccount_cache {
1071 $myaccount_cache ||= new FS::ClientAPI_SessionCache( {
1072 'namespace' => 'FS::ClientAPI::MyAccount',
1076 sub capture_payment {
1079 warn "$me capture_payment called on $packet\n" if $DEBUG;
1082 # identify processor/gateway from called back URL
1085 my $conf = new FS::Conf;
1087 my $payment_gateway;
1088 if ( my $gwnum = $conf->config('selfservice-payment_gateway') ) {
1089 $payment_gateway = qsearchs('payment_gateway', { 'gatewaynum' => $gwnum })
1090 or die "configured gatewaynum $gwnum not found!";
1093 my $url = $packet->{url};
1095 $payment_gateway = qsearchs('payment_gateway',
1096 { 'gateway_callback_url' => popurl(0, $url) }
1098 if (!$payment_gateway) {
1100 my ( $processor, $login, $password, $action, @bop_options ) =
1101 $conf->config('business-onlinepayment');
1102 $action ||= 'normal authorization';
1103 pop @bop_options if scalar(@bop_options) % 2 && $bop_options[-1] =~ /^\s*$/;
1104 die "No real-time processor is enabled - ".
1105 "did you set the business-onlinepayment configuration value?\n"
1108 $payment_gateway = new FS::payment_gateway( {
1109 gateway_namespace => $conf->config('business-onlinepayment-namespace'),
1110 gateway_module => $processor,
1111 gateway_username => $login,
1112 gateway_password => $password,
1113 gateway_action => $action,
1114 options => [ ( @bop_options ) ],
1119 die "No real-time third party processor is enabled - ".
1120 "did you set the business-onlinepayment configuration value?\n*"
1121 unless $payment_gateway->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
1124 # locate pending transaction
1127 eval "use Business::OnlineThirdPartyPayment";
1131 new Business::OnlineThirdPartyPayment( $payment_gateway->gateway_module,
1132 @{ [ $payment_gateway->options ] },
1135 my $paypendingnum = $transaction->reference($packet->{data});
1137 my $cust_pay_pending =
1138 qsearchs('cust_pay_pending', { paypendingnum => $paypendingnum } );
1140 unless ($cust_pay_pending) {
1141 my $bill_error = "No payment is being processed with id $paypendingnum".
1142 "; Transaction aborted.";
1143 return { error => '_decline', bill_error => $bill_error };
1146 if ($cust_pay_pending->status ne 'thirdparty') {
1147 my $bill_error = "Payment with id $paypendingnum is not thirdparty, but ".
1148 $cust_pay_pending->status. "; Transaction aborted.";
1149 return { error => '_decline', bill_error => $bill_error };
1152 my $cust_main = $cust_pay_pending->cust_main;
1153 if ( $packet->{cancel} ) {
1154 # the user has chosen not to make this payment
1155 # (probably should be a separate API call, but I don't want to duplicate
1156 # all of the above...which should eventually go away)
1157 my $error = $cust_pay_pending->delete;
1158 # don't show any errors related to this; they're not meaningful
1159 warn "error canceling pending payment $paypendingnum: $error\n" if $error;
1160 return { 'error' => '_cancel',
1161 'session_id' => $cust_pay_pending->session_id };
1163 # create the payment
1165 $cust_main->realtime_botpp_capture( $cust_pay_pending,
1170 return { 'error' => ( $bill_error->{bill_error} ? '_decline' : '' ),