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
201 payinfo paycvv paydate payname
202 paystart_month paystart_year payissue
205 referral_custnum comments
210 return { 'error' => "Illegal payment type" }
211 unless grep { $_ eq $packet->{'payby'} }
212 $conf->config('signup_server-payby');
214 $cust_main->payinfo($cust_main->daytime)
215 if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
217 my @invoicing_list = split( /\s*\,\s*/, $packet->{'invoicing_list'} );
219 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
221 return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
224 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
225 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
226 my $svcpart = $part_pkg->svcpart('svc_acct');
229 if ( $packet->{'reg_code'} ) {
230 $reg_code = qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
231 'agentnum' => $agentnum, } )
232 or return { 'error' => 'Unknown registration code' };
235 my $cust_pkg = new FS::cust_pkg ( {
236 #later#'custnum' => $custnum,
237 'pkgpart' => $packet->{'pkgpart'},
238 'promo_code' => $packet->{'promo_code'},
239 'reg_code' => $packet->{'reg_code'},
241 #my $error = $cust_pkg->check;
242 #return { 'error' => $error } if $error;
244 my $svc_acct = new FS::svc_acct ( {
245 'svcpart' => $svcpart,
246 map { $_ => $packet->{$_} }
247 qw( username _password sec_phrase popnum ),
252 while ( exists($packet->{"snarf_machine$snarfnum"})
253 && length($packet->{"snarf_machine$snarfnum"}) ) {
254 my $acct_snarf = new FS::acct_snarf ( {
255 'machine' => $packet->{"snarf_machine$snarfnum"},
256 'protocol' => $packet->{"snarf_protocol$snarfnum"},
257 'username' => $packet->{"snarf_username$snarfnum"},
258 '_password' => $packet->{"snarf_password$snarfnum"},
261 push @acct_snarf, $acct_snarf;
263 $svc_acct->child_objects( \@acct_snarf );
265 my $y = $svc_acct->setdefault; # arguably should be in new method
266 return { 'error' => $y } if $y && !ref($y);
268 #$error = $svc_acct->check;
269 #return { 'error' => $error } if $error;
271 #setup a job dependancy to delay provisioning
272 my $placeholder = new FS::queue ( {
273 'job' => 'FS::ClientAPI::Signup::__placeholder',
274 'status' => 'locked',
276 my $error = $placeholder->insert;
277 return { 'error' => $error } if $error;
280 tie my %hash, 'Tie::RefHash';
281 %hash = ( $cust_pkg => [ $svc_acct ] );
283 $error = $cust_main->insert(
286 'depend_jobnum' => $placeholder->jobnum,
289 my $perror = $placeholder->delete;
290 $error .= " (Additionally, error removing placeholder: $perror)" if $perror;
291 return { 'error' => $error };
294 if ( $conf->exists('signup_server-realtime') ) {
296 #warn "[fs_signup_server] Billing customer...\n" if $Debug;
298 my $bill_error = $cust_main->bill;
299 #warn "[fs_signup_server] error billing new customer: $bill_error"
302 $cust_main->apply_payments;
303 $cust_main->apply_credits;
305 $bill_error = $cust_main->collect;
306 #warn "[fs_signup_server] error collecting from new customer: $bill_error"
309 if ( $cust_main->balance > 0 ) {
311 #this makes sense. credit is "un-doing" the invoice
312 $cust_main->credit( $cust_main->balance, 'signup server decline' );
313 $cust_main->apply_credits;
315 #should check list for errors...
316 #$cust_main->suspend;
317 local $FS::svc_Common::noexport_hack = 1;
318 $cust_main->cancel('quiet'=>1);
320 my $perror = $placeholder->depended_delete;
321 warn "error removing provisioning jobs after decline: $perror" if $perror;
323 $perror = $placeholder->delete;
324 warn "error removing placeholder after decline: $perror" if $perror;
327 return { 'error' => '_decline' };
333 $error = $reg_code->delete;
334 return { 'error' => $error } if $error;
337 $error = $placeholder->delete;
338 return { 'error' => $error } if $error;
340 return { error => '' };