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;
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 'security_phrase' => $conf->exists('security_phrase'),
70 'payby' => [ $conf->config('signup_server-payby') ],
72 'cvv_enabled' => defined dbdef->table('cust_main')->column('paycvv'),
74 'ship_enabled' => defined dbdef->table('cust_main')->column('ship_last'),
76 'msgcat' => { map { $_=>gettext($_) } qw(
77 passwords_dont_match invalid_card unknown_card_type not_a empty_password
80 'statedefault' => $conf->config('statedefault') || 'CA',
82 'countrydefault' => $conf->config('countrydefault') || 'US',
84 'refnum' => $conf->config('signup_server-default_refnum'),
88 my $agentnum = $conf->config('signup_server-default_agentnum');
91 if ( exists $packet->{'session_id'} ) {
92 my $cache = new FS::ClientAPI_SessionCache( {
93 'namespace' => 'FS::ClientAPI::Agent',
95 $session = $cache->get($packet->{'session_id'});
97 $agentnum = $session->{'agentnum'};
99 return { 'error' => "Can't resume session" }; #better error message
103 $signup_info->{'part_pkg'} = [];
105 if ( $packet->{'reg_code'} ) {
106 $signup_info->{'part_pkg'} =
107 [ map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
108 grep { $_->svcpart('svc_acct') }
110 qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
111 'agentnum' => $agentnum, } )
115 $signup_info->{'error'} = 'Unknown registration code'
116 unless @{ $signup_info->{'part_pkg'} };
118 } elsif ( $packet->{'promo_code'} ) {
120 $signup_info->{'part_pkg'} =
121 [ map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
122 grep { $_->svcpart('svc_acct') }
123 qsearch( 'part_pkg', { 'promo_code' => {
125 value=>$packet->{'promo_code'}
127 'disabled' => '', } )
130 $signup_info->{'error'} = 'Unknown promotional code'
131 unless @{ $signup_info->{'part_pkg'} };
134 if ( $agentnum && ! @{ $signup_info->{'part_pkg'} } ) {
135 $signup_info->{'part_pkg'} = $signup_info->{'agentnum2part_pkg'}{$agentnum};
138 # delete $signup_info->{'part_pkg'};
142 my $agent_signup_info = { %$signup_info };
143 delete $agent_signup_info->{agentnum2part_pkg};
144 $agent_signup_info->{'agent'} = $session->{'agent'};
155 my $conf = new FS::Conf;
157 #things that aren't necessary in base class, but are for signup server
158 #return "Passwords don't match"
159 # if $hashref->{'_password'} ne $hashref->{'_password2'}
160 return { 'error' => gettext('empty_password') }
161 unless length($packet->{'_password'});
162 # a bit inefficient for large numbers of pops
163 return { 'error' => gettext('no_access_number_selected') }
164 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
167 if ( exists $packet->{'session_id'} ) {
168 my $cache = new FS::ClientAPI_SessionCache( {
169 'namespace' => 'FS::ClientAPI::Agent',
171 my $session = $cache->get($packet->{'session_id'});
173 $agentnum = $session->{'agentnum'};
175 return { 'error' => "Can't resume session" }; #better error message
178 $agentnum = $packet->{agentnum}
179 || $conf->config('signup_server-default_agentnum');
182 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
183 # common that are still here and library them.
184 my $cust_main = new FS::cust_main ( {
186 'agentnum' => $agentnum,
187 'refnum' => $packet->{refnum}
188 || $conf->config('signup_server-default_refnum'),
190 map { $_ => $packet->{$_} } qw(
192 last first ss company address1 address2
193 city county state zip country
196 ship_last ship_first ship_ss ship_company ship_address1 ship_address2
197 ship_city ship_county ship_state ship_zip ship_country
198 ship_daytime ship_night ship_fax
200 payby payinfo paycvv paydate payname referral_custnum comments
205 return { 'error' => "Illegal payment type" }
206 unless grep { $_ eq $packet->{'payby'} }
207 $conf->config('signup_server-payby');
209 $cust_main->payinfo($cust_main->daytime)
210 if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
212 my @invoicing_list = split( /\s*\,\s*/, $packet->{'invoicing_list'} );
214 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
216 return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
219 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
220 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
221 my $svcpart = $part_pkg->svcpart('svc_acct');
224 if ( $packet->{'reg_code'} ) {
225 $reg_code = qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
226 'agentnum' => $agentnum, } )
227 or return { 'error' => 'Unknown registration code' };
230 my $cust_pkg = new FS::cust_pkg ( {
231 #later#'custnum' => $custnum,
232 'pkgpart' => $packet->{'pkgpart'},
233 'promo_code' => $packet->{'promo_code'},
234 'reg_code' => $packet->{'reg_code'},
236 #my $error = $cust_pkg->check;
237 #return { 'error' => $error } if $error;
239 my $svc_acct = new FS::svc_acct ( {
240 'svcpart' => $svcpart,
241 map { $_ => $packet->{$_} }
242 qw( username _password sec_phrase popnum ),
247 while ( exists($packet->{"snarf_machine$snarfnum"})
248 && length($packet->{"snarf_machine$snarfnum"}) ) {
249 my $acct_snarf = new FS::acct_snarf ( {
250 'machine' => $packet->{"snarf_machine$snarfnum"},
251 'protocol' => $packet->{"snarf_protocol$snarfnum"},
252 'username' => $packet->{"snarf_username$snarfnum"},
253 '_password' => $packet->{"snarf_password$snarfnum"},
256 push @acct_snarf, $acct_snarf;
258 $svc_acct->child_objects( \@acct_snarf );
260 my $y = $svc_acct->setdefault; # arguably should be in new method
261 return { 'error' => $y } if $y && !ref($y);
263 #$error = $svc_acct->check;
264 #return { 'error' => $error } if $error;
266 #setup a job dependancy to delay provisioning
267 my $placeholder = new FS::queue ( {
268 'job' => 'FS::ClientAPI::Signup::__placeholder',
269 'status' => 'locked',
271 my $error = $placeholder->insert;
272 return { 'error' => $error } if $error;
275 tie my %hash, 'Tie::RefHash';
276 %hash = ( $cust_pkg => [ $svc_acct ] );
278 $error = $cust_main->insert(
281 'depend_jobnum' => $placeholder->jobnum,
284 my $perror = $placeholder->delete;
285 $error .= " (Additionally, error removing placeholder: $perror)" if $perror;
286 return { 'error' => $error };
289 if ( $conf->exists('signup_server-realtime') ) {
291 #warn "[fs_signup_server] Billing customer...\n" if $Debug;
293 my $bill_error = $cust_main->bill;
294 #warn "[fs_signup_server] error billing new customer: $bill_error"
297 $cust_main->apply_payments;
298 $cust_main->apply_credits;
300 $bill_error = $cust_main->collect;
301 #warn "[fs_signup_server] error collecting from new customer: $bill_error"
304 if ( $cust_main->balance > 0 ) {
306 #this makes sense. credit is "un-doing" the invoice
307 $cust_main->credit( $cust_main->balance, 'signup server decline' );
308 $cust_main->apply_credits;
310 #should check list for errors...
311 #$cust_main->suspend;
312 local $FS::svc_Common::noexport_hack = 1;
313 $cust_main->cancel('quiet'=>1);
315 my $perror = $placeholder->depended_delete;
316 warn "error removing provisioning jobs after decline: $perror" if $perror;
318 $perror = $placeholder->delete;
319 warn "error removing placeholder after decline: $perror" if $perror;
322 return { 'error' => '_decline' };
328 $error = $reg_code->delete;
329 return { 'error' => $error } if $error;
332 $error = $placeholder->delete;
333 return { 'error' => $error } if $error;
335 return { error => '' };