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'),
87 'default_pkgpart' => $conf->config('signup_server-default_pkgpart'),
91 my @addl = qw( signup_server-classnum2 signup_server-classnum3 );
93 if ( grep { $conf->exists($_) } @addl ) {
95 $signup_info->{optional_packages} = [];
97 foreach my $addl ( @addl ) {
98 my $classnum = $conf->config($addl) or next;
99 my @pkgs = map { $_->hashref }
100 qsearch( 'part_pkg', { classnum => $classnum } );
101 push @{$signup_info->{optional_packages}}, \@pkgs;
106 my $agentnum = $conf->config('signup_server-default_agentnum');
109 if ( exists $packet->{'session_id'} ) {
110 my $cache = new FS::ClientAPI_SessionCache( {
111 'namespace' => 'FS::ClientAPI::Agent',
113 $session = $cache->get($packet->{'session_id'});
115 $agentnum = $session->{'agentnum'};
117 return { 'error' => "Can't resume session" }; #better error message
121 $signup_info->{'part_pkg'} = [];
123 if ( $packet->{'reg_code'} ) {
124 $signup_info->{'part_pkg'} =
125 [ map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
126 grep { $_->svcpart('svc_acct') }
128 qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
129 'agentnum' => $agentnum, } )
133 $signup_info->{'error'} = 'Unknown registration code'
134 unless @{ $signup_info->{'part_pkg'} };
136 } elsif ( $packet->{'promo_code'} ) {
138 $signup_info->{'part_pkg'} =
139 [ map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
140 grep { $_->svcpart('svc_acct') }
141 qsearch( 'part_pkg', { 'promo_code' => {
143 value=>$packet->{'promo_code'}
145 'disabled' => '', } )
148 $signup_info->{'error'} = 'Unknown promotional code'
149 unless @{ $signup_info->{'part_pkg'} };
152 if ( $agentnum && ! @{ $signup_info->{'part_pkg'} } ) {
153 $signup_info->{'part_pkg'} = $signup_info->{'agentnum2part_pkg'}{$agentnum};
156 # delete $signup_info->{'part_pkg'};
160 my $agent_signup_info = { %$signup_info };
161 delete $agent_signup_info->{agentnum2part_pkg};
162 $agent_signup_info->{'agent'} = $session->{'agent'};
173 my $conf = new FS::Conf;
175 #things that aren't necessary in base class, but are for signup server
176 #return "Passwords don't match"
177 # if $hashref->{'_password'} ne $hashref->{'_password2'}
178 return { 'error' => gettext('empty_password') }
179 unless length($packet->{'_password'});
180 # a bit inefficient for large numbers of pops
181 return { 'error' => gettext('no_access_number_selected') }
182 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
185 if ( exists $packet->{'session_id'} ) {
186 my $cache = new FS::ClientAPI_SessionCache( {
187 'namespace' => 'FS::ClientAPI::Agent',
189 my $session = $cache->get($packet->{'session_id'});
191 $agentnum = $session->{'agentnum'};
193 return { 'error' => "Can't resume session" }; #better error message
196 $agentnum = $packet->{agentnum}
197 || $conf->config('signup_server-default_agentnum');
200 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
201 # common that are still here and library them.
202 my $cust_main = new FS::cust_main ( {
204 'agentnum' => $agentnum,
205 'refnum' => $packet->{refnum}
206 || $conf->config('signup_server-default_refnum'),
208 map { $_ => $packet->{$_} } qw(
210 last first ss company address1 address2
211 city county state zip country
214 ship_last ship_first ship_ss ship_company ship_address1 ship_address2
215 ship_city ship_county ship_state ship_zip ship_country
216 ship_daytime ship_night ship_fax
219 payinfo paycvv paydate payname
220 paystart_month paystart_year payissue
223 referral_custnum comments
228 return { 'error' => "Illegal payment type" }
229 unless grep { $_ eq $packet->{'payby'} }
230 $conf->config('signup_server-payby');
232 $cust_main->payinfo($cust_main->daytime)
233 if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
235 my @invoicing_list = split( /\s*\,\s*/, $packet->{'invoicing_list'} );
237 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
239 return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
242 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
243 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
244 my $svcpart = $part_pkg->svcpart('svc_acct');
247 if ( $packet->{'reg_code'} ) {
248 $reg_code = qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
249 'agentnum' => $agentnum, } )
250 or return { 'error' => 'Unknown registration code' };
253 my $cust_pkg = new FS::cust_pkg ( {
254 #later#'custnum' => $custnum,
255 'pkgpart' => $packet->{'pkgpart'},
256 'promo_code' => $packet->{'promo_code'},
257 'reg_code' => $packet->{'reg_code'},
259 #my $error = $cust_pkg->check;
260 #return { 'error' => $error } if $error;
262 my $svc_acct = new FS::svc_acct ( {
263 'svcpart' => $svcpart,
264 map { $_ => $packet->{$_} }
265 qw( username _password sec_phrase popnum ),
270 while ( exists($packet->{"snarf_machine$snarfnum"})
271 && length($packet->{"snarf_machine$snarfnum"}) ) {
272 my $acct_snarf = new FS::acct_snarf ( {
273 'machine' => $packet->{"snarf_machine$snarfnum"},
274 'protocol' => $packet->{"snarf_protocol$snarfnum"},
275 'username' => $packet->{"snarf_username$snarfnum"},
276 '_password' => $packet->{"snarf_password$snarfnum"},
279 push @acct_snarf, $acct_snarf;
281 $svc_acct->child_objects( \@acct_snarf );
283 my $y = $svc_acct->setdefault; # arguably should be in new method
284 return { 'error' => $y } if $y && !ref($y);
286 #$error = $svc_acct->check;
287 #return { 'error' => $error } if $error;
289 #setup a job dependancy to delay provisioning
290 my $placeholder = new FS::queue ( {
291 'job' => 'FS::ClientAPI::Signup::__placeholder',
292 'status' => 'locked',
294 my $error = $placeholder->insert;
295 return { 'error' => $error } if $error;
298 tie my %hash, 'Tie::RefHash';
299 %hash = ( $cust_pkg => [ $svc_acct ] );
301 $error = $cust_main->insert(
304 'depend_jobnum' => $placeholder->jobnum,
307 my $perror = $placeholder->delete;
308 $error .= " (Additionally, error removing placeholder: $perror)" if $perror;
309 return { 'error' => $error };
312 if ( $conf->exists('signup_server-realtime') ) {
314 #warn "[fs_signup_server] Billing customer...\n" if $Debug;
316 my $bill_error = $cust_main->bill;
317 #warn "[fs_signup_server] error billing new customer: $bill_error"
320 $cust_main->apply_payments;
321 $cust_main->apply_credits;
323 $bill_error = $cust_main->collect('realtime' => 1);
324 #warn "[fs_signup_server] error collecting from new customer: $bill_error"
327 if ( $cust_main->balance > 0 ) {
329 #this makes sense. credit is "un-doing" the invoice
330 $cust_main->credit( $cust_main->balance, 'signup server decline' );
331 $cust_main->apply_credits;
333 #should check list for errors...
334 #$cust_main->suspend;
335 local $FS::svc_Common::noexport_hack = 1;
336 $cust_main->cancel('quiet'=>1);
338 my $perror = $placeholder->depended_delete;
339 warn "error removing provisioning jobs after decline: $perror" if $perror;
341 $perror = $placeholder->delete;
342 warn "error removing placeholder after decline: $perror" if $perror;
345 return { 'error' => '_decline' };
351 $error = $reg_code->delete;
352 return { 'error' => $error } if $error;
355 $error = $placeholder->delete;
356 return { 'error' => $error } if $error;
358 return { error => '' };