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
124 $signup_info->{'part_pkg'} = [];
126 if ( $packet->{'reg_code'} ) {
127 $signup_info->{'part_pkg'} =
128 [ map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
129 grep { $_->svcpart('svc_acct') }
131 qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
132 'agentnum' => $agentnum, } )
136 $signup_info->{'error'} = 'Unknown registration code'
137 unless @{ $signup_info->{'part_pkg'} };
139 } elsif ( $packet->{'promo_code'} ) {
141 $signup_info->{'part_pkg'} =
142 [ map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
143 grep { $_->svcpart('svc_acct') }
144 qsearch( 'part_pkg', { 'promo_code' => {
146 value=>$packet->{'promo_code'}
148 'disabled' => '', } )
151 $signup_info->{'error'} = 'Unknown promotional code'
152 unless @{ $signup_info->{'part_pkg'} };
155 if ( $agentnum && ! @{ $signup_info->{'part_pkg'} } ) {
156 $signup_info->{'part_pkg'} = $signup_info->{'agentnum2part_pkg'}{$agentnum};
159 # delete $signup_info->{'part_pkg'};
163 my $agent_signup_info = { %$signup_info };
164 delete $agent_signup_info->{agentnum2part_pkg};
165 $agent_signup_info->{'agent'} = $session->{'agent'};
176 my $conf = new FS::Conf;
178 #things that aren't necessary in base class, but are for signup server
179 #return "Passwords don't match"
180 # if $hashref->{'_password'} ne $hashref->{'_password2'}
181 return { 'error' => gettext('empty_password') }
182 unless length($packet->{'_password'});
183 # a bit inefficient for large numbers of pops
184 return { 'error' => gettext('no_access_number_selected') }
185 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
188 if ( exists $packet->{'session_id'} ) {
189 my $cache = new FS::ClientAPI_SessionCache( {
190 'namespace' => 'FS::ClientAPI::Agent',
192 my $session = $cache->get($packet->{'session_id'});
194 $agentnum = $session->{'agentnum'};
196 return { 'error' => "Can't resume session" }; #better error message
199 $agentnum = $packet->{agentnum}
200 || $conf->config('signup_server-default_agentnum');
203 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
204 # common that are still here and library them.
205 my $cust_main = new FS::cust_main ( {
207 'agentnum' => $agentnum,
208 'refnum' => $packet->{refnum}
209 || $conf->config('signup_server-default_refnum'),
211 map { $_ => $packet->{$_} } qw(
213 last first ss company address1 address2
214 city county state zip country
217 ship_last ship_first ship_ss ship_company ship_address1 ship_address2
218 ship_city ship_county ship_state ship_zip ship_country
219 ship_daytime ship_night ship_fax
222 payinfo paycvv paydate payname
223 paystart_month paystart_year payissue
226 referral_custnum comments
231 return { 'error' => "Illegal payment type" }
232 unless grep { $_ eq $packet->{'payby'} }
233 $conf->config('signup_server-payby');
235 $cust_main->payinfo($cust_main->daytime)
236 if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
238 my @invoicing_list = split( /\s*\,\s*/, $packet->{'invoicing_list'} );
240 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
242 return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
245 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
246 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
247 my $svcpart = $part_pkg->svcpart('svc_acct');
250 if ( $packet->{'reg_code'} ) {
251 $reg_code = qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
252 'agentnum' => $agentnum, } )
253 or return { 'error' => 'Unknown registration code' };
256 my $cust_pkg = new FS::cust_pkg ( {
257 #later#'custnum' => $custnum,
258 'pkgpart' => $packet->{'pkgpart'},
259 'promo_code' => $packet->{'promo_code'},
260 'reg_code' => $packet->{'reg_code'},
262 #my $error = $cust_pkg->check;
263 #return { 'error' => $error } if $error;
265 my $svc_acct = new FS::svc_acct ( {
266 'svcpart' => $svcpart,
267 map { $_ => $packet->{$_} }
268 qw( username _password sec_phrase popnum ),
273 while ( exists($packet->{"snarf_machine$snarfnum"})
274 && length($packet->{"snarf_machine$snarfnum"}) ) {
275 my $acct_snarf = new FS::acct_snarf ( {
276 'machine' => $packet->{"snarf_machine$snarfnum"},
277 'protocol' => $packet->{"snarf_protocol$snarfnum"},
278 'username' => $packet->{"snarf_username$snarfnum"},
279 '_password' => $packet->{"snarf_password$snarfnum"},
282 push @acct_snarf, $acct_snarf;
284 $svc_acct->child_objects( \@acct_snarf );
286 my $y = $svc_acct->setdefault; # arguably should be in new method
287 return { 'error' => $y } if $y && !ref($y);
289 #$error = $svc_acct->check;
290 #return { 'error' => $error } if $error;
292 #setup a job dependancy to delay provisioning
293 my $placeholder = new FS::queue ( {
294 'job' => 'FS::ClientAPI::Signup::__placeholder',
295 'status' => 'locked',
297 my $error = $placeholder->insert;
298 return { 'error' => $error } if $error;
301 tie my %hash, 'Tie::RefHash';
302 %hash = ( $cust_pkg => [ $svc_acct ] );
304 $error = $cust_main->insert(
307 'depend_jobnum' => $placeholder->jobnum,
310 my $perror = $placeholder->delete;
311 $error .= " (Additionally, error removing placeholder: $perror)" if $perror;
312 return { 'error' => $error };
315 if ( $conf->exists('signup_server-realtime') ) {
317 #warn "[fs_signup_server] Billing customer...\n" if $Debug;
319 my $bill_error = $cust_main->bill;
320 #warn "[fs_signup_server] error billing new customer: $bill_error"
323 $cust_main->apply_payments;
324 $cust_main->apply_credits;
326 $bill_error = $cust_main->collect('realtime' => 1);
327 #warn "[fs_signup_server] error collecting from new customer: $bill_error"
330 if ( $cust_main->balance > 0 ) {
332 #this makes sense. credit is "un-doing" the invoice
333 $cust_main->credit( $cust_main->balance, 'signup server decline' );
334 $cust_main->apply_credits;
336 #should check list for errors...
337 #$cust_main->suspend;
338 local $FS::svc_Common::noexport_hack = 1;
339 $cust_main->cancel('quiet'=>1);
341 my $perror = $placeholder->depended_delete;
342 warn "error removing provisioning jobs after decline: $perror" if $perror;
344 $perror = $placeholder->delete;
345 warn "error removing placeholder after decline: $perror" if $perror;
348 return { 'error' => '_decline' };
354 $error = $reg_code->delete;
355 return { 'error' => $error } if $error;
358 $error = $placeholder->delete;
359 return { 'error' => $error } if $error;
361 return { error => '' };