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 'require_cvv' => $conf->exists('signup-require_cvv'),
179 'stateid_enabled' => $conf->exists('show_stateid'),
180 'paystate_enabled' => $conf->exists('show_bankstate'),
184 'statedefault' => scalar($conf->config('statedefault')) || 'CA',
185 'countrydefault' => scalar($conf->config('countrydefault')) || 'US',
186 'refnum' => scalar($conf->config('signup_server-default_refnum')),
187 'signup_service' => $svc_x,
188 'company_name' => scalar($conf->config('company_name')),
190 'logo' => scalar($conf->config_binary('logo.png')),
191 'prepaid_template_custnum' => $conf->exists('signup_server-prepaid-template-custnum'),
194 $cache->set('signup_info_cache', $signup_info_cache);
198 my $signup_info = { %$signup_info_cache };
199 warn "$me signup info loaded\n" if $DEBUG > 1;
200 warn Dumper($signup_info). "\n" if $DEBUG > 2;
202 my @addl = qw( signup_server-classnum2 signup_server-classnum3 );
204 if ( grep { $conf->exists($_) } @addl ) {
206 $signup_info->{optional_packages} = [];
208 foreach my $addl ( @addl ) {
210 warn "$me adding optional package info\n" if $DEBUG > 1;
212 my $classnum = $conf->config($addl) or next;
215 'freq_pretty' => $_->freq_pretty,
216 'options' => { $_->options },
220 qsearch( 'part_pkg', { classnum => $classnum } );
222 push @{$signup_info->{optional_packages}}, \@pkgs;
224 warn "$me done adding opt. package info for $classnum\n" if $DEBUG > 1;
230 my $agentnum = $packet->{'agentnum'}
231 || $conf->config('signup_server-default_agentnum');
232 $agentnum =~ /^(\d*)$/ or die "illegal agentnum";
236 if ( exists $packet->{'session_id'} ) {
238 warn "$me loading agent session\n" if $DEBUG > 1;
239 my $cache = new FS::ClientAPI_SessionCache( {
240 'namespace' => 'FS::ClientAPI::Agent',
242 $session = $cache->get($packet->{'session_id'});
244 $agentnum = $session->{'agentnum'};
246 return { 'error' => "Can't resume session" }; #better error message
248 warn "$me done loading agent session\n" if $DEBUG > 1;
250 } elsif ( exists $packet->{'customer_session_id'} ) {
252 warn "$me loading customer session\n" if $DEBUG > 1;
253 my $cache = new FS::ClientAPI_SessionCache( {
254 'namespace' => 'FS::ClientAPI::MyAccount',
256 $session = $cache->get($packet->{'customer_session_id'});
258 my $custnum = $session->{'custnum'};
259 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum });
260 return { 'error' => "Can't find your customer record" } unless $cust_main;
261 $agentnum = $cust_main->agentnum;
263 return { 'error' => "Can't resume session" }; #better error message
265 warn "$me done loading customer session\n" if $DEBUG > 1;
269 $signup_info->{'part_pkg'} = [];
271 if ( $packet->{'reg_code'} ) {
273 warn "$me setting package list via reg_code\n" if $DEBUG > 1;
275 $signup_info->{'part_pkg'} =
276 [ map { { 'payby' => [ $_->payby ],
277 'freq_pretty' => $_->freq_pretty,
278 'options' => { $_->options },
282 grep { $_->svcpart($svc_x) }
284 qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
285 'agentnum' => $agentnum, } )
289 $signup_info->{'error'} = 'Unknown registration code'
290 unless @{ $signup_info->{'part_pkg'} };
292 warn "$me done setting package list via reg_code\n" if $DEBUG > 1;
294 } elsif ( $packet->{'promo_code'} ) {
296 warn "$me setting package list via promo_code\n" if $DEBUG > 1;
298 $signup_info->{'part_pkg'} =
299 [ map { { 'payby' => [ $_->payby ],
300 'freq_pretty' => $_->freq_pretty,
301 'options' => { $_->options },
304 grep { $_->svcpart($svc_x) }
305 qsearch( 'part_pkg', { 'promo_code' => {
307 value=>$packet->{'promo_code'}
309 'disabled' => '', } )
312 $signup_info->{'error'} = 'Unknown promotional code'
313 unless @{ $signup_info->{'part_pkg'} };
315 warn "$me done setting package list via promo_code\n" if $DEBUG > 1;
320 warn "$me setting agent-specific payment flag\n" if $DEBUG > 1;
321 my $agent = qsearchs('agent', { 'agentnum' => $agentnum } )
322 or return { 'error' => "Self-service agent #$agentnum does not exist" };
323 warn "$me has agent $agent\n" if $DEBUG > 1;
324 my @paybys = @{ $signup_info->{'payby'} };
325 $signup_info->{'hide_payment_fields'} = [];
327 my $gatewaynum = $conf->config('selfservice-payment_gateway');
330 $force_gateway = qsearchs('payment_gateway', { gatewaynum => $gatewaynum });
331 warn "using forced gateway #$gatewaynum - " .
332 $force_gateway->gateway_username . '@' . $force_gateway->gateway_module
334 die "configured gatewaynum $gatewaynum not found!" if !$force_gateway;
336 foreach my $payby (@paybys) {
337 warn "$me checking $payby payment fields\n" if $DEBUG > 1;
339 if ( FS::payby->realtime($payby) ) {
340 my $gateway = $force_gateway ||
341 $agent->payment_gateway( 'method' => FS::payby->payby2bop($payby),
344 if ( $gateway && $gateway->gateway_namespace
345 eq 'Business::OnlineThirdPartyPayment'
347 warn "$me hiding $payby payment fields\n" if $DEBUG > 1;
351 push @{$signup_info->{'hide_payment_fields'}}, $hide;
353 warn "$me done setting agent-specific payment flag\n" if $DEBUG > 1;
355 warn "$me setting agent-specific package list\n" if $DEBUG > 1;
356 $signup_info->{'part_pkg'} = $signup_info->{'agentnum2part_pkg'}{$agentnum}
357 unless @{ $signup_info->{'part_pkg'} };
358 warn "$me done setting agent-specific package list\n" if $DEBUG > 1;
360 warn "$me setting agent-specific adv. source list\n" if $DEBUG > 1;
361 $signup_info->{'part_referral'} =
365 'table' => 'part_referral',
366 'hashref' => { 'disabled' => '' },
367 'extra_sql' => "AND ( agentnum = $agentnum ".
368 " OR agentnum IS NULL ) ",
372 warn "$me done setting agent-specific adv. source list\n" if $DEBUG > 1;
374 $signup_info->{'agent_name'} = $agent->agent;
376 $signup_info->{'company_name'} = $conf->config('company_name', $agentnum);
378 #some of the above could probably be cached, too
380 my $signup_info_cache_agent = $cache->get("signup_info_cache_agent$agentnum");
382 if ( $signup_info_cache_agent ) {
384 warn "$me loading cached signup info for agentnum $agentnum\n"
389 warn "$me populating signup info cache for agentnum $agentnum\n"
392 $signup_info_cache_agent = {
393 #( map { $_ => scalar( $conf->config($_, $agentnum) ) }
394 # qw( company_name ) ),
395 ( map { $_ => scalar( $conf->config("selfservice-$_", $agentnum ) ) }
396 qw( body_bgcolor box_bgcolor menu_bgcolor ) ),
397 ( map { $_ => join("\n", $conf->config("selfservice-$_", $agentnum ) ) }
398 qw( head body_header body_footer ) ),
399 ( map { $_ => join("\n", $conf->config("signup_server-$_", $agentnum ) ) }
400 qw( terms_of_service ) ),
402 ( map { $_ => scalar($conf->exists($_, $agentnum)) }
403 qw(cust_main-require_phone agent-ship_address) ),
406 if ( $signup_info_cache_agent->{'agent-ship_address'}
407 && $agent->agent_cust_main ) {
409 my $cust_main = $agent->agent_cust_main;
410 my $location = $cust_main->ship_location;
411 $signup_info_cache_agent->{"ship_$_"} = $location->get($_)
412 foreach qw( address1 city county state zip country );
416 $cache->set("signup_info_cache_agent$agentnum", $signup_info_cache_agent);
420 $signup_info->{$_} = $signup_info_cache_agent->{$_}
421 foreach keys %$signup_info_cache_agent;
425 # delete $signup_info->{'part_pkg'};
428 warn "$me sorting package list\n" if $DEBUG > 1;
429 $signup_info->{'part_pkg'} = [ sort { $a->{pkg} cmp $b->{pkg} } # case?
430 @{ $signup_info->{'part_pkg'} }
432 warn "$me done sorting package list\n" if $DEBUG > 1;
434 if ( exists $packet->{'session_id'} ) {
435 my $agent_signup_info = { %$signup_info };
436 delete $agent_signup_info->{agentnum2part_pkg};
437 $agent_signup_info->{'agent'} = $session->{'agent'};
438 return $agent_signup_info;
440 elsif ( exists $packet->{'keys'} ) {
441 my @keys = @{ $packet->{'keys'} };
442 return { map { $_ => $signup_info->{$_} } @keys };
450 sub domain_select_hash {
455 if ($packet->{pkgpart}) {
456 my $part_pkg = qsearchs('part_pkg' => { 'pkgpart' => $packet->{pkgpart} } );
457 #$packet->{svcpart} = $part_pkg->svcpart('svc_acct')
458 $packet->{svcpart} = $part_pkg->svcpart
462 if ($packet->{svcpart}) {
463 my $part_svc = qsearchs('part_svc' => { 'svcpart' => $packet->{svcpart} } );
464 $response->{'domsvc'} = $part_svc->part_svc_column('domsvc')->columnvalue
465 if ($part_svc && $part_svc->part_svc_column('domsvc')->columnflag eq 'D');
468 $response->{'domains'}
469 = { domain_select_hash FS::svc_acct( map { $_ => $packet->{$_} }
479 my $conf = new FS::Conf;
480 my $svc_x = $conf->config('signup_server-service') || 'svc_acct';
482 if ( $svc_x eq 'svc_acct' ) {
484 #things that aren't necessary in base class, but are for signup server
485 #return "Passwords don't match"
486 # if $hashref->{'_password'} ne $hashref->{'_password2'}
487 return { 'error' => gettext('empty_password') }
488 unless length($packet->{'_password'});
489 # a bit inefficient for large numbers of pops
490 return { 'error' => gettext('no_access_number_selected') }
491 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
494 elsif ( $svc_x eq 'svc_pbx' ) {
495 #possibly some validation will be needed
499 if ( exists $packet->{'session_id'} ) {
500 my $cache = new FS::ClientAPI_SessionCache( {
501 'namespace' => 'FS::ClientAPI::Agent',
503 my $session = $cache->get($packet->{'session_id'});
505 $agentnum = $session->{'agentnum'};
507 return { 'error' => "Can't resume session" }; #better error message
510 $agentnum = $packet->{agentnum}
511 || $conf->config('signup_server-default_agentnum');
514 my ($bill_hash, $ship_hash);
515 foreach my $f (FS::cust_main->location_fields) {
516 # avoid having to change this in front-end code
517 $bill_hash->{$f} = $packet->{"bill_$f"} || $packet->{$f};
518 $ship_hash->{$f} = $packet->{"ship_$f"};
521 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
522 # common that are still here and library them.
523 my $template_custnum = $conf->config('signup_server-prepaid-template-custnum');
525 if ( $template_custnum && $packet->{prepaid_shortform} ) {
527 my $template_cust = qsearchs('cust_main', { 'custnum' => $template_custnum } );
528 return { 'error' => 'Configuration error' } unless $template_cust;
529 $cust_main = new FS::cust_main ( {
530 'agentnum' => $agentnum,
531 'refnum' => $packet->{refnum}
532 || $conf->config('signup_server-default_refnum'),
534 ( map { $_ => $template_cust->$_ } qw(
535 last first company daytime night fax
539 ( map { $_ => $packet->{$_} } qw(
540 ss stateid stateid_state
543 payinfo paycvv paydate payname paystate paytype
544 paystart_month paystart_year payissue
547 referral_custnum comments
553 $bill_hash = { $template_cust->bill_location->location_hash };
554 $ship_hash = { $template_cust->ship_location->location_hash };
558 $cust_main = new FS::cust_main ( {
560 'agentnum' => $agentnum,
561 'refnum' => $packet->{refnum}
562 || $conf->config('signup_server-default_refnum'),
564 map { $_ => $packet->{$_} } qw(
565 last first ss company
566 daytime night fax stateid stateid_state
568 payinfo paycvv paydate payname paystate paytype
569 paystart_month paystart_year payissue
572 referral_custnum comments
578 my $bill_location = FS::cust_location->new($bill_hash);
580 my $agent = qsearchs('agent', { 'agentnum' => $agentnum } );
581 if ( $conf->exists('agent-ship_address', $agentnum)
582 && $agent->agent_custnum ) {
584 my $agent_cust_main = $agent->agent_cust_main;
585 my $prefix = length($agent_cust_main->ship_last) ? 'ship_' : '';
586 $ship_location = FS::cust_location->new({
587 $agent_cust_main->ship_location->location_hash
591 # we don't have an equivalent of the "same" checkbox in selfservice
592 # so is there a ship address, and if so, is it different from the billing
594 elsif ( length($ship_hash->{address1}) > 0 and
595 grep { $bill_hash->{$_} ne $ship_hash->{$_} } keys(%$ship_hash)
598 $ship_location = FS::cust_location->new( $ship_hash );
602 $ship_location = $bill_location;
605 $cust_main->set('bill_location' => $bill_location);
606 $cust_main->set('ship_location' => $ship_location);
608 return { 'error' => "Illegal payment type" }
609 unless grep { $_ eq $packet->{'payby'} }
610 $conf->config('signup_server-payby');
612 if (FS::payby->realtime($packet->{payby})
613 and not $conf->exists('signup_server-third_party_as_card')) {
614 my $payby = $packet->{payby};
616 my $agent = qsearchs('agent', { 'agentnum' => $agentnum });
617 return { 'error' => "Unknown reseller" }
621 my $gatewaynum = $conf->config('selfservice-payment_gateway');
623 $gw = qsearchs('payment_gateway', { gatewaynum => $gatewaynum });
624 die "configured gatewaynum $gatewaynum not found!" if !$gw;
627 $gw = $agent->payment_gateway( 'method' => FS::payby->payby2bop($payby),
632 $cust_main->payby('BILL') # MCRD better?
633 if $gw && $gw->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
636 return { 'error' => "CVV2 is required" }
637 if $cust_main->payby =~ /^(CARD|DCRD)$/
638 && ! $cust_main->paycvv
639 && $conf->exists('signup-require_cvv');
641 $cust_main->payinfo($cust_main->daytime)
642 if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
644 my @invoicing_list = $packet->{'invoicing_list'}
645 ? split( /\s*\,\s*/, $packet->{'invoicing_list'} )
648 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
650 return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
653 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
654 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
655 my $svcpart = $part_pkg->svcpart($svc_x);
658 if ( $packet->{'reg_code'} ) {
659 $reg_code = qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
660 'agentnum' => $agentnum, } )
661 or return { 'error' => 'Unknown registration code' };
664 my $cust_pkg = new FS::cust_pkg ( {
665 #later#'custnum' => $custnum,
666 'pkgpart' => $packet->{'pkgpart'},
667 'promo_code' => $packet->{'promo_code'},
668 'reg_code' => $packet->{'reg_code'},
670 #my $error = $cust_pkg->check;
671 #return { 'error' => $error } if $error;
673 #should be all auto-magic and shit
675 if ( $svc_x eq 'svc_acct' ) {
677 my $svc = new FS::svc_acct {
678 'svcpart' => $svcpart,
679 map { $_ => $packet->{$_} }
680 qw( username _password sec_phrase popnum domsvc ),
685 while ( exists($packet->{"snarf_machine$snarfnum"})
686 && length($packet->{"snarf_machine$snarfnum"}) ) {
687 my $acct_snarf = new FS::acct_snarf ( {
688 'machine' => $packet->{"snarf_machine$snarfnum"},
689 'protocol' => $packet->{"snarf_protocol$snarfnum"},
690 'username' => $packet->{"snarf_username$snarfnum"},
691 '_password' => $packet->{"snarf_password$snarfnum"},
694 push @acct_snarf, $acct_snarf;
696 $svc->child_objects( \@acct_snarf );
699 } elsif ( $svc_x eq 'svc_phone' ) {
701 push @svc, new FS::svc_phone ( {
702 'svcpart' => $svcpart,
703 map { $_ => $packet->{$_} }
704 qw( countrycode phonenum sip_password pin ),
707 } elsif ( $svc_x eq 'svc_pbx' ) {
709 push @svc, new FS::svc_pbx ( {
710 'svcpart' => $svcpart,
711 map { $_ => $packet->{$_} }
716 die "unknown signup service $svc_x";
719 if ($packet->{'mac_addr'} && $conf->exists('signup_server-mac_addr_svcparts'))
722 my %mac_addr_svcparts = map { $_ => 1 }
723 $conf->config('signup_server-mac_addr_svcparts');
724 my @pkg_svc = grep { $_->quantity && $mac_addr_svcparts{$_->svcpart} }
725 $cust_pkg->part_pkg->pkg_svc;
727 return { 'error' => 'No service defined to assign mac address' }
730 my $svc = new FS::svc_acct {
731 'svcpart' => $pkg_svc[0]->svcpart, #multiple matches? alas..
732 'username' => $packet->{'mac_addr'},
733 '_password' => '', #blank as requested (set passwordmin to 0)
740 foreach my $svc ( @svc ) {
741 my $y = $svc->setdefault; # arguably should be in new method
742 return { 'error' => $y } if $y && !ref($y);
743 #$error = $svc->check;
744 #return { 'error' => $error } if $error;
747 #setup a job dependancy to delay provisioning
748 my $placeholder = new FS::queue ( {
749 'job' => 'FS::ClientAPI::Signup::__placeholder',
750 'status' => 'locked',
752 my $error = $placeholder->insert;
753 return { 'error' => $error } if $error;
756 tie my %hash, 'Tie::RefHash';
757 %hash = ( $cust_pkg => \@svc );
759 $error = $cust_main->insert(
762 'depend_jobnum' => $placeholder->jobnum,
765 my $perror = $placeholder->delete;
766 $error .= " (Additionally, error removing placeholder: $perror)" if $perror;
767 return { 'error' => $error };
770 if ( $conf->exists('signup_server-realtime') ) {
772 #warn "$me Billing customer...\n" if $Debug;
774 my $bill_error = $cust_main->bill( 'depend_jobnum'=>$placeholder->jobnum );
775 #warn "$me error billing new customer: $bill_error"
778 $bill_error = $cust_main->apply_payments_and_credits;
779 #warn "$me error applying payments and credits for".
780 # " new customer: $bill_error"
783 unless ( $packet->{payby} eq 'PREPAY' ) {
784 $bill_error = $cust_main->realtime_collect(
785 method => FS::payby->payby2bop( $packet->{payby} ),
786 depend_jobnum => $placeholder->jobnum,
789 #warn "$me error collecting from new customer: $bill_error"
793 if ($bill_error && ref($bill_error) eq 'HASH') {
794 return { 'error' => '_collect',
795 ( map { $_ => $bill_error->{$_} }
796 qw(popup_url reference collectitems)
798 amount => $cust_main->balance,
802 $bill_error = $cust_main->apply_payments_and_credits;
803 #warn "$me error applying payments and credits for".
804 # " new customer: $bill_error"
807 if ( $cust_main->balance > 0 ) {
809 #this makes sense. credit is "un-doing" the invoice
810 $cust_main->credit( $cust_main->balance, 'signup server decline',
811 'reason_type' => $conf->config('signup_credit_type'),
813 $cust_main->apply_credits;
815 #should check list for errors...
816 #$cust_main->suspend;
817 local $FS::svc_Common::noexport_hack = 1;
818 $cust_main->cancel('quiet'=>1);
820 my $perror = $placeholder->depended_delete;
821 warn "error removing provisioning jobs after decline: $perror" if $perror;
823 $perror = $placeholder->delete;
824 warn "error removing placeholder after decline: $perror" if $perror;
827 return { 'error' => '_decline' };
833 $error = $reg_code->delete;
834 return { 'error' => $error } if $error;
837 $error = $placeholder->delete;
838 return { 'error' => $error } if $error;
840 if ( $conf->exists('signup-duplicate_cc-warn_hours') ) {
841 my $hours = $conf->config('signup-duplicate_cc-warn_hours');
842 my $ban = new FS::banned_pay $cust_main->_new_banned_pay_hashref;
843 $ban->end_date( int( time + $hours*3600 ) );
844 $ban->bantype('warn');
845 $ban->reason('signup-duplicate_cc-warn_hours');
846 $error = $ban->insert;
847 warn "WARNING: error inserting temporary banned_pay for ".
848 " signup-duplicate_cc-warn_hours (proceeding anyway): $error"
852 my %return = ( 'error' => '',
853 'signup_service' => $svc_x,
854 'custnum' => $cust_main->custnum,
859 $return{'svcnum'} = $svc[0]->svcnum;
861 if ( $svc_x eq 'svc_acct' ) {
862 $return{$_} = $svc[0]->$_() for qw( username _password );
863 } elsif ( $svc_x eq 'svc_phone' ) {
864 $return{$_} = $svc[0]->$_() for qw(countrycode phonenum sip_password pin);
865 } elsif ( $svc_x eq 'svc_pbx' ) {
866 #$return{$_} = $svc[0]->$_() for qw( ) #nothing yet
868 return {'error' => "configuration error: unknown signup service $svc_x"};
869 #die "unknown signup service $svc_x";
870 # return an error that's visible to someone somewhere
879 #false laziness w/ above
880 # fresh restart to support "free account" portals with 3.x/4.x-style
881 # addressless accounts
882 # and a contact (for self-service login)
883 sub new_customer_minimal {
886 my $conf = new FS::Conf;
887 my $svc_x = $conf->config('signup_server-service') || 'svc_acct';
889 if ( $svc_x eq 'svc_acct' ) {
891 #things that aren't necessary in base class, but are for signup server
892 #return "Passwords don't match"
893 # if $hashref->{'_password'} ne $hashref->{'_password2'}
894 return { 'error' => gettext('empty_password') }
895 unless length($packet->{'_password'});
896 # a bit inefficient for large numbers of pops
897 return { 'error' => gettext('no_access_number_selected') }
898 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
901 elsif ( $svc_x eq 'svc_pbx' ) {
902 #possibly some validation will be needed
906 if ( exists $packet->{'session_id'} ) {
907 my $cache = new FS::ClientAPI_SessionCache( {
908 'namespace' => 'FS::ClientAPI::Agent',
910 my $session = $cache->get($packet->{'session_id'});
912 $agentnum = $session->{'agentnum'};
914 return { 'error' => "Can't resume session" }; #better error message
917 $agentnum = $packet->{agentnum}
918 || $conf->config('signup_server-default_agentnum');
921 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
922 # common that are still here and library them.
924 my $cust_main = new FS::cust_main ( {
926 'agentnum' => $agentnum,
927 'refnum' => $packet->{refnum}
928 || $conf->config('signup_server-default_refnum'),
931 map { $_ => $packet->{$_} } qw(
932 last first ss company
938 my @invoicing_list = $packet->{'invoicing_list'}
939 ? split( /\s*\,\s*/, $packet->{'invoicing_list'} )
943 tie my %hash, 'Tie::RefHash', ();
946 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
952 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
953 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
954 my $svcpart = $part_pkg->svcpart($svc_x);
956 my $cust_pkg = new FS::cust_pkg ( {
957 #later#'custnum' => $custnum,
958 'pkgpart' => $packet->{'pkgpart'},
960 #my $error = $cust_pkg->check;
961 #return { 'error' => $error } if $error;
963 #should be all auto-magic and shit
964 if ( $svc_x eq 'svc_acct' ) {
966 my $svc = new FS::svc_acct {
967 'svcpart' => $svcpart,
968 map { $_ => $packet->{$_} }
969 qw( username _password sec_phrase popnum domsvc ),
974 } elsif ( $svc_x eq 'svc_phone' ) {
976 push @svc, new FS::svc_phone ( {
977 'svcpart' => $svcpart,
978 map { $_ => $packet->{$_} }
979 qw( countrycode phonenum sip_password pin ),
982 } elsif ( $svc_x eq 'svc_pbx' ) {
984 push @svc, new FS::svc_pbx ( {
985 'svcpart' => $svcpart,
986 map { $_ => $packet->{$_} }
991 die "unknown signup service $svc_x";
994 foreach my $svc ( @svc ) {
995 my $y = $svc->setdefault; # arguably should be in new method
996 return { 'error' => $y } if $y && !ref($y);
997 #$error = $svc->check;
998 #return { 'error' => $error } if $error;
1002 tie my %hash, 'Tie::RefHash';
1003 $hash{ $cust_pkg } = \@svc;
1008 if ( $invoicing_list[0] && $packet->{'_password'} ) {
1010 new FS::contact { 'first' => $cust_main->first,
1011 'last' => $cust_main->get('last'),
1012 '_password' => $packet->{'_password'},
1013 'emailaddress' => $invoicing_list[0],
1014 'selfservice_access' => 'Y',
1019 my $error = $cust_main->insert(
1024 return { 'error' => $error } if $error;
1026 my $session = { 'custnum' => $cust_main->custnum };
1030 $session_id = sha1_hex(time(). {}. rand(). $$)
1031 } until ( ! defined _myaccount_cache->get($session_id) ); #just in case
1033 _cache->set( $session_id, $session, '1 hour' ); # 1 hour?
1035 my %return = ( 'error' => '',
1036 'signup_service' => $svc_x,
1037 'custnum' => $cust_main->custnum,
1038 'session_id' => $session_id,
1043 $return{'svcnum'} = $svc[0]->svcnum;
1045 if ( $svc_x eq 'svc_acct' ) {
1046 $return{$_} = $svc[0]->$_() for qw( username _password );
1047 } elsif ( $svc_x eq 'svc_phone' ) {
1048 $return{$_} = $svc[0]->$_() for qw(countrycode phonenum sip_password pin);
1049 } elsif ( $svc_x eq 'svc_pbx' ) {
1050 #$return{$_} = $svc[0]->$_() for qw( ) #nothing yet
1052 return {'error' => "configuration error: unknown signup service $svc_x"};
1053 #die "unknown signup service $svc_x";
1054 # return an error that's visible to someone somewhere
1063 use vars qw( $myaccount_cache );
1064 sub _myaccount_cache {
1065 $myaccount_cache ||= new FS::ClientAPI_SessionCache( {
1066 'namespace' => 'FS::ClientAPI::MyAccount',
1070 sub capture_payment {
1073 warn "$me capture_payment called on $packet\n" if $DEBUG;
1076 # identify processor/gateway from called back URL
1079 my $conf = new FS::Conf;
1081 my $payment_gateway;
1082 if ( my $gwnum = $conf->config('selfservice-payment_gateway') ) {
1083 $payment_gateway = qsearchs('payment_gateway', { 'gatewaynum' => $gwnum })
1084 or die "configured gatewaynum $gwnum not found!";
1087 my $url = $packet->{url};
1089 $payment_gateway = qsearchs('payment_gateway',
1090 { 'gateway_callback_url' => popurl(0, $url) }
1092 if (!$payment_gateway) {
1094 my ( $processor, $login, $password, $action, @bop_options ) =
1095 $conf->config('business-onlinepayment');
1096 $action ||= 'normal authorization';
1097 pop @bop_options if scalar(@bop_options) % 2 && $bop_options[-1] =~ /^\s*$/;
1098 die "No real-time processor is enabled - ".
1099 "did you set the business-onlinepayment configuration value?\n"
1102 $payment_gateway = new FS::payment_gateway( {
1103 gateway_namespace => $conf->config('business-onlinepayment-namespace'),
1104 gateway_module => $processor,
1105 gateway_username => $login,
1106 gateway_password => $password,
1107 gateway_action => $action,
1108 options => [ ( @bop_options ) ],
1113 die "No real-time third party processor is enabled - ".
1114 "did you set the business-onlinepayment configuration value?\n*"
1115 unless $payment_gateway->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
1118 # locate pending transaction
1121 eval "use Business::OnlineThirdPartyPayment";
1125 new Business::OnlineThirdPartyPayment( $payment_gateway->gateway_module,
1126 @{ [ $payment_gateway->options ] },
1129 my $paypendingnum = $transaction->reference($packet->{data});
1131 my $cust_pay_pending =
1132 qsearchs('cust_pay_pending', { paypendingnum => $paypendingnum } );
1134 unless ($cust_pay_pending) {
1135 my $bill_error = "No payment is being processed with id $paypendingnum".
1136 "; Transaction aborted.";
1137 return { error => '_decline', bill_error => $bill_error };
1140 if ($cust_pay_pending->status ne 'thirdparty') {
1141 my $bill_error = "Payment with id $paypendingnum is not thirdparty, but ".
1142 $cust_pay_pending->status. "; Transaction aborted.";
1143 return { error => '_decline', bill_error => $bill_error };
1146 my $cust_main = $cust_pay_pending->cust_main;
1147 if ( $packet->{cancel} ) {
1148 # the user has chosen not to make this payment
1149 # (probably should be a separate API call, but I don't want to duplicate
1150 # all of the above...which should eventually go away)
1151 my $error = $cust_pay_pending->delete;
1152 # don't show any errors related to this; they're not meaningful
1153 warn "error canceling pending payment $paypendingnum: $error\n" if $error;
1154 return { 'error' => '_cancel',
1155 'session_id' => $cust_pay_pending->session_id };
1157 # create the payment
1159 $cust_main->realtime_botpp_capture( $cust_pay_pending,
1164 return { 'error' => ( $bill_error->{bill_error} ? '_decline' : '' ),