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 ], %{$_->hashref} } }
57 grep { $_->svcpart('svc_acct') && $href->{ $_->pkgpart } }
58 qsearch( 'part_pkg', { 'disabled' => '' } )
60 } qsearch('agent', dbdef->table('agent')->column('disabled')
61 ? { 'disabled' => '' }
66 'svc_acct_pop' => [ map { $_->hashref } qsearch('svc_acct_pop',{} ) ],
68 'emailinvoiceonly' => $conf->exists('emailinvoiceonly'),
70 'security_phrase' => $conf->exists('security_phrase'),
72 'payby' => [ $conf->config('signup_server-payby') ],
74 'card_types' => card_types(),
76 'cvv_enabled' => defined dbdef->table('cust_main')->column('paycvv'),
78 'ship_enabled' => defined dbdef->table('cust_main')->column('ship_last'),
80 'msgcat' => { map { $_=>gettext($_) } qw(
81 passwords_dont_match invalid_card unknown_card_type not_a empty_password illegal_or_empty_text
84 'statedefault' => $conf->config('statedefault') || 'CA',
86 'countrydefault' => $conf->config('countrydefault') || 'US',
88 'refnum' => $conf->config('signup_server-default_refnum'),
90 'default_pkgpart' => $conf->config('signup_server-default_pkgpart'),
94 my @addl = qw( signup_server-classnum2 signup_server-classnum3 );
96 if ( grep { $conf->exists($_) } @addl ) {
98 $signup_info->{optional_packages} = [];
100 foreach my $addl ( @addl ) {
101 my $classnum = $conf->config($addl) or next;
102 my @pkgs = map { $_->hashref }
103 qsearch( 'part_pkg', { classnum => $classnum } );
104 push @{$signup_info->{optional_packages}}, \@pkgs;
109 my $agentnum = $packet->{'agentnum'}
110 || $conf->config('signup_server-default_agentnum');
113 if ( exists $packet->{'session_id'} ) {
114 my $cache = new FS::ClientAPI_SessionCache( {
115 'namespace' => 'FS::ClientAPI::Agent',
117 $session = $cache->get($packet->{'session_id'});
119 $agentnum = $session->{'agentnum'};
121 return { 'error' => "Can't resume session" }; #better error message
123 }elsif( exists $packet->{'customer_session_id'} ) {
124 my $cache = new FS::ClientAPI_SessionCache( {
125 'namespace' => 'FS::ClientAPI::MyAccount',
127 $session = $cache->get($packet->{'customer_session_id'});
129 my $custnum = $session->{'custnum'};
130 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum });
131 return { 'error' => "Can't find your customer record" } unless $cust_main;
132 $agentnum = $cust_main->agentnum;
134 return { 'error' => "Can't resume session" }; #better error message
138 $signup_info->{'part_pkg'} = [];
140 if ( $packet->{'reg_code'} ) {
141 $signup_info->{'part_pkg'} =
142 [ map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
143 grep { $_->svcpart('svc_acct') }
145 qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
146 'agentnum' => $agentnum, } )
150 $signup_info->{'error'} = 'Unknown registration code'
151 unless @{ $signup_info->{'part_pkg'} };
153 } elsif ( $packet->{'promo_code'} ) {
155 $signup_info->{'part_pkg'} =
156 [ map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
157 grep { $_->svcpart('svc_acct') }
158 qsearch( 'part_pkg', { 'promo_code' => {
160 value=>$packet->{'promo_code'}
162 'disabled' => '', } )
165 $signup_info->{'error'} = 'Unknown promotional code'
166 unless @{ $signup_info->{'part_pkg'} };
169 if ( $agentnum && ! @{ $signup_info->{'part_pkg'} } ) {
170 $signup_info->{'part_pkg'} = $signup_info->{'agentnum2part_pkg'}{$agentnum};
173 # delete $signup_info->{'part_pkg'};
176 if ( exists $packet->{'session_id'} ) {
177 my $agent_signup_info = { %$signup_info };
178 delete $agent_signup_info->{agentnum2part_pkg};
179 $agent_signup_info->{'agent'} = $session->{'agent'};
190 my $conf = new FS::Conf;
192 #things that aren't necessary in base class, but are for signup server
193 #return "Passwords don't match"
194 # if $hashref->{'_password'} ne $hashref->{'_password2'}
195 return { 'error' => gettext('empty_password') }
196 unless length($packet->{'_password'});
197 # a bit inefficient for large numbers of pops
198 return { 'error' => gettext('no_access_number_selected') }
199 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
202 if ( exists $packet->{'session_id'} ) {
203 my $cache = new FS::ClientAPI_SessionCache( {
204 'namespace' => 'FS::ClientAPI::Agent',
206 my $session = $cache->get($packet->{'session_id'});
208 $agentnum = $session->{'agentnum'};
210 return { 'error' => "Can't resume session" }; #better error message
213 $agentnum = $packet->{agentnum}
214 || $conf->config('signup_server-default_agentnum');
217 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
218 # common that are still here and library them.
219 my $cust_main = new FS::cust_main ( {
221 'agentnum' => $agentnum,
222 'refnum' => $packet->{refnum}
223 || $conf->config('signup_server-default_refnum'),
225 map { $_ => $packet->{$_} } qw(
227 last first ss company address1 address2
228 city county state zip country
231 ship_last ship_first ship_ss ship_company ship_address1 ship_address2
232 ship_city ship_county ship_state ship_zip ship_country
233 ship_daytime ship_night ship_fax
236 payinfo paycvv paydate payname
237 paystart_month paystart_year payissue
240 referral_custnum comments
245 return { 'error' => "Illegal payment type" }
246 unless grep { $_ eq $packet->{'payby'} }
247 $conf->config('signup_server-payby');
249 $cust_main->payinfo($cust_main->daytime)
250 if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
252 my @invoicing_list = split( /\s*\,\s*/, $packet->{'invoicing_list'} );
254 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
256 return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
259 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
260 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
261 my $svcpart = $part_pkg->svcpart('svc_acct');
264 if ( $packet->{'reg_code'} ) {
265 $reg_code = qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
266 'agentnum' => $agentnum, } )
267 or return { 'error' => 'Unknown registration code' };
270 my $cust_pkg = new FS::cust_pkg ( {
271 #later#'custnum' => $custnum,
272 'pkgpart' => $packet->{'pkgpart'},
273 'promo_code' => $packet->{'promo_code'},
274 'reg_code' => $packet->{'reg_code'},
276 #my $error = $cust_pkg->check;
277 #return { 'error' => $error } if $error;
279 my $svc_acct = new FS::svc_acct ( {
280 'svcpart' => $svcpart,
281 map { $_ => $packet->{$_} }
282 qw( username _password sec_phrase popnum ),
287 while ( exists($packet->{"snarf_machine$snarfnum"})
288 && length($packet->{"snarf_machine$snarfnum"}) ) {
289 my $acct_snarf = new FS::acct_snarf ( {
290 'machine' => $packet->{"snarf_machine$snarfnum"},
291 'protocol' => $packet->{"snarf_protocol$snarfnum"},
292 'username' => $packet->{"snarf_username$snarfnum"},
293 '_password' => $packet->{"snarf_password$snarfnum"},
296 push @acct_snarf, $acct_snarf;
298 $svc_acct->child_objects( \@acct_snarf );
300 my $y = $svc_acct->setdefault; # arguably should be in new method
301 return { 'error' => $y } if $y && !ref($y);
303 #$error = $svc_acct->check;
304 #return { 'error' => $error } if $error;
306 #setup a job dependancy to delay provisioning
307 my $placeholder = new FS::queue ( {
308 'job' => 'FS::ClientAPI::Signup::__placeholder',
309 'status' => 'locked',
311 my $error = $placeholder->insert;
312 return { 'error' => $error } if $error;
315 tie my %hash, 'Tie::RefHash';
316 %hash = ( $cust_pkg => [ $svc_acct ] );
318 $error = $cust_main->insert(
321 'depend_jobnum' => $placeholder->jobnum,
324 my $perror = $placeholder->delete;
325 $error .= " (Additionally, error removing placeholder: $perror)" if $perror;
326 return { 'error' => $error };
329 if ( $conf->exists('signup_server-realtime') ) {
331 #warn "[fs_signup_server] Billing customer...\n" if $Debug;
333 my $bill_error = $cust_main->bill;
334 #warn "[fs_signup_server] error billing new customer: $bill_error"
337 $cust_main->apply_payments;
338 $cust_main->apply_credits;
340 $bill_error = $cust_main->collect('realtime' => 1);
341 #warn "[fs_signup_server] error collecting from new customer: $bill_error"
344 if ( $cust_main->balance > 0 ) {
346 #this makes sense. credit is "un-doing" the invoice
347 $cust_main->credit( $cust_main->balance, 'signup server decline' );
348 $cust_main->apply_credits;
350 #should check list for errors...
351 #$cust_main->suspend;
352 local $FS::svc_Common::noexport_hack = 1;
353 $cust_main->cancel('quiet'=>1);
355 my $perror = $placeholder->depended_delete;
356 warn "error removing provisioning jobs after decline: $perror" if $perror;
358 $perror = $placeholder->delete;
359 warn "error removing placeholder after decline: $perror" if $perror;
362 return { 'error' => '_decline' };
368 $error = $reg_code->delete;
369 return { 'error' => $error } if $error;
372 $error = $placeholder->delete;
373 return { 'error' => $error } if $error;
375 return { error => '' };