1 package FS::ClientAPI::Signup;
4 use vars qw( $DEBUG $me );
7 use Digest::SHA qw(sha512_hex);
9 use FS::Record qw(qsearch qsearchs dbdef);
10 use FS::CGI qw(popurl);
11 use FS::Msgcat qw(gettext);
12 use FS::Misc qw(card_types);
13 use FS::ClientAPI_SessionCache;
15 use FS::cust_main_county;
30 $me = '[FS::ClientAPI::Signup]';
33 warn "$me clear_cache called\n" if $DEBUG;
34 my $cache = new FS::ClientAPI_SessionCache( {
35 'namespace' => 'FS::ClientAPI::Signup',
44 warn "$me signup_info called on $packet\n" if $DEBUG;
46 my $conf = new FS::Conf;
47 my $svc_x = $conf->config('signup_server-service') || 'svc_acct';
49 my $cache = new FS::ClientAPI_SessionCache( {
50 'namespace' => 'FS::ClientAPI::Signup',
52 my $signup_info_cache = $cache->get('signup_info_cache');
54 if ( $signup_info_cache ) {
56 warn "$me loading cached signup info\n" if $DEBUG > 1;
60 warn "$me populating signup info cache\n" if $DEBUG > 1;
62 my $agentnum2part_pkg =
66 my $href = $agent->pkgpart_hashref;
69 map { { 'payby' => [ $_->payby ],
70 'freq_pretty' => $_->freq_pretty,
71 'options' => { $_->options },
74 grep { $_->svcpart($svc_x)
75 && ( $href->{ $_->pkgpart }
77 && $_->agentnum == $agent->agentnum
81 qsearch( 'part_pkg', { 'disabled' => '' } )
83 } qsearch('agent', { 'disabled' => '' })
86 my $msgcat = { map { $_=>gettext($_) }
87 qw( passwords_dont_match invalid_card unknown_card_type
88 not_a empty_password illegal_or_empty_text )
90 warn "msgcat: ". Dumper($msgcat). "\n" if $DEBUG > 2;
92 my $label = { map { $_ => FS::Msgcat::_gettext($_) }
93 qw( stateid stateid_state )
95 warn "label: ". Dumper($label). "\n" if $DEBUG > 2;
97 my @agent_fields = qw( agentnum agent );
99 my @bools = qw( emailinvoiceonly security_phrase );
101 my @signup_bools = qw( no_company recommend_daytime recommend_email );
103 my @signup_server_scalars = qw( default_pkgpart default_svcpart default_domsvc );
105 my @selfservice_textareas = qw( head body_header body_footer );
107 my @selfservice_scalars = qw(
108 body_bgcolor box_bgcolor
109 text_color link_color vlink_color hlink_color alink_color
110 font title_color title_align title_size menu_bgcolor menu_fontsize
113 #XXX my @selfservice_bools = qw(
114 # menu_skipblanks menu_skipheadings menu_nounderline
117 #my $selfservice_binaries = qw(
118 # title_left_image title_right_image
119 # menu_top_image menu_body_image menu_bottom_image
122 $signup_info_cache = {
124 'cust_main_county' => [ map $_->hashref,
125 qsearch('cust_main_county', {} )
128 'agent' => [ map { my $agent = $_;
129 +{ map { $_ => $agent->get($_) } @agent_fields }
131 qsearch('agent', { 'disabled' => '' } )
134 'part_referral' => [ map $_->hashref,
135 qsearch('part_referral', { 'disabled' => '' } )
138 'agentnum2part_pkg' => $agentnum2part_pkg,
140 'svc_acct_pop' => [ map $_->hashref, qsearch('svc_acct_pop',{} ) ],
142 'emailinvoiceonly' => $conf->exists('emailinvoiceonly'),
144 'security_phrase' => $conf->exists('security_phrase'),
146 'nomadix' => $conf->exists('signup_server-nomadix'),
148 'payby' => [ $conf->config('signup_server-payby') ],
150 'payby_longname' => [ map { FS::payby->longname($_) }
151 $conf->config('signup_server-payby') ],
153 'card_types' => card_types(),
155 ( map { $_ => $conf->exists("signup-$_") } @signup_bools ),
157 ( map { $_ => scalar($conf->config("signup_server-$_")) }
158 @signup_server_scalars
161 ( map { $_ => join("\n", $conf->config("selfservice-$_")) }
162 @selfservice_textareas
164 ( map { $_ => scalar($conf->config("selfservice-$_")) }
168 #( map { $_ => scalar($conf->config_binary("selfservice-$_")) }
169 # @selfservice_binaries
172 'agentnum2part_pkg' => $agentnum2part_pkg,
173 'svc_acct_pop' => [ map $_->hashref, qsearch('svc_acct_pop',{} ) ],
174 'nomadix' => $conf->exists('signup_server-nomadix'),
175 'payby' => [ $conf->config('signup_server-payby') ],
176 'card_types' => card_types(),
177 'paytypes' => [ @FS::cust_main::paytypes ],
179 'require_cvv' => $conf->exists('signup-require_cvv'),
180 'stateid_enabled' => $conf->exists('show_stateid'),
181 'paystate_enabled' => $conf->exists('show_bankstate'),
182 'exempt_groups' => [ grep /\S/, $conf->config('tax-cust_exempt-groups') ],
186 'statedefault' => scalar($conf->config('statedefault')) || 'CA',
187 'countrydefault' => scalar($conf->config('countrydefault')) || 'US',
188 'refnum' => scalar($conf->config('signup_server-default_refnum')),
189 'signup_service' => $svc_x,
190 'company_name' => scalar($conf->config('company_name')),
192 'logo' => scalar($conf->config_binary('logo.png')),
193 'prepaid_template_custnum' => $conf->exists('signup_server-prepaid-template-custnum'),
196 $cache->set('signup_info_cache', $signup_info_cache);
200 my $signup_info = { %$signup_info_cache };
201 warn "$me signup info loaded\n" if $DEBUG > 1;
202 warn Dumper($signup_info). "\n" if $DEBUG > 2;
204 my @addl = qw( signup_server-classnum2 signup_server-classnum3 );
206 if ( grep { $conf->exists($_) } @addl ) {
208 $signup_info->{optional_packages} = [];
210 foreach my $addl ( @addl ) {
212 warn "$me adding optional package info\n" if $DEBUG > 1;
214 my $classnum = $conf->config($addl) or next;
217 'freq_pretty' => $_->freq_pretty,
218 'options' => { $_->options },
222 qsearch( 'part_pkg', { classnum => $classnum } );
224 push @{$signup_info->{optional_packages}}, \@pkgs;
226 warn "$me done adding opt. package info for $classnum\n" if $DEBUG > 1;
232 my $agentnum = $packet->{'agentnum'}
233 || $conf->config('signup_server-default_agentnum');
234 $agentnum =~ /^(\d*)$/ or die "illegal agentnum";
238 if ( exists $packet->{'session_id'} ) {
240 warn "$me loading agent session\n" if $DEBUG > 1;
241 my $cache = new FS::ClientAPI_SessionCache( {
242 'namespace' => 'FS::ClientAPI::Agent',
244 $session = $cache->get($packet->{'session_id'});
246 $agentnum = $session->{'agentnum'};
248 return { 'error' => "Can't resume session" }; #better error message
250 warn "$me done loading agent session\n" if $DEBUG > 1;
252 } elsif ( exists $packet->{'customer_session_id'} ) {
254 warn "$me loading customer session\n" if $DEBUG > 1;
255 my $cache = new FS::ClientAPI_SessionCache( {
256 'namespace' => 'FS::ClientAPI::MyAccount',
258 $session = $cache->get($packet->{'customer_session_id'});
260 my $custnum = $session->{'custnum'};
261 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum });
262 return { 'error' => "Can't find your customer record" } unless $cust_main;
263 $agentnum = $cust_main->agentnum;
265 return { 'error' => "Can't resume session" }; #better error message
267 warn "$me done loading customer session\n" if $DEBUG > 1;
271 $signup_info->{'part_pkg'} = [];
273 if ( $packet->{'reg_code'} ) {
275 warn "$me setting package list via reg_code\n" if $DEBUG > 1;
277 $signup_info->{'part_pkg'} =
278 [ map { { 'payby' => [ $_->payby ],
279 'freq_pretty' => $_->freq_pretty,
280 'options' => { $_->options },
284 grep { $_->svcpart($svc_x) }
286 qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
287 'agentnum' => $agentnum, } )
291 $signup_info->{'error'} = 'Unknown registration code'
292 unless @{ $signup_info->{'part_pkg'} };
294 warn "$me done setting package list via reg_code\n" if $DEBUG > 1;
296 } elsif ( $packet->{'promo_code'} ) {
298 warn "$me setting package list via promo_code\n" if $DEBUG > 1;
300 $signup_info->{'part_pkg'} =
301 [ map { { 'payby' => [ $_->payby ],
302 'freq_pretty' => $_->freq_pretty,
303 'options' => { $_->options },
306 grep { $_->svcpart($svc_x) }
307 qsearch( 'part_pkg', { 'promo_code' => {
309 value=>$packet->{'promo_code'}
311 'disabled' => '', } )
314 $signup_info->{'error'} = 'Unknown promotional code'
315 unless @{ $signup_info->{'part_pkg'} };
317 warn "$me done setting package list via promo_code\n" if $DEBUG > 1;
322 warn "$me setting agent-specific payment flag\n" if $DEBUG > 1;
323 my $agent = qsearchs('agent', { 'agentnum' => $agentnum } )
324 or return { 'error' => "Self-service agent #$agentnum does not exist" };
325 warn "$me has agent $agent\n" if $DEBUG > 1;
326 my @paybys = @{ $signup_info->{'payby'} };
327 $signup_info->{'hide_payment_fields'} = [];
329 my $gatewaynum = $conf->config('selfservice-payment_gateway');
332 $force_gateway = qsearchs('payment_gateway', { gatewaynum => $gatewaynum });
333 warn "using forced gateway #$gatewaynum - " .
334 $force_gateway->gateway_username . '@' . $force_gateway->gateway_module
336 die "configured gatewaynum $gatewaynum not found!" if !$force_gateway;
338 foreach my $payby (@paybys) {
339 warn "$me checking $payby payment fields\n" if $DEBUG > 1;
341 if ( FS::payby->realtime($payby) ) {
342 my $gateway = $force_gateway ||
343 $agent->payment_gateway( 'method' => FS::payby->payby2bop($payby),
346 if ( $gateway && $gateway->gateway_namespace
347 eq 'Business::OnlineThirdPartyPayment'
349 warn "$me hiding $payby payment fields\n" if $DEBUG > 1;
353 push @{$signup_info->{'hide_payment_fields'}}, $hide;
355 warn "$me done setting agent-specific payment flag\n" if $DEBUG > 1;
357 warn "$me setting agent-specific package list\n" if $DEBUG > 1;
358 $signup_info->{'part_pkg'} = $signup_info->{'agentnum2part_pkg'}{$agentnum}
359 unless @{ $signup_info->{'part_pkg'} };
360 warn "$me done setting agent-specific package list\n" if $DEBUG > 1;
362 warn "$me setting agent-specific adv. source list\n" if $DEBUG > 1;
363 $signup_info->{'part_referral'} =
367 'table' => 'part_referral',
368 'hashref' => { 'disabled' => '' },
369 'extra_sql' => "AND ( agentnum = $agentnum ".
370 " OR agentnum IS NULL ) ",
374 warn "$me done setting agent-specific adv. source list\n" if $DEBUG > 1;
376 $signup_info->{'agent_name'} = $agent->agent;
378 $signup_info->{'company_name'} = $conf->config('company_name', $agentnum);
380 #some of the above could probably be cached, too
382 my $signup_info_cache_agent = $cache->get("signup_info_cache_agent$agentnum");
384 if ( $signup_info_cache_agent ) {
386 warn "$me loading cached signup info for agentnum $agentnum\n"
391 warn "$me populating signup info cache for agentnum $agentnum\n"
394 $signup_info_cache_agent = {
395 #( map { $_ => scalar( $conf->config($_, $agentnum) ) }
396 # qw( company_name ) ),
397 ( map { $_ => scalar( $conf->config("selfservice-$_", $agentnum ) ) }
398 qw( body_bgcolor box_bgcolor menu_bgcolor ) ),
399 ( map { $_ => join("\n", $conf->config("selfservice-$_", $agentnum ) ) }
400 qw( head body_header body_footer ) ),
401 ( map { $_ => join("\n", $conf->config("signup_server-$_", $agentnum ) ) }
402 qw( terms_of_service ) ),
404 ( map { $_ => scalar($conf->exists($_, $agentnum)) }
405 qw(cust_main-require_phone agent-ship_address) ),
408 if ( $signup_info_cache_agent->{'agent-ship_address'}
409 && $agent->agent_cust_main ) {
411 my $cust_main = $agent->agent_cust_main;
412 my $location = $cust_main->ship_location;
413 $signup_info_cache_agent->{"ship_$_"} = $location->get($_)
414 foreach qw( address1 city county state zip country );
418 $cache->set("signup_info_cache_agent$agentnum", $signup_info_cache_agent);
422 $signup_info->{$_} = $signup_info_cache_agent->{$_}
423 foreach keys %$signup_info_cache_agent;
427 # delete $signup_info->{'part_pkg'};
430 warn "$me sorting package list\n" if $DEBUG > 1;
431 $signup_info->{'part_pkg'} = [ sort { $a->{pkg} cmp $b->{pkg} } # case?
432 @{ $signup_info->{'part_pkg'} }
434 warn "$me done sorting package list\n" if $DEBUG > 1;
436 if ( exists $packet->{'session_id'} ) {
437 my $agent_signup_info = { %$signup_info };
438 delete $agent_signup_info->{agentnum2part_pkg};
439 $agent_signup_info->{'agent'} = $session->{'agent'};
440 return $agent_signup_info;
442 elsif ( exists $packet->{'keys'} ) {
443 my @keys = @{ $packet->{'keys'} };
444 return { map { $_ => $signup_info->{$_} } @keys };
452 sub domain_select_hash {
457 if ($packet->{pkgpart}) {
458 my $part_pkg = qsearchs('part_pkg' => { 'pkgpart' => $packet->{pkgpart} } );
459 #$packet->{svcpart} = $part_pkg->svcpart('svc_acct')
460 $packet->{svcpart} = $part_pkg->svcpart
464 if ($packet->{svcpart}) {
465 my $part_svc = qsearchs('part_svc' => { 'svcpart' => $packet->{svcpart} } );
466 $response->{'domsvc'} = $part_svc->part_svc_column('domsvc')->columnvalue
467 if ($part_svc && $part_svc->part_svc_column('domsvc')->columnflag eq 'D');
470 $response->{'domains'}
471 = { domain_select_hash FS::svc_acct( map { $_ => $packet->{$_} }
481 my $conf = new FS::Conf;
482 my $svc_x = $conf->config('signup_server-service') || 'svc_acct';
484 if ( $svc_x eq 'svc_acct' ) {
486 #things that aren't necessary in base class, but are for signup server
487 #return "Passwords don't match"
488 # if $hashref->{'_password'} ne $hashref->{'_password2'}
489 return { 'error' => gettext('empty_password') }
490 unless length($packet->{'_password'});
491 # a bit inefficient for large numbers of pops
492 return { 'error' => gettext('no_access_number_selected') }
493 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
496 elsif ( $svc_x eq 'svc_pbx' ) {
497 #possibly some validation will be needed
501 if ( exists $packet->{'session_id'} ) {
502 my $cache = new FS::ClientAPI_SessionCache( {
503 'namespace' => 'FS::ClientAPI::Agent',
505 my $session = $cache->get($packet->{'session_id'});
507 $agentnum = $session->{'agentnum'};
509 return { 'error' => "Can't resume session" }; #better error message
512 $agentnum = $packet->{agentnum}
513 || $conf->config('signup_server-default_agentnum');
516 my ($bill_hash, $ship_hash);
517 foreach my $f (FS::cust_main->location_fields) {
518 # avoid having to change this in front-end code
519 $bill_hash->{$f} = $packet->{"bill_$f"} || $packet->{$f};
520 $ship_hash->{$f} = $packet->{"ship_$f"};
523 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
524 # common that are still here and library them.
527 'agentnum' => $agentnum,
528 'refnum' => $packet->{refnum}
529 || $conf->config('signup_server-default_refnum'),
530 'tagnum' => [ FS::part_tag->default_tags ],
532 ( map { $_ => $packet->{$_} } qw(
533 ss stateid stateid_state
536 payinfo paycvv paydate payname paystate paytype
537 paystart_month paystart_year payissue
540 referral_custnum comments
546 my $template_custnum = $conf->config('signup_server-prepaid-template-custnum');
548 if ( $template_custnum && $packet->{prepaid_shortform} ) {
550 my $template_cust = qsearchs('cust_main', { 'custnum' => $template_custnum } );
551 return { 'error' => 'Configuration error' } unless $template_cust;
552 $cust_main = new FS::cust_main ( {
554 map { $_ => $template_cust->$_ } qw(
555 last first company daytime night fax mobile
559 $bill_hash = { $template_cust->bill_location->location_hash };
560 $ship_hash = { $template_cust->ship_location->location_hash };
564 $cust_main = new FS::cust_main ( {
566 map { $_ => $packet->{$_} } qw(
567 last first company daytime night fax mobile
573 my $bill_location = FS::cust_location->new($bill_hash);
575 my $agent = qsearchs('agent', { 'agentnum' => $agentnum } );
576 if ( $conf->exists('agent-ship_address', $agentnum)
577 && $agent->agent_custnum ) {
579 my $agent_cust_main = $agent->agent_cust_main;
580 my $prefix = length($agent_cust_main->ship_last) ? 'ship_' : '';
581 $ship_location = FS::cust_location->new({
582 $agent_cust_main->ship_location->location_hash
586 # we don't have an equivalent of the "same" checkbox in selfservice
587 # so is there a ship address, and if so, is it different from the billing
589 elsif ( length($ship_hash->{address1}) > 0 and
590 grep { $bill_hash->{$_} ne $ship_hash->{$_} } keys(%$ship_hash)
593 $ship_location = FS::cust_location->new( $ship_hash );
597 $ship_location = $bill_location;
600 $cust_main->set('bill_location' => $bill_location);
601 $cust_main->set('ship_location' => $ship_location);
603 return { 'error' => "Illegal payment type" }
604 unless grep { $_ eq $packet->{'payby'} }
605 $conf->config('signup_server-payby');
607 if (FS::payby->realtime($packet->{payby})
608 and not $conf->exists('signup_server-third_party_as_card')) {
609 my $payby = $packet->{payby};
611 my $agent = qsearchs('agent', { 'agentnum' => $agentnum });
612 return { 'error' => "Unknown reseller" }
616 my $gatewaynum = $conf->config('selfservice-payment_gateway');
618 $gw = qsearchs('payment_gateway', { gatewaynum => $gatewaynum });
619 die "configured gatewaynum $gatewaynum not found!" if !$gw;
622 $gw = $agent->payment_gateway( 'method' => FS::payby->payby2bop($payby),
627 $cust_main->payby('BILL') # MCRD better?
628 if $gw && $gw->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
631 return { 'error' => "CVV2 is required" }
632 if $cust_main->payby =~ /^(CARD|DCRD)$/
633 && ! $cust_main->paycvv
634 && $conf->exists('signup-require_cvv');
636 $cust_main->payinfo($cust_main->daytime)
637 if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
639 my @invoicing_list = $packet->{'invoicing_list'}
640 ? split( /\s*\,\s*/, $packet->{'invoicing_list'} )
643 my %insert_options = ();
645 my @exempt_groups = grep /\S/, $conf->config('tax-cust_exempt-groups');
646 my @tax_exempt = grep { $packet->{"tax_$_"} eq 'Y' } @exempt_groups;
647 $insert_options{'tax_exemption'} = {
648 map { $_ => $packet->{"tax_$_".'_num'} } @tax_exempt
651 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
653 return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
656 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
657 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
658 my $svcpart = $part_pkg->svcpart($svc_x);
661 if ( $packet->{'reg_code'} ) {
662 $reg_code = qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
663 'agentnum' => $agentnum, } )
664 or return { 'error' => 'Unknown registration code' };
667 my $cust_pkg = new FS::cust_pkg ( {
668 #later#'custnum' => $custnum,
669 'pkgpart' => $packet->{'pkgpart'},
670 'promo_code' => $packet->{'promo_code'},
671 'reg_code' => $packet->{'reg_code'},
673 #my $error = $cust_pkg->check;
674 #return { 'error' => $error } if $error;
676 #should be all auto-magic and shit
678 if ( $svc_x eq 'svc_acct' ) {
680 my $svc = new FS::svc_acct {
681 'svcpart' => $svcpart,
682 map { $_ => $packet->{$_} }
683 qw( username _password sec_phrase popnum domsvc ),
688 while ( exists($packet->{"snarf_machine$snarfnum"})
689 && length($packet->{"snarf_machine$snarfnum"}) ) {
690 my $acct_snarf = new FS::acct_snarf ( {
691 'machine' => $packet->{"snarf_machine$snarfnum"},
692 'protocol' => $packet->{"snarf_protocol$snarfnum"},
693 'username' => $packet->{"snarf_username$snarfnum"},
694 '_password' => $packet->{"snarf_password$snarfnum"},
697 push @acct_snarf, $acct_snarf;
699 $svc->child_objects( \@acct_snarf );
702 } elsif ( $svc_x eq 'svc_phone' ) {
704 push @svc, new FS::svc_phone ( {
705 'svcpart' => $svcpart,
706 map { $_ => $packet->{$_} }
707 qw( countrycode phonenum sip_password pin ),
710 } elsif ( $svc_x eq 'svc_pbx' ) {
712 push @svc, new FS::svc_pbx ( {
713 'svcpart' => $svcpart,
714 map { $_ => $packet->{$_} }
719 die "unknown signup service $svc_x";
722 if ($packet->{'mac_addr'} && $conf->exists('signup_server-mac_addr_svcparts'))
725 my %mac_addr_svcparts = map { $_ => 1 }
726 $conf->config('signup_server-mac_addr_svcparts');
727 my @pkg_svc = grep { $_->quantity && $mac_addr_svcparts{$_->svcpart} }
728 $cust_pkg->part_pkg->pkg_svc;
730 return { 'error' => 'No service defined to assign mac address' }
733 my $svc = new FS::svc_acct {
734 'svcpart' => $pkg_svc[0]->svcpart, #multiple matches? alas..
735 'username' => $packet->{'mac_addr'},
736 '_password' => '', #blank as requested (set passwordmin to 0)
743 foreach my $svc ( @svc ) {
744 my $y = $svc->setdefault; # arguably should be in new method
745 return { 'error' => $y } if $y && !ref($y);
746 #$error = $svc->check;
747 #return { 'error' => $error } if $error;
750 #setup a job dependancy to delay provisioning
751 my $placeholder = new FS::queue ( {
752 'job' => 'FS::ClientAPI::Signup::__placeholder',
753 'status' => 'locked',
755 my $error = $placeholder->insert;
756 return { 'error' => $error } if $error;
759 tie my %hash, 'Tie::RefHash';
760 %hash = ( $cust_pkg => \@svc );
762 $error = $cust_main->insert(
765 'depend_jobnum' => $placeholder->jobnum,
769 my $perror = $placeholder->delete;
770 $error .= " (Additionally, error removing placeholder: $perror)" if $perror;
771 return { 'error' => $error };
774 if ( $conf->exists('signup_server-realtime') ) {
776 #warn "$me Billing customer...\n" if $Debug;
778 my $bill_error = $cust_main->bill( 'depend_jobnum'=>$placeholder->jobnum );
779 #warn "$me error billing new customer: $bill_error"
782 $bill_error = $cust_main->apply_payments_and_credits;
783 #warn "$me error applying payments and credits for".
784 # " new customer: $bill_error"
787 unless ( $packet->{payby} eq 'PREPAY' ) {
788 $bill_error = $cust_main->realtime_collect(
789 method => FS::payby->payby2bop( $packet->{payby} ),
790 depend_jobnum => $placeholder->jobnum,
793 #warn "$me error collecting from new customer: $bill_error"
797 if ($bill_error && ref($bill_error) eq 'HASH') {
798 return { 'error' => '_collect',
799 ( map { $_ => $bill_error->{$_} }
800 qw(popup_url reference collectitems)
802 amount => $cust_main->balance,
806 $bill_error = $cust_main->apply_payments_and_credits;
807 #warn "$me error applying payments and credits for".
808 # " new customer: $bill_error"
811 if ( $cust_main->balance > 0 ) {
813 #this makes sense. credit is "un-doing" the invoice
814 $cust_main->credit( $cust_main->balance, 'signup server decline',
815 'reason_type' => $conf->config('signup_credit_type'),
817 $cust_main->apply_credits;
819 #should check list for errors...
820 #$cust_main->suspend;
821 local $FS::svc_Common::noexport_hack = 1;
822 $cust_main->cancel('quiet'=>1);
824 my $perror = $placeholder->depended_delete;
825 warn "error removing provisioning jobs after decline: $perror" if $perror;
827 $perror = $placeholder->delete;
828 warn "error removing placeholder after decline: $perror" if $perror;
831 return { 'error' => '_decline' };
837 $error = $reg_code->delete;
838 return { 'error' => $error } if $error;
841 $error = $placeholder->delete;
842 return { 'error' => $error } if $error;
844 if ( $conf->exists('signup-duplicate_cc-warn_hours') ) {
845 my $hours = $conf->config('signup-duplicate_cc-warn_hours');
846 my $ban = new FS::banned_pay $cust_main->_new_banned_pay_hashref;
847 $ban->end_date( int( time + $hours*3600 ) );
848 $ban->bantype('warn');
849 $ban->reason('signup-duplicate_cc-warn_hours');
850 $error = $ban->insert;
851 warn "WARNING: error inserting temporary banned_pay for ".
852 " signup-duplicate_cc-warn_hours (proceeding anyway): $error"
856 my %return = ( 'error' => '',
857 'signup_service' => $svc_x,
858 'custnum' => $cust_main->custnum,
863 $return{'svcnum'} = $svc[0]->svcnum;
865 if ( $svc_x eq 'svc_acct' ) {
866 $return{$_} = $svc[0]->$_() for qw( username _password );
867 } elsif ( $svc_x eq 'svc_phone' ) {
868 $return{$_} = $svc[0]->$_() for qw(countrycode phonenum sip_password pin);
869 } elsif ( $svc_x eq 'svc_pbx' ) {
870 #$return{$_} = $svc[0]->$_() for qw( ) #nothing yet
872 return {'error' => "configuration error: unknown signup service $svc_x"};
873 #die "unknown signup service $svc_x";
874 # return an error that's visible to someone somewhere
883 #false laziness w/ above
884 # fresh restart to support "free account" portals with 3.x/4.x-style
885 # addressless accounts
886 # and a contact (for self-service login)
887 sub new_customer_minimal {
890 my $conf = new FS::Conf;
891 my $svc_x = $conf->config('signup_server-service') || 'svc_acct';
893 if ( $svc_x eq 'svc_acct' ) {
895 #things that aren't necessary in base class, but are for signup server
896 #return "Passwords don't match"
897 # if $hashref->{'_password'} ne $hashref->{'_password2'}
898 return { 'error' => gettext('empty_password') }
899 unless length($packet->{'_password'});
900 # a bit inefficient for large numbers of pops
901 return { 'error' => gettext('no_access_number_selected') }
902 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
905 elsif ( $svc_x eq 'svc_pbx' ) {
906 #possibly some validation will be needed
910 if ( exists $packet->{'session_id'} ) {
911 my $cache = new FS::ClientAPI_SessionCache( {
912 'namespace' => 'FS::ClientAPI::Agent',
914 my $session = $cache->get($packet->{'session_id'});
916 $agentnum = $session->{'agentnum'};
918 return { 'error' => "Can't resume session" }; #better error message
921 $agentnum = $packet->{agentnum}
922 || $conf->config('signup_server-default_agentnum');
925 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
926 # common that are still here and library them.
928 my $cust_main = new FS::cust_main ( {
929 'agentnum' => $agentnum,
930 'refnum' => $packet->{refnum}
931 || $conf->config('signup_server-default_refnum'),
932 'tagnum' => [ FS::part_tag->default_tags ],
935 map { $_ => $packet->{$_} } qw(
936 last first company daytime night fax mobile
942 my @invoicing_list = $packet->{'invoicing_list'}
943 ? split( /\s*\,\s*/, $packet->{'invoicing_list'} )
947 tie my %hash, 'Tie::RefHash', ();
950 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
956 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
957 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
958 my $svcpart = $part_pkg->svcpart($svc_x);
960 my $cust_pkg = new FS::cust_pkg ( {
961 #later#'custnum' => $custnum,
962 'pkgpart' => $packet->{'pkgpart'},
964 #my $error = $cust_pkg->check;
965 #return { 'error' => $error } if $error;
967 #should be all auto-magic and shit
968 if ( $svc_x eq 'svc_acct' ) {
970 my $svc = new FS::svc_acct {
971 'svcpart' => $svcpart,
972 map { $_ => $packet->{$_} }
973 qw( username _password sec_phrase popnum domsvc ),
978 } elsif ( $svc_x eq 'svc_phone' ) {
980 push @svc, new FS::svc_phone ( {
981 'svcpart' => $svcpart,
982 map { $_ => $packet->{$_} }
983 qw( countrycode phonenum sip_password pin ),
986 } elsif ( $svc_x eq 'svc_pbx' ) {
988 push @svc, new FS::svc_pbx ( {
989 'svcpart' => $svcpart,
990 map { $_ => $packet->{$_} }
995 die "unknown signup service $svc_x";
998 foreach my $svc ( @svc ) {
999 my $y = $svc->setdefault; # arguably should be in new method
1000 return { 'error' => $y } if $y && !ref($y);
1001 #$error = $svc->check;
1002 #return { 'error' => $error } if $error;
1006 tie my %hash, 'Tie::RefHash';
1007 $hash{ $cust_pkg } = \@svc;
1012 if ( $invoicing_list[0] && $packet->{'_password'} ) {
1014 new FS::contact { 'first' => $cust_main->first,
1015 'last' => $cust_main->get('last'),
1016 '_password' => $packet->{'_password'},
1017 'emailaddress' => $invoicing_list[0],
1018 'selfservice_access' => 'Y',
1023 my $error = $cust_main->insert(
1028 return { 'error' => $error } if $error;
1030 my $session = { 'custnum' => $cust_main->custnum };
1034 $session_id = sha1_hex(time(). {}. rand(). $$)
1035 } until ( ! defined _myaccount_cache->get($session_id) ); #just in case
1037 _cache->set( $session_id, $session, '1 hour' ); # 1 hour?
1039 my %return = ( 'error' => '',
1040 'signup_service' => $svc_x,
1041 'custnum' => $cust_main->custnum,
1042 'session_id' => $session_id,
1047 $return{'svcnum'} = $svc[0]->svcnum;
1049 if ( $svc_x eq 'svc_acct' ) {
1050 $return{$_} = $svc[0]->$_() for qw( username _password );
1051 } elsif ( $svc_x eq 'svc_phone' ) {
1052 $return{$_} = $svc[0]->$_() for qw(countrycode phonenum sip_password pin);
1053 } elsif ( $svc_x eq 'svc_pbx' ) {
1054 #$return{$_} = $svc[0]->$_() for qw( ) #nothing yet
1056 return {'error' => "configuration error: unknown signup service $svc_x"};
1057 #die "unknown signup service $svc_x";
1058 # return an error that's visible to someone somewhere
1067 use vars qw( $myaccount_cache );
1068 sub _myaccount_cache {
1069 $myaccount_cache ||= new FS::ClientAPI_SessionCache( {
1070 'namespace' => 'FS::ClientAPI::MyAccount',
1074 sub capture_payment {
1077 warn "$me capture_payment called on $packet\n" if $DEBUG;
1080 # identify processor/gateway from called back URL
1083 my $conf = new FS::Conf;
1085 my $payment_gateway;
1086 if ( my $gwnum = $conf->config('selfservice-payment_gateway') ) {
1087 $payment_gateway = qsearchs('payment_gateway', { 'gatewaynum' => $gwnum })
1088 or die "configured gatewaynum $gwnum not found!";
1091 my $url = $packet->{url};
1093 $payment_gateway = qsearchs('payment_gateway',
1094 { 'gateway_callback_url' => popurl(0, $url) }
1096 if (!$payment_gateway) {
1098 my ( $processor, $login, $password, $action, @bop_options ) =
1099 $conf->config('business-onlinepayment');
1100 $action ||= 'normal authorization';
1101 pop @bop_options if scalar(@bop_options) % 2 && $bop_options[-1] =~ /^\s*$/;
1102 die "No real-time processor is enabled - ".
1103 "did you set the business-onlinepayment configuration value?\n"
1106 $payment_gateway = new FS::payment_gateway( {
1107 gateway_namespace => $conf->config('business-onlinepayment-namespace'),
1108 gateway_module => $processor,
1109 gateway_username => $login,
1110 gateway_password => $password,
1111 gateway_action => $action,
1112 options => [ ( @bop_options ) ],
1117 die "No real-time third party processor is enabled - ".
1118 "did you set the business-onlinepayment configuration value?\n*"
1119 unless $payment_gateway->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
1122 # locate pending transaction
1125 eval "use Business::OnlineThirdPartyPayment";
1129 new Business::OnlineThirdPartyPayment( $payment_gateway->gateway_module,
1130 @{ [ $payment_gateway->options ] },
1133 my $paypendingnum = $transaction->reference($packet->{data});
1135 my $cust_pay_pending =
1136 qsearchs('cust_pay_pending', { paypendingnum => $paypendingnum } );
1138 unless ($cust_pay_pending) {
1139 my $bill_error = "No payment is being processed with id $paypendingnum".
1140 "; Transaction aborted.";
1141 return { error => '_decline', bill_error => $bill_error };
1144 if ($cust_pay_pending->status ne 'thirdparty') {
1145 my $bill_error = "Payment with id $paypendingnum is not thirdparty, but ".
1146 $cust_pay_pending->status. "; Transaction aborted.";
1147 return { error => '_decline', bill_error => $bill_error };
1150 my $cust_main = $cust_pay_pending->cust_main;
1151 if ( $packet->{cancel} ) {
1152 # the user has chosen not to make this payment
1153 # (probably should be a separate API call, but I don't want to duplicate
1154 # all of the above...which should eventually go away)
1155 my $error = $cust_pay_pending->delete;
1156 # don't show any errors related to this; they're not meaningful
1157 warn "error canceling pending payment $paypendingnum: $error\n" if $error;
1158 return { 'error' => '_cancel',
1159 'session_id' => $cust_pay_pending->session_id };
1161 # create the payment
1163 $cust_main->realtime_botpp_capture( $cust_pay_pending,
1168 return { 'error' => ( $bill_error->{bill_error} ? '_decline' : '' ),