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 for performance;
29 [ map { $_->hashref } qsearch('cust_main_county', {}) ],
34 qsearch('agent', dbdef->table('agent')->column('disabled')
35 ? { 'disabled' => '' }
43 qsearch('part_referral',
44 dbdef->table('part_referral')->column('disabled')
45 ? { 'disabled' => '' }
50 'agentnum2part_pkg' =>
53 my $href = $_->pkgpart_hashref;
56 map { { 'payby' => [ $_->payby ],
57 'freq_pretty' => $_->freq_pretty,
58 'options' => { $_->options },
61 grep { $_->svcpart('svc_acct') && $href->{ $_->pkgpart } }
62 qsearch( 'part_pkg', { 'disabled' => '' } )
64 } qsearch('agent', dbdef->table('agent')->column('disabled')
65 ? { 'disabled' => '' }
70 'svc_acct_pop' => [ map { $_->hashref } qsearch('svc_acct_pop',{} ) ],
72 'emailinvoiceonly' => $conf->exists('emailinvoiceonly'),
74 'security_phrase' => $conf->exists('security_phrase'),
76 'payby' => [ $conf->config('signup_server-payby') ],
78 'card_types' => card_types(),
80 'cvv_enabled' => defined dbdef->table('cust_main')->column('paycvv'),
82 'ship_enabled' => defined dbdef->table('cust_main')->column('ship_last'),
84 'msgcat' => { map { $_=>gettext($_) } qw(
85 passwords_dont_match invalid_card unknown_card_type not_a empty_password illegal_or_empty_text
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 @addl = qw( signup_server-classnum2 signup_server-classnum3 );
100 if ( grep { $conf->exists($_) } @addl ) {
102 $signup_info->{optional_packages} = [];
103 $signup_info->{optional_packages_options} = [];
105 foreach my $addl ( @addl ) {
106 my $classnum = $conf->config($addl) or next;
107 my @pkgs = map { $_->hashref }
108 qsearch( 'part_pkg', { classnum => $classnum } );
109 push @{$signup_info->{optional_packages}}, \@pkgs;
110 my @options = map { { $_->options,
111 'freq_pretty' => $_->freq_pretty
113 push @{$signup_info->{options_packages_options}}, \@options
118 my $agentnum = $packet->{'agentnum'}
119 || $conf->config('signup_server-default_agentnum');
122 if ( exists $packet->{'session_id'} ) {
123 my $cache = new FS::ClientAPI_SessionCache( {
124 'namespace' => 'FS::ClientAPI::Agent',
126 $session = $cache->get($packet->{'session_id'});
128 $agentnum = $session->{'agentnum'};
130 return { 'error' => "Can't resume session" }; #better error message
132 }elsif( exists $packet->{'customer_session_id'} ) {
133 my $cache = new FS::ClientAPI_SessionCache( {
134 'namespace' => 'FS::ClientAPI::MyAccount',
136 $session = $cache->get($packet->{'customer_session_id'});
138 my $custnum = $session->{'custnum'};
139 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum });
140 return { 'error' => "Can't find your customer record" } unless $cust_main;
141 $agentnum = $cust_main->agentnum;
143 return { 'error' => "Can't resume session" }; #better error message
147 $signup_info->{'part_pkg'} = [];
149 if ( $packet->{'reg_code'} ) {
150 $signup_info->{'part_pkg'} =
151 [ map { { 'payby' => [ $_->payby ],
152 'freq_pretty' => $_->freq_pretty,
153 'options' => { $_->options },
156 grep { $_->svcpart('svc_acct') }
158 qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
159 'agentnum' => $agentnum, } )
163 $signup_info->{'error'} = 'Unknown registration code'
164 unless @{ $signup_info->{'part_pkg'} };
166 } elsif ( $packet->{'promo_code'} ) {
168 $signup_info->{'part_pkg'} =
169 [ map { { 'payby' => [ $_->payby ],
170 'freq_pretty' => $_->freq_pretty,
171 'options' => { $_->options },
174 grep { $_->svcpart('svc_acct') }
175 qsearch( 'part_pkg', { 'promo_code' => {
177 value=>$packet->{'promo_code'}
179 'disabled' => '', } )
182 $signup_info->{'error'} = 'Unknown promotional code'
183 unless @{ $signup_info->{'part_pkg'} };
186 if ( $agentnum && ! @{ $signup_info->{'part_pkg'} } ) {
187 $signup_info->{'part_pkg'} = $signup_info->{'agentnum2part_pkg'}{$agentnum};
190 # delete $signup_info->{'part_pkg'};
193 if ( exists $packet->{'session_id'} ) {
194 my $agent_signup_info = { %$signup_info };
195 delete $agent_signup_info->{agentnum2part_pkg};
196 $agent_signup_info->{'agent'} = $session->{'agent'};
207 my $conf = new FS::Conf;
209 #things that aren't necessary in base class, but are for signup server
210 #return "Passwords don't match"
211 # if $hashref->{'_password'} ne $hashref->{'_password2'}
212 return { 'error' => gettext('empty_password') }
213 unless length($packet->{'_password'});
214 # a bit inefficient for large numbers of pops
215 return { 'error' => gettext('no_access_number_selected') }
216 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
219 if ( exists $packet->{'session_id'} ) {
220 my $cache = new FS::ClientAPI_SessionCache( {
221 'namespace' => 'FS::ClientAPI::Agent',
223 my $session = $cache->get($packet->{'session_id'});
225 $agentnum = $session->{'agentnum'};
227 return { 'error' => "Can't resume session" }; #better error message
230 $agentnum = $packet->{agentnum}
231 || $conf->config('signup_server-default_agentnum');
234 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
235 # common that are still here and library them.
236 my $cust_main = new FS::cust_main ( {
238 'agentnum' => $agentnum,
239 'refnum' => $packet->{refnum}
240 || $conf->config('signup_server-default_refnum'),
242 map { $_ => $packet->{$_} } qw(
244 last first ss company address1 address2
245 city county state zip country
248 ship_last ship_first ship_ss ship_company ship_address1 ship_address2
249 ship_city ship_county ship_state ship_zip ship_country
250 ship_daytime ship_night ship_fax
253 payinfo paycvv paydate payname
254 paystart_month paystart_year payissue
257 referral_custnum comments
262 return { 'error' => "Illegal payment type" }
263 unless grep { $_ eq $packet->{'payby'} }
264 $conf->config('signup_server-payby');
266 $cust_main->payinfo($cust_main->daytime)
267 if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
269 my @invoicing_list = split( /\s*\,\s*/, $packet->{'invoicing_list'} );
271 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
273 return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
276 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
277 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
278 my $svcpart = $part_pkg->svcpart('svc_acct');
281 if ( $packet->{'reg_code'} ) {
282 $reg_code = qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
283 'agentnum' => $agentnum, } )
284 or return { 'error' => 'Unknown registration code' };
287 my $cust_pkg = new FS::cust_pkg ( {
288 #later#'custnum' => $custnum,
289 'pkgpart' => $packet->{'pkgpart'},
290 'promo_code' => $packet->{'promo_code'},
291 'reg_code' => $packet->{'reg_code'},
293 #my $error = $cust_pkg->check;
294 #return { 'error' => $error } if $error;
296 my $svc_acct = new FS::svc_acct ( {
297 'svcpart' => $svcpart,
298 map { $_ => $packet->{$_} }
299 qw( username _password sec_phrase popnum ),
304 while ( exists($packet->{"snarf_machine$snarfnum"})
305 && length($packet->{"snarf_machine$snarfnum"}) ) {
306 my $acct_snarf = new FS::acct_snarf ( {
307 'machine' => $packet->{"snarf_machine$snarfnum"},
308 'protocol' => $packet->{"snarf_protocol$snarfnum"},
309 'username' => $packet->{"snarf_username$snarfnum"},
310 '_password' => $packet->{"snarf_password$snarfnum"},
313 push @acct_snarf, $acct_snarf;
315 $svc_acct->child_objects( \@acct_snarf );
317 my $y = $svc_acct->setdefault; # arguably should be in new method
318 return { 'error' => $y } if $y && !ref($y);
320 #$error = $svc_acct->check;
321 #return { 'error' => $error } if $error;
323 #setup a job dependancy to delay provisioning
324 my $placeholder = new FS::queue ( {
325 'job' => 'FS::ClientAPI::Signup::__placeholder',
326 'status' => 'locked',
328 my $error = $placeholder->insert;
329 return { 'error' => $error } if $error;
332 tie my %hash, 'Tie::RefHash';
333 %hash = ( $cust_pkg => [ $svc_acct ] );
335 $error = $cust_main->insert(
338 'depend_jobnum' => $placeholder->jobnum,
341 my $perror = $placeholder->delete;
342 $error .= " (Additionally, error removing placeholder: $perror)" if $perror;
343 return { 'error' => $error };
346 if ( $conf->exists('signup_server-realtime') ) {
348 #warn "[fs_signup_server] Billing customer...\n" if $Debug;
350 my $bill_error = $cust_main->bill;
351 #warn "[fs_signup_server] error billing new customer: $bill_error"
354 $cust_main->apply_payments_and_credits;
356 $bill_error = $cust_main->collect('realtime' => 1);
357 #warn "[fs_signup_server] error collecting from new customer: $bill_error"
360 if ( $cust_main->balance > 0 ) {
362 #this makes sense. credit is "un-doing" the invoice
363 $cust_main->credit( $cust_main->balance, 'signup server decline' );
364 $cust_main->apply_credits;
366 #should check list for errors...
367 #$cust_main->suspend;
368 local $FS::svc_Common::noexport_hack = 1;
369 $cust_main->cancel('quiet'=>1);
371 my $perror = $placeholder->depended_delete;
372 warn "error removing provisioning jobs after decline: $perror" if $perror;
374 $perror = $placeholder->delete;
375 warn "error removing placeholder after decline: $perror" if $perror;
378 return { 'error' => '_decline' };
384 $error = $reg_code->delete;
385 return { 'error' => $error } if $error;
388 $error = $placeholder->delete;
389 return { 'error' => $error } if $error;
391 return { error => '' };