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 $cust_main->payinfo($cust_main->daytime)
649 if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
651 my @invoicing_list = $packet->{'invoicing_list'}
652 ? split( /\s*\,\s*/, $packet->{'invoicing_list'} )
655 my @exempt_groups = grep /\S/, $conf->config('tax-cust_exempt-groups');
656 my @tax_exempt = grep { $packet->{"tax_$_"} eq 'Y' } @exempt_groups;
657 $insert_options{'tax_exemption'} = {
658 map { $_ => $packet->{"tax_$_".'_num'} } @tax_exempt
661 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
663 return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
666 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
667 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
670 if ( $packet->{'reg_code'} ) {
671 $reg_code = qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
672 'agentnum' => $agentnum, } )
673 or return { 'error' => 'Unknown registration code' };
676 my $cust_pkg = new FS::cust_pkg ( {
677 #later#'custnum' => $custnum,
678 'pkgpart' => $packet->{'pkgpart'},
679 'promo_code' => $packet->{'promo_code'},
680 'reg_code' => $packet->{'reg_code'},
682 #my $error = $cust_pkg->check;
683 #return { 'error' => $error } if $error;
686 unless ( $svc_x eq 'none' ) {
688 my $svcpart = $part_pkg->svcpart($svc_x);
689 #should be all auto-magic and shit
690 if ( $svc_x eq 'svc_acct' ) {
692 my $svc = new FS::svc_acct {
693 'svcpart' => $svcpart,
694 map { $_ => $packet->{$_} }
695 qw( username _password sec_phrase popnum domsvc ),
698 my $error = $svc->is_password_allowed($packet->{_password});
699 return { error => $error } if $error;
703 while ( exists($packet->{"snarf_machine$snarfnum"})
704 && length($packet->{"snarf_machine$snarfnum"}) ) {
705 my $acct_snarf = new FS::acct_snarf ( {
706 'machine' => $packet->{"snarf_machine$snarfnum"},
707 'protocol' => $packet->{"snarf_protocol$snarfnum"},
708 'username' => $packet->{"snarf_username$snarfnum"},
709 '_password' => $packet->{"snarf_password$snarfnum"},
712 push @acct_snarf, $acct_snarf;
714 $svc->child_objects( \@acct_snarf );
717 } elsif ( $svc_x eq 'svc_phone' ) {
719 push @svc, new FS::svc_phone ( {
720 'svcpart' => $svcpart,
721 map { $_ => $packet->{$_} }
722 qw( countrycode phonenum sip_password pin ),
725 } elsif ( $svc_x eq 'svc_pbx' ) {
727 push @svc, new FS::svc_pbx ( {
728 'svcpart' => $svcpart,
729 map { $_ => $packet->{$_} }
734 die "unknown signup service $svc_x";
739 if ($packet->{'mac_addr'} && $conf->exists('signup_server-mac_addr_svcparts'))
742 my %mac_addr_svcparts = map { $_ => 1 }
743 $conf->config('signup_server-mac_addr_svcparts');
744 my @pkg_svc = grep { $_->quantity && $mac_addr_svcparts{$_->svcpart} }
745 $cust_pkg->part_pkg->pkg_svc;
747 return { 'error' => 'No service defined to assign mac address' }
750 my $svc = new FS::svc_acct {
751 'svcpart' => $pkg_svc[0]->svcpart, #multiple matches? alas..
752 'username' => $packet->{'mac_addr'},
753 '_password' => '', #blank as requested (set passwordmin to 0)
760 foreach my $svc ( @svc ) {
761 my $y = $svc->setdefault; # arguably should be in new method
762 return { 'error' => $y } if $y && !ref($y);
763 #$error = $svc->check;
764 #return { 'error' => $error } if $error;
767 #setup a job dependancy to delay provisioning
768 my $placeholder = new FS::queue ( {
769 'job' => 'FS::ClientAPI::Signup::__placeholder',
770 'status' => 'locked',
772 my $error = $placeholder->insert;
773 return { 'error' => $error } if $error;
776 tie my %hash, 'Tie::RefHash';
777 %hash = ( $cust_pkg => \@svc );
779 $error = $cust_main->insert(
782 'depend_jobnum' => $placeholder->jobnum,
786 my $perror = $placeholder->delete;
787 $error .= " (Additionally, error removing placeholder: $perror)" if $perror;
788 return { 'error' => $error };
791 if ( $conf->exists('signup_server-realtime') ) {
793 #warn "$me Billing customer...\n" if $Debug;
796 my $bill_error = $cust_main->bill(
797 'depend_jobnum' => $placeholder->jobnum,
798 'return_bill' => \@cust_bill,
800 #warn "$me error billing new customer: $bill_error"
803 $bill_error = $cust_main->apply_payments_and_credits;
804 #warn "$me error applying payments and credits for".
805 # " new customer: $bill_error"
808 unless ( $packet->{payby} eq 'PREPAY' ) {
809 $bill_error = $cust_main->realtime_collect(
810 method => FS::payby->payby2bop( $packet->{payby} ),
811 depend_jobnum => $placeholder->jobnum,
814 #warn "$me error collecting from new customer: $bill_error"
818 if ($bill_error && ref($bill_error) eq 'HASH') {
819 return { 'error' => '_collect',
820 ( map { $_ => $bill_error->{$_} }
821 qw(popup_url reference collectitems)
823 amount => $cust_main->balance,
827 $bill_error = $cust_main->apply_payments_and_credits;
828 #warn "$me error applying payments and credits for".
829 # " new customer: $bill_error"
832 if ( $cust_main->balance > 0 ) {
834 #this used to apply a credit, but now we can void invoices...
835 foreach my $cust_bill (@cust_bill) {
836 my $voiderror = $cust_bill->void('automatic payment failed');
837 warn "Error voiding cust bill after decline: $voiderror" if $voiderror;
840 #should check list for errors...
841 #$cust_main->suspend;
842 local $FS::svc_Common::noexport_hack = 1;
843 $cust_main->cancel('quiet'=>1);
845 my $perror = $placeholder->depended_delete;
846 warn "error removing provisioning jobs after decline: $perror" if $perror;
848 $perror = $placeholder->delete;
849 warn "error removing placeholder after decline: $perror" if $perror;
852 return { 'error' => '_decline' };
858 $error = $reg_code->delete;
859 return { 'error' => $error } if $error;
862 $error = $placeholder->delete;
863 return { 'error' => $error } if $error;
865 if ( $conf->exists('signup-duplicate_cc-warn_hours') ) {
866 my $hours = $conf->config('signup-duplicate_cc-warn_hours');
867 my $ban = new FS::banned_pay $cust_main->_new_banned_pay_hashref;
868 $ban->end_date( int( time + $hours*3600 ) );
869 $ban->bantype('warn');
870 $ban->reason('signup-duplicate_cc-warn_hours');
871 $error = $ban->insert;
872 warn "WARNING: error inserting temporary banned_pay for ".
873 " signup-duplicate_cc-warn_hours (proceeding anyway): $error"
877 my %return = ( 'error' => '',
878 'signup_service' => $svc_x,
879 'custnum' => $cust_main->custnum,
884 $return{'svcnum'} = $svc[0]->svcnum;
886 if ( $svc_x eq 'svc_acct' ) {
887 $return{$_} = $svc[0]->$_() for qw( username _password );
888 } elsif ( $svc_x eq 'svc_phone' ) {
889 $return{$_} = $svc[0]->$_() for qw(countrycode phonenum sip_password pin);
890 } elsif ( $svc_x eq 'svc_pbx' ) {
891 #$return{$_} = $svc[0]->$_() for qw( ) #nothing yet
893 return {'error' => "configuration error: unknown signup service $svc_x"};
894 #die "unknown signup service $svc_x";
895 # return an error that's visible to someone somewhere
904 #false laziness w/ above
905 # fresh restart to support "free account" portals with 3.x/4.x-style
906 # addressless accounts
907 # and a contact (for self-service login)
908 sub new_customer_minimal {
911 my $conf = new FS::Conf;
912 my $svc_x = $conf->config('signup_server-service') || 'svc_acct';
914 if ( $svc_x eq 'svc_acct' ) {
916 #things that aren't necessary in base class, but are for signup server
917 #return "Passwords don't match"
918 # if $hashref->{'_password'} ne $hashref->{'_password2'}
919 return { 'error' => gettext('empty_password') }
920 unless length($packet->{'_password'});
921 # a bit inefficient for large numbers of pops
922 return { 'error' => gettext('no_access_number_selected') }
923 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
926 elsif ( $svc_x eq 'svc_pbx' ) {
927 #possibly some validation will be needed
931 if ( exists $packet->{'session_id'} ) {
932 my $cache = new FS::ClientAPI_SessionCache( {
933 'namespace' => 'FS::ClientAPI::Agent',
935 my $session = $cache->get($packet->{'session_id'});
937 $agentnum = $session->{'agentnum'};
939 return { 'error' => "Can't resume session" }; #better error message
942 $agentnum = $packet->{agentnum}
943 || $conf->config('signup_server-default_agentnum');
946 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
947 # common that are still here and library them.
949 my $cust_main = new FS::cust_main ( {
950 'agentnum' => $agentnum,
951 'refnum' => $packet->{refnum}
952 || $conf->config('signup_server-default_refnum'),
953 'tagnum' => [ FS::part_tag->default_tags ],
955 map { $_ => $packet->{$_} } qw(
957 last first company daytime night fax mobile
958 ss stateid stateid_state
966 if ( $packet->{payby} =~ /^(CARD|DCRD|CHEK|DCHK)$/ ) {
969 map { $_ => $packet->{$_} } qw(
971 payinfo paycvv paydate payname paystate paytype
972 paystart_month paystart_year payissue
979 if ( grep length($packet->{$_}), FS::cust_main->location_fields ) {
981 foreach my $f (FS::cust_main->location_fields) {
982 $bill_hash->{$f} = $packet->{$f};
984 my $bill_location = FS::cust_location->new($bill_hash);
985 $cust_main->set('bill_location' => $bill_location);
986 $cust_main->set('ship_location' => $bill_location);
989 my @invoicing_list = $packet->{'invoicing_list'}
990 ? split( /\s*\,\s*/, $packet->{'invoicing_list'} )
994 tie my %hash, 'Tie::RefHash', ();
997 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
1003 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
1004 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
1006 my $cust_pkg = new FS::cust_pkg ( {
1007 #later#'custnum' => $custnum,
1008 'pkgpart' => $packet->{'pkgpart'},
1010 #my $error = $cust_pkg->check;
1011 #return { 'error' => $error } if $error;
1013 unless ( $svc_x eq 'none' ) {
1015 my $svcpart = $part_pkg->svcpart($svc_x);
1016 #should be all auto-magic and shit
1017 if ( $svc_x eq 'svc_acct' ) {
1019 my $svc = new FS::svc_acct {
1020 'svcpart' => $svcpart,
1021 map { $_ => $packet->{$_} }
1022 qw( username _password sec_phrase popnum domsvc ),
1027 } elsif ( $svc_x eq 'svc_phone' ) {
1029 push @svc, new FS::svc_phone ( {
1030 'svcpart' => $svcpart,
1031 map { $_ => $packet->{$_} }
1032 qw( countrycode phonenum sip_password pin ),
1035 } elsif ( $svc_x eq 'svc_pbx' ) {
1037 push @svc, new FS::svc_pbx ( {
1038 'svcpart' => $svcpart,
1039 map { $_ => $packet->{$_} }
1044 die "unknown signup service $svc_x";
1049 foreach my $svc ( @svc ) {
1050 my $y = $svc->setdefault; # arguably should be in new method
1051 return { 'error' => $y } if $y && !ref($y);
1052 #$error = $svc->check;
1053 #return { 'error' => $error } if $error;
1057 tie my %hash, 'Tie::RefHash';
1058 $hash{ $cust_pkg } = \@svc;
1062 if ( $invoicing_list[0] && $packet->{'_password'} ) {
1064 new FS::contact { 'first' => $cust_main->first,
1065 'last' => $cust_main->get('last'),
1066 '_password' => $packet->{'_password'},
1067 'emailaddress' => $invoicing_list[0],
1068 'selfservice_access' => 'Y',
1073 my $error = $cust_main->insert(
1078 return { 'error' => $error } if $error;
1080 my $session = { 'custnum' => $cust_main->custnum };
1084 $session_id = sha512_hex(time(). {}. rand(). $$)
1085 } until ( ! defined _myaccount_cache->get($session_id) ); #just in case
1087 _myaccount_cache->set( $session_id, $session, '1 hour' ); # 1 hour?
1089 my %return = ( 'error' => '',
1090 'signup_service' => $svc_x,
1091 'custnum' => $cust_main->custnum,
1092 'session_id' => $session_id,
1093 map { $_ => $cust_main->$_ } qw( first last company ),
1098 $return{'svcnum'} = $svc[0]->svcnum;
1100 if ( $svc_x eq 'svc_acct' ) {
1101 $return{$_} = $svc[0]->$_() for qw( username _password );
1102 } elsif ( $svc_x eq 'svc_phone' ) {
1103 $return{$_} = $svc[0]->$_() for qw(countrycode phonenum sip_password pin);
1104 } elsif ( $svc_x eq 'svc_pbx' ) {
1105 #$return{$_} = $svc[0]->$_() for qw( ) #nothing yet
1107 return {'error' => "configuration error: unknown signup service $svc_x"};
1108 #die "unknown signup service $svc_x";
1109 # return an error that's visible to someone somewhere
1118 use vars qw( $myaccount_cache );
1119 sub _myaccount_cache {
1120 $myaccount_cache ||= new FS::ClientAPI_SessionCache( {
1121 'namespace' => 'FS::ClientAPI::MyAccount',
1125 sub capture_payment {
1128 warn "$me capture_payment called on $packet\n" if $DEBUG;
1131 # identify processor/gateway from called back URL
1134 my $conf = new FS::Conf;
1136 my $payment_gateway;
1137 if ( my $gwnum = $conf->config('selfservice-payment_gateway') ) {
1138 $payment_gateway = qsearchs('payment_gateway', { 'gatewaynum' => $gwnum })
1139 or die "configured gatewaynum $gwnum not found!";
1142 my $url = $packet->{url};
1144 $payment_gateway = qsearchs('payment_gateway',
1145 { 'gateway_callback_url' => popurl(0, $url) }
1147 if (!$payment_gateway) {
1149 my ( $processor, $login, $password, $action, @bop_options ) =
1150 $conf->config('business-onlinepayment');
1151 $action ||= 'normal authorization';
1152 pop @bop_options if scalar(@bop_options) % 2 && $bop_options[-1] =~ /^\s*$/;
1153 die "No real-time processor is enabled - ".
1154 "did you set the business-onlinepayment configuration value?\n"
1157 $payment_gateway = new FS::payment_gateway( {
1158 gateway_namespace => $conf->config('business-onlinepayment-namespace'),
1159 gateway_module => $processor,
1160 gateway_username => $login,
1161 gateway_password => $password,
1162 gateway_action => $action,
1163 options => [ ( @bop_options ) ],
1168 die "No real-time third party processor is enabled - ".
1169 "did you set the business-onlinepayment configuration value?\n*"
1170 unless $payment_gateway->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
1173 # locate pending transaction
1176 eval "use Business::OnlineThirdPartyPayment";
1180 new Business::OnlineThirdPartyPayment( $payment_gateway->gateway_module,
1181 @{ [ $payment_gateway->options ] },
1184 my $paypendingnum = $transaction->reference($packet->{data});
1186 my $cust_pay_pending =
1187 qsearchs('cust_pay_pending', { paypendingnum => $paypendingnum } );
1189 unless ($cust_pay_pending) {
1190 my $bill_error = "No payment is being processed with id $paypendingnum".
1191 "; Transaction aborted.";
1192 return { error => '_decline', bill_error => $bill_error };
1195 if ($cust_pay_pending->status ne 'thirdparty') {
1196 my $bill_error = "Payment with id $paypendingnum is not thirdparty, but ".
1197 $cust_pay_pending->status. "; Transaction aborted.";
1198 return { error => '_decline', bill_error => $bill_error };
1201 my $cust_main = $cust_pay_pending->cust_main;
1202 if ( $packet->{cancel} ) {
1203 # the user has chosen not to make this payment
1204 # (probably should be a separate API call, but I don't want to duplicate
1205 # all of the above...which should eventually go away)
1206 my $error = $cust_pay_pending->delete;
1207 # don't show any errors related to this; they're not meaningful
1208 warn "error canceling pending payment $paypendingnum: $error\n" if $error;
1209 return { 'error' => '_cancel',
1210 'session_id' => $cust_pay_pending->session_id };
1212 # create the payment
1214 $cust_main->realtime_botpp_capture( $cust_pay_pending,
1219 return { 'error' => ( $bill_error->{bill_error} ? '_decline' : '' ),