1 package FS::ClientAPI::Signup;
6 use FS::Record qw(qsearch qsearchs dbdef);
7 use FS::Msgcat qw(gettext);
9 use FS::cust_main_county;
22 my $conf = new FS::Conf;
24 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 'security_phrase' => $conf->exists('security_phrase'),
69 'payby' => [ $conf->config('signup_server-payby') ],
71 'cvv_enabled' => defined dbdef->table('cust_main')->column('paycvv'),
73 'ship_enabled' => defined dbdef->table('cust_main')->column('ship_last'),
75 'msgcat' => { map { $_=>gettext($_) } qw(
76 passwords_dont_match invalid_card unknown_card_type not_a empty_password
79 'statedefault' => $conf->config('statedefault') || 'CA',
81 'countrydefault' => $conf->config('countrydefault') || 'US',
83 'refnum' => $conf->config('signup_server-default_refnum'),
87 my $agentnum = $conf->config('signup_server-default_agentnum');
90 if ( exists $packet->{'session_id'} ) {
91 my $cache = new Cache::SharedMemoryCache( {
92 'namespace' => 'FS::ClientAPI::Agent',
94 $session = $cache->get($packet->{'session_id'});
96 $agentnum = $session->{'agentnum'};
98 return { 'error' => "Can't resume session" }; #better error message
102 $signup_info->{'part_pkg'} = [];
104 if ( $packet->{'reg_code'} ) {
105 $signup_info->{'part_pkg'} =
106 [ map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
107 grep { $_->svcpart('svc_acct') }
109 qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
110 'agentnum' => $agentnum, } )
114 $signup_info->{'error'} = 'Unknown registration code'
115 unless @{ $signup_info->{'part_pkg'} };
117 } elsif ( $packet->{'promo_code'} ) {
119 $signup_info->{'part_pkg'} =
120 [ map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
121 grep { $_->svcpart('svc_acct') }
122 qsearch( 'part_pkg', { 'promo_code' => {
124 value=>$packet->{'promo_code'}
126 'disabled' => '', } )
129 $signup_info->{'error'} = 'Unknown promotional code'
130 unless @{ $signup_info->{'part_pkg'} };
133 if ( $agentnum && ! @{ $signup_info->{'part_pkg'} } ) {
134 $signup_info->{'part_pkg'} = $signup_info->{'agentnum2part_pkg'}{$agentnum};
137 # delete $signup_info->{'part_pkg'};
141 my $agent_signup_info = { %$signup_info };
142 delete $agent_signup_info->{agentnum2part_pkg};
143 $agent_signup_info->{'agent'} = $session->{'agent'};
154 my $conf = new FS::Conf;
156 #things that aren't necessary in base class, but are for signup server
157 #return "Passwords don't match"
158 # if $hashref->{'_password'} ne $hashref->{'_password2'}
159 return { 'error' => gettext('empty_password') }
160 unless length($packet->{'_password'});
161 # a bit inefficient for large numbers of pops
162 return { 'error' => gettext('no_access_number_selected') }
163 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
166 if ( exists $packet->{'session_id'} ) {
167 my $cache = new Cache::SharedMemoryCache( {
168 'namespace' => 'FS::ClientAPI::Agent',
170 my $session = $cache->get($packet->{'session_id'});
172 $agentnum = $session->{'agentnum'};
174 return { 'error' => "Can't resume session" }; #better error message
177 $agentnum = $packet->{agentnum}
178 || $conf->config('signup_server-default_agentnum');
181 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
182 # common that are still here and library them.
183 my $cust_main = new FS::cust_main ( {
185 'agentnum' => $agentnum,
186 'refnum' => $packet->{refnum}
187 || $conf->config('signup_server-default_refnum'),
189 map { $_ => $packet->{$_} } qw(
191 last first ss company address1 address2
192 city county state zip country
195 ship_last ship_first ship_ss ship_company ship_address1 ship_address2
196 ship_city ship_county ship_state ship_zip ship_country
197 ship_daytime ship_night ship_fax
199 payby payinfo paycvv paydate payname referral_custnum comments
204 return { 'error' => "Illegal payment type" }
205 unless grep { $_ eq $packet->{'payby'} }
206 $conf->config('signup_server-payby');
208 $cust_main->payinfo($cust_main->daytime)
209 if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
211 my @invoicing_list = split( /\s*\,\s*/, $packet->{'invoicing_list'} );
213 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
215 return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
218 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
219 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
220 my $svcpart = $part_pkg->svcpart('svc_acct');
223 if ( $packet->{'reg_code'} ) {
224 $reg_code = qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
225 'agentnum' => $agentnum, } )
226 or return { 'error' => 'Unknown registration code' };
229 my $cust_pkg = new FS::cust_pkg ( {
230 #later#'custnum' => $custnum,
231 'pkgpart' => $packet->{'pkgpart'},
232 'promo_code' => $packet->{'promo_code'},
233 'reg_code' => $packet->{'reg_code'},
235 #my $error = $cust_pkg->check;
236 #return { 'error' => $error } if $error;
238 my $svc_acct = new FS::svc_acct ( {
239 'svcpart' => $svcpart,
240 map { $_ => $packet->{$_} }
241 qw( username _password sec_phrase popnum ),
246 while ( exists($packet->{"snarf_machine$snarfnum"})
247 && length($packet->{"snarf_machine$snarfnum"}) ) {
248 my $acct_snarf = new FS::acct_snarf ( {
249 'machine' => $packet->{"snarf_machine$snarfnum"},
250 'protocol' => $packet->{"snarf_protocol$snarfnum"},
251 'username' => $packet->{"snarf_username$snarfnum"},
252 '_password' => $packet->{"snarf_password$snarfnum"},
255 push @acct_snarf, $acct_snarf;
257 $svc_acct->child_objects( \@acct_snarf );
259 my $y = $svc_acct->setdefault; # arguably should be in new method
260 return { 'error' => $y } if $y && !ref($y);
262 #$error = $svc_acct->check;
263 #return { 'error' => $error } if $error;
265 #setup a job dependancy to delay provisioning
266 my $placeholder = new FS::queue ( {
267 'job' => 'FS::ClientAPI::Signup::__placeholder',
268 'status' => 'locked',
270 my $error = $placeholder->insert;
271 return { 'error' => $error } if $error;
274 tie my %hash, 'Tie::RefHash';
275 %hash = ( $cust_pkg => [ $svc_acct ] );
277 $error = $cust_main->insert(
280 'depend_jobnum' => $placeholder->jobnum,
283 my $perror = $placeholder->delete;
284 $error .= " (Additionally, error removing placeholder: $perror)" if $perror;
285 return { 'error' => $error };
288 if ( $conf->exists('signup_server-realtime') ) {
290 #warn "[fs_signup_server] Billing customer...\n" if $Debug;
292 my $bill_error = $cust_main->bill;
293 #warn "[fs_signup_server] error billing new customer: $bill_error"
296 $cust_main->apply_payments;
297 $cust_main->apply_credits;
299 $bill_error = $cust_main->collect;
300 #warn "[fs_signup_server] error collecting from new customer: $bill_error"
303 if ( $cust_main->balance > 0 ) {
305 #this makes sense. credit is "un-doing" the invoice
306 $cust_main->credit( $cust_main->balance, 'signup server decline' );
307 $cust_main->apply_credits;
309 #should check list for errors...
310 #$cust_main->suspend;
311 local $FS::svc_Common::noexport_hack = 1;
312 $cust_main->cancel('quiet'=>1);
314 my $perror = $placeholder->depended_delete;
315 warn "error removing provisioning jobs after decline: $perror" if $perror;
317 $perror = $placeholder->delete;
318 warn "error removing placeholder after decline: $perror" if $perror;
321 return { 'error' => '_decline' };
327 $error = $reg_code->delete;
328 return { 'error' => $error } if $error;
331 $error = $placeholder->delete;
332 return { 'error' => $error } if $error;
334 return { error => '' };