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;
32 $me = '[FS::ClientAPI::Signup]';
35 warn "$me clear_cache called\n" if $DEBUG;
36 my $cache = new FS::ClientAPI_SessionCache( {
37 'namespace' => 'FS::ClientAPI::Signup',
46 warn "$me signup_info called on $packet\n" if $DEBUG;
48 my $conf = new FS::Conf;
49 my $svc_x = $conf->config('signup_server-service') || 'svc_acct';
51 my $cache = new FS::ClientAPI_SessionCache( {
52 'namespace' => 'FS::ClientAPI::Signup',
54 my $signup_info_cache = $cache->get('signup_info_cache');
56 if ( $signup_info_cache ) {
58 warn "$me loading cached signup info\n" if $DEBUG > 1;
62 warn "$me populating signup info cache\n" if $DEBUG > 1;
64 my $agentnum2part_pkg =
68 my $href = $agent->pkgpart_hashref;
71 map { { 'payby' => [ $_->payby ],
72 'freq_pretty' => $_->freq_pretty,
73 'options' => { $_->options },
76 grep { $_->svcpart($svc_x)
77 && ( $href->{ $_->pkgpart }
79 && $_->agentnum == $agent->agentnum
83 qsearch( 'part_pkg', { 'disabled' => '' } )
85 } qsearch('agent', { 'disabled' => '' })
88 my $msgcat = { map { $_=>gettext($_) }
89 qw( passwords_dont_match invalid_card unknown_card_type
90 not_a empty_password illegal_or_empty_text )
92 warn "msgcat: ". Dumper($msgcat). "\n" if $DEBUG > 2;
94 my $label = { map { $_ => FS::Msgcat::_gettext($_) }
95 qw( stateid stateid_state )
97 warn "label: ". Dumper($label). "\n" if $DEBUG > 2;
99 my @agent_fields = qw( agentnum agent );
101 my @bools = qw( emailinvoiceonly security_phrase );
103 my @signup_bools = qw( no_company recommend_daytime recommend_email );
105 my @signup_server_scalars = qw( default_pkgpart default_svcpart default_domsvc );
107 my @selfservice_textareas = qw( head body_header body_footer );
109 my @selfservice_scalars = qw(
110 body_bgcolor box_bgcolor
111 text_color link_color vlink_color hlink_color alink_color
112 font title_color title_align title_size menu_bgcolor menu_fontsize
115 #XXX my @selfservice_bools = qw(
116 # menu_skipblanks menu_skipheadings menu_nounderline
119 #my $selfservice_binaries = qw(
120 # title_left_image title_right_image
121 # menu_top_image menu_body_image menu_bottom_image
124 $signup_info_cache = {
126 'cust_main_county' => [ map $_->hashref,
127 qsearch('cust_main_county', {} )
130 'agent' => [ map { my $agent = $_;
131 +{ map { $_ => $agent->get($_) } @agent_fields }
133 qsearch('agent', { 'disabled' => '' } )
136 'part_referral' => [ map $_->hashref,
137 qsearch('part_referral', { 'disabled' => '' } )
140 'agentnum2part_pkg' => $agentnum2part_pkg,
142 'svc_acct_pop' => [ map $_->hashref, qsearch('svc_acct_pop',{} ) ],
144 'emailinvoiceonly' => $conf->exists('emailinvoiceonly'),
146 'security_phrase' => $conf->exists('security_phrase'),
148 'nomadix' => $conf->exists('signup_server-nomadix'),
150 'payby' => [ $conf->config('signup_server-payby') ],
152 'payby_longname' => [ map { FS::payby->longname($_) }
153 $conf->config('signup_server-payby') ],
155 'card_types' => card_types(),
157 ( map { $_ => $conf->exists("signup-$_") } @signup_bools ),
159 ( map { $_ => scalar($conf->config("signup_server-$_")) }
160 @signup_server_scalars
163 ( map { $_ => join("\n", $conf->config("selfservice-$_")) }
164 @selfservice_textareas
166 ( map { $_ => scalar($conf->config("selfservice-$_")) }
170 #( map { $_ => scalar($conf->config_binary("selfservice-$_")) }
171 # @selfservice_binaries
174 'agentnum2part_pkg' => $agentnum2part_pkg,
175 'svc_acct_pop' => [ map $_->hashref, qsearch('svc_acct_pop',{} ) ],
176 'nomadix' => $conf->exists('signup_server-nomadix'),
177 'payby' => [ $conf->config('signup_server-payby') ],
178 'card_types' => card_types(),
179 'paytypes' => [ FS::cust_payby->paytypes ],
181 'require_cvv' => $conf->exists('signup-require_cvv'),
182 'stateid_enabled' => $conf->exists('show_stateid'),
183 'paystate_enabled' => $conf->exists('show_bankstate'),
184 'exempt_groups' => [ grep /\S/, $conf->config('tax-cust_exempt-groups') ],
188 'statedefault' => scalar($conf->config('statedefault')) || 'CA',
189 'countrydefault' => scalar($conf->config('countrydefault')) || 'US',
190 'refnum' => scalar($conf->config('signup_server-default_refnum')),
191 'signup_service' => $svc_x,
192 'company_name' => scalar($conf->config('company_name')),
194 'logo' => scalar($conf->config_binary('logo.png')),
195 'prepaid_template_custnum' => $conf->exists('signup_server-prepaid-template-custnum'),
198 $cache->set('signup_info_cache', $signup_info_cache);
202 my $signup_info = { %$signup_info_cache };
203 warn "$me signup info loaded\n" if $DEBUG > 1;
204 warn Dumper($signup_info). "\n" if $DEBUG > 2;
206 my @addl = qw( signup_server-classnum2 signup_server-classnum3 );
208 if ( grep { $conf->exists($_) } @addl ) {
210 $signup_info->{optional_packages} = [];
212 foreach my $addl ( @addl ) {
214 warn "$me adding optional package info\n" if $DEBUG > 1;
216 my $classnum = $conf->config($addl) or next;
219 'freq_pretty' => $_->freq_pretty,
220 'options' => { $_->options },
224 qsearch( 'part_pkg', { classnum => $classnum } );
226 push @{$signup_info->{optional_packages}}, \@pkgs;
228 warn "$me done adding opt. package info for $classnum\n" if $DEBUG > 1;
234 my $agentnum = $packet->{'agentnum'}
235 || $conf->config('signup_server-default_agentnum');
236 $agentnum =~ /^(\d*)$/ or die "illegal agentnum";
240 if ( exists $packet->{'session_id'} ) {
242 warn "$me loading agent session\n" if $DEBUG > 1;
243 my $cache = new FS::ClientAPI_SessionCache( {
244 'namespace' => 'FS::ClientAPI::Agent',
246 $session = $cache->get($packet->{'session_id'});
248 $agentnum = $session->{'agentnum'};
250 return { 'error' => "Can't resume session" }; #better error message
252 warn "$me done loading agent session\n" if $DEBUG > 1;
254 } elsif ( exists $packet->{'customer_session_id'} ) {
256 warn "$me loading customer session\n" if $DEBUG > 1;
257 my $cache = new FS::ClientAPI_SessionCache( {
258 'namespace' => 'FS::ClientAPI::MyAccount',
260 $session = $cache->get($packet->{'customer_session_id'});
262 my $custnum = $session->{'custnum'};
263 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum });
264 return { 'error' => "Can't find your customer record" } unless $cust_main;
265 $agentnum = $cust_main->agentnum;
267 return { 'error' => "Can't resume session" }; #better error message
269 warn "$me done loading customer session\n" if $DEBUG > 1;
273 $signup_info->{'part_pkg'} = [];
275 if ( $packet->{'reg_code'} ) {
277 warn "$me setting package list via reg_code\n" if $DEBUG > 1;
279 $signup_info->{'part_pkg'} =
280 [ map { { 'payby' => [ $_->payby ],
281 'freq_pretty' => $_->freq_pretty,
282 'options' => { $_->options },
286 grep { $_->svcpart($svc_x) }
288 qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
289 'agentnum' => $agentnum, } )
293 $signup_info->{'error'} = 'Unknown registration code'
294 unless @{ $signup_info->{'part_pkg'} };
296 warn "$me done setting package list via reg_code\n" if $DEBUG > 1;
298 } elsif ( $packet->{'promo_code'} ) {
300 warn "$me setting package list via promo_code\n" if $DEBUG > 1;
302 $signup_info->{'part_pkg'} =
303 [ map { { 'payby' => [ $_->payby ],
304 'freq_pretty' => $_->freq_pretty,
305 'options' => { $_->options },
308 grep { $_->svcpart($svc_x) }
309 qsearch( 'part_pkg', { 'promo_code' => {
311 value=>$packet->{'promo_code'}
313 'disabled' => '', } )
316 $signup_info->{'error'} = 'Unknown promotional code'
317 unless @{ $signup_info->{'part_pkg'} };
319 warn "$me done setting package list via promo_code\n" if $DEBUG > 1;
324 warn "$me setting agent-specific payment flag\n" if $DEBUG > 1;
325 my $agent = qsearchs('agent', { 'agentnum' => $agentnum } )
326 or return { 'error' => "Self-service agent #$agentnum does not exist" };
327 warn "$me has agent $agent\n" if $DEBUG > 1;
328 my @paybys = @{ $signup_info->{'payby'} };
329 $signup_info->{'hide_payment_fields'} = [];
331 my $gatewaynum = $conf->config('selfservice-payment_gateway');
334 $force_gateway = qsearchs('payment_gateway', { gatewaynum => $gatewaynum });
335 warn "using forced gateway #$gatewaynum - " .
336 $force_gateway->gateway_username . '@' . $force_gateway->gateway_module
338 die "configured gatewaynum $gatewaynum not found!" if !$force_gateway;
340 foreach my $payby (@paybys) {
341 warn "$me checking $payby payment fields\n" if $DEBUG > 1;
343 if ( FS::payby->realtime($payby) ) {
344 my $gateway = $force_gateway ||
345 $agent->payment_gateway( 'method' => FS::payby->payby2bop($payby),
348 if ( $gateway && $gateway->gateway_namespace
349 eq 'Business::OnlineThirdPartyPayment'
351 warn "$me hiding $payby payment fields\n" if $DEBUG > 1;
355 push @{$signup_info->{'hide_payment_fields'}}, $hide;
357 warn "$me done setting agent-specific payment flag\n" if $DEBUG > 1;
359 warn "$me setting agent-specific package list\n" if $DEBUG > 1;
360 $signup_info->{'part_pkg'} = $signup_info->{'agentnum2part_pkg'}{$agentnum}
361 unless @{ $signup_info->{'part_pkg'} };
362 warn "$me done setting agent-specific package list\n" if $DEBUG > 1;
364 warn "$me setting agent-specific adv. source list\n" if $DEBUG > 1;
365 $signup_info->{'part_referral'} =
369 'table' => 'part_referral',
370 'hashref' => { 'disabled' => '' },
371 'extra_sql' => "AND ( agentnum = $agentnum ".
372 " OR agentnum IS NULL ) ",
376 warn "$me done setting agent-specific adv. source list\n" if $DEBUG > 1;
378 $signup_info->{'agent_name'} = $agent->agent;
380 $signup_info->{'company_name'} = $conf->config('company_name', $agentnum);
382 #some of the above could probably be cached, too
384 my $signup_info_cache_agent = $cache->get("signup_info_cache_agent$agentnum");
386 if ( $signup_info_cache_agent ) {
388 warn "$me loading cached signup info for agentnum $agentnum\n"
393 warn "$me populating signup info cache for agentnum $agentnum\n"
396 $signup_info_cache_agent = {
397 #( map { $_ => scalar( $conf->config($_, $agentnum) ) }
398 # qw( company_name ) ),
399 ( map { $_ => scalar( $conf->config("selfservice-$_", $agentnum ) ) }
400 qw( body_bgcolor box_bgcolor menu_bgcolor ) ),
401 ( map { $_ => join("\n", $conf->config("selfservice-$_", $agentnum ) ) }
402 qw( head body_header body_footer ) ),
403 ( map { $_ => join("\n", $conf->config("signup_server-$_", $agentnum ) ) }
404 qw( terms_of_service ) ),
406 ( map { $_ => scalar($conf->exists($_, $agentnum)) }
407 qw(cust_main-require_phone agent-ship_address) ),
410 if ( $signup_info_cache_agent->{'agent-ship_address'}
411 && $agent->agent_cust_main ) {
413 my $cust_main = $agent->agent_cust_main;
414 my $location = $cust_main->ship_location;
415 $signup_info_cache_agent->{"ship_$_"} = $location->get($_)
416 foreach qw( address1 city county state zip country );
420 $cache->set("signup_info_cache_agent$agentnum", $signup_info_cache_agent);
424 $signup_info->{$_} = $signup_info_cache_agent->{$_}
425 foreach keys %$signup_info_cache_agent;
429 # delete $signup_info->{'part_pkg'};
432 warn "$me sorting package list\n" if $DEBUG > 1;
433 $signup_info->{'part_pkg'} = [ sort { $a->{pkg} cmp $b->{pkg} } # case?
434 @{ $signup_info->{'part_pkg'} }
436 warn "$me done sorting package list\n" if $DEBUG > 1;
438 if ( exists $packet->{'session_id'} ) {
439 my $agent_signup_info = { %$signup_info };
440 delete $agent_signup_info->{agentnum2part_pkg};
441 $agent_signup_info->{'agent'} = $session->{'agent'};
442 return $agent_signup_info;
444 elsif ( exists $packet->{'keys'} ) {
445 my @keys = @{ $packet->{'keys'} };
446 return { map { $_ => $signup_info->{$_} } @keys };
454 sub domain_select_hash {
459 if ($packet->{pkgpart}) {
460 my $part_pkg = qsearchs('part_pkg' => { 'pkgpart' => $packet->{pkgpart} } );
461 #$packet->{svcpart} = $part_pkg->svcpart('svc_acct')
462 $packet->{svcpart} = $part_pkg->svcpart
466 if ($packet->{svcpart}) {
467 my $part_svc = qsearchs('part_svc' => { 'svcpart' => $packet->{svcpart} } );
468 $response->{'domsvc'} = $part_svc->part_svc_column('domsvc')->columnvalue
469 if ($part_svc && $part_svc->part_svc_column('domsvc')->columnflag eq 'D');
472 $response->{'domains'}
473 = { domain_select_hash FS::svc_acct( map { $_ => $packet->{$_} }
483 my $conf = new FS::Conf;
484 my $svc_x = $conf->config('signup_server-service') || 'svc_acct';
486 if ( $svc_x eq 'svc_acct' ) {
488 #things that aren't necessary in base class, but are for signup server
489 #return "Passwords don't match"
490 # if $hashref->{'_password'} ne $hashref->{'_password2'}
491 return { 'error' => gettext('empty_password') }
492 unless length($packet->{'_password'});
493 # a bit inefficient for large numbers of pops
494 return { 'error' => gettext('no_access_number_selected') }
495 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
498 elsif ( $svc_x eq 'svc_pbx' ) {
499 #possibly some validation will be needed
503 if ( exists $packet->{'session_id'} ) {
504 my $cache = new FS::ClientAPI_SessionCache( {
505 'namespace' => 'FS::ClientAPI::Agent',
507 my $session = $cache->get($packet->{'session_id'});
509 $agentnum = $session->{'agentnum'};
511 return { 'error' => "Can't resume session" }; #better error message
514 $agentnum = $packet->{agentnum}
515 || $conf->config('signup_server-default_agentnum');
518 my ($bill_hash, $ship_hash);
519 foreach my $f (FS::cust_main->location_fields) {
520 # avoid having to change this in front-end code
521 $bill_hash->{$f} = $packet->{"bill_$f"} || $packet->{$f};
522 $ship_hash->{$f} = $packet->{"ship_$f"};
525 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
526 # common that are still here and library them.
529 'agentnum' => $agentnum,
530 'refnum' => $packet->{refnum}
531 || $conf->config('signup_server-default_refnum'),
532 'tagnum' => [ FS::part_tag->default_tags ],
534 ( map { $_ => $packet->{$_} } qw(
536 ss stateid stateid_state
538 referral_custnum comments
544 my %insert_options = ();
545 if ( $packet->{payby} =~ /^(CARD|DCRD|CHEK|DCHK)$/ ) {
546 $insert_options{cust_payby} = [
548 map { $_ => $packet->{$_} } qw(
550 payinfo paycvv paydate payname paystate paytype
551 paystart_month paystart_year payissue
558 my $template_custnum = $conf->config('signup_server-prepaid-template-custnum');
560 if ( $template_custnum && $packet->{prepaid_shortform} ) {
562 my $template_cust = qsearchs('cust_main', { 'custnum' => $template_custnum } );
563 return { 'error' => 'Configuration error' } unless $template_cust;
564 $cust_main = new FS::cust_main ( {
566 map { $_ => $template_cust->$_ } qw(
567 last first company daytime night fax mobile
571 $bill_hash = { $template_cust->bill_location->location_hash };
572 $ship_hash = { $template_cust->ship_location->location_hash };
576 $cust_main = new FS::cust_main ( {
578 map { $_ => $packet->{$_} } qw(
579 last first company daytime night fax mobile
585 my $bill_location = FS::cust_location->new($bill_hash);
587 my $agent = qsearchs('agent', { 'agentnum' => $agentnum } );
588 if ( $conf->exists('agent-ship_address', $agentnum)
589 && $agent->agent_custnum ) {
591 my $agent_cust_main = $agent->agent_cust_main;
592 my $prefix = length($agent_cust_main->ship_last) ? 'ship_' : '';
593 $ship_location = FS::cust_location->new({
594 $agent_cust_main->ship_location->location_hash
598 # we don't have an equivalent of the "same" checkbox in selfservice
599 # so is there a ship address, and if so, is it different from the billing
601 elsif ( length($ship_hash->{address1}) > 0 and
602 grep { $bill_hash->{$_} ne $ship_hash->{$_} } keys(%$ship_hash)
605 $ship_location = FS::cust_location->new( $ship_hash );
609 $ship_location = $bill_location;
612 $cust_main->set('bill_location' => $bill_location);
613 $cust_main->set('ship_location' => $ship_location);
615 return { 'error' => "Illegal payment type" }
616 unless grep { $_ eq $packet->{'payby'} }
617 $conf->config('signup_server-payby');
619 if (FS::payby->realtime($packet->{payby})
620 and not $conf->exists('signup_server-third_party_as_card')) {
621 my $payby = $packet->{payby};
623 my $agent = qsearchs('agent', { 'agentnum' => $agentnum });
624 return { 'error' => "Unknown reseller" }
628 my $gatewaynum = $conf->config('selfservice-payment_gateway');
630 $gw = qsearchs('payment_gateway', { gatewaynum => $gatewaynum });
631 die "configured gatewaynum $gatewaynum not found!" if !$gw;
634 $gw = $agent->payment_gateway( 'method' => FS::payby->payby2bop($payby),
639 $cust_main->payby('BILL') # MCRD better? no, that's for something else
640 if $gw && $gw->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
643 return { 'error' => "CVV2 is required" }
644 if $cust_main->payby =~ /^(CARD|DCRD)$/
645 && ! $cust_main->paycvv
646 && $conf->exists('signup-require_cvv');
648 my @invoicing_list = $packet->{'invoicing_list'}
649 ? split( /\s*\,\s*/, $packet->{'invoicing_list'} )
652 my @exempt_groups = grep /\S/, $conf->config('tax-cust_exempt-groups');
653 my @tax_exempt = grep { $packet->{"tax_$_"} eq 'Y' } @exempt_groups;
654 $insert_options{'tax_exemption'} = {
655 map { $_ => $packet->{"tax_$_".'_num'} } @tax_exempt
658 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
660 return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
663 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
664 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
667 if ( $packet->{'reg_code'} ) {
668 $reg_code = qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
669 'agentnum' => $agentnum, } )
670 or return { 'error' => 'Unknown registration code' };
673 my $cust_pkg = new FS::cust_pkg ( {
674 #later#'custnum' => $custnum,
675 'pkgpart' => $packet->{'pkgpart'},
676 'promo_code' => $packet->{'promo_code'},
677 'reg_code' => $packet->{'reg_code'},
679 #my $error = $cust_pkg->check;
680 #return { 'error' => $error } if $error;
683 unless ( $svc_x eq 'none' ) {
685 my $svcpart = $part_pkg->svcpart($svc_x);
686 #should be all auto-magic and shit
687 if ( $svc_x eq 'svc_acct' ) {
689 my $svc = new FS::svc_acct {
690 'svcpart' => $svcpart,
691 map { $_ => $packet->{$_} }
692 qw( username _password sec_phrase popnum domsvc ),
695 my $error = $svc->is_password_allowed($packet->{_password});
696 return { error => $error } if $error;
700 while ( exists($packet->{"snarf_machine$snarfnum"})
701 && length($packet->{"snarf_machine$snarfnum"}) ) {
702 my $acct_snarf = new FS::acct_snarf ( {
703 'machine' => $packet->{"snarf_machine$snarfnum"},
704 'protocol' => $packet->{"snarf_protocol$snarfnum"},
705 'username' => $packet->{"snarf_username$snarfnum"},
706 '_password' => $packet->{"snarf_password$snarfnum"},
709 push @acct_snarf, $acct_snarf;
711 $svc->child_objects( \@acct_snarf );
714 } elsif ( $svc_x eq 'svc_phone' ) {
716 push @svc, new FS::svc_phone ( {
717 'svcpart' => $svcpart,
718 map { $_ => $packet->{$_} }
719 qw( countrycode phonenum sip_password pin ),
722 } elsif ( $svc_x eq 'svc_pbx' ) {
724 push @svc, new FS::svc_pbx ( {
725 'svcpart' => $svcpart,
726 map { $_ => $packet->{$_} }
731 die "unknown signup service $svc_x";
736 if ($packet->{'mac_addr'} && $conf->exists('signup_server-mac_addr_svcparts'))
739 my %mac_addr_svcparts = map { $_ => 1 }
740 $conf->config('signup_server-mac_addr_svcparts');
741 my @pkg_svc = grep { $_->quantity && $mac_addr_svcparts{$_->svcpart} }
742 $cust_pkg->part_pkg->pkg_svc;
744 return { 'error' => 'No service defined to assign mac address' }
747 my $svc = new FS::svc_acct {
748 'svcpart' => $pkg_svc[0]->svcpart, #multiple matches? alas..
749 'username' => $packet->{'mac_addr'},
750 '_password' => '', #blank as requested (set passwordmin to 0)
757 foreach my $svc ( @svc ) {
758 my $y = $svc->setdefault; # arguably should be in new method
759 return { 'error' => $y } if $y && !ref($y);
760 #$error = $svc->check;
761 #return { 'error' => $error } if $error;
764 #setup a job dependancy to delay provisioning
765 my $placeholder = new FS::queue ( {
766 'job' => 'FS::ClientAPI::Signup::__placeholder',
767 'status' => 'locked',
769 my $error = $placeholder->insert;
770 return { 'error' => $error } if $error;
773 tie my %hash, 'Tie::RefHash';
774 %hash = ( $cust_pkg => \@svc );
776 $error = $cust_main->insert(
779 'depend_jobnum' => $placeholder->jobnum,
783 my $perror = $placeholder->delete;
784 $error .= " (Additionally, error removing placeholder: $perror)" if $perror;
785 return { 'error' => $error };
788 if ( $conf->exists('signup_server-realtime') ) {
790 #warn "$me Billing customer...\n" if $Debug;
793 my $bill_error = $cust_main->bill(
794 'depend_jobnum' => $placeholder->jobnum,
795 'return_bill' => \@cust_bill,
797 #warn "$me error billing new customer: $bill_error"
800 $bill_error = $cust_main->apply_payments_and_credits;
801 #warn "$me error applying payments and credits for".
802 # " new customer: $bill_error"
805 unless ( $packet->{payby} eq 'PREPAY' ) {
806 $bill_error = $cust_main->realtime_collect(
807 method => FS::payby->payby2bop( $packet->{payby} ),
808 depend_jobnum => $placeholder->jobnum,
811 #warn "$me error collecting from new customer: $bill_error"
815 if ($bill_error && ref($bill_error) eq 'HASH') {
816 return { 'error' => '_collect',
817 ( map { $_ => $bill_error->{$_} }
818 qw(popup_url reference collectitems)
820 amount => $cust_main->balance,
824 $bill_error = $cust_main->apply_payments_and_credits;
825 #warn "$me error applying payments and credits for".
826 # " new customer: $bill_error"
829 if ( $cust_main->balance > 0 ) {
831 #this used to apply a credit, but now we can void invoices...
832 foreach my $cust_bill (@cust_bill) {
833 my $voiderror = $cust_bill->void('automatic payment failed');
834 warn "Error voiding cust bill after decline: $voiderror" if $voiderror;
837 #should check list for errors...
838 #$cust_main->suspend;
839 local $FS::svc_Common::noexport_hack = 1;
840 $cust_main->cancel('quiet'=>1);
842 my $perror = $placeholder->depended_delete;
843 warn "error removing provisioning jobs after decline: $perror" if $perror;
845 $perror = $placeholder->delete;
846 warn "error removing placeholder after decline: $perror" if $perror;
849 return { 'error' => '_decline' };
855 $error = $reg_code->delete;
856 return { 'error' => $error } if $error;
859 $error = $placeholder->delete;
860 return { 'error' => $error } if $error;
862 if ( $conf->exists('signup-duplicate_cc-warn_hours') ) {
863 my $hours = $conf->config('signup-duplicate_cc-warn_hours');
864 my $ban = new FS::banned_pay $cust_main->_new_banned_pay_hashref;
865 $ban->end_date( int( time + $hours*3600 ) );
866 $ban->bantype('warn');
867 $ban->reason('signup-duplicate_cc-warn_hours');
868 $error = $ban->insert;
869 warn "WARNING: error inserting temporary banned_pay for ".
870 " signup-duplicate_cc-warn_hours (proceeding anyway): $error"
874 my %return = ( 'error' => '',
875 'signup_service' => $svc_x,
876 'custnum' => $cust_main->custnum,
881 $return{'svcnum'} = $svc[0]->svcnum;
883 if ( $svc_x eq 'svc_acct' ) {
884 $return{$_} = $svc[0]->$_() for qw( username _password );
885 } elsif ( $svc_x eq 'svc_phone' ) {
886 $return{$_} = $svc[0]->$_() for qw(countrycode phonenum sip_password pin);
887 } elsif ( $svc_x eq 'svc_pbx' ) {
888 #$return{$_} = $svc[0]->$_() for qw( ) #nothing yet
890 return {'error' => "configuration error: unknown signup service $svc_x"};
891 #die "unknown signup service $svc_x";
892 # return an error that's visible to someone somewhere
901 #false laziness w/ above
902 # fresh restart to support "free account" portals with 3.x/4.x-style
903 # addressless accounts
904 # and a contact (for self-service login)
905 sub new_customer_minimal {
908 my $conf = new FS::Conf;
909 my $svc_x = $conf->config('signup_server-service') || 'svc_acct';
911 if ( $svc_x eq 'svc_acct' ) {
913 #things that aren't necessary in base class, but are for signup server
914 #return "Passwords don't match"
915 # if $hashref->{'_password'} ne $hashref->{'_password2'}
916 return { 'error' => gettext('empty_password') }
917 unless length($packet->{'_password'});
918 # a bit inefficient for large numbers of pops
919 return { 'error' => gettext('no_access_number_selected') }
920 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
923 elsif ( $svc_x eq 'svc_pbx' ) {
924 #possibly some validation will be needed
928 if ( exists $packet->{'session_id'} ) {
929 my $cache = new FS::ClientAPI_SessionCache( {
930 'namespace' => 'FS::ClientAPI::Agent',
932 my $session = $cache->get($packet->{'session_id'});
934 $agentnum = $session->{'agentnum'};
936 return { 'error' => "Can't resume session" }; #better error message
939 $agentnum = $packet->{agentnum}
940 || $conf->config('signup_server-default_agentnum');
943 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
944 # common that are still here and library them.
946 my $cust_main = new FS::cust_main ( {
947 'agentnum' => $agentnum,
948 'refnum' => $packet->{refnum}
949 || $conf->config('signup_server-default_refnum'),
950 'tagnum' => [ FS::part_tag->default_tags ],
952 map { $_ => $packet->{$_} } qw(
954 last first company daytime night fax mobile
955 ss stateid stateid_state
963 if ( $packet->{payby} =~ /^(CARD|DCRD|CHEK|DCHK)$/ ) {
966 map { $_ => $packet->{$_} } qw(
968 payinfo paycvv paydate payname paystate paytype
969 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;
1059 if ( $invoicing_list[0] && $packet->{'_password'} ) {
1061 new FS::contact { 'first' => $cust_main->first,
1062 'last' => $cust_main->get('last'),
1063 '_password' => $packet->{'_password'},
1064 'emailaddress' => $invoicing_list[0],
1065 'selfservice_access' => 'Y',
1070 my $error = $cust_main->insert(
1075 return { 'error' => $error } if $error;
1077 my $session = { 'custnum' => $cust_main->custnum };
1081 $session_id = sha512_hex(time(). {}. rand(). $$)
1082 } until ( ! defined _myaccount_cache->get($session_id) ); #just in case
1084 _myaccount_cache->set( $session_id, $session, '1 hour' ); # 1 hour?
1086 my %return = ( 'error' => '',
1087 'signup_service' => $svc_x,
1088 'custnum' => $cust_main->custnum,
1089 'session_id' => $session_id,
1090 map { $_ => $cust_main->$_ } qw( first last company ),
1095 $return{'svcnum'} = $svc[0]->svcnum;
1097 if ( $svc_x eq 'svc_acct' ) {
1098 $return{$_} = $svc[0]->$_() for qw( username _password );
1099 } elsif ( $svc_x eq 'svc_phone' ) {
1100 $return{$_} = $svc[0]->$_() for qw(countrycode phonenum sip_password pin);
1101 } elsif ( $svc_x eq 'svc_pbx' ) {
1102 #$return{$_} = $svc[0]->$_() for qw( ) #nothing yet
1104 return {'error' => "configuration error: unknown signup service $svc_x"};
1105 #die "unknown signup service $svc_x";
1106 # return an error that's visible to someone somewhere
1115 use vars qw( $myaccount_cache );
1116 sub _myaccount_cache {
1117 $myaccount_cache ||= new FS::ClientAPI_SessionCache( {
1118 'namespace' => 'FS::ClientAPI::MyAccount',
1122 sub capture_payment {
1125 warn "$me capture_payment called on $packet\n" if $DEBUG;
1128 # identify processor/gateway from called back URL
1131 my $conf = new FS::Conf;
1133 my $payment_gateway;
1134 if ( my $gwnum = $conf->config('selfservice-payment_gateway') ) {
1135 $payment_gateway = qsearchs('payment_gateway', { 'gatewaynum' => $gwnum })
1136 or die "configured gatewaynum $gwnum not found!";
1139 my $url = $packet->{url};
1141 $payment_gateway = qsearchs('payment_gateway',
1142 { 'gateway_callback_url' => popurl(0, $url) }
1144 if (!$payment_gateway) {
1146 my ( $processor, $login, $password, $action, @bop_options ) =
1147 $conf->config('business-onlinepayment');
1148 $action ||= 'normal authorization';
1149 pop @bop_options if scalar(@bop_options) % 2 && $bop_options[-1] =~ /^\s*$/;
1150 die "No real-time processor is enabled - ".
1151 "did you set the business-onlinepayment configuration value?\n"
1154 $payment_gateway = new FS::payment_gateway( {
1155 gateway_namespace => $conf->config('business-onlinepayment-namespace'),
1156 gateway_module => $processor,
1157 gateway_username => $login,
1158 gateway_password => $password,
1159 gateway_action => $action,
1160 options => [ ( @bop_options ) ],
1165 die "No real-time third party processor is enabled - ".
1166 "did you set the business-onlinepayment configuration value?\n*"
1167 unless $payment_gateway->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
1170 # locate pending transaction
1173 eval "use Business::OnlineThirdPartyPayment";
1177 new Business::OnlineThirdPartyPayment( $payment_gateway->gateway_module,
1178 @{ [ $payment_gateway->options ] },
1181 my $paypendingnum = $transaction->reference($packet->{data});
1183 my $cust_pay_pending =
1184 qsearchs('cust_pay_pending', { paypendingnum => $paypendingnum } );
1186 unless ($cust_pay_pending) {
1187 my $bill_error = "No payment is being processed with id $paypendingnum".
1188 "; Transaction aborted.";
1189 return { error => '_decline', bill_error => $bill_error };
1192 if ($cust_pay_pending->status ne 'thirdparty') {
1193 my $bill_error = "Payment with id $paypendingnum is not thirdparty, but ".
1194 $cust_pay_pending->status. "; Transaction aborted.";
1195 return { error => '_decline', bill_error => $bill_error };
1198 my $cust_main = $cust_pay_pending->cust_main;
1199 if ( $packet->{cancel} ) {
1200 # the user has chosen not to make this payment
1201 # (probably should be a separate API call, but I don't want to duplicate
1202 # all of the above...which should eventually go away)
1203 my $error = $cust_pay_pending->delete;
1204 # don't show any errors related to this; they're not meaningful
1205 warn "error canceling pending payment $paypendingnum: $error\n" if $error;
1206 return { 'error' => '_cancel',
1207 'session_id' => $cust_pay_pending->session_id };
1209 # create the payment
1211 $cust_main->realtime_botpp_capture( $cust_pay_pending,
1216 return { 'error' => ( $bill_error->{bill_error} ? '_decline' : '' ),