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;
29 $me = '[FS::ClientAPI::Signup]';
32 warn "$me clear_cache called\n" if $DEBUG;
33 my $cache = new FS::ClientAPI_SessionCache( {
34 'namespace' => 'FS::ClientAPI::Signup',
43 warn "$me signup_info called on $packet\n" if $DEBUG;
45 my $conf = new FS::Conf;
46 my $svc_x = $conf->config('signup_server-service') || 'svc_acct';
48 my $cache = new FS::ClientAPI_SessionCache( {
49 'namespace' => 'FS::ClientAPI::Signup',
51 my $signup_info_cache = $cache->get('signup_info_cache');
53 if ( $signup_info_cache ) {
55 warn "$me loading cached signup info\n" if $DEBUG > 1;
59 warn "$me populating signup info cache\n" if $DEBUG > 1;
61 my $agentnum2part_pkg =
65 my $href = $agent->pkgpart_hashref;
68 map { { 'payby' => [ $_->payby ],
69 'freq_pretty' => $_->freq_pretty,
70 'options' => { $_->options },
73 grep { $_->svcpart($svc_x)
74 && ( $href->{ $_->pkgpart }
76 && $_->agentnum == $agent->agentnum
80 qsearch( 'part_pkg', { 'disabled' => '' } )
82 } qsearch('agent', { 'disabled' => '' })
85 my $msgcat = { map { $_=>gettext($_) }
86 qw( passwords_dont_match invalid_card unknown_card_type
87 not_a empty_password illegal_or_empty_text )
89 warn "msgcat: ". Dumper($msgcat). "\n" if $DEBUG > 2;
91 my $label = { map { $_ => FS::Msgcat::_gettext($_) }
92 qw( stateid stateid_state )
94 warn "label: ". Dumper($label). "\n" if $DEBUG > 2;
96 my @agent_fields = qw( agentnum agent );
98 my @bools = qw( emailinvoiceonly security_phrase );
100 my @signup_bools = qw( no_company recommend_daytime recommend_email );
102 my @signup_server_scalars = qw( default_pkgpart default_svcpart default_domsvc );
104 my @selfservice_textareas = qw( head body_header body_footer );
106 my @selfservice_scalars = qw(
107 body_bgcolor box_bgcolor
108 text_color link_color vlink_color hlink_color alink_color
109 font title_color title_align title_size menu_bgcolor menu_fontsize
112 #XXX my @selfservice_bools = qw(
113 # menu_skipblanks menu_skipheadings menu_nounderline
116 #my $selfservice_binaries = qw(
117 # title_left_image title_right_image
118 # menu_top_image menu_body_image menu_bottom_image
121 $signup_info_cache = {
123 'cust_main_county' => [ map $_->hashref,
124 qsearch('cust_main_county', {} )
127 'agent' => [ map { my $agent = $_;
128 +{ map { $_ => $agent->get($_) } @agent_fields }
130 qsearch('agent', { 'disabled' => '' } )
133 'part_referral' => [ map $_->hashref,
134 qsearch('part_referral', { 'disabled' => '' } )
137 'agentnum2part_pkg' => $agentnum2part_pkg,
139 'svc_acct_pop' => [ map $_->hashref, qsearch('svc_acct_pop',{} ) ],
141 'emailinvoiceonly' => $conf->exists('emailinvoiceonly'),
143 'security_phrase' => $conf->exists('security_phrase'),
145 'nomadix' => $conf->exists('signup_server-nomadix'),
147 'payby' => [ $conf->config('signup_server-payby') ],
149 'payby_longname' => [ map { FS::payby->longname($_) }
150 $conf->config('signup_server-payby') ],
152 'card_types' => card_types(),
154 ( map { $_ => $conf->exists("signup-$_") } @signup_bools ),
156 ( map { $_ => scalar($conf->config("signup_server-$_")) }
157 @signup_server_scalars
160 ( map { $_ => join("\n", $conf->config("selfservice-$_")) }
161 @selfservice_textareas
163 ( map { $_ => scalar($conf->config("selfservice-$_")) }
167 #( map { $_ => scalar($conf->config_binary("selfservice-$_")) }
168 # @selfservice_binaries
171 'agentnum2part_pkg' => $agentnum2part_pkg,
172 'svc_acct_pop' => [ map $_->hashref, qsearch('svc_acct_pop',{} ) ],
173 'nomadix' => $conf->exists('signup_server-nomadix'),
174 'payby' => [ $conf->config('signup_server-payby') ],
175 'card_types' => card_types(),
176 'paytypes' => [ @FS::cust_main::paytypes ],
178 'stateid_enabled' => $conf->exists('show_stateid'),
179 'paystate_enabled' => $conf->exists('show_bankstate'),
183 'statedefault' => scalar($conf->config('statedefault')) || 'CA',
184 'countrydefault' => scalar($conf->config('countrydefault')) || 'US',
185 'refnum' => scalar($conf->config('signup_server-default_refnum')),
186 'signup_service' => $svc_x,
187 'company_name' => scalar($conf->config('company_name')),
189 'logo' => scalar($conf->config_binary('logo.png')),
190 'prepaid_template_custnum' => $conf->exists('signup_server-prepaid-template-custnum'),
193 $cache->set('signup_info_cache', $signup_info_cache);
197 my $signup_info = { %$signup_info_cache };
198 warn "$me signup info loaded\n" if $DEBUG > 1;
199 warn Dumper($signup_info). "\n" if $DEBUG > 2;
201 my @addl = qw( signup_server-classnum2 signup_server-classnum3 );
203 if ( grep { $conf->exists($_) } @addl ) {
205 $signup_info->{optional_packages} = [];
207 foreach my $addl ( @addl ) {
209 warn "$me adding optional package info\n" if $DEBUG > 1;
211 my $classnum = $conf->config($addl) or next;
214 'freq_pretty' => $_->freq_pretty,
215 'options' => { $_->options },
219 qsearch( 'part_pkg', { classnum => $classnum } );
221 push @{$signup_info->{optional_packages}}, \@pkgs;
223 warn "$me done adding opt. package info for $classnum\n" if $DEBUG > 1;
229 my $agentnum = $packet->{'agentnum'}
230 || $conf->config('signup_server-default_agentnum');
231 $agentnum =~ /^(\d*)$/ or die "illegal agentnum";
235 if ( exists $packet->{'session_id'} ) {
237 warn "$me loading agent session\n" if $DEBUG > 1;
238 my $cache = new FS::ClientAPI_SessionCache( {
239 'namespace' => 'FS::ClientAPI::Agent',
241 $session = $cache->get($packet->{'session_id'});
243 $agentnum = $session->{'agentnum'};
245 return { 'error' => "Can't resume session" }; #better error message
247 warn "$me done loading agent session\n" if $DEBUG > 1;
249 } elsif ( exists $packet->{'customer_session_id'} ) {
251 warn "$me loading customer session\n" if $DEBUG > 1;
252 my $cache = new FS::ClientAPI_SessionCache( {
253 'namespace' => 'FS::ClientAPI::MyAccount',
255 $session = $cache->get($packet->{'customer_session_id'});
257 my $custnum = $session->{'custnum'};
258 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum });
259 return { 'error' => "Can't find your customer record" } unless $cust_main;
260 $agentnum = $cust_main->agentnum;
262 return { 'error' => "Can't resume session" }; #better error message
264 warn "$me done loading customer session\n" if $DEBUG > 1;
268 $signup_info->{'part_pkg'} = [];
270 if ( $packet->{'reg_code'} ) {
272 warn "$me setting package list via reg_code\n" if $DEBUG > 1;
274 $signup_info->{'part_pkg'} =
275 [ map { { 'payby' => [ $_->payby ],
276 'freq_pretty' => $_->freq_pretty,
277 'options' => { $_->options },
281 grep { $_->svcpart($svc_x) }
283 qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
284 'agentnum' => $agentnum, } )
288 $signup_info->{'error'} = 'Unknown registration code'
289 unless @{ $signup_info->{'part_pkg'} };
291 warn "$me done setting package list via reg_code\n" if $DEBUG > 1;
293 } elsif ( $packet->{'promo_code'} ) {
295 warn "$me setting package list via promo_code\n" if $DEBUG > 1;
297 $signup_info->{'part_pkg'} =
298 [ map { { 'payby' => [ $_->payby ],
299 'freq_pretty' => $_->freq_pretty,
300 'options' => { $_->options },
303 grep { $_->svcpart($svc_x) }
304 qsearch( 'part_pkg', { 'promo_code' => {
306 value=>$packet->{'promo_code'}
308 'disabled' => '', } )
311 $signup_info->{'error'} = 'Unknown promotional code'
312 unless @{ $signup_info->{'part_pkg'} };
314 warn "$me done setting package list via promo_code\n" if $DEBUG > 1;
319 warn "$me setting agent-specific payment flag\n" if $DEBUG > 1;
320 my $agent = qsearchs('agent', { 'agentnum' => $agentnum } )
321 or return { 'error' => "Self-service agent #$agentnum does not exist" };
322 warn "$me has agent $agent\n" if $DEBUG > 1;
323 my @paybys = @{ $signup_info->{'payby'} };
324 $signup_info->{'hide_payment_fields'} = [];
326 my $gatewaynum = $conf->config('selfservice-payment_gateway');
329 $force_gateway = qsearchs('payment_gateway', { gatewaynum => $gatewaynum });
330 warn "using forced gateway #$gatewaynum - " .
331 $force_gateway->gateway_username . '@' . $force_gateway->gateway_module
333 die "configured gatewaynum $gatewaynum not found!" if !$force_gateway;
335 foreach my $payby (@paybys) {
336 warn "$me checking $payby payment fields\n" if $DEBUG > 1;
338 if ( FS::payby->realtime($payby) ) {
339 my $gateway = $force_gateway ||
340 $agent->payment_gateway( 'method' => FS::payby->payby2bop($payby),
343 if ( $gateway && $gateway->gateway_namespace
344 eq 'Business::OnlineThirdPartyPayment'
346 warn "$me hiding $payby payment fields\n" if $DEBUG > 1;
350 push @{$signup_info->{'hide_payment_fields'}}, $hide;
352 warn "$me done setting agent-specific payment flag\n" if $DEBUG > 1;
354 warn "$me setting agent-specific package list\n" if $DEBUG > 1;
355 $signup_info->{'part_pkg'} = $signup_info->{'agentnum2part_pkg'}{$agentnum}
356 unless @{ $signup_info->{'part_pkg'} };
357 warn "$me done setting agent-specific package list\n" if $DEBUG > 1;
359 warn "$me setting agent-specific adv. source list\n" if $DEBUG > 1;
360 $signup_info->{'part_referral'} =
364 'table' => 'part_referral',
365 'hashref' => { 'disabled' => '' },
366 'extra_sql' => "AND ( agentnum = $agentnum ".
367 " OR agentnum IS NULL ) ",
371 warn "$me done setting agent-specific adv. source list\n" if $DEBUG > 1;
373 $signup_info->{'agent_name'} = $agent->agent;
375 $signup_info->{'company_name'} = $conf->config('company_name', $agentnum);
377 #some of the above could probably be cached, too
379 my $signup_info_cache_agent = $cache->get("signup_info_cache_agent$agentnum");
381 if ( $signup_info_cache_agent ) {
383 warn "$me loading cached signup info for agentnum $agentnum\n"
388 warn "$me populating signup info cache for agentnum $agentnum\n"
391 $signup_info_cache_agent = {
392 #( map { $_ => scalar( $conf->config($_, $agentnum) ) }
393 # qw( company_name ) ),
394 ( map { $_ => scalar( $conf->config("selfservice-$_", $agentnum ) ) }
395 qw( body_bgcolor box_bgcolor menu_bgcolor ) ),
396 ( map { $_ => join("\n", $conf->config("selfservice-$_", $agentnum ) ) }
397 qw( head body_header body_footer ) ),
398 ( map { $_ => join("\n", $conf->config("signup_server-$_", $agentnum ) ) }
399 qw( terms_of_service ) ),
401 ( map { $_ => scalar($conf->exists($_, $agentnum)) }
402 qw(cust_main-require_phone agent-ship_address) ),
405 if ( $signup_info_cache_agent->{'agent-ship_address'}
406 && $agent->agent_cust_main ) {
408 my $cust_main = $agent->agent_cust_main;
409 my $location = $cust_main->ship_location;
410 $signup_info_cache_agent->{"ship_$_"} = $location->get($_)
411 foreach qw( address1 city county state zip country );
415 $cache->set("signup_info_cache_agent$agentnum", $signup_info_cache_agent);
419 $signup_info->{$_} = $signup_info_cache_agent->{$_}
420 foreach keys %$signup_info_cache_agent;
424 # delete $signup_info->{'part_pkg'};
427 warn "$me sorting package list\n" if $DEBUG > 1;
428 $signup_info->{'part_pkg'} = [ sort { $a->{pkg} cmp $b->{pkg} } # case?
429 @{ $signup_info->{'part_pkg'} }
431 warn "$me done sorting package list\n" if $DEBUG > 1;
433 if ( exists $packet->{'session_id'} ) {
434 my $agent_signup_info = { %$signup_info };
435 delete $agent_signup_info->{agentnum2part_pkg};
436 $agent_signup_info->{'agent'} = $session->{'agent'};
437 return $agent_signup_info;
439 elsif ( exists $packet->{'keys'} ) {
440 my @keys = @{ $packet->{'keys'} };
441 return { map { $_ => $signup_info->{$_} } @keys };
449 sub domain_select_hash {
454 if ($packet->{pkgpart}) {
455 my $part_pkg = qsearchs('part_pkg' => { 'pkgpart' => $packet->{pkgpart} } );
456 #$packet->{svcpart} = $part_pkg->svcpart('svc_acct')
457 $packet->{svcpart} = $part_pkg->svcpart
461 if ($packet->{svcpart}) {
462 my $part_svc = qsearchs('part_svc' => { 'svcpart' => $packet->{svcpart} } );
463 $response->{'domsvc'} = $part_svc->part_svc_column('domsvc')->columnvalue
464 if ($part_svc && $part_svc->part_svc_column('domsvc')->columnflag eq 'D');
467 $response->{'domains'}
468 = { domain_select_hash FS::svc_acct( map { $_ => $packet->{$_} }
478 my $conf = new FS::Conf;
479 my $svc_x = $conf->config('signup_server-service') || 'svc_acct';
481 if ( $svc_x eq 'svc_acct' ) {
483 #things that aren't necessary in base class, but are for signup server
484 #return "Passwords don't match"
485 # if $hashref->{'_password'} ne $hashref->{'_password2'}
486 return { 'error' => gettext('empty_password') }
487 unless length($packet->{'_password'});
488 # a bit inefficient for large numbers of pops
489 return { 'error' => gettext('no_access_number_selected') }
490 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
493 elsif ( $svc_x eq 'svc_pbx' ) {
494 #possibly some validation will be needed
498 if ( exists $packet->{'session_id'} ) {
499 my $cache = new FS::ClientAPI_SessionCache( {
500 'namespace' => 'FS::ClientAPI::Agent',
502 my $session = $cache->get($packet->{'session_id'});
504 $agentnum = $session->{'agentnum'};
506 return { 'error' => "Can't resume session" }; #better error message
509 $agentnum = $packet->{agentnum}
510 || $conf->config('signup_server-default_agentnum');
513 my ($bill_hash, $ship_hash);
514 foreach my $f (FS::cust_main->location_fields) {
515 # avoid having to change this in front-end code
516 $bill_hash->{$f} = $packet->{"bill_$f"} || $packet->{$f};
517 $ship_hash->{$f} = $packet->{"ship_$f"};
520 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
521 # common that are still here and library them.
522 my $template_custnum = $conf->config('signup_server-prepaid-template-custnum');
524 if ( $template_custnum && $packet->{prepaid_shortform} ) {
526 my $template_cust = qsearchs('cust_main', { 'custnum' => $template_custnum } );
527 return { 'error' => 'Configuration error' } unless $template_cust;
528 $cust_main = new FS::cust_main ( {
529 'agentnum' => $agentnum,
530 'refnum' => $packet->{refnum}
531 || $conf->config('signup_server-default_refnum'),
533 ( map { $_ => $template_cust->$_ } qw(
534 last first company daytime night fax
538 ( map { $_ => $packet->{$_} } qw(
539 ss stateid stateid_state
542 payinfo paycvv paydate payname paystate paytype
543 paystart_month paystart_year payissue
546 referral_custnum comments
552 $bill_hash = { $template_cust->bill_location->location_hash };
553 $ship_hash = { $template_cust->ship_location->location_hash };
557 $cust_main = new FS::cust_main ( {
559 'agentnum' => $agentnum,
560 'refnum' => $packet->{refnum}
561 || $conf->config('signup_server-default_refnum'),
563 map { $_ => $packet->{$_} } qw(
564 last first ss company
565 daytime night fax stateid stateid_state
567 payinfo paycvv paydate payname paystate paytype
568 paystart_month paystart_year payissue
571 referral_custnum comments
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?
632 if $gw && $gw->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
635 $cust_main->payinfo($cust_main->daytime)
636 if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
638 my @invoicing_list = $packet->{'invoicing_list'}
639 ? split( /\s*\,\s*/, $packet->{'invoicing_list'} )
642 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
644 return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
647 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
648 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
649 my $svcpart = $part_pkg->svcpart($svc_x);
652 if ( $packet->{'reg_code'} ) {
653 $reg_code = qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
654 'agentnum' => $agentnum, } )
655 or return { 'error' => 'Unknown registration code' };
658 my $cust_pkg = new FS::cust_pkg ( {
659 #later#'custnum' => $custnum,
660 'pkgpart' => $packet->{'pkgpart'},
661 'promo_code' => $packet->{'promo_code'},
662 'reg_code' => $packet->{'reg_code'},
664 #my $error = $cust_pkg->check;
665 #return { 'error' => $error } if $error;
667 #should be all auto-magic and shit
669 if ( $svc_x eq 'svc_acct' ) {
671 my $svc = new FS::svc_acct {
672 'svcpart' => $svcpart,
673 map { $_ => $packet->{$_} }
674 qw( username _password sec_phrase popnum domsvc ),
679 while ( exists($packet->{"snarf_machine$snarfnum"})
680 && length($packet->{"snarf_machine$snarfnum"}) ) {
681 my $acct_snarf = new FS::acct_snarf ( {
682 'machine' => $packet->{"snarf_machine$snarfnum"},
683 'protocol' => $packet->{"snarf_protocol$snarfnum"},
684 'username' => $packet->{"snarf_username$snarfnum"},
685 '_password' => $packet->{"snarf_password$snarfnum"},
688 push @acct_snarf, $acct_snarf;
690 $svc->child_objects( \@acct_snarf );
693 } elsif ( $svc_x eq 'svc_phone' ) {
695 push @svc, new FS::svc_phone ( {
696 'svcpart' => $svcpart,
697 map { $_ => $packet->{$_} }
698 qw( countrycode phonenum sip_password pin ),
701 } elsif ( $svc_x eq 'svc_pbx' ) {
703 push @svc, new FS::svc_pbx ( {
704 'svcpart' => $svcpart,
705 map { $_ => $packet->{$_} }
710 die "unknown signup service $svc_x";
713 if ($packet->{'mac_addr'} && $conf->exists('signup_server-mac_addr_svcparts'))
716 my %mac_addr_svcparts = map { $_ => 1 }
717 $conf->config('signup_server-mac_addr_svcparts');
718 my @pkg_svc = grep { $_->quantity && $mac_addr_svcparts{$_->svcpart} }
719 $cust_pkg->part_pkg->pkg_svc;
721 return { 'error' => 'No service defined to assign mac address' }
724 my $svc = new FS::svc_acct {
725 'svcpart' => $pkg_svc[0]->svcpart, #multiple matches? alas..
726 'username' => $packet->{'mac_addr'},
727 '_password' => '', #blank as requested (set passwordmin to 0)
734 foreach my $svc ( @svc ) {
735 my $y = $svc->setdefault; # arguably should be in new method
736 return { 'error' => $y } if $y && !ref($y);
737 #$error = $svc->check;
738 #return { 'error' => $error } if $error;
741 #setup a job dependancy to delay provisioning
742 my $placeholder = new FS::queue ( {
743 'job' => 'FS::ClientAPI::Signup::__placeholder',
744 'status' => 'locked',
746 my $error = $placeholder->insert;
747 return { 'error' => $error } if $error;
750 tie my %hash, 'Tie::RefHash';
751 %hash = ( $cust_pkg => \@svc );
753 $error = $cust_main->insert(
756 'depend_jobnum' => $placeholder->jobnum,
759 my $perror = $placeholder->delete;
760 $error .= " (Additionally, error removing placeholder: $perror)" if $perror;
761 return { 'error' => $error };
764 if ( $conf->exists('signup_server-realtime') ) {
766 #warn "$me Billing customer...\n" if $Debug;
768 my $bill_error = $cust_main->bill( 'depend_jobnum'=>$placeholder->jobnum );
769 #warn "$me error billing new customer: $bill_error"
772 $bill_error = $cust_main->apply_payments_and_credits;
773 #warn "$me error applying payments and credits for".
774 # " new customer: $bill_error"
777 unless ( $packet->{payby} eq 'PREPAY' ) {
778 $bill_error = $cust_main->realtime_collect(
779 method => FS::payby->payby2bop( $packet->{payby} ),
780 depend_jobnum => $placeholder->jobnum,
783 #warn "$me error collecting from new customer: $bill_error"
787 if ($bill_error && ref($bill_error) eq 'HASH') {
788 return { 'error' => '_collect',
789 ( map { $_ => $bill_error->{$_} }
790 qw(popup_url reference collectitems)
792 amount => $cust_main->balance,
796 $bill_error = $cust_main->apply_payments_and_credits;
797 #warn "$me error applying payments and credits for".
798 # " new customer: $bill_error"
801 if ( $cust_main->balance > 0 ) {
803 #this makes sense. credit is "un-doing" the invoice
804 $cust_main->credit( $cust_main->balance, 'signup server decline',
805 'reason_type' => $conf->config('signup_credit_type'),
807 $cust_main->apply_credits;
809 #should check list for errors...
810 #$cust_main->suspend;
811 local $FS::svc_Common::noexport_hack = 1;
812 $cust_main->cancel('quiet'=>1);
814 my $perror = $placeholder->depended_delete;
815 warn "error removing provisioning jobs after decline: $perror" if $perror;
817 $perror = $placeholder->delete;
818 warn "error removing placeholder after decline: $perror" if $perror;
821 return { 'error' => '_decline' };
827 $error = $reg_code->delete;
828 return { 'error' => $error } if $error;
831 $error = $placeholder->delete;
832 return { 'error' => $error } if $error;
834 if ( $conf->exists('signup-duplicate_cc-warn_hours') ) {
835 my $hours = $conf->config('signup-duplicate_cc-warn_hours');
836 my $ban = new FS::banned_pay $cust_main->_new_banned_pay_hashref;
837 $ban->end_date( int( time + $hours*3600 ) );
838 $ban->bantype('warn');
839 $ban->reason('signup-duplicate_cc-warn_hours');
840 $error = $ban->insert;
841 warn "WARNING: error inserting temporary banned_pay for ".
842 " signup-duplicate_cc-warn_hours (proceeding anyway): $error"
846 my %return = ( 'error' => '',
847 'signup_service' => $svc_x,
848 'custnum' => $cust_main->custnum,
853 $return{'svcnum'} = $svc[0]->svcnum;
855 if ( $svc_x eq 'svc_acct' ) {
856 $return{$_} = $svc[0]->$_() for qw( username _password );
857 } elsif ( $svc_x eq 'svc_phone' ) {
858 $return{$_} = $svc[0]->$_() for qw(countrycode phonenum sip_password pin);
859 } elsif ( $svc_x eq 'svc_pbx' ) {
860 #$return{$_} = $svc[0]->$_() for qw( ) #nothing yet
862 return {'error' => "configuration error: unknown signup service $svc_x"};
863 #die "unknown signup service $svc_x";
864 # return an error that's visible to someone somewhere
873 #false laziness w/ above
874 # fresh restart to support "free account" portals with 3.x/4.x-style
875 # addressless accounts
876 sub new_customer_minimal {
879 my $conf = new FS::Conf;
880 my $svc_x = $conf->config('signup_server-service') || 'svc_acct';
882 if ( $svc_x eq 'svc_acct' ) {
884 #things that aren't necessary in base class, but are for signup server
885 #return "Passwords don't match"
886 # if $hashref->{'_password'} ne $hashref->{'_password2'}
887 return { 'error' => gettext('empty_password') }
888 unless length($packet->{'_password'});
889 # a bit inefficient for large numbers of pops
890 return { 'error' => gettext('no_access_number_selected') }
891 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
894 elsif ( $svc_x eq 'svc_pbx' ) {
895 #possibly some validation will be needed
899 if ( exists $packet->{'session_id'} ) {
900 my $cache = new FS::ClientAPI_SessionCache( {
901 'namespace' => 'FS::ClientAPI::Agent',
903 my $session = $cache->get($packet->{'session_id'});
905 $agentnum = $session->{'agentnum'};
907 return { 'error' => "Can't resume session" }; #better error message
910 $agentnum = $packet->{agentnum}
911 || $conf->config('signup_server-default_agentnum');
914 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
915 # common that are still here and library them.
917 my $cust_main = new FS::cust_main ( {
919 'agentnum' => $agentnum,
920 'refnum' => $packet->{refnum}
921 || $conf->config('signup_server-default_refnum'),
924 map { $_ => $packet->{$_} } qw(
925 last first ss company
931 my @invoicing_list = $packet->{'invoicing_list'}
932 ? split( /\s*\,\s*/, $packet->{'invoicing_list'} )
935 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
937 return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
940 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
941 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
942 my $svcpart = $part_pkg->svcpart($svc_x);
944 my $cust_pkg = new FS::cust_pkg ( {
945 #later#'custnum' => $custnum,
946 'pkgpart' => $packet->{'pkgpart'},
948 #my $error = $cust_pkg->check;
949 #return { 'error' => $error } if $error;
951 #should be all auto-magic and shit
953 if ( $svc_x eq 'svc_acct' ) {
955 my $svc = new FS::svc_acct {
956 'svcpart' => $svcpart,
957 map { $_ => $packet->{$_} }
958 qw( username _password sec_phrase popnum domsvc ),
963 } elsif ( $svc_x eq 'svc_phone' ) {
965 push @svc, new FS::svc_phone ( {
966 'svcpart' => $svcpart,
967 map { $_ => $packet->{$_} }
968 qw( countrycode phonenum sip_password pin ),
971 } elsif ( $svc_x eq 'svc_pbx' ) {
973 push @svc, new FS::svc_pbx ( {
974 'svcpart' => $svcpart,
975 map { $_ => $packet->{$_} }
980 die "unknown signup service $svc_x";
983 foreach my $svc ( @svc ) {
984 my $y = $svc->setdefault; # arguably should be in new method
985 return { 'error' => $y } if $y && !ref($y);
986 #$error = $svc->check;
987 #return { 'error' => $error } if $error;
991 tie my %hash, 'Tie::RefHash';
992 %hash = ( $cust_pkg => \@svc );
994 my $error = $cust_main->insert(
998 return { 'error' => $error } if $error;
1000 my $session = { 'custnum' => $cust_main->custnum };
1004 $session_id = sha1_hex(time(). {}. rand(). $$)
1005 } until ( ! defined _myaccount_cache->get($session_id) ); #just in case
1007 _cache->set( $session_id, $session, '1 hour' ); # 1 hour?
1009 my %return = ( 'error' => '',
1010 'signup_service' => $svc_x,
1011 'custnum' => $cust_main->custnum,
1012 'session_id' => $session_id,
1017 $return{'svcnum'} = $svc[0]->svcnum;
1019 if ( $svc_x eq 'svc_acct' ) {
1020 $return{$_} = $svc[0]->$_() for qw( username _password );
1021 } elsif ( $svc_x eq 'svc_phone' ) {
1022 $return{$_} = $svc[0]->$_() for qw(countrycode phonenum sip_password pin);
1023 } elsif ( $svc_x eq 'svc_pbx' ) {
1024 #$return{$_} = $svc[0]->$_() for qw( ) #nothing yet
1026 return {'error' => "configuration error: unknown signup service $svc_x"};
1027 #die "unknown signup service $svc_x";
1028 # return an error that's visible to someone somewhere
1037 use vars qw( $myaccount_cache );
1038 sub _myaccount_cache {
1039 $myaccount_cache ||= new FS::ClientAPI_SessionCache( {
1040 'namespace' => 'FS::ClientAPI::MyAccount',
1044 sub capture_payment {
1047 warn "$me capture_payment called on $packet\n" if $DEBUG;
1050 # identify processor/gateway from called back URL
1053 my $conf = new FS::Conf;
1055 my $payment_gateway;
1056 if ( my $gwnum = $conf->config('selfservice-payment_gateway') ) {
1057 $payment_gateway = qsearchs('payment_gateway', { 'gatewaynum' => $gwnum })
1058 or die "configured gatewaynum $gwnum not found!";
1061 my $url = $packet->{url};
1063 $payment_gateway = qsearchs('payment_gateway',
1064 { 'gateway_callback_url' => popurl(0, $url) }
1066 if (!$payment_gateway) {
1068 my ( $processor, $login, $password, $action, @bop_options ) =
1069 $conf->config('business-onlinepayment');
1070 $action ||= 'normal authorization';
1071 pop @bop_options if scalar(@bop_options) % 2 && $bop_options[-1] =~ /^\s*$/;
1072 die "No real-time processor is enabled - ".
1073 "did you set the business-onlinepayment configuration value?\n"
1076 $payment_gateway = new FS::payment_gateway( {
1077 gateway_namespace => $conf->config('business-onlinepayment-namespace'),
1078 gateway_module => $processor,
1079 gateway_username => $login,
1080 gateway_password => $password,
1081 gateway_action => $action,
1082 options => [ ( @bop_options ) ],
1087 die "No real-time third party processor is enabled - ".
1088 "did you set the business-onlinepayment configuration value?\n*"
1089 unless $payment_gateway->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
1092 # locate pending transaction
1095 eval "use Business::OnlineThirdPartyPayment";
1099 new Business::OnlineThirdPartyPayment( $payment_gateway->gateway_module,
1100 @{ [ $payment_gateway->options ] },
1103 my $paypendingnum = $transaction->reference($packet->{data});
1105 my $cust_pay_pending =
1106 qsearchs('cust_pay_pending', { paypendingnum => $paypendingnum } );
1108 unless ($cust_pay_pending) {
1109 my $bill_error = "No payment is being processed with id $paypendingnum".
1110 "; Transaction aborted.";
1111 return { error => '_decline', bill_error => $bill_error };
1114 if ($cust_pay_pending->status ne 'thirdparty') {
1115 my $bill_error = "Payment with id $paypendingnum is not thirdparty, but ".
1116 $cust_pay_pending->status. "; Transaction aborted.";
1117 return { error => '_decline', bill_error => $bill_error };
1120 my $cust_main = $cust_pay_pending->cust_main;
1121 if ( $packet->{cancel} ) {
1122 # the user has chosen not to make this payment
1123 # (probably should be a separate API call, but I don't want to duplicate
1124 # all of the above...which should eventually go away)
1125 my $error = $cust_pay_pending->delete;
1126 # don't show any errors related to this; they're not meaningful
1127 warn "error canceling pending payment $paypendingnum: $error\n" if $error;
1128 return { 'error' => '_cancel',
1129 'session_id' => $cust_pay_pending->session_id };
1131 # create the payment
1133 $cust_main->realtime_botpp_capture( $cust_pay_pending,
1138 return { 'error' => ( $bill_error->{bill_error} ? '_decline' : '' ),