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);
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;
31 $me = '[FS::ClientAPI::Signup]';
34 warn "$me clear_cache called\n" if $DEBUG;
35 my $cache = new FS::ClientAPI_SessionCache( {
36 'namespace' => 'FS::ClientAPI::Signup',
45 warn "$me signup_info called on $packet\n" if $DEBUG;
47 my $conf = new FS::Conf;
48 my $svc_x = $conf->config('signup_server-service') || 'svc_acct';
50 my $cache = new FS::ClientAPI_SessionCache( {
51 'namespace' => 'FS::ClientAPI::Signup',
53 my $signup_info_cache = $cache->get('signup_info_cache');
55 if ( $signup_info_cache ) {
57 warn "$me loading cached signup info\n" if $DEBUG > 1;
61 warn "$me populating signup info cache\n" if $DEBUG > 1;
63 my $agentnum2part_pkg =
67 my $href = $agent->pkgpart_hashref;
70 map { { 'payby' => [ $_->payby ],
71 'freq_pretty' => $_->freq_pretty,
72 'options' => { $_->options },
75 grep { $_->svcpart($svc_x)
76 && ( $href->{ $_->pkgpart }
78 && $_->agentnum == $agent->agentnum
82 qsearch( 'part_pkg', { 'disabled' => '' } )
84 } qsearch('agent', { 'disabled' => '' })
87 my $msgcat = { map { $_=>gettext($_) }
88 qw( passwords_dont_match invalid_card unknown_card_type
89 not_a empty_password illegal_or_empty_text )
91 warn "msgcat: ". Dumper($msgcat). "\n" if $DEBUG > 2;
93 my $label = { map { $_ => FS::Msgcat::_gettext($_) }
94 qw( stateid stateid_state )
96 warn "label: ". Dumper($label). "\n" if $DEBUG > 2;
98 my @agent_fields = qw( agentnum agent );
100 my @bools = qw( emailinvoiceonly security_phrase );
102 my @signup_bools = qw( no_company recommend_daytime recommend_email );
104 my @signup_server_scalars = qw( default_pkgpart default_svcpart default_domsvc );
106 my @selfservice_textareas = qw( head body_header body_footer );
108 my @selfservice_scalars = qw(
109 body_bgcolor box_bgcolor
110 text_color link_color vlink_color hlink_color alink_color
111 font title_color title_align title_size menu_bgcolor menu_fontsize
114 #XXX my @selfservice_bools = qw(
115 # menu_skipblanks menu_skipheadings menu_nounderline
118 #my $selfservice_binaries = qw(
119 # title_left_image title_right_image
120 # menu_top_image menu_body_image menu_bottom_image
123 $signup_info_cache = {
125 'cust_main_county' => [ map $_->hashref,
126 qsearch('cust_main_county', {} )
129 'agent' => [ map { my $agent = $_;
130 +{ map { $_ => $agent->get($_) } @agent_fields }
132 qsearch('agent', { 'disabled' => '' } )
135 'part_referral' => [ map $_->hashref,
136 qsearch('part_referral', { 'disabled' => '' } )
139 'agentnum2part_pkg' => $agentnum2part_pkg,
141 'svc_acct_pop' => [ map $_->hashref, qsearch('svc_acct_pop',{} ) ],
143 'emailinvoiceonly' => $conf->exists('emailinvoiceonly'),
145 'security_phrase' => $conf->exists('security_phrase'),
147 'nomadix' => $conf->exists('signup_server-nomadix'),
149 'payby' => [ $conf->config('signup_server-payby') ],
151 'payby_longname' => [ map { FS::payby->longname($_) }
152 $conf->config('signup_server-payby') ],
154 'card_types' => card_types(),
156 ( map { $_ => $conf->exists("signup-$_") } @signup_bools ),
158 ( map { $_ => scalar($conf->config("signup_server-$_")) }
159 @signup_server_scalars
162 ( map { $_ => join("\n", $conf->config("selfservice-$_")) }
163 @selfservice_textareas
165 ( map { $_ => scalar($conf->config("selfservice-$_")) }
169 #( map { $_ => scalar($conf->config_binary("selfservice-$_")) }
170 # @selfservice_binaries
173 'agentnum2part_pkg' => $agentnum2part_pkg,
174 'svc_acct_pop' => [ map $_->hashref, qsearch('svc_acct_pop',{} ) ],
175 'nomadix' => $conf->exists('signup_server-nomadix'),
176 'payby' => [ $conf->config('signup_server-payby') ],
177 'card_types' => card_types(),
178 'paytypes' => [ @FS::cust_main::paytypes ],
180 'require_cvv' => $conf->exists('signup-require_cvv'),
181 'stateid_enabled' => $conf->exists('show_stateid'),
182 'paystate_enabled' => $conf->exists('show_bankstate'),
183 'exempt_groups' => [ grep /\S/, $conf->config('tax-cust_exempt-groups') ],
187 'statedefault' => scalar($conf->config('statedefault')) || 'CA',
188 'countrydefault' => scalar($conf->config('countrydefault')) || 'US',
189 'refnum' => scalar($conf->config('signup_server-default_refnum')),
190 'signup_service' => $svc_x,
191 'company_name' => scalar($conf->config('company_name')),
193 'logo' => scalar($conf->config_binary('logo.png')),
194 'prepaid_template_custnum' => $conf->exists('signup_server-prepaid-template-custnum'),
197 $cache->set('signup_info_cache', $signup_info_cache);
201 my $signup_info = { %$signup_info_cache };
202 warn "$me signup info loaded\n" if $DEBUG > 1;
203 warn Dumper($signup_info). "\n" if $DEBUG > 2;
205 my @addl = qw( signup_server-classnum2 signup_server-classnum3 );
207 if ( grep { $conf->exists($_) } @addl ) {
209 $signup_info->{optional_packages} = [];
211 foreach my $addl ( @addl ) {
213 warn "$me adding optional package info\n" if $DEBUG > 1;
215 my $classnum = $conf->config($addl) or next;
218 'freq_pretty' => $_->freq_pretty,
219 'options' => { $_->options },
223 qsearch( 'part_pkg', { classnum => $classnum } );
225 push @{$signup_info->{optional_packages}}, \@pkgs;
227 warn "$me done adding opt. package info for $classnum\n" if $DEBUG > 1;
233 my $agentnum = $packet->{'agentnum'}
234 || $conf->config('signup_server-default_agentnum');
235 $agentnum =~ /^(\d*)$/ or die "illegal agentnum";
239 if ( exists $packet->{'session_id'} ) {
241 warn "$me loading agent session\n" if $DEBUG > 1;
242 my $cache = new FS::ClientAPI_SessionCache( {
243 'namespace' => 'FS::ClientAPI::Agent',
245 $session = $cache->get($packet->{'session_id'});
247 $agentnum = $session->{'agentnum'};
249 return { 'error' => "Can't resume session" }; #better error message
251 warn "$me done loading agent session\n" if $DEBUG > 1;
253 } elsif ( exists $packet->{'customer_session_id'} ) {
255 warn "$me loading customer session\n" if $DEBUG > 1;
256 my $cache = new FS::ClientAPI_SessionCache( {
257 'namespace' => 'FS::ClientAPI::MyAccount',
259 $session = $cache->get($packet->{'customer_session_id'});
261 my $custnum = $session->{'custnum'};
262 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum });
263 return { 'error' => "Can't find your customer record" } unless $cust_main;
264 $agentnum = $cust_main->agentnum;
266 return { 'error' => "Can't resume session" }; #better error message
268 warn "$me done loading customer session\n" if $DEBUG > 1;
272 $signup_info->{'part_pkg'} = [];
274 if ( $packet->{'reg_code'} ) {
276 warn "$me setting package list via reg_code\n" if $DEBUG > 1;
278 $signup_info->{'part_pkg'} =
279 [ map { { 'payby' => [ $_->payby ],
280 'freq_pretty' => $_->freq_pretty,
281 'options' => { $_->options },
285 grep { $_->svcpart($svc_x) }
287 qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
288 'agentnum' => $agentnum, } )
292 $signup_info->{'error'} = 'Unknown registration code'
293 unless @{ $signup_info->{'part_pkg'} };
295 warn "$me done setting package list via reg_code\n" if $DEBUG > 1;
297 } elsif ( $packet->{'promo_code'} ) {
299 warn "$me setting package list via promo_code\n" if $DEBUG > 1;
301 $signup_info->{'part_pkg'} =
302 [ map { { 'payby' => [ $_->payby ],
303 'freq_pretty' => $_->freq_pretty,
304 'options' => { $_->options },
307 grep { $_->svcpart($svc_x) }
308 qsearch( 'part_pkg', { 'promo_code' => {
310 value=>$packet->{'promo_code'}
312 'disabled' => '', } )
315 $signup_info->{'error'} = 'Unknown promotional code'
316 unless @{ $signup_info->{'part_pkg'} };
318 warn "$me done setting package list via promo_code\n" if $DEBUG > 1;
323 warn "$me setting agent-specific payment flag\n" if $DEBUG > 1;
324 my $agent = qsearchs('agent', { 'agentnum' => $agentnum } )
325 or return { 'error' => "Self-service agent #$agentnum does not exist" };
326 warn "$me has agent $agent\n" if $DEBUG > 1;
327 my @paybys = @{ $signup_info->{'payby'} };
328 $signup_info->{'hide_payment_fields'} = [];
330 my $gatewaynum = $conf->config('selfservice-payment_gateway');
333 $force_gateway = qsearchs('payment_gateway', { gatewaynum => $gatewaynum });
334 warn "using forced gateway #$gatewaynum - " .
335 $force_gateway->gateway_username . '@' . $force_gateway->gateway_module
337 die "configured gatewaynum $gatewaynum not found!" if !$force_gateway;
339 foreach my $payby (@paybys) {
340 warn "$me checking $payby payment fields\n" if $DEBUG > 1;
342 if ( FS::payby->realtime($payby) ) {
343 my $gateway = $force_gateway ||
344 $agent->payment_gateway( 'method' => FS::payby->payby2bop($payby),
347 if ( $gateway && $gateway->gateway_namespace
348 eq 'Business::OnlineThirdPartyPayment'
350 warn "$me hiding $payby payment fields\n" if $DEBUG > 1;
354 push @{$signup_info->{'hide_payment_fields'}}, $hide;
356 warn "$me done setting agent-specific payment flag\n" if $DEBUG > 1;
358 warn "$me setting agent-specific package list\n" if $DEBUG > 1;
359 $signup_info->{'part_pkg'} = $signup_info->{'agentnum2part_pkg'}{$agentnum}
360 unless @{ $signup_info->{'part_pkg'} };
361 warn "$me done setting agent-specific package list\n" if $DEBUG > 1;
363 warn "$me setting agent-specific adv. source list\n" if $DEBUG > 1;
364 $signup_info->{'part_referral'} =
368 'table' => 'part_referral',
369 'hashref' => { 'disabled' => '' },
370 'extra_sql' => "AND ( agentnum = $agentnum ".
371 " OR agentnum IS NULL ) ",
375 warn "$me done setting agent-specific adv. source list\n" if $DEBUG > 1;
377 $signup_info->{'agent_name'} = $agent->agent;
379 $signup_info->{'company_name'} = $conf->config('company_name', $agentnum);
381 #some of the above could probably be cached, too
383 my $signup_info_cache_agent = $cache->get("signup_info_cache_agent$agentnum");
385 if ( $signup_info_cache_agent ) {
387 warn "$me loading cached signup info for agentnum $agentnum\n"
392 warn "$me populating signup info cache for agentnum $agentnum\n"
395 $signup_info_cache_agent = {
396 #( map { $_ => scalar( $conf->config($_, $agentnum) ) }
397 # qw( company_name ) ),
398 ( map { $_ => scalar( $conf->config("selfservice-$_", $agentnum ) ) }
399 qw( body_bgcolor box_bgcolor menu_bgcolor ) ),
400 ( map { $_ => join("\n", $conf->config("selfservice-$_", $agentnum ) ) }
401 qw( head body_header body_footer ) ),
402 ( map { $_ => join("\n", $conf->config("signup_server-$_", $agentnum ) ) }
403 qw( terms_of_service ) ),
405 ( map { $_ => scalar($conf->exists($_, $agentnum)) }
406 qw(cust_main-require_phone agent-ship_address) ),
409 if ( $signup_info_cache_agent->{'agent-ship_address'}
410 && $agent->agent_cust_main ) {
412 my $cust_main = $agent->agent_cust_main;
413 my $location = $cust_main->ship_location;
414 $signup_info_cache_agent->{"ship_$_"} = $location->get($_)
415 foreach qw( address1 city county state zip country );
419 $cache->set("signup_info_cache_agent$agentnum", $signup_info_cache_agent);
423 $signup_info->{$_} = $signup_info_cache_agent->{$_}
424 foreach keys %$signup_info_cache_agent;
428 # delete $signup_info->{'part_pkg'};
431 warn "$me sorting package list\n" if $DEBUG > 1;
432 $signup_info->{'part_pkg'} = [ sort { $a->{pkg} cmp $b->{pkg} } # case?
433 @{ $signup_info->{'part_pkg'} }
435 warn "$me done sorting package list\n" if $DEBUG > 1;
437 if ( exists $packet->{'session_id'} ) {
438 my $agent_signup_info = { %$signup_info };
439 delete $agent_signup_info->{agentnum2part_pkg};
440 $agent_signup_info->{'agent'} = $session->{'agent'};
441 return $agent_signup_info;
443 elsif ( exists $packet->{'keys'} ) {
444 my @keys = @{ $packet->{'keys'} };
445 return { map { $_ => $signup_info->{$_} } @keys };
453 sub domain_select_hash {
458 if ($packet->{pkgpart}) {
459 my $part_pkg = qsearchs('part_pkg' => { 'pkgpart' => $packet->{pkgpart} } );
460 #$packet->{svcpart} = $part_pkg->svcpart('svc_acct')
461 $packet->{svcpart} = $part_pkg->svcpart
465 if ($packet->{svcpart}) {
466 my $part_svc = qsearchs('part_svc' => { 'svcpart' => $packet->{svcpart} } );
467 $response->{'domsvc'} = $part_svc->part_svc_column('domsvc')->columnvalue
468 if ($part_svc && $part_svc->part_svc_column('domsvc')->columnflag eq 'D');
471 $response->{'domains'}
472 = { domain_select_hash FS::svc_acct( map { $_ => $packet->{$_} }
482 my $conf = new FS::Conf;
483 my $svc_x = $conf->config('signup_server-service') || 'svc_acct';
485 if ( $svc_x eq 'svc_acct' ) {
487 #things that aren't necessary in base class, but are for signup server
488 #return "Passwords don't match"
489 # if $hashref->{'_password'} ne $hashref->{'_password2'}
490 return { 'error' => gettext('empty_password') }
491 unless length($packet->{'_password'});
492 # a bit inefficient for large numbers of pops
493 return { 'error' => gettext('no_access_number_selected') }
494 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
497 elsif ( $svc_x eq 'svc_pbx' ) {
498 #possibly some validation will be needed
502 if ( exists $packet->{'session_id'} ) {
503 my $cache = new FS::ClientAPI_SessionCache( {
504 'namespace' => 'FS::ClientAPI::Agent',
506 my $session = $cache->get($packet->{'session_id'});
508 $agentnum = $session->{'agentnum'};
510 return { 'error' => "Can't resume session" }; #better error message
513 $agentnum = $packet->{agentnum}
514 || $conf->config('signup_server-default_agentnum');
517 my ($bill_hash, $ship_hash);
518 foreach my $f (FS::cust_main->location_fields) {
519 # avoid having to change this in front-end code
520 $bill_hash->{$f} = $packet->{"bill_$f"} || $packet->{$f};
521 $ship_hash->{$f} = $packet->{"ship_$f"};
524 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
525 # common that are still here and library them.
528 'agentnum' => $agentnum,
529 'refnum' => $packet->{refnum}
530 || $conf->config('signup_server-default_refnum'),
531 'tagnum' => [ FS::part_tag->default_tags ],
533 ( map { $_ => $packet->{$_} } qw(
535 ss stateid stateid_state
538 payinfo paycvv paydate payname paystate paytype
539 paystart_month paystart_year payissue
544 referral_custnum comments
550 my $template_custnum = $conf->config('signup_server-prepaid-template-custnum');
552 if ( $template_custnum && $packet->{prepaid_shortform} ) {
554 my $template_cust = qsearchs('cust_main', { 'custnum' => $template_custnum } );
555 return { 'error' => 'Configuration error' } unless $template_cust;
556 $cust_main = new FS::cust_main ( {
558 map { $_ => $template_cust->$_ } qw(
559 last first company daytime night fax mobile
563 $bill_hash = { $template_cust->bill_location->location_hash };
564 $ship_hash = { $template_cust->ship_location->location_hash };
568 $cust_main = new FS::cust_main ( {
570 map { $_ => $packet->{$_} } qw(
571 last first company daytime night fax mobile
577 my $bill_location = FS::cust_location->new($bill_hash);
579 my $agent = qsearchs('agent', { 'agentnum' => $agentnum } );
580 if ( $conf->exists('agent-ship_address', $agentnum)
581 && $agent->agent_custnum ) {
583 my $agent_cust_main = $agent->agent_cust_main;
584 my $prefix = length($agent_cust_main->ship_last) ? 'ship_' : '';
585 $ship_location = FS::cust_location->new({
586 $agent_cust_main->ship_location->location_hash
590 # we don't have an equivalent of the "same" checkbox in selfservice
591 # so is there a ship address, and if so, is it different from the billing
593 elsif ( length($ship_hash->{address1}) > 0 and
594 grep { $bill_hash->{$_} ne $ship_hash->{$_} } keys(%$ship_hash)
597 $ship_location = FS::cust_location->new( $ship_hash );
601 $ship_location = $bill_location;
604 $cust_main->set('bill_location' => $bill_location);
605 $cust_main->set('ship_location' => $ship_location);
607 return { 'error' => "Illegal payment type" }
608 unless grep { $_ eq $packet->{'payby'} }
609 $conf->config('signup_server-payby');
611 if (FS::payby->realtime($packet->{payby})
612 and not $conf->exists('signup_server-third_party_as_card')) {
613 my $payby = $packet->{payby};
615 my $agent = qsearchs('agent', { 'agentnum' => $agentnum });
616 return { 'error' => "Unknown reseller" }
620 my $gatewaynum = $conf->config('selfservice-payment_gateway');
622 $gw = qsearchs('payment_gateway', { gatewaynum => $gatewaynum });
623 die "configured gatewaynum $gatewaynum not found!" if !$gw;
626 $gw = $agent->payment_gateway( 'method' => FS::payby->payby2bop($payby),
631 $cust_main->payby('BILL') # MCRD better? no, that's for something else
632 if $gw && $gw->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
635 return { 'error' => "CVV2 is required" }
636 if $cust_main->payby =~ /^(CARD|DCRD)$/
637 && ! $cust_main->paycvv
638 && $conf->exists('signup-require_cvv');
640 $cust_main->payinfo($cust_main->daytime)
641 if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
643 if ( grep length($packet->{$_}), FS::cust_main->location_fields ) {
645 foreach my $f (FS::cust_main->location_fields) {
646 $bill_hash->{$f} = $packet->{$f};
648 my $bill_location = FS::cust_location->new($bill_hash);
649 $cust_main->set('bill_location' => $bill_location);
650 $cust_main->set('ship_location' => $bill_location);
653 my @invoicing_list = $packet->{'invoicing_list'}
654 ? split( /\s*\,\s*/, $packet->{'invoicing_list'} )
657 my %insert_options = ();
659 my @exempt_groups = grep /\S/, $conf->config('tax-cust_exempt-groups');
660 my @tax_exempt = grep { $packet->{"tax_$_"} eq 'Y' } @exempt_groups;
661 $insert_options{'tax_exemption'} = {
662 map { $_ => $packet->{"tax_$_".'_num'} } @tax_exempt
665 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
667 return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
670 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
671 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
674 if ( $packet->{'reg_code'} ) {
675 $reg_code = qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
676 'agentnum' => $agentnum, } )
677 or return { 'error' => 'Unknown registration code' };
680 my $cust_pkg = new FS::cust_pkg ( {
681 #later#'custnum' => $custnum,
682 'pkgpart' => $packet->{'pkgpart'},
683 'promo_code' => $packet->{'promo_code'},
684 'reg_code' => $packet->{'reg_code'},
686 #my $error = $cust_pkg->check;
687 #return { 'error' => $error } if $error;
690 unless ( $svc_x eq 'none' ) {
692 my $svcpart = $part_pkg->svcpart($svc_x);
693 #should be all auto-magic and shit
694 if ( $svc_x eq 'svc_acct' ) {
696 my $svc = new FS::svc_acct {
697 'svcpart' => $svcpart,
698 map { $_ => $packet->{$_} }
699 qw( username _password sec_phrase popnum domsvc ),
702 my $error = $svc->is_password_allowed($packet->{_password});
703 $error = '' if $conf->config_bool('password-insecure', $agentnum);
704 return { error => $error } if $error;
708 while ( exists($packet->{"snarf_machine$snarfnum"})
709 && length($packet->{"snarf_machine$snarfnum"}) ) {
710 my $acct_snarf = new FS::acct_snarf ( {
711 'machine' => $packet->{"snarf_machine$snarfnum"},
712 'protocol' => $packet->{"snarf_protocol$snarfnum"},
713 'username' => $packet->{"snarf_username$snarfnum"},
714 '_password' => $packet->{"snarf_password$snarfnum"},
717 push @acct_snarf, $acct_snarf;
719 $svc->child_objects( \@acct_snarf );
722 } elsif ( $svc_x eq 'svc_phone' ) {
724 push @svc, new FS::svc_phone ( {
725 'svcpart' => $svcpart,
726 map { $_ => $packet->{$_} }
727 qw( countrycode phonenum sip_password pin ),
730 } elsif ( $svc_x eq 'svc_pbx' ) {
732 push @svc, new FS::svc_pbx ( {
733 'svcpart' => $svcpart,
734 map { $_ => $packet->{$_} }
739 die "unknown signup service $svc_x";
744 if ($packet->{'mac_addr'} && $conf->exists('signup_server-mac_addr_svcparts'))
747 my %mac_addr_svcparts = map { $_ => 1 }
748 $conf->config('signup_server-mac_addr_svcparts');
749 my @pkg_svc = grep { $_->quantity && $mac_addr_svcparts{$_->svcpart} }
750 $cust_pkg->part_pkg->pkg_svc;
752 return { 'error' => 'No service defined to assign mac address' }
755 my $svc = new FS::svc_acct {
756 'svcpart' => $pkg_svc[0]->svcpart, #multiple matches? alas..
757 'username' => $packet->{'mac_addr'},
758 '_password' => '', #blank as requested (set passwordmin to 0)
765 foreach my $svc ( @svc ) {
766 my $y = $svc->setdefault; # arguably should be in new method
767 return { 'error' => $y } if $y && !ref($y);
768 #$error = $svc->check;
769 #return { 'error' => $error } if $error;
772 #setup a job dependancy to delay provisioning
773 my $placeholder = new FS::queue ( {
774 'job' => 'FS::ClientAPI::Signup::__placeholder',
775 'status' => 'locked',
777 my $error = $placeholder->insert;
778 return { 'error' => $error } if $error;
781 tie my %hash, 'Tie::RefHash';
782 %hash = ( $cust_pkg => \@svc );
784 $error = $cust_main->insert(
787 'depend_jobnum' => $placeholder->jobnum,
791 my $perror = $placeholder->delete;
792 $error .= " (Additionally, error removing placeholder: $perror)" if $perror;
793 return { 'error' => $error };
796 if ( $conf->exists('signup_server-realtime') ) {
798 #warn "$me Billing customer...\n" if $Debug;
801 my $bill_error = $cust_main->bill(
802 'depend_jobnum' => $placeholder->jobnum,
803 'return_bill' => \@cust_bill,
805 #warn "$me error billing new customer: $bill_error"
808 $bill_error = $cust_main->apply_payments_and_credits;
809 #warn "$me error applying payments and credits for".
810 # " new customer: $bill_error"
813 unless ( $packet->{payby} eq 'PREPAY' ) {
814 $bill_error = $cust_main->realtime_collect(
815 method => FS::payby->payby2bop( $packet->{payby} ),
816 depend_jobnum => $placeholder->jobnum,
819 #warn "$me error collecting from new customer: $bill_error"
823 if ($bill_error && ref($bill_error) eq 'HASH') {
824 return { 'error' => '_collect',
825 ( map { $_ => $bill_error->{$_} }
826 qw(popup_url reference collectitems)
828 amount => $cust_main->balance,
832 $bill_error = $cust_main->apply_payments_and_credits;
833 #warn "$me error applying payments and credits for".
834 # " new customer: $bill_error"
837 if ( $cust_main->balance > 0 ) {
839 #this used to apply a credit, but now we can void invoices...
840 foreach my $cust_bill (@cust_bill) {
841 my $voiderror = $cust_bill->void('automatic payment failed');
842 warn "Error voiding cust bill after decline: $voiderror" if $voiderror;
845 #should check list for errors...
846 #$cust_main->suspend;
847 local $FS::svc_Common::noexport_hack = 1;
848 $cust_main->cancel('quiet'=>1);
850 my $perror = $placeholder->depended_delete;
851 warn "error removing provisioning jobs after decline: $perror" if $perror;
853 $perror = $placeholder->delete;
854 warn "error removing placeholder after decline: $perror" if $perror;
857 return { 'error' => '_decline' };
863 $error = $reg_code->delete;
864 return { 'error' => $error } if $error;
867 $error = $placeholder->delete;
868 return { 'error' => $error } if $error;
870 if ( $conf->exists('signup-duplicate_cc-warn_hours') ) {
871 my $hours = $conf->config('signup-duplicate_cc-warn_hours');
872 my $ban = new FS::banned_pay $cust_main->_new_banned_pay_hashref;
873 $ban->end_date( int( time + $hours*3600 ) );
874 $ban->bantype('warn');
875 $ban->reason('signup-duplicate_cc-warn_hours');
876 $error = $ban->insert;
877 warn "WARNING: error inserting temporary banned_pay for ".
878 " signup-duplicate_cc-warn_hours (proceeding anyway): $error"
882 my %return = ( 'error' => '',
883 'signup_service' => $svc_x,
884 'custnum' => $cust_main->custnum,
889 $return{'svcnum'} = $svc[0]->svcnum;
891 if ( $svc_x eq 'svc_acct' ) {
892 $return{$_} = $svc[0]->$_() for qw( username _password );
893 } elsif ( $svc_x eq 'svc_phone' ) {
894 $return{$_} = $svc[0]->$_() for qw(countrycode phonenum sip_password pin);
895 } elsif ( $svc_x eq 'svc_pbx' ) {
896 #$return{$_} = $svc[0]->$_() for qw( ) #nothing yet
898 return {'error' => "configuration error: unknown signup service $svc_x"};
899 #die "unknown signup service $svc_x";
900 # return an error that's visible to someone somewhere
909 #false laziness w/ above
910 # fresh restart to support "free account" portals with 3.x/4.x-style
911 # addressless accounts
912 # and a contact (for self-service login)
913 sub new_customer_minimal {
916 my $conf = new FS::Conf;
917 my $svc_x = $conf->config('signup_server-service') || 'svc_acct';
919 if ( $svc_x eq 'svc_acct' ) {
921 #things that aren't necessary in base class, but are for signup server
922 #return "Passwords don't match"
923 # if $hashref->{'_password'} ne $hashref->{'_password2'}
924 return { 'error' => gettext('empty_password') }
925 unless length($packet->{'_password'});
926 # a bit inefficient for large numbers of pops
927 return { 'error' => gettext('no_access_number_selected') }
928 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
931 elsif ( $svc_x eq 'svc_pbx' ) {
932 #possibly some validation will be needed
936 if ( exists $packet->{'session_id'} ) {
937 my $cache = new FS::ClientAPI_SessionCache( {
938 'namespace' => 'FS::ClientAPI::Agent',
940 my $session = $cache->get($packet->{'session_id'});
942 $agentnum = $session->{'agentnum'};
944 return { 'error' => "Can't resume session" }; #better error message
947 $agentnum = $packet->{agentnum}
948 || $conf->config('signup_server-default_agentnum');
951 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
952 # common that are still here and library them.
954 my $cust_main = new FS::cust_main ( {
955 'agentnum' => $agentnum,
956 'refnum' => $packet->{refnum}
957 || $conf->config('signup_server-default_refnum'),
958 'tagnum' => [ FS::part_tag->default_tags ],
961 map { $_ => $packet->{$_} } qw(
963 last first company daytime night fax mobile
964 ss stateid stateid_state
967 payinfo paycvv paydate payname paystate paytype
968 paystart_month paystart_year payissue
976 if ( grep length($packet->{$_}), FS::cust_main->location_fields ) {
978 foreach my $f (FS::cust_main->location_fields) {
979 $bill_hash->{$f} = $packet->{$f};
981 my $bill_location = FS::cust_location->new($bill_hash);
982 $cust_main->set('bill_location' => $bill_location);
983 $cust_main->set('ship_location' => $bill_location);
986 my @invoicing_list = $packet->{'invoicing_list'}
987 ? split( /\s*\,\s*/, $packet->{'invoicing_list'} )
991 tie my %hash, 'Tie::RefHash', ();
994 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
1000 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
1001 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
1003 my $cust_pkg = new FS::cust_pkg ( {
1004 #later#'custnum' => $custnum,
1005 'pkgpart' => $packet->{'pkgpart'},
1007 #my $error = $cust_pkg->check;
1008 #return { 'error' => $error } if $error;
1010 unless ( $svc_x eq 'none' ) {
1012 my $svcpart = $part_pkg->svcpart($svc_x);
1013 #should be all auto-magic and shit
1014 if ( $svc_x eq 'svc_acct' ) {
1016 my $svc = new FS::svc_acct {
1017 'svcpart' => $svcpart,
1018 map { $_ => $packet->{$_} }
1019 qw( username _password sec_phrase popnum domsvc ),
1024 } elsif ( $svc_x eq 'svc_phone' ) {
1026 push @svc, new FS::svc_phone ( {
1027 'svcpart' => $svcpart,
1028 map { $_ => $packet->{$_} }
1029 qw( countrycode phonenum sip_password pin ),
1032 } elsif ( $svc_x eq 'svc_pbx' ) {
1034 push @svc, new FS::svc_pbx ( {
1035 'svcpart' => $svcpart,
1036 map { $_ => $packet->{$_} }
1041 die "unknown signup service $svc_x";
1046 foreach my $svc ( @svc ) {
1047 my $y = $svc->setdefault; # arguably should be in new method
1048 return { 'error' => $y } if $y && !ref($y);
1049 #$error = $svc->check;
1050 #return { 'error' => $error } if $error;
1054 tie my %hash, 'Tie::RefHash';
1055 $hash{ $cust_pkg } = \@svc;
1060 if ( $invoicing_list[0] && $packet->{'_password'} ) {
1062 new FS::contact { 'first' => $cust_main->first,
1063 'last' => $cust_main->get('last'),
1064 '_password' => $packet->{'_password'},
1065 'emailaddress' => $invoicing_list[0],
1066 'selfservice_access' => 'Y',
1071 my $error = $cust_main->insert(
1076 return { 'error' => $error } if $error;
1078 my $session = { 'custnum' => $cust_main->custnum };
1082 $session_id = sha512_hex(time(). {}. rand(). $$)
1083 } until ( ! defined _myaccount_cache->get($session_id) ); #just in case
1085 _myaccount_cache->set( $session_id, $session, '1 hour' ); # 1 hour?
1087 my %return = ( 'error' => '',
1088 'signup_service' => $svc_x,
1089 'custnum' => $cust_main->custnum,
1090 'session_id' => $session_id,
1091 map { $_ => $cust_main->$_ } qw( first last company ),
1096 $return{'svcnum'} = $svc[0]->svcnum;
1098 if ( $svc_x eq 'svc_acct' ) {
1099 $return{$_} = $svc[0]->$_() for qw( username _password );
1100 } elsif ( $svc_x eq 'svc_phone' ) {
1101 $return{$_} = $svc[0]->$_() for qw(countrycode phonenum sip_password pin);
1102 } elsif ( $svc_x eq 'svc_pbx' ) {
1103 #$return{$_} = $svc[0]->$_() for qw( ) #nothing yet
1105 return {'error' => "configuration error: unknown signup service $svc_x"};
1106 #die "unknown signup service $svc_x";
1107 # return an error that's visible to someone somewhere
1116 use vars qw( $myaccount_cache );
1117 sub _myaccount_cache {
1118 $myaccount_cache ||= new FS::ClientAPI_SessionCache( {
1119 'namespace' => 'FS::ClientAPI::MyAccount',
1123 sub capture_payment {
1126 warn "$me capture_payment called on $packet\n" if $DEBUG;
1129 # identify processor/gateway from called back URL
1132 my $conf = new FS::Conf;
1134 my $payment_gateway;
1135 if ( my $gwnum = $conf->config('selfservice-payment_gateway') ) {
1136 $payment_gateway = qsearchs('payment_gateway', { 'gatewaynum' => $gwnum })
1137 or die "configured gatewaynum $gwnum not found!";
1140 my $url = $packet->{url};
1142 $payment_gateway = qsearchs('payment_gateway',
1143 { 'gateway_callback_url' => popurl(0, $url) }
1145 if (!$payment_gateway) {
1147 my ( $processor, $login, $password, $action, @bop_options ) =
1148 $conf->config('business-onlinepayment');
1149 $action ||= 'normal authorization';
1150 pop @bop_options if scalar(@bop_options) % 2 && $bop_options[-1] =~ /^\s*$/;
1151 die "No real-time processor is enabled - ".
1152 "did you set the business-onlinepayment configuration value?\n"
1155 $payment_gateway = new FS::payment_gateway( {
1156 gateway_namespace => $conf->config('business-onlinepayment-namespace'),
1157 gateway_module => $processor,
1158 gateway_username => $login,
1159 gateway_password => $password,
1160 gateway_action => $action,
1161 options => [ ( @bop_options ) ],
1166 die "No real-time third party processor is enabled - ".
1167 "did you set the business-onlinepayment configuration value?\n*"
1168 unless $payment_gateway->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
1171 # locate pending transaction
1174 eval "use Business::OnlineThirdPartyPayment";
1178 new Business::OnlineThirdPartyPayment( $payment_gateway->gateway_module,
1179 @{ [ $payment_gateway->options ] },
1182 my $paypendingnum = $transaction->reference($packet->{data});
1184 my $cust_pay_pending =
1185 qsearchs('cust_pay_pending', { paypendingnum => $paypendingnum } );
1187 unless ($cust_pay_pending) {
1188 my $bill_error = "No payment is being processed with id $paypendingnum".
1189 "; Transaction aborted.";
1190 return { error => '_decline', bill_error => $bill_error };
1193 if ($cust_pay_pending->status ne 'thirdparty') {
1194 my $bill_error = "Payment with id $paypendingnum is not thirdparty, but ".
1195 $cust_pay_pending->status. "; Transaction aborted.";
1196 return { error => '_decline', bill_error => $bill_error };
1199 my $cust_main = $cust_pay_pending->cust_main;
1200 if ( $packet->{cancel} ) {
1201 # the user has chosen not to make this payment
1202 # (probably should be a separate API call, but I don't want to duplicate
1203 # all of the above...which should eventually go away)
1204 my $error = $cust_pay_pending->delete;
1205 # don't show any errors related to this; they're not meaningful
1206 warn "error canceling pending payment $paypendingnum: $error\n" if $error;
1207 return { 'error' => '_cancel',
1208 'session_id' => $cust_pay_pending->session_id };
1210 # create the payment
1212 $cust_main->realtime_botpp_capture( $cust_pay_pending,
1217 return { 'error' => ( $bill_error->{bill_error} ? '_decline' : '' ),