1 package FS::ClientAPI::Signup;
6 use FS::Record qw(qsearch qsearchs dbdef);
7 use FS::Msgcat qw(gettext);
8 use FS::Misc qw(card_types);
9 use FS::ClientAPI_SessionCache;
11 use FS::cust_main_county;
24 my $conf = new FS::Conf;
26 use vars qw($signup_info_cache); #cache for performance;
27 $signup_info_cache ||= {
29 [ map { $_->hashref } qsearch('cust_main_county', {}) ],
34 qsearch('agent', { 'disabled' => '' } )
40 qsearch('part_referral', { 'disabled' => '' })
43 'agentnum2part_pkg' =>
46 my $href = $_->pkgpart_hashref;
49 map { { 'payby' => [ $_->payby ],
50 'freq_pretty' => $_->freq_pretty,
51 'options' => { $_->options },
54 grep { $_->svcpart('svc_acct') && $href->{ $_->pkgpart } }
55 qsearch( 'part_pkg', { 'disabled' => '' } )
57 } qsearch('agent', { 'disabled' => '' })
60 'svc_acct_pop' => [ map { $_->hashref } qsearch('svc_acct_pop',{} ) ],
62 'emailinvoiceonly' => $conf->exists('emailinvoiceonly'),
64 'security_phrase' => $conf->exists('security_phrase'),
66 'payby' => [ $conf->config('signup_server-payby') ],
68 'card_types' => card_types(),
70 'paytypes' => [ @FS::cust_main::paytypes ],
72 'cvv_enabled' => defined dbdef->table('cust_main')->column('paycvv'), # 1,
74 'stateid_enabled' => $conf->exists('show_stateid'),
76 'paystate_enabled' => $conf->exists('show_bankstate'),
78 'ship_enabled' => defined dbdef->table('cust_main')->column('ship_last'),#1,
80 'msgcat' => { map { $_=>gettext($_) } qw(
81 passwords_dont_match invalid_card unknown_card_type not_a empty_password illegal_or_empty_text
84 'label' => { map { $_ => FS::Msgcat::_gettext($_) } qw(
88 'statedefault' => $conf->config('statedefault') || 'CA',
90 'countrydefault' => $conf->config('countrydefault') || 'US',
92 'refnum' => $conf->config('signup_server-default_refnum'),
94 'default_pkgpart' => $conf->config('signup_server-default_pkgpart'),
98 my $signup_info = { %$signup_info_cache };
100 my @addl = qw( signup_server-classnum2 signup_server-classnum3 );
102 if ( grep { $conf->exists($_) } @addl ) {
104 $signup_info->{optional_packages} = [];
106 foreach my $addl ( @addl ) {
107 my $classnum = $conf->config($addl) or next;
110 'freq_pretty' => $_->freq_pretty,
111 'options' => { $_->options },
115 qsearch( 'part_pkg', { classnum => $classnum } );
117 push @{$signup_info->{optional_packages}}, \@pkgs;
123 my $agentnum = $packet->{'agentnum'}
124 || $conf->config('signup_server-default_agentnum');
125 $agentnum =~ /^(\d*)$/ or die "illegal agentnum";
129 if ( exists $packet->{'session_id'} ) {
130 my $cache = new FS::ClientAPI_SessionCache( {
131 'namespace' => 'FS::ClientAPI::Agent',
133 $session = $cache->get($packet->{'session_id'});
135 $agentnum = $session->{'agentnum'};
137 return { 'error' => "Can't resume session" }; #better error message
139 }elsif( exists $packet->{'customer_session_id'} ) {
140 my $cache = new FS::ClientAPI_SessionCache( {
141 'namespace' => 'FS::ClientAPI::MyAccount',
143 $session = $cache->get($packet->{'customer_session_id'});
145 my $custnum = $session->{'custnum'};
146 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum });
147 return { 'error' => "Can't find your customer record" } unless $cust_main;
148 $agentnum = $cust_main->agentnum;
150 return { 'error' => "Can't resume session" }; #better error message
154 $signup_info->{'part_pkg'} = [];
156 if ( $packet->{'reg_code'} ) {
157 $signup_info->{'part_pkg'} =
158 [ map { { 'payby' => [ $_->payby ],
159 'freq_pretty' => $_->freq_pretty,
160 'options' => { $_->options },
164 grep { $_->svcpart('svc_acct') }
166 qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
167 'agentnum' => $agentnum, } )
171 $signup_info->{'error'} = 'Unknown registration code'
172 unless @{ $signup_info->{'part_pkg'} };
174 } elsif ( $packet->{'promo_code'} ) {
176 $signup_info->{'part_pkg'} =
177 [ map { { 'payby' => [ $_->payby ],
178 'freq_pretty' => $_->freq_pretty,
179 'options' => { $_->options },
182 grep { $_->svcpart('svc_acct') }
183 qsearch( 'part_pkg', { 'promo_code' => {
185 value=>$packet->{'promo_code'}
187 'disabled' => '', } )
190 $signup_info->{'error'} = 'Unknown promotional code'
191 unless @{ $signup_info->{'part_pkg'} };
195 $signup_info->{'part_pkg'} = $signup_info->{'agentnum2part_pkg'}{$agentnum}
196 unless @{ $signup_info->{'part_pkg'} };
198 $signup_info->{'part_referral'} =
202 'table' => 'part_referral',
203 'hashref' => { 'disabled' => '' },
204 'extra_sql' => "AND ( agentnum = $agentnum ".
205 " OR agentnum IS NULL ) ",
212 # delete $signup_info->{'part_pkg'};
215 $signup_info->{'part_pkg'} = [ sort { $a->{pkg} cmp $b->{pkg} } # case?
216 @{ $signup_info->{'part_pkg'} }
219 if ( exists $packet->{'session_id'} ) {
220 my $agent_signup_info = { %$signup_info };
221 delete $agent_signup_info->{agentnum2part_pkg};
222 $agent_signup_info->{'agent'} = $session->{'agent'};
233 my $conf = new FS::Conf;
235 #things that aren't necessary in base class, but are for signup server
236 #return "Passwords don't match"
237 # if $hashref->{'_password'} ne $hashref->{'_password2'}
238 return { 'error' => gettext('empty_password') }
239 unless length($packet->{'_password'});
240 # a bit inefficient for large numbers of pops
241 return { 'error' => gettext('no_access_number_selected') }
242 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
245 if ( exists $packet->{'session_id'} ) {
246 my $cache = new FS::ClientAPI_SessionCache( {
247 'namespace' => 'FS::ClientAPI::Agent',
249 my $session = $cache->get($packet->{'session_id'});
251 $agentnum = $session->{'agentnum'};
253 return { 'error' => "Can't resume session" }; #better error message
256 $agentnum = $packet->{agentnum}
257 || $conf->config('signup_server-default_agentnum');
260 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
261 # common that are still here and library them.
262 my $cust_main = new FS::cust_main ( {
264 'agentnum' => $agentnum,
265 'refnum' => $packet->{refnum}
266 || $conf->config('signup_server-default_refnum'),
268 map { $_ => $packet->{$_} } qw(
270 last first ss company address1 address2
271 city county state zip country
272 daytime night fax stateid stateid_state
274 ship_last ship_first ship_ss ship_company ship_address1 ship_address2
275 ship_city ship_county ship_state ship_zip ship_country
276 ship_daytime ship_night ship_fax
279 payinfo paycvv paydate payname paystate paytype
280 paystart_month paystart_year payissue
283 referral_custnum comments
288 return { 'error' => "Illegal payment type" }
289 unless grep { $_ eq $packet->{'payby'} }
290 $conf->config('signup_server-payby');
292 $cust_main->payinfo($cust_main->daytime)
293 if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
295 my @invoicing_list = $packet->{'invoicing_list'}
296 ? split( /\s*\,\s*/, $packet->{'invoicing_list'} )
299 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
301 return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
304 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
305 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
306 my $svcpart = $part_pkg->svcpart('svc_acct');
309 if ( $packet->{'reg_code'} ) {
310 $reg_code = qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
311 'agentnum' => $agentnum, } )
312 or return { 'error' => 'Unknown registration code' };
315 my $cust_pkg = new FS::cust_pkg ( {
316 #later#'custnum' => $custnum,
317 'pkgpart' => $packet->{'pkgpart'},
318 'promo_code' => $packet->{'promo_code'},
319 'reg_code' => $packet->{'reg_code'},
321 #my $error = $cust_pkg->check;
322 #return { 'error' => $error } if $error;
324 my $svc_acct = new FS::svc_acct ( {
325 'svcpart' => $svcpart,
326 map { $_ => $packet->{$_} }
327 qw( username _password sec_phrase popnum ),
332 while ( exists($packet->{"snarf_machine$snarfnum"})
333 && length($packet->{"snarf_machine$snarfnum"}) ) {
334 my $acct_snarf = new FS::acct_snarf ( {
335 'machine' => $packet->{"snarf_machine$snarfnum"},
336 'protocol' => $packet->{"snarf_protocol$snarfnum"},
337 'username' => $packet->{"snarf_username$snarfnum"},
338 '_password' => $packet->{"snarf_password$snarfnum"},
341 push @acct_snarf, $acct_snarf;
343 $svc_acct->child_objects( \@acct_snarf );
345 my $y = $svc_acct->setdefault; # arguably should be in new method
346 return { 'error' => $y } if $y && !ref($y);
348 #$error = $svc_acct->check;
349 #return { 'error' => $error } if $error;
351 #setup a job dependancy to delay provisioning
352 my $placeholder = new FS::queue ( {
353 'job' => 'FS::ClientAPI::Signup::__placeholder',
354 'status' => 'locked',
356 my $error = $placeholder->insert;
357 return { 'error' => $error } if $error;
360 tie my %hash, 'Tie::RefHash';
361 %hash = ( $cust_pkg => [ $svc_acct ] );
363 $error = $cust_main->insert(
366 'depend_jobnum' => $placeholder->jobnum,
369 my $perror = $placeholder->delete;
370 $error .= " (Additionally, error removing placeholder: $perror)" if $perror;
371 return { 'error' => $error };
374 if ( $conf->exists('signup_server-realtime') ) {
376 #warn "[fs_signup_server] Billing customer...\n" if $Debug;
378 my $bill_error = $cust_main->bill;
379 #warn "[fs_signup_server] error billing new customer: $bill_error"
382 $cust_main->apply_payments_and_credits;
384 $bill_error = $cust_main->collect('realtime' => 1);
385 #warn "[fs_signup_server] error collecting from new customer: $bill_error"
388 if ( $cust_main->balance > 0 ) {
390 #this makes sense. credit is "un-doing" the invoice
391 $cust_main->credit( $cust_main->balance, 'signup server decline' );
392 $cust_main->apply_credits;
394 #should check list for errors...
395 #$cust_main->suspend;
396 local $FS::svc_Common::noexport_hack = 1;
397 $cust_main->cancel('quiet'=>1);
399 my $perror = $placeholder->depended_delete;
400 warn "error removing provisioning jobs after decline: $perror" if $perror;
402 $perror = $placeholder->delete;
403 warn "error removing placeholder after decline: $perror" if $perror;
406 return { 'error' => '_decline' };
412 $error = $reg_code->delete;
413 return { 'error' => $error } if $error;
416 $error = $placeholder->delete;
417 return { 'error' => $error } if $error;
419 return { error => '' };