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 = $conf->config('signup_server-default_agentnum');
112 if ( exists $packet->{'session_id'} ) {
113 my $cache = new FS::ClientAPI_SessionCache( {
114 'namespace' => 'FS::ClientAPI::Agent',
116 $session = $cache->get($packet->{'session_id'});
118 $agentnum = $session->{'agentnum'};
120 return { 'error' => "Can't resume session" }; #better error message
122 }elsif( exists $packet->{'customer_session_id'} ) {
123 my $cache = new FS::ClientAPI_SessionCache( {
124 'namespace' => 'FS::ClientAPI::MyAccount',
126 $session = $cache->get($packet->{'customer_session_id'});
128 my $custnum = $session->{'custnum'};
129 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum });
130 return { 'error' => "Can't find your customer record" } unless $cust_main;
131 $agentnum = $cust_main->agentnum;
133 return { 'error' => "Can't resume session" }; #better error message
137 $signup_info->{'part_pkg'} = [];
139 if ( $packet->{'reg_code'} ) {
140 $signup_info->{'part_pkg'} =
141 [ map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
142 grep { $_->svcpart('svc_acct') }
144 qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
145 'agentnum' => $agentnum, } )
149 $signup_info->{'error'} = 'Unknown registration code'
150 unless @{ $signup_info->{'part_pkg'} };
152 } elsif ( $packet->{'promo_code'} ) {
154 $signup_info->{'part_pkg'} =
155 [ map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
156 grep { $_->svcpart('svc_acct') }
157 qsearch( 'part_pkg', { 'promo_code' => {
159 value=>$packet->{'promo_code'}
161 'disabled' => '', } )
164 $signup_info->{'error'} = 'Unknown promotional code'
165 unless @{ $signup_info->{'part_pkg'} };
168 if ( $agentnum && ! @{ $signup_info->{'part_pkg'} } ) {
169 $signup_info->{'part_pkg'} = $signup_info->{'agentnum2part_pkg'}{$agentnum};
172 # delete $signup_info->{'part_pkg'};
175 if ( exists $packet->{'session_id'} ) {
176 my $agent_signup_info = { %$signup_info };
177 delete $agent_signup_info->{agentnum2part_pkg};
178 $agent_signup_info->{'agent'} = $session->{'agent'};
189 my $conf = new FS::Conf;
191 #things that aren't necessary in base class, but are for signup server
192 #return "Passwords don't match"
193 # if $hashref->{'_password'} ne $hashref->{'_password2'}
194 return { 'error' => gettext('empty_password') }
195 unless length($packet->{'_password'});
196 # a bit inefficient for large numbers of pops
197 return { 'error' => gettext('no_access_number_selected') }
198 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
201 if ( exists $packet->{'session_id'} ) {
202 my $cache = new FS::ClientAPI_SessionCache( {
203 'namespace' => 'FS::ClientAPI::Agent',
205 my $session = $cache->get($packet->{'session_id'});
207 $agentnum = $session->{'agentnum'};
209 return { 'error' => "Can't resume session" }; #better error message
212 $agentnum = $packet->{agentnum}
213 || $conf->config('signup_server-default_agentnum');
216 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
217 # common that are still here and library them.
218 my $cust_main = new FS::cust_main ( {
220 'agentnum' => $agentnum,
221 'refnum' => $packet->{refnum}
222 || $conf->config('signup_server-default_refnum'),
224 map { $_ => $packet->{$_} } qw(
226 last first ss company address1 address2
227 city county state zip country
230 ship_last ship_first ship_ss ship_company ship_address1 ship_address2
231 ship_city ship_county ship_state ship_zip ship_country
232 ship_daytime ship_night ship_fax
235 payinfo paycvv paydate payname
236 paystart_month paystart_year payissue
239 referral_custnum comments
244 return { 'error' => "Illegal payment type" }
245 unless grep { $_ eq $packet->{'payby'} }
246 $conf->config('signup_server-payby');
248 $cust_main->payinfo($cust_main->daytime)
249 if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
251 my @invoicing_list = split( /\s*\,\s*/, $packet->{'invoicing_list'} );
253 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
255 return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
258 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
259 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
260 my $svcpart = $part_pkg->svcpart('svc_acct');
263 if ( $packet->{'reg_code'} ) {
264 $reg_code = qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
265 'agentnum' => $agentnum, } )
266 or return { 'error' => 'Unknown registration code' };
269 my $cust_pkg = new FS::cust_pkg ( {
270 #later#'custnum' => $custnum,
271 'pkgpart' => $packet->{'pkgpart'},
272 'promo_code' => $packet->{'promo_code'},
273 'reg_code' => $packet->{'reg_code'},
275 #my $error = $cust_pkg->check;
276 #return { 'error' => $error } if $error;
278 my $svc_acct = new FS::svc_acct ( {
279 'svcpart' => $svcpart,
280 map { $_ => $packet->{$_} }
281 qw( username _password sec_phrase popnum ),
286 while ( exists($packet->{"snarf_machine$snarfnum"})
287 && length($packet->{"snarf_machine$snarfnum"}) ) {
288 my $acct_snarf = new FS::acct_snarf ( {
289 'machine' => $packet->{"snarf_machine$snarfnum"},
290 'protocol' => $packet->{"snarf_protocol$snarfnum"},
291 'username' => $packet->{"snarf_username$snarfnum"},
292 '_password' => $packet->{"snarf_password$snarfnum"},
295 push @acct_snarf, $acct_snarf;
297 $svc_acct->child_objects( \@acct_snarf );
299 my $y = $svc_acct->setdefault; # arguably should be in new method
300 return { 'error' => $y } if $y && !ref($y);
302 #$error = $svc_acct->check;
303 #return { 'error' => $error } if $error;
305 #setup a job dependancy to delay provisioning
306 my $placeholder = new FS::queue ( {
307 'job' => 'FS::ClientAPI::Signup::__placeholder',
308 'status' => 'locked',
310 my $error = $placeholder->insert;
311 return { 'error' => $error } if $error;
314 tie my %hash, 'Tie::RefHash';
315 %hash = ( $cust_pkg => [ $svc_acct ] );
317 $error = $cust_main->insert(
320 'depend_jobnum' => $placeholder->jobnum,
323 my $perror = $placeholder->delete;
324 $error .= " (Additionally, error removing placeholder: $perror)" if $perror;
325 return { 'error' => $error };
328 if ( $conf->exists('signup_server-realtime') ) {
330 #warn "[fs_signup_server] Billing customer...\n" if $Debug;
332 my $bill_error = $cust_main->bill;
333 #warn "[fs_signup_server] error billing new customer: $bill_error"
336 $cust_main->apply_payments;
337 $cust_main->apply_credits;
339 $bill_error = $cust_main->collect('realtime' => 1);
340 #warn "[fs_signup_server] error collecting from new customer: $bill_error"
343 if ( $cust_main->balance > 0 ) {
345 #this makes sense. credit is "un-doing" the invoice
346 $cust_main->credit( $cust_main->balance, 'signup server decline' );
347 $cust_main->apply_credits;
349 #should check list for errors...
350 #$cust_main->suspend;
351 local $FS::svc_Common::noexport_hack = 1;
352 $cust_main->cancel('quiet'=>1);
354 my $perror = $placeholder->depended_delete;
355 warn "error removing provisioning jobs after decline: $perror" if $perror;
357 $perror = $placeholder->delete;
358 warn "error removing placeholder after decline: $perror" if $perror;
361 return { 'error' => '_decline' };
367 $error = $reg_code->delete;
368 return { 'error' => $error } if $error;
371 $error = $placeholder->delete;
372 return { 'error' => $error } if $error;
374 return { error => '' };