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 $agentnum = $conf->config('signup_server-default_agentnum');
92 if ( exists $packet->{'session_id'} ) {
93 my $cache = new FS::ClientAPI_SessionCache( {
94 'namespace' => 'FS::ClientAPI::Agent',
96 $session = $cache->get($packet->{'session_id'});
98 $agentnum = $session->{'agentnum'};
100 return { 'error' => "Can't resume session" }; #better error message
104 $signup_info->{'part_pkg'} = [];
106 if ( $packet->{'reg_code'} ) {
107 $signup_info->{'part_pkg'} =
108 [ map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
109 grep { $_->svcpart('svc_acct') }
111 qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
112 'agentnum' => $agentnum, } )
116 $signup_info->{'error'} = 'Unknown registration code'
117 unless @{ $signup_info->{'part_pkg'} };
119 } elsif ( $packet->{'promo_code'} ) {
121 $signup_info->{'part_pkg'} =
122 [ map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
123 grep { $_->svcpart('svc_acct') }
124 qsearch( 'part_pkg', { 'promo_code' => {
126 value=>$packet->{'promo_code'}
128 'disabled' => '', } )
131 $signup_info->{'error'} = 'Unknown promotional code'
132 unless @{ $signup_info->{'part_pkg'} };
135 if ( $agentnum && ! @{ $signup_info->{'part_pkg'} } ) {
136 $signup_info->{'part_pkg'} = $signup_info->{'agentnum2part_pkg'}{$agentnum};
139 # delete $signup_info->{'part_pkg'};
143 my $agent_signup_info = { %$signup_info };
144 delete $agent_signup_info->{agentnum2part_pkg};
145 $agent_signup_info->{'agent'} = $session->{'agent'};
156 my $conf = new FS::Conf;
158 #things that aren't necessary in base class, but are for signup server
159 #return "Passwords don't match"
160 # if $hashref->{'_password'} ne $hashref->{'_password2'}
161 return { 'error' => gettext('empty_password') }
162 unless length($packet->{'_password'});
163 # a bit inefficient for large numbers of pops
164 return { 'error' => gettext('no_access_number_selected') }
165 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
168 if ( exists $packet->{'session_id'} ) {
169 my $cache = new FS::ClientAPI_SessionCache( {
170 'namespace' => 'FS::ClientAPI::Agent',
172 my $session = $cache->get($packet->{'session_id'});
174 $agentnum = $session->{'agentnum'};
176 return { 'error' => "Can't resume session" }; #better error message
179 $agentnum = $packet->{agentnum}
180 || $conf->config('signup_server-default_agentnum');
183 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
184 # common that are still here and library them.
185 my $cust_main = new FS::cust_main ( {
187 'agentnum' => $agentnum,
188 'refnum' => $packet->{refnum}
189 || $conf->config('signup_server-default_refnum'),
191 map { $_ => $packet->{$_} } qw(
193 last first ss company address1 address2
194 city county state zip country
197 ship_last ship_first ship_ss ship_company ship_address1 ship_address2
198 ship_city ship_county ship_state ship_zip ship_country
199 ship_daytime ship_night ship_fax
202 payinfo paycvv paydate payname
203 paystart_month paystart_year payissue
206 referral_custnum comments
211 return { 'error' => "Illegal payment type" }
212 unless grep { $_ eq $packet->{'payby'} }
213 $conf->config('signup_server-payby');
215 $cust_main->payinfo($cust_main->daytime)
216 if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
218 my @invoicing_list = split( /\s*\,\s*/, $packet->{'invoicing_list'} );
220 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
222 return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
225 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
226 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
227 my $svcpart = $part_pkg->svcpart('svc_acct');
230 if ( $packet->{'reg_code'} ) {
231 $reg_code = qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
232 'agentnum' => $agentnum, } )
233 or return { 'error' => 'Unknown registration code' };
236 my $cust_pkg = new FS::cust_pkg ( {
237 #later#'custnum' => $custnum,
238 'pkgpart' => $packet->{'pkgpart'},
239 'promo_code' => $packet->{'promo_code'},
240 'reg_code' => $packet->{'reg_code'},
242 #my $error = $cust_pkg->check;
243 #return { 'error' => $error } if $error;
245 my $svc_acct = new FS::svc_acct ( {
246 'svcpart' => $svcpart,
247 map { $_ => $packet->{$_} }
248 qw( username _password sec_phrase popnum ),
253 while ( exists($packet->{"snarf_machine$snarfnum"})
254 && length($packet->{"snarf_machine$snarfnum"}) ) {
255 my $acct_snarf = new FS::acct_snarf ( {
256 'machine' => $packet->{"snarf_machine$snarfnum"},
257 'protocol' => $packet->{"snarf_protocol$snarfnum"},
258 'username' => $packet->{"snarf_username$snarfnum"},
259 '_password' => $packet->{"snarf_password$snarfnum"},
262 push @acct_snarf, $acct_snarf;
264 $svc_acct->child_objects( \@acct_snarf );
266 my $y = $svc_acct->setdefault; # arguably should be in new method
267 return { 'error' => $y } if $y && !ref($y);
269 #$error = $svc_acct->check;
270 #return { 'error' => $error } if $error;
272 #setup a job dependancy to delay provisioning
273 my $placeholder = new FS::queue ( {
274 'job' => 'FS::ClientAPI::Signup::__placeholder',
275 'status' => 'locked',
277 my $error = $placeholder->insert;
278 return { 'error' => $error } if $error;
281 tie my %hash, 'Tie::RefHash';
282 %hash = ( $cust_pkg => [ $svc_acct ] );
284 $error = $cust_main->insert(
287 'depend_jobnum' => $placeholder->jobnum,
290 my $perror = $placeholder->delete;
291 $error .= " (Additionally, error removing placeholder: $perror)" if $perror;
292 return { 'error' => $error };
295 if ( $conf->exists('signup_server-realtime') ) {
297 #warn "[fs_signup_server] Billing customer...\n" if $Debug;
299 my $bill_error = $cust_main->bill;
300 #warn "[fs_signup_server] error billing new customer: $bill_error"
303 $cust_main->apply_payments;
304 $cust_main->apply_credits;
306 $bill_error = $cust_main->collect('realtime' => 1);
307 #warn "[fs_signup_server] error collecting from new customer: $bill_error"
310 if ( $cust_main->balance > 0 ) {
312 #this makes sense. credit is "un-doing" the invoice
313 $cust_main->credit( $cust_main->balance, 'signup server decline' );
314 $cust_main->apply_credits;
316 #should check list for errors...
317 #$cust_main->suspend;
318 local $FS::svc_Common::noexport_hack = 1;
319 $cust_main->cancel('quiet'=>1);
321 my $perror = $placeholder->depended_delete;
322 warn "error removing provisioning jobs after decline: $perror" if $perror;
324 $perror = $placeholder->delete;
325 warn "error removing placeholder after decline: $perror" if $perror;
328 return { 'error' => '_decline' };
334 $error = $reg_code->delete;
335 return { 'error' => $error } if $error;
338 $error = $placeholder->delete;
339 return { 'error' => $error } if $error;
341 return { error => '' };