1 package FS::ClientAPI::Signup;
4 use vars qw( $DEBUG $me );
7 use Digest::SHA qw(sha512_hex);
9 use FS::Record qw(qsearch qsearchs dbdef);
10 use FS::CGI qw(popurl);
11 use FS::Msgcat qw(gettext);
12 use FS::Misc qw(card_types);
13 use FS::ClientAPI_SessionCache;
15 use FS::cust_main_county;
29 $me = '[FS::ClientAPI::Signup]';
32 warn "$me clear_cache called\n" if $DEBUG;
33 my $cache = new FS::ClientAPI_SessionCache( {
34 'namespace' => 'FS::ClientAPI::Signup',
43 warn "$me signup_info called on $packet\n" if $DEBUG;
45 my $conf = new FS::Conf;
46 my $svc_x = $conf->config('signup_server-service') || 'svc_acct';
48 my $cache = new FS::ClientAPI_SessionCache( {
49 'namespace' => 'FS::ClientAPI::Signup',
51 my $signup_info_cache = $cache->get('signup_info_cache');
53 if ( $signup_info_cache ) {
55 warn "$me loading cached signup info\n" if $DEBUG > 1;
59 warn "$me populating signup info cache\n" if $DEBUG > 1;
61 my $agentnum2part_pkg =
65 my $href = $agent->pkgpart_hashref;
68 map { { 'payby' => [ $_->payby ],
69 'freq_pretty' => $_->freq_pretty,
70 'options' => { $_->options },
73 grep { $_->svcpart($svc_x)
74 && ( $href->{ $_->pkgpart }
76 && $_->agentnum == $agent->agentnum
80 qsearch( 'part_pkg', { 'disabled' => '' } )
82 } qsearch('agent', { 'disabled' => '' })
85 my $msgcat = { map { $_=>gettext($_) }
86 qw( passwords_dont_match invalid_card unknown_card_type
87 not_a empty_password illegal_or_empty_text )
89 warn "msgcat: ". Dumper($msgcat). "\n" if $DEBUG > 2;
91 my $label = { map { $_ => FS::Msgcat::_gettext($_) }
92 qw( stateid stateid_state )
94 warn "label: ". Dumper($label). "\n" if $DEBUG > 2;
96 my @agent_fields = qw( agentnum agent );
98 my @bools = qw( emailinvoiceonly security_phrase );
100 my @signup_bools = qw( no_company recommend_daytime recommend_email );
102 my @signup_server_scalars = qw( default_pkgpart default_svcpart default_domsvc );
104 my @selfservice_textareas = qw( head body_header body_footer );
106 my @selfservice_scalars = qw(
107 body_bgcolor box_bgcolor
108 text_color link_color vlink_color hlink_color alink_color
109 font title_color title_align title_size menu_bgcolor menu_fontsize
112 #XXX my @selfservice_bools = qw(
113 # menu_skipblanks menu_skipheadings menu_nounderline
116 #my $selfservice_binaries = qw(
117 # title_left_image title_right_image
118 # menu_top_image menu_body_image menu_bottom_image
121 $signup_info_cache = {
123 'cust_main_county' => [ map $_->hashref,
124 qsearch('cust_main_county', {} )
127 'agent' => [ map { my $agent = $_;
128 +{ map { $_ => $agent->get($_) } @agent_fields }
130 qsearch('agent', { 'disabled' => '' } )
133 'part_referral' => [ map $_->hashref,
134 qsearch('part_referral', { 'disabled' => '' } )
137 'agentnum2part_pkg' => $agentnum2part_pkg,
139 'svc_acct_pop' => [ map $_->hashref, qsearch('svc_acct_pop',{} ) ],
141 'emailinvoiceonly' => $conf->exists('emailinvoiceonly'),
143 'security_phrase' => $conf->exists('security_phrase'),
145 'nomadix' => $conf->exists('signup_server-nomadix'),
147 'payby' => [ $conf->config('signup_server-payby') ],
149 'payby_longname' => [ map { FS::payby->longname($_) }
150 $conf->config('signup_server-payby') ],
152 'card_types' => card_types(),
154 ( map { $_ => $conf->exists("signup-$_") } @signup_bools ),
156 ( map { $_ => scalar($conf->config("signup_server-$_")) }
157 @signup_server_scalars
160 ( map { $_ => join("\n", $conf->config("selfservice-$_")) }
161 @selfservice_textareas
163 ( map { $_ => scalar($conf->config("selfservice-$_")) }
167 #( map { $_ => scalar($conf->config_binary("selfservice-$_")) }
168 # @selfservice_binaries
171 'agentnum2part_pkg' => $agentnum2part_pkg,
172 'svc_acct_pop' => [ map $_->hashref, qsearch('svc_acct_pop',{} ) ],
173 'nomadix' => $conf->exists('signup_server-nomadix'),
174 'payby' => [ $conf->config('signup_server-payby') ],
175 'card_types' => card_types(),
176 'paytypes' => [ @FS::cust_main::paytypes ],
178 'require_cvv' => $conf->exists('signup-require_cvv'),
179 'stateid_enabled' => $conf->exists('show_stateid'),
180 'paystate_enabled' => $conf->exists('show_bankstate'),
181 'exempt_groups' => [ grep /\S/, $conf->config('tax-cust_exempt-groups') ],
185 'statedefault' => scalar($conf->config('statedefault')) || 'CA',
186 'countrydefault' => scalar($conf->config('countrydefault')) || 'US',
187 'refnum' => scalar($conf->config('signup_server-default_refnum')),
188 'signup_service' => $svc_x,
189 'company_name' => scalar($conf->config('company_name')),
191 'logo' => scalar($conf->config_binary('logo.png')),
192 'prepaid_template_custnum' => $conf->exists('signup_server-prepaid-template-custnum'),
195 $cache->set('signup_info_cache', $signup_info_cache);
199 my $signup_info = { %$signup_info_cache };
200 warn "$me signup info loaded\n" if $DEBUG > 1;
201 warn Dumper($signup_info). "\n" if $DEBUG > 2;
203 my @addl = qw( signup_server-classnum2 signup_server-classnum3 );
205 if ( grep { $conf->exists($_) } @addl ) {
207 $signup_info->{optional_packages} = [];
209 foreach my $addl ( @addl ) {
211 warn "$me adding optional package info\n" if $DEBUG > 1;
213 my $classnum = $conf->config($addl) or next;
216 'freq_pretty' => $_->freq_pretty,
217 'options' => { $_->options },
221 qsearch( 'part_pkg', { classnum => $classnum } );
223 push @{$signup_info->{optional_packages}}, \@pkgs;
225 warn "$me done adding opt. package info for $classnum\n" if $DEBUG > 1;
231 my $agentnum = $packet->{'agentnum'}
232 || $conf->config('signup_server-default_agentnum');
233 $agentnum =~ /^(\d*)$/ or die "illegal agentnum";
237 if ( exists $packet->{'session_id'} ) {
239 warn "$me loading agent session\n" if $DEBUG > 1;
240 my $cache = new FS::ClientAPI_SessionCache( {
241 'namespace' => 'FS::ClientAPI::Agent',
243 $session = $cache->get($packet->{'session_id'});
245 $agentnum = $session->{'agentnum'};
247 return { 'error' => "Can't resume session" }; #better error message
249 warn "$me done loading agent session\n" if $DEBUG > 1;
251 } elsif ( exists $packet->{'customer_session_id'} ) {
253 warn "$me loading customer session\n" if $DEBUG > 1;
254 my $cache = new FS::ClientAPI_SessionCache( {
255 'namespace' => 'FS::ClientAPI::MyAccount',
257 $session = $cache->get($packet->{'customer_session_id'});
259 my $custnum = $session->{'custnum'};
260 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum });
261 return { 'error' => "Can't find your customer record" } unless $cust_main;
262 $agentnum = $cust_main->agentnum;
264 return { 'error' => "Can't resume session" }; #better error message
266 warn "$me done loading customer session\n" if $DEBUG > 1;
270 $signup_info->{'part_pkg'} = [];
272 if ( $packet->{'reg_code'} ) {
274 warn "$me setting package list via reg_code\n" if $DEBUG > 1;
276 $signup_info->{'part_pkg'} =
277 [ map { { 'payby' => [ $_->payby ],
278 'freq_pretty' => $_->freq_pretty,
279 'options' => { $_->options },
283 grep { $_->svcpart($svc_x) }
285 qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
286 'agentnum' => $agentnum, } )
290 $signup_info->{'error'} = 'Unknown registration code'
291 unless @{ $signup_info->{'part_pkg'} };
293 warn "$me done setting package list via reg_code\n" if $DEBUG > 1;
295 } elsif ( $packet->{'promo_code'} ) {
297 warn "$me setting package list via promo_code\n" if $DEBUG > 1;
299 $signup_info->{'part_pkg'} =
300 [ map { { 'payby' => [ $_->payby ],
301 'freq_pretty' => $_->freq_pretty,
302 'options' => { $_->options },
305 grep { $_->svcpart($svc_x) }
306 qsearch( 'part_pkg', { 'promo_code' => {
308 value=>$packet->{'promo_code'}
310 'disabled' => '', } )
313 $signup_info->{'error'} = 'Unknown promotional code'
314 unless @{ $signup_info->{'part_pkg'} };
316 warn "$me done setting package list via promo_code\n" if $DEBUG > 1;
321 warn "$me setting agent-specific payment flag\n" if $DEBUG > 1;
322 my $agent = qsearchs('agent', { 'agentnum' => $agentnum } )
323 or return { 'error' => "Self-service agent #$agentnum does not exist" };
324 warn "$me has agent $agent\n" if $DEBUG > 1;
325 my @paybys = @{ $signup_info->{'payby'} };
326 $signup_info->{'hide_payment_fields'} = [];
328 my $gatewaynum = $conf->config('selfservice-payment_gateway');
331 $force_gateway = qsearchs('payment_gateway', { gatewaynum => $gatewaynum });
332 warn "using forced gateway #$gatewaynum - " .
333 $force_gateway->gateway_username . '@' . $force_gateway->gateway_module
335 die "configured gatewaynum $gatewaynum not found!" if !$force_gateway;
337 foreach my $payby (@paybys) {
338 warn "$me checking $payby payment fields\n" if $DEBUG > 1;
340 if ( FS::payby->realtime($payby) ) {
341 my $gateway = $force_gateway ||
342 $agent->payment_gateway( 'method' => FS::payby->payby2bop($payby),
345 if ( $gateway && $gateway->gateway_namespace
346 eq 'Business::OnlineThirdPartyPayment'
348 warn "$me hiding $payby payment fields\n" if $DEBUG > 1;
352 push @{$signup_info->{'hide_payment_fields'}}, $hide;
354 warn "$me done setting agent-specific payment flag\n" if $DEBUG > 1;
356 warn "$me setting agent-specific package list\n" if $DEBUG > 1;
357 $signup_info->{'part_pkg'} = $signup_info->{'agentnum2part_pkg'}{$agentnum}
358 unless @{ $signup_info->{'part_pkg'} };
359 warn "$me done setting agent-specific package list\n" if $DEBUG > 1;
361 warn "$me setting agent-specific adv. source list\n" if $DEBUG > 1;
362 $signup_info->{'part_referral'} =
366 'table' => 'part_referral',
367 'hashref' => { 'disabled' => '' },
368 'extra_sql' => "AND ( agentnum = $agentnum ".
369 " OR agentnum IS NULL ) ",
373 warn "$me done setting agent-specific adv. source list\n" if $DEBUG > 1;
375 $signup_info->{'agent_name'} = $agent->agent;
377 $signup_info->{'company_name'} = $conf->config('company_name', $agentnum);
379 #some of the above could probably be cached, too
381 my $signup_info_cache_agent = $cache->get("signup_info_cache_agent$agentnum");
383 if ( $signup_info_cache_agent ) {
385 warn "$me loading cached signup info for agentnum $agentnum\n"
390 warn "$me populating signup info cache for agentnum $agentnum\n"
393 $signup_info_cache_agent = {
394 #( map { $_ => scalar( $conf->config($_, $agentnum) ) }
395 # qw( company_name ) ),
396 ( map { $_ => scalar( $conf->config("selfservice-$_", $agentnum ) ) }
397 qw( body_bgcolor box_bgcolor menu_bgcolor ) ),
398 ( map { $_ => join("\n", $conf->config("selfservice-$_", $agentnum ) ) }
399 qw( head body_header body_footer ) ),
400 ( map { $_ => join("\n", $conf->config("signup_server-$_", $agentnum ) ) }
401 qw( terms_of_service ) ),
403 ( map { $_ => scalar($conf->exists($_, $agentnum)) }
404 qw(cust_main-require_phone agent-ship_address) ),
407 if ( $signup_info_cache_agent->{'agent-ship_address'}
408 && $agent->agent_cust_main ) {
410 my $cust_main = $agent->agent_cust_main;
411 my $location = $cust_main->ship_location;
412 $signup_info_cache_agent->{"ship_$_"} = $location->get($_)
413 foreach qw( address1 city county state zip country );
417 $cache->set("signup_info_cache_agent$agentnum", $signup_info_cache_agent);
421 $signup_info->{$_} = $signup_info_cache_agent->{$_}
422 foreach keys %$signup_info_cache_agent;
426 # delete $signup_info->{'part_pkg'};
429 warn "$me sorting package list\n" if $DEBUG > 1;
430 $signup_info->{'part_pkg'} = [ sort { $a->{pkg} cmp $b->{pkg} } # case?
431 @{ $signup_info->{'part_pkg'} }
433 warn "$me done sorting package list\n" if $DEBUG > 1;
435 if ( exists $packet->{'session_id'} ) {
436 my $agent_signup_info = { %$signup_info };
437 delete $agent_signup_info->{agentnum2part_pkg};
438 $agent_signup_info->{'agent'} = $session->{'agent'};
439 return $agent_signup_info;
441 elsif ( exists $packet->{'keys'} ) {
442 my @keys = @{ $packet->{'keys'} };
443 return { map { $_ => $signup_info->{$_} } @keys };
451 sub domain_select_hash {
456 if ($packet->{pkgpart}) {
457 my $part_pkg = qsearchs('part_pkg' => { 'pkgpart' => $packet->{pkgpart} } );
458 #$packet->{svcpart} = $part_pkg->svcpart('svc_acct')
459 $packet->{svcpart} = $part_pkg->svcpart
463 if ($packet->{svcpart}) {
464 my $part_svc = qsearchs('part_svc' => { 'svcpart' => $packet->{svcpart} } );
465 $response->{'domsvc'} = $part_svc->part_svc_column('domsvc')->columnvalue
466 if ($part_svc && $part_svc->part_svc_column('domsvc')->columnflag eq 'D');
469 $response->{'domains'}
470 = { domain_select_hash FS::svc_acct( map { $_ => $packet->{$_} }
480 my $conf = new FS::Conf;
481 my $svc_x = $conf->config('signup_server-service') || 'svc_acct';
483 if ( $svc_x eq 'svc_acct' ) {
485 #things that aren't necessary in base class, but are for signup server
486 #return "Passwords don't match"
487 # if $hashref->{'_password'} ne $hashref->{'_password2'}
488 return { 'error' => gettext('empty_password') }
489 unless length($packet->{'_password'});
490 # a bit inefficient for large numbers of pops
491 return { 'error' => gettext('no_access_number_selected') }
492 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
495 elsif ( $svc_x eq 'svc_pbx' ) {
496 #possibly some validation will be needed
500 if ( exists $packet->{'session_id'} ) {
501 my $cache = new FS::ClientAPI_SessionCache( {
502 'namespace' => 'FS::ClientAPI::Agent',
504 my $session = $cache->get($packet->{'session_id'});
506 $agentnum = $session->{'agentnum'};
508 return { 'error' => "Can't resume session" }; #better error message
511 $agentnum = $packet->{agentnum}
512 || $conf->config('signup_server-default_agentnum');
515 my ($bill_hash, $ship_hash);
516 foreach my $f (FS::cust_main->location_fields) {
517 # avoid having to change this in front-end code
518 $bill_hash->{$f} = $packet->{"bill_$f"} || $packet->{$f};
519 $ship_hash->{$f} = $packet->{"ship_$f"};
522 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
523 # common that are still here and library them.
524 my $template_custnum = $conf->config('signup_server-prepaid-template-custnum');
526 if ( $template_custnum && $packet->{prepaid_shortform} ) {
528 my $template_cust = qsearchs('cust_main', { 'custnum' => $template_custnum } );
529 return { 'error' => 'Configuration error' } unless $template_cust;
530 $cust_main = new FS::cust_main ( {
531 'agentnum' => $agentnum,
532 'refnum' => $packet->{refnum}
533 || $conf->config('signup_server-default_refnum'),
535 ( map { $_ => $template_cust->$_ } qw(
536 last first company daytime night fax mobile
540 ( map { $_ => $packet->{$_} } qw(
541 ss stateid stateid_state
544 payinfo paycvv paydate payname paystate paytype
545 paystart_month paystart_year payissue
548 referral_custnum comments
554 $bill_hash = { $template_cust->bill_location->location_hash };
555 $ship_hash = { $template_cust->ship_location->location_hash };
559 $cust_main = new FS::cust_main ( {
561 'agentnum' => $agentnum,
562 'refnum' => $packet->{refnum}
563 || $conf->config('signup_server-default_refnum'),
565 map { $_ => $packet->{$_} } qw(
566 last first ss company
567 daytime night fax mobile
568 stateid stateid_state
570 payinfo paycvv paydate payname paystate paytype
571 paystart_month paystart_year payissue
574 referral_custnum comments
580 my $bill_location = FS::cust_location->new($bill_hash);
582 my $agent = qsearchs('agent', { 'agentnum' => $agentnum } );
583 if ( $conf->exists('agent-ship_address', $agentnum)
584 && $agent->agent_custnum ) {
586 my $agent_cust_main = $agent->agent_cust_main;
587 my $prefix = length($agent_cust_main->ship_last) ? 'ship_' : '';
588 $ship_location = FS::cust_location->new({
589 $agent_cust_main->ship_location->location_hash
593 # we don't have an equivalent of the "same" checkbox in selfservice
594 # so is there a ship address, and if so, is it different from the billing
596 elsif ( length($ship_hash->{address1}) > 0 and
597 grep { $bill_hash->{$_} ne $ship_hash->{$_} } keys(%$ship_hash)
600 $ship_location = FS::cust_location->new( $ship_hash );
604 $ship_location = $bill_location;
607 $cust_main->set('bill_location' => $bill_location);
608 $cust_main->set('ship_location' => $ship_location);
610 return { 'error' => "Illegal payment type" }
611 unless grep { $_ eq $packet->{'payby'} }
612 $conf->config('signup_server-payby');
614 if (FS::payby->realtime($packet->{payby})
615 and not $conf->exists('signup_server-third_party_as_card')) {
616 my $payby = $packet->{payby};
618 my $agent = qsearchs('agent', { 'agentnum' => $agentnum });
619 return { 'error' => "Unknown reseller" }
623 my $gatewaynum = $conf->config('selfservice-payment_gateway');
625 $gw = qsearchs('payment_gateway', { gatewaynum => $gatewaynum });
626 die "configured gatewaynum $gatewaynum not found!" if !$gw;
629 $gw = $agent->payment_gateway( 'method' => FS::payby->payby2bop($payby),
634 $cust_main->payby('BILL') # MCRD better?
635 if $gw && $gw->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
638 return { 'error' => "CVV2 is required" }
639 if $cust_main->payby =~ /^(CARD|DCRD)$/
640 && ! $cust_main->paycvv
641 && $conf->exists('signup-require_cvv');
643 $cust_main->payinfo($cust_main->daytime)
644 if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
646 my @invoicing_list = $packet->{'invoicing_list'}
647 ? split( /\s*\,\s*/, $packet->{'invoicing_list'} )
650 my %insert_options = ();
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" };
665 my $svcpart = $part_pkg->svcpart($svc_x);
668 if ( $packet->{'reg_code'} ) {
669 $reg_code = qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
670 'agentnum' => $agentnum, } )
671 or return { 'error' => 'Unknown registration code' };
674 my $cust_pkg = new FS::cust_pkg ( {
675 #later#'custnum' => $custnum,
676 'pkgpart' => $packet->{'pkgpart'},
677 'promo_code' => $packet->{'promo_code'},
678 'reg_code' => $packet->{'reg_code'},
680 #my $error = $cust_pkg->check;
681 #return { 'error' => $error } if $error;
683 #should be all auto-magic and shit
685 if ( $svc_x eq 'svc_acct' ) {
687 my $svc = new FS::svc_acct {
688 'svcpart' => $svcpart,
689 map { $_ => $packet->{$_} }
690 qw( username _password sec_phrase popnum domsvc ),
695 while ( exists($packet->{"snarf_machine$snarfnum"})
696 && length($packet->{"snarf_machine$snarfnum"}) ) {
697 my $acct_snarf = new FS::acct_snarf ( {
698 'machine' => $packet->{"snarf_machine$snarfnum"},
699 'protocol' => $packet->{"snarf_protocol$snarfnum"},
700 'username' => $packet->{"snarf_username$snarfnum"},
701 '_password' => $packet->{"snarf_password$snarfnum"},
704 push @acct_snarf, $acct_snarf;
706 $svc->child_objects( \@acct_snarf );
709 } elsif ( $svc_x eq 'svc_phone' ) {
711 push @svc, new FS::svc_phone ( {
712 'svcpart' => $svcpart,
713 map { $_ => $packet->{$_} }
714 qw( countrycode phonenum sip_password pin ),
717 } elsif ( $svc_x eq 'svc_pbx' ) {
719 push @svc, new FS::svc_pbx ( {
720 'svcpart' => $svcpart,
721 map { $_ => $packet->{$_} }
726 die "unknown signup service $svc_x";
729 if ($packet->{'mac_addr'} && $conf->exists('signup_server-mac_addr_svcparts'))
732 my %mac_addr_svcparts = map { $_ => 1 }
733 $conf->config('signup_server-mac_addr_svcparts');
734 my @pkg_svc = grep { $_->quantity && $mac_addr_svcparts{$_->svcpart} }
735 $cust_pkg->part_pkg->pkg_svc;
737 return { 'error' => 'No service defined to assign mac address' }
740 my $svc = new FS::svc_acct {
741 'svcpart' => $pkg_svc[0]->svcpart, #multiple matches? alas..
742 'username' => $packet->{'mac_addr'},
743 '_password' => '', #blank as requested (set passwordmin to 0)
750 foreach my $svc ( @svc ) {
751 my $y = $svc->setdefault; # arguably should be in new method
752 return { 'error' => $y } if $y && !ref($y);
753 #$error = $svc->check;
754 #return { 'error' => $error } if $error;
757 #setup a job dependancy to delay provisioning
758 my $placeholder = new FS::queue ( {
759 'job' => 'FS::ClientAPI::Signup::__placeholder',
760 'status' => 'locked',
762 my $error = $placeholder->insert;
763 return { 'error' => $error } if $error;
766 tie my %hash, 'Tie::RefHash';
767 %hash = ( $cust_pkg => \@svc );
769 $error = $cust_main->insert(
772 'depend_jobnum' => $placeholder->jobnum,
776 my $perror = $placeholder->delete;
777 $error .= " (Additionally, error removing placeholder: $perror)" if $perror;
778 return { 'error' => $error };
781 if ( $conf->exists('signup_server-realtime') ) {
783 #warn "$me Billing customer...\n" if $Debug;
785 my $bill_error = $cust_main->bill( 'depend_jobnum'=>$placeholder->jobnum );
786 #warn "$me error billing new customer: $bill_error"
789 $bill_error = $cust_main->apply_payments_and_credits;
790 #warn "$me error applying payments and credits for".
791 # " new customer: $bill_error"
794 unless ( $packet->{payby} eq 'PREPAY' ) {
795 $bill_error = $cust_main->realtime_collect(
796 method => FS::payby->payby2bop( $packet->{payby} ),
797 depend_jobnum => $placeholder->jobnum,
800 #warn "$me error collecting from new customer: $bill_error"
804 if ($bill_error && ref($bill_error) eq 'HASH') {
805 return { 'error' => '_collect',
806 ( map { $_ => $bill_error->{$_} }
807 qw(popup_url reference collectitems)
809 amount => $cust_main->balance,
813 $bill_error = $cust_main->apply_payments_and_credits;
814 #warn "$me error applying payments and credits for".
815 # " new customer: $bill_error"
818 if ( $cust_main->balance > 0 ) {
820 #this makes sense. credit is "un-doing" the invoice
821 $cust_main->credit( $cust_main->balance, 'signup server decline',
822 'reason_type' => $conf->config('signup_credit_type'),
824 $cust_main->apply_credits;
826 #should check list for errors...
827 #$cust_main->suspend;
828 local $FS::svc_Common::noexport_hack = 1;
829 $cust_main->cancel('quiet'=>1);
831 my $perror = $placeholder->depended_delete;
832 warn "error removing provisioning jobs after decline: $perror" if $perror;
834 $perror = $placeholder->delete;
835 warn "error removing placeholder after decline: $perror" if $perror;
838 return { 'error' => '_decline' };
844 $error = $reg_code->delete;
845 return { 'error' => $error } if $error;
848 $error = $placeholder->delete;
849 return { 'error' => $error } if $error;
851 if ( $conf->exists('signup-duplicate_cc-warn_hours') ) {
852 my $hours = $conf->config('signup-duplicate_cc-warn_hours');
853 my $ban = new FS::banned_pay $cust_main->_new_banned_pay_hashref;
854 $ban->end_date( int( time + $hours*3600 ) );
855 $ban->bantype('warn');
856 $ban->reason('signup-duplicate_cc-warn_hours');
857 $error = $ban->insert;
858 warn "WARNING: error inserting temporary banned_pay for ".
859 " signup-duplicate_cc-warn_hours (proceeding anyway): $error"
863 my %return = ( 'error' => '',
864 'signup_service' => $svc_x,
865 'custnum' => $cust_main->custnum,
870 $return{'svcnum'} = $svc[0]->svcnum;
872 if ( $svc_x eq 'svc_acct' ) {
873 $return{$_} = $svc[0]->$_() for qw( username _password );
874 } elsif ( $svc_x eq 'svc_phone' ) {
875 $return{$_} = $svc[0]->$_() for qw(countrycode phonenum sip_password pin);
876 } elsif ( $svc_x eq 'svc_pbx' ) {
877 #$return{$_} = $svc[0]->$_() for qw( ) #nothing yet
879 return {'error' => "configuration error: unknown signup service $svc_x"};
880 #die "unknown signup service $svc_x";
881 # return an error that's visible to someone somewhere
890 #false laziness w/ above
891 # fresh restart to support "free account" portals with 3.x/4.x-style
892 # addressless accounts
893 # and a contact (for self-service login)
894 sub new_customer_minimal {
897 my $conf = new FS::Conf;
898 my $svc_x = $conf->config('signup_server-service') || 'svc_acct';
900 if ( $svc_x eq 'svc_acct' ) {
902 #things that aren't necessary in base class, but are for signup server
903 #return "Passwords don't match"
904 # if $hashref->{'_password'} ne $hashref->{'_password2'}
905 return { 'error' => gettext('empty_password') }
906 unless length($packet->{'_password'});
907 # a bit inefficient for large numbers of pops
908 return { 'error' => gettext('no_access_number_selected') }
909 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
912 elsif ( $svc_x eq 'svc_pbx' ) {
913 #possibly some validation will be needed
917 if ( exists $packet->{'session_id'} ) {
918 my $cache = new FS::ClientAPI_SessionCache( {
919 'namespace' => 'FS::ClientAPI::Agent',
921 my $session = $cache->get($packet->{'session_id'});
923 $agentnum = $session->{'agentnum'};
925 return { 'error' => "Can't resume session" }; #better error message
928 $agentnum = $packet->{agentnum}
929 || $conf->config('signup_server-default_agentnum');
932 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
933 # common that are still here and library them.
935 my $cust_main = new FS::cust_main ( {
937 'agentnum' => $agentnum,
938 'refnum' => $packet->{refnum}
939 || $conf->config('signup_server-default_refnum'),
942 map { $_ => $packet->{$_} } qw(
943 last first ss company
944 daytime night fax mobile
949 my @invoicing_list = $packet->{'invoicing_list'}
950 ? split( /\s*\,\s*/, $packet->{'invoicing_list'} )
954 tie my %hash, 'Tie::RefHash', ();
957 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
963 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
964 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
965 my $svcpart = $part_pkg->svcpart($svc_x);
967 my $cust_pkg = new FS::cust_pkg ( {
968 #later#'custnum' => $custnum,
969 'pkgpart' => $packet->{'pkgpart'},
971 #my $error = $cust_pkg->check;
972 #return { 'error' => $error } if $error;
974 #should be all auto-magic and shit
975 if ( $svc_x eq 'svc_acct' ) {
977 my $svc = new FS::svc_acct {
978 'svcpart' => $svcpart,
979 map { $_ => $packet->{$_} }
980 qw( username _password sec_phrase popnum domsvc ),
985 } elsif ( $svc_x eq 'svc_phone' ) {
987 push @svc, new FS::svc_phone ( {
988 'svcpart' => $svcpart,
989 map { $_ => $packet->{$_} }
990 qw( countrycode phonenum sip_password pin ),
993 } elsif ( $svc_x eq 'svc_pbx' ) {
995 push @svc, new FS::svc_pbx ( {
996 'svcpart' => $svcpart,
997 map { $_ => $packet->{$_} }
1002 die "unknown signup service $svc_x";
1005 foreach my $svc ( @svc ) {
1006 my $y = $svc->setdefault; # arguably should be in new method
1007 return { 'error' => $y } if $y && !ref($y);
1008 #$error = $svc->check;
1009 #return { 'error' => $error } if $error;
1013 tie my %hash, 'Tie::RefHash';
1014 $hash{ $cust_pkg } = \@svc;
1019 if ( $invoicing_list[0] && $packet->{'_password'} ) {
1021 new FS::contact { 'first' => $cust_main->first,
1022 'last' => $cust_main->get('last'),
1023 '_password' => $packet->{'_password'},
1024 'emailaddress' => $invoicing_list[0],
1025 'selfservice_access' => 'Y',
1030 my $error = $cust_main->insert(
1035 return { 'error' => $error } if $error;
1037 my $session = { 'custnum' => $cust_main->custnum };
1041 $session_id = sha1_hex(time(). {}. rand(). $$)
1042 } until ( ! defined _myaccount_cache->get($session_id) ); #just in case
1044 _cache->set( $session_id, $session, '1 hour' ); # 1 hour?
1046 my %return = ( 'error' => '',
1047 'signup_service' => $svc_x,
1048 'custnum' => $cust_main->custnum,
1049 'session_id' => $session_id,
1054 $return{'svcnum'} = $svc[0]->svcnum;
1056 if ( $svc_x eq 'svc_acct' ) {
1057 $return{$_} = $svc[0]->$_() for qw( username _password );
1058 } elsif ( $svc_x eq 'svc_phone' ) {
1059 $return{$_} = $svc[0]->$_() for qw(countrycode phonenum sip_password pin);
1060 } elsif ( $svc_x eq 'svc_pbx' ) {
1061 #$return{$_} = $svc[0]->$_() for qw( ) #nothing yet
1063 return {'error' => "configuration error: unknown signup service $svc_x"};
1064 #die "unknown signup service $svc_x";
1065 # return an error that's visible to someone somewhere
1074 use vars qw( $myaccount_cache );
1075 sub _myaccount_cache {
1076 $myaccount_cache ||= new FS::ClientAPI_SessionCache( {
1077 'namespace' => 'FS::ClientAPI::MyAccount',
1081 sub capture_payment {
1084 warn "$me capture_payment called on $packet\n" if $DEBUG;
1087 # identify processor/gateway from called back URL
1090 my $conf = new FS::Conf;
1092 my $payment_gateway;
1093 if ( my $gwnum = $conf->config('selfservice-payment_gateway') ) {
1094 $payment_gateway = qsearchs('payment_gateway', { 'gatewaynum' => $gwnum })
1095 or die "configured gatewaynum $gwnum not found!";
1098 my $url = $packet->{url};
1100 $payment_gateway = qsearchs('payment_gateway',
1101 { 'gateway_callback_url' => popurl(0, $url) }
1103 if (!$payment_gateway) {
1105 my ( $processor, $login, $password, $action, @bop_options ) =
1106 $conf->config('business-onlinepayment');
1107 $action ||= 'normal authorization';
1108 pop @bop_options if scalar(@bop_options) % 2 && $bop_options[-1] =~ /^\s*$/;
1109 die "No real-time processor is enabled - ".
1110 "did you set the business-onlinepayment configuration value?\n"
1113 $payment_gateway = new FS::payment_gateway( {
1114 gateway_namespace => $conf->config('business-onlinepayment-namespace'),
1115 gateway_module => $processor,
1116 gateway_username => $login,
1117 gateway_password => $password,
1118 gateway_action => $action,
1119 options => [ ( @bop_options ) ],
1124 die "No real-time third party processor is enabled - ".
1125 "did you set the business-onlinepayment configuration value?\n*"
1126 unless $payment_gateway->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
1129 # locate pending transaction
1132 eval "use Business::OnlineThirdPartyPayment";
1136 new Business::OnlineThirdPartyPayment( $payment_gateway->gateway_module,
1137 @{ [ $payment_gateway->options ] },
1140 my $paypendingnum = $transaction->reference($packet->{data});
1142 my $cust_pay_pending =
1143 qsearchs('cust_pay_pending', { paypendingnum => $paypendingnum } );
1145 unless ($cust_pay_pending) {
1146 my $bill_error = "No payment is being processed with id $paypendingnum".
1147 "; Transaction aborted.";
1148 return { error => '_decline', bill_error => $bill_error };
1151 if ($cust_pay_pending->status ne 'thirdparty') {
1152 my $bill_error = "Payment with id $paypendingnum is not thirdparty, but ".
1153 $cust_pay_pending->status. "; Transaction aborted.";
1154 return { error => '_decline', bill_error => $bill_error };
1157 my $cust_main = $cust_pay_pending->cust_main;
1158 if ( $packet->{cancel} ) {
1159 # the user has chosen not to make this payment
1160 # (probably should be a separate API call, but I don't want to duplicate
1161 # all of the above...which should eventually go away)
1162 my $error = $cust_pay_pending->delete;
1163 # don't show any errors related to this; they're not meaningful
1164 warn "error canceling pending payment $paypendingnum: $error\n" if $error;
1165 return { 'error' => '_cancel',
1166 'session_id' => $cust_pay_pending->session_id };
1168 # create the payment
1170 $cust_main->realtime_botpp_capture( $cust_pay_pending,
1175 return { 'error' => ( $bill_error->{bill_error} ? '_decline' : '' ),