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} = [];
104 foreach my $addl ( @addl ) {
105 my $classnum = $conf->config($addl) or next;
108 'freq_pretty' => $_->freq_pretty,
109 'options' => { $_->options },
113 qsearch( 'part_pkg', { classnum => $classnum } );
115 push @{$signup_info->{optional_packages}}, \@pkgs;
121 my $agentnum = $packet->{'agentnum'}
122 || $conf->config('signup_server-default_agentnum');
125 if ( exists $packet->{'session_id'} ) {
126 my $cache = new FS::ClientAPI_SessionCache( {
127 'namespace' => 'FS::ClientAPI::Agent',
129 $session = $cache->get($packet->{'session_id'});
131 $agentnum = $session->{'agentnum'};
133 return { 'error' => "Can't resume session" }; #better error message
135 }elsif( exists $packet->{'customer_session_id'} ) {
136 my $cache = new FS::ClientAPI_SessionCache( {
137 'namespace' => 'FS::ClientAPI::MyAccount',
139 $session = $cache->get($packet->{'customer_session_id'});
141 my $custnum = $session->{'custnum'};
142 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum });
143 return { 'error' => "Can't find your customer record" } unless $cust_main;
144 $agentnum = $cust_main->agentnum;
146 return { 'error' => "Can't resume session" }; #better error message
150 $signup_info->{'part_pkg'} = [];
152 if ( $packet->{'reg_code'} ) {
153 $signup_info->{'part_pkg'} =
154 [ map { { 'payby' => [ $_->payby ],
155 'freq_pretty' => $_->freq_pretty,
156 'options' => { $_->options },
160 grep { $_->svcpart('svc_acct') }
162 qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
163 'agentnum' => $agentnum, } )
167 $signup_info->{'error'} = 'Unknown registration code'
168 unless @{ $signup_info->{'part_pkg'} };
170 } elsif ( $packet->{'promo_code'} ) {
172 $signup_info->{'part_pkg'} =
173 [ map { { 'payby' => [ $_->payby ],
174 'freq_pretty' => $_->freq_pretty,
175 'options' => { $_->options },
178 grep { $_->svcpart('svc_acct') }
179 qsearch( 'part_pkg', { 'promo_code' => {
181 value=>$packet->{'promo_code'}
183 'disabled' => '', } )
186 $signup_info->{'error'} = 'Unknown promotional code'
187 unless @{ $signup_info->{'part_pkg'} };
190 if ( $agentnum && ! @{ $signup_info->{'part_pkg'} } ) {
191 $signup_info->{'part_pkg'} = $signup_info->{'agentnum2part_pkg'}{$agentnum};
194 # delete $signup_info->{'part_pkg'};
197 if ( exists $packet->{'session_id'} ) {
198 my $agent_signup_info = { %$signup_info };
199 delete $agent_signup_info->{agentnum2part_pkg};
200 $agent_signup_info->{'agent'} = $session->{'agent'};
211 my $conf = new FS::Conf;
213 #things that aren't necessary in base class, but are for signup server
214 #return "Passwords don't match"
215 # if $hashref->{'_password'} ne $hashref->{'_password2'}
216 return { 'error' => gettext('empty_password') }
217 unless length($packet->{'_password'});
218 # a bit inefficient for large numbers of pops
219 return { 'error' => gettext('no_access_number_selected') }
220 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
223 if ( exists $packet->{'session_id'} ) {
224 my $cache = new FS::ClientAPI_SessionCache( {
225 'namespace' => 'FS::ClientAPI::Agent',
227 my $session = $cache->get($packet->{'session_id'});
229 $agentnum = $session->{'agentnum'};
231 return { 'error' => "Can't resume session" }; #better error message
234 $agentnum = $packet->{agentnum}
235 || $conf->config('signup_server-default_agentnum');
238 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
239 # common that are still here and library them.
240 my $cust_main = new FS::cust_main ( {
242 'agentnum' => $agentnum,
243 'refnum' => $packet->{refnum}
244 || $conf->config('signup_server-default_refnum'),
246 map { $_ => $packet->{$_} } qw(
248 last first ss company address1 address2
249 city county state zip country
252 ship_last ship_first ship_ss ship_company ship_address1 ship_address2
253 ship_city ship_county ship_state ship_zip ship_country
254 ship_daytime ship_night ship_fax
257 payinfo paycvv paydate payname
258 paystart_month paystart_year payissue
261 referral_custnum comments
266 return { 'error' => "Illegal payment type" }
267 unless grep { $_ eq $packet->{'payby'} }
268 $conf->config('signup_server-payby');
270 $cust_main->payinfo($cust_main->daytime)
271 if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
273 my @invoicing_list = split( /\s*\,\s*/, $packet->{'invoicing_list'} );
275 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
277 return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
280 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
281 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
282 my $svcpart = $part_pkg->svcpart('svc_acct');
285 if ( $packet->{'reg_code'} ) {
286 $reg_code = qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
287 'agentnum' => $agentnum, } )
288 or return { 'error' => 'Unknown registration code' };
291 my $cust_pkg = new FS::cust_pkg ( {
292 #later#'custnum' => $custnum,
293 'pkgpart' => $packet->{'pkgpart'},
294 'promo_code' => $packet->{'promo_code'},
295 'reg_code' => $packet->{'reg_code'},
297 #my $error = $cust_pkg->check;
298 #return { 'error' => $error } if $error;
300 my $svc_acct = new FS::svc_acct ( {
301 'svcpart' => $svcpart,
302 map { $_ => $packet->{$_} }
303 qw( username _password sec_phrase popnum ),
308 while ( exists($packet->{"snarf_machine$snarfnum"})
309 && length($packet->{"snarf_machine$snarfnum"}) ) {
310 my $acct_snarf = new FS::acct_snarf ( {
311 'machine' => $packet->{"snarf_machine$snarfnum"},
312 'protocol' => $packet->{"snarf_protocol$snarfnum"},
313 'username' => $packet->{"snarf_username$snarfnum"},
314 '_password' => $packet->{"snarf_password$snarfnum"},
317 push @acct_snarf, $acct_snarf;
319 $svc_acct->child_objects( \@acct_snarf );
321 my $y = $svc_acct->setdefault; # arguably should be in new method
322 return { 'error' => $y } if $y && !ref($y);
324 #$error = $svc_acct->check;
325 #return { 'error' => $error } if $error;
327 #setup a job dependancy to delay provisioning
328 my $placeholder = new FS::queue ( {
329 'job' => 'FS::ClientAPI::Signup::__placeholder',
330 'status' => 'locked',
332 my $error = $placeholder->insert;
333 return { 'error' => $error } if $error;
336 tie my %hash, 'Tie::RefHash';
337 %hash = ( $cust_pkg => [ $svc_acct ] );
339 $error = $cust_main->insert(
342 'depend_jobnum' => $placeholder->jobnum,
345 my $perror = $placeholder->delete;
346 $error .= " (Additionally, error removing placeholder: $perror)" if $perror;
347 return { 'error' => $error };
350 if ( $conf->exists('signup_server-realtime') ) {
352 #warn "[fs_signup_server] Billing customer...\n" if $Debug;
354 my $bill_error = $cust_main->bill;
355 #warn "[fs_signup_server] error billing new customer: $bill_error"
358 $cust_main->apply_payments_and_credits;
360 $bill_error = $cust_main->collect('realtime' => 1);
361 #warn "[fs_signup_server] error collecting from new customer: $bill_error"
364 if ( $cust_main->balance > 0 ) {
366 #this makes sense. credit is "un-doing" the invoice
367 $cust_main->credit( $cust_main->balance, 'signup server decline' );
368 $cust_main->apply_credits;
370 #should check list for errors...
371 #$cust_main->suspend;
372 local $FS::svc_Common::noexport_hack = 1;
373 $cust_main->cancel('quiet'=>1);
375 my $perror = $placeholder->depended_delete;
376 warn "error removing provisioning jobs after decline: $perror" if $perror;
378 $perror = $placeholder->delete;
379 warn "error removing placeholder after decline: $perror" if $perror;
382 return { 'error' => '_decline' };
388 $error = $reg_code->delete;
389 return { 'error' => $error } if $error;
392 $error = $placeholder->delete;
393 return { 'error' => $error } if $error;
395 return { error => '' };