1 package FS::ClientAPI::Signup;
6 use FS::Record qw(qsearch qsearchs dbdef);
7 use FS::Msgcat qw(gettext);
8 use FS::ClientAPI_SessionCache;
10 use FS::cust_main_county;
23 my $conf = new FS::Conf;
25 use vars qw($signup_info); #cache for performance;
28 [ map { $_->hashref } qsearch('cust_main_county', {}) ],
33 qsearch('agent', dbdef->table('agent')->column('disabled')
34 ? { 'disabled' => '' }
42 qsearch('part_referral',
43 dbdef->table('part_referral')->column('disabled')
44 ? { 'disabled' => '' }
49 'agentnum2part_pkg' =>
52 my $href = $_->pkgpart_hashref;
55 map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
56 grep { $_->svcpart('svc_acct') && $href->{ $_->pkgpart } }
57 qsearch( 'part_pkg', { 'disabled' => '' } )
59 } qsearch('agent', dbdef->table('agent')->column('disabled')
60 ? { 'disabled' => '' }
65 'svc_acct_pop' => [ map { $_->hashref } qsearch('svc_acct_pop',{} ) ],
67 'emailinvoiceonly' => $conf->exists('emailinvoiceonly'),
69 'security_phrase' => $conf->exists('security_phrase'),
71 'payby' => [ $conf->config('signup_server-payby') ],
73 'cvv_enabled' => defined dbdef->table('cust_main')->column('paycvv'),
75 'ship_enabled' => defined dbdef->table('cust_main')->column('ship_last'),
77 'msgcat' => { map { $_=>gettext($_) } qw(
78 passwords_dont_match invalid_card unknown_card_type not_a empty_password illegal_or_empty_text
81 'statedefault' => $conf->config('statedefault') || 'CA',
83 'countrydefault' => $conf->config('countrydefault') || 'US',
85 'refnum' => $conf->config('signup_server-default_refnum'),
89 my @addl = qw( signup_server-classnum2 signup_server-classnum3 );
91 if ( grep { $conf->exists($_) } @addl ) {
93 $signup_info->{optional_packages} = [];
95 foreach my $addl ( @addl ) {
96 my $classnum = $conf->config($addl) or next;
97 my @pkgs = map { $_->hashref }
98 qsearch( 'part_pkg', { classnum => $classnum } );
99 push @{$signup_info->{optional_packages}}, \@pkgs;
104 my $agentnum = $conf->config('signup_server-default_agentnum');
107 if ( exists $packet->{'session_id'} ) {
108 my $cache = new FS::ClientAPI_SessionCache( {
109 'namespace' => 'FS::ClientAPI::Agent',
111 $session = $cache->get($packet->{'session_id'});
113 $agentnum = $session->{'agentnum'};
115 return { 'error' => "Can't resume session" }; #better error message
119 $signup_info->{'part_pkg'} = [];
121 if ( $packet->{'reg_code'} ) {
122 $signup_info->{'part_pkg'} =
123 [ map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
124 grep { $_->svcpart('svc_acct') }
126 qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
127 'agentnum' => $agentnum, } )
131 $signup_info->{'error'} = 'Unknown registration code'
132 unless @{ $signup_info->{'part_pkg'} };
134 } elsif ( $packet->{'promo_code'} ) {
136 $signup_info->{'part_pkg'} =
137 [ map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
138 grep { $_->svcpart('svc_acct') }
139 qsearch( 'part_pkg', { 'promo_code' => {
141 value=>$packet->{'promo_code'}
143 'disabled' => '', } )
146 $signup_info->{'error'} = 'Unknown promotional code'
147 unless @{ $signup_info->{'part_pkg'} };
150 if ( $agentnum && ! @{ $signup_info->{'part_pkg'} } ) {
151 $signup_info->{'part_pkg'} = $signup_info->{'agentnum2part_pkg'}{$agentnum};
154 # delete $signup_info->{'part_pkg'};
158 my $agent_signup_info = { %$signup_info };
159 delete $agent_signup_info->{agentnum2part_pkg};
160 $agent_signup_info->{'agent'} = $session->{'agent'};
171 my $conf = new FS::Conf;
173 #things that aren't necessary in base class, but are for signup server
174 #return "Passwords don't match"
175 # if $hashref->{'_password'} ne $hashref->{'_password2'}
176 return { 'error' => gettext('empty_password') }
177 unless length($packet->{'_password'});
178 # a bit inefficient for large numbers of pops
179 return { 'error' => gettext('no_access_number_selected') }
180 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
183 if ( exists $packet->{'session_id'} ) {
184 my $cache = new FS::ClientAPI_SessionCache( {
185 'namespace' => 'FS::ClientAPI::Agent',
187 my $session = $cache->get($packet->{'session_id'});
189 $agentnum = $session->{'agentnum'};
191 return { 'error' => "Can't resume session" }; #better error message
194 $agentnum = $packet->{agentnum}
195 || $conf->config('signup_server-default_agentnum');
198 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
199 # common that are still here and library them.
200 my $cust_main = new FS::cust_main ( {
202 'agentnum' => $agentnum,
203 'refnum' => $packet->{refnum}
204 || $conf->config('signup_server-default_refnum'),
206 map { $_ => $packet->{$_} } qw(
208 last first ss company address1 address2
209 city county state zip country
212 ship_last ship_first ship_ss ship_company ship_address1 ship_address2
213 ship_city ship_county ship_state ship_zip ship_country
214 ship_daytime ship_night ship_fax
217 payinfo paycvv paydate payname
218 paystart_month paystart_year payissue
221 referral_custnum comments
226 return { 'error' => "Illegal payment type" }
227 unless grep { $_ eq $packet->{'payby'} }
228 $conf->config('signup_server-payby');
230 $cust_main->payinfo($cust_main->daytime)
231 if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
233 my @invoicing_list = split( /\s*\,\s*/, $packet->{'invoicing_list'} );
235 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
237 return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
240 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
241 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
242 my $svcpart = $part_pkg->svcpart('svc_acct');
245 if ( $packet->{'reg_code'} ) {
246 $reg_code = qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
247 'agentnum' => $agentnum, } )
248 or return { 'error' => 'Unknown registration code' };
251 my $cust_pkg = new FS::cust_pkg ( {
252 #later#'custnum' => $custnum,
253 'pkgpart' => $packet->{'pkgpart'},
254 'promo_code' => $packet->{'promo_code'},
255 'reg_code' => $packet->{'reg_code'},
257 #my $error = $cust_pkg->check;
258 #return { 'error' => $error } if $error;
260 my $svc_acct = new FS::svc_acct ( {
261 'svcpart' => $svcpart,
262 map { $_ => $packet->{$_} }
263 qw( username _password sec_phrase popnum ),
268 while ( exists($packet->{"snarf_machine$snarfnum"})
269 && length($packet->{"snarf_machine$snarfnum"}) ) {
270 my $acct_snarf = new FS::acct_snarf ( {
271 'machine' => $packet->{"snarf_machine$snarfnum"},
272 'protocol' => $packet->{"snarf_protocol$snarfnum"},
273 'username' => $packet->{"snarf_username$snarfnum"},
274 '_password' => $packet->{"snarf_password$snarfnum"},
277 push @acct_snarf, $acct_snarf;
279 $svc_acct->child_objects( \@acct_snarf );
281 my $y = $svc_acct->setdefault; # arguably should be in new method
282 return { 'error' => $y } if $y && !ref($y);
284 #$error = $svc_acct->check;
285 #return { 'error' => $error } if $error;
287 #setup a job dependancy to delay provisioning
288 my $placeholder = new FS::queue ( {
289 'job' => 'FS::ClientAPI::Signup::__placeholder',
290 'status' => 'locked',
292 my $error = $placeholder->insert;
293 return { 'error' => $error } if $error;
296 tie my %hash, 'Tie::RefHash';
297 %hash = ( $cust_pkg => [ $svc_acct ] );
299 $error = $cust_main->insert(
302 'depend_jobnum' => $placeholder->jobnum,
305 my $perror = $placeholder->delete;
306 $error .= " (Additionally, error removing placeholder: $perror)" if $perror;
307 return { 'error' => $error };
310 if ( $conf->exists('signup_server-realtime') ) {
312 #warn "[fs_signup_server] Billing customer...\n" if $Debug;
314 my $bill_error = $cust_main->bill;
315 #warn "[fs_signup_server] error billing new customer: $bill_error"
318 $cust_main->apply_payments;
319 $cust_main->apply_credits;
321 $bill_error = $cust_main->collect('realtime' => 1);
322 #warn "[fs_signup_server] error collecting from new customer: $bill_error"
325 if ( $cust_main->balance > 0 ) {
327 #this makes sense. credit is "un-doing" the invoice
328 $cust_main->credit( $cust_main->balance, 'signup server decline' );
329 $cust_main->apply_credits;
331 #should check list for errors...
332 #$cust_main->suspend;
333 local $FS::svc_Common::noexport_hack = 1;
334 $cust_main->cancel('quiet'=>1);
336 my $perror = $placeholder->depended_delete;
337 warn "error removing provisioning jobs after decline: $perror" if $perror;
339 $perror = $placeholder->delete;
340 warn "error removing placeholder after decline: $perror" if $perror;
343 return { 'error' => '_decline' };
349 $error = $reg_code->delete;
350 return { 'error' => $error } if $error;
353 $error = $placeholder->delete;
354 return { 'error' => $error } if $error;
356 return { error => '' };