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;
18 use FS::ClientAPI; #hmm
19 FS::ClientAPI->register_handlers(
20 'Signup/signup_info' => \&signup_info,
21 'Signup/new_customer' => \&new_customer,
27 my $conf = new FS::Conf;
29 use vars qw($signup_info); #cache for performance;
33 [ map { $_->hashref } qsearch('cust_main_county', {}) ],
38 qsearch('agent', dbdef->table('agent')->column('disabled')
39 ? { 'disabled' => '' }
47 qsearch('part_referral',
48 dbdef->table('part_referral')->column('disabled')
49 ? { 'disabled' => '' }
54 'agentnum2part_pkg' =>
57 my $href = $_->pkgpart_hashref;
60 map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
61 grep { $_->svcpart('svc_acct') && $href->{ $_->pkgpart } }
62 qsearch( 'part_pkg', { 'disabled' => '' } )
64 } qsearch('agent', dbdef->table('agent')->column('disabled')
65 ? { 'disabled' => '' }
70 'svc_acct_pop' => [ map { $_->hashref } qsearch('svc_acct_pop',{} ) ],
72 'security_phrase' => $conf->exists('security_phrase'),
74 'payby' => [ $conf->config('signup_server-payby') ],
76 'cvv_enabled' => defined dbdef->table('cust_main')->column('paycvv'),
78 'ship_enabled' => defined dbdef->table('cust_main')->column('ship_last'),
80 'msgcat' => { map { $_=>gettext($_) } qw(
81 passwords_dont_match invalid_card unknown_card_type not_a empty_password
84 'statedefault' => $conf->config('statedefault') || 'CA',
86 'countrydefault' => $conf->config('countrydefault') || 'US',
88 'refnum' => $conf->config('signup_server-default_refnum'),
92 my $agentnum = $conf->config('signup_server-default_agentnum');
95 if ( exists $packet->{'session_id'} ) {
96 my $cache = new Cache::SharedMemoryCache( {
97 'namespace' => 'FS::ClientAPI::Agent',
99 $session = $cache->get($packet->{'session_id'});
101 $agentnum = $session->{'agentnum'};
103 return { 'error' => "Can't resume session" }; #better error message
107 $signup_info->{'part_pkg'} = [];
108 if ( $packet->{'promo_code'} ) {
109 $signup_info->{'part_pkg'} =
110 [ map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
111 grep { $_->svcpart('svc_acct') }
112 qsearch( 'part_pkg', { 'promo_code' => {
114 value=>$packet->{'promo_code'}
116 'disabled' => '', } )
119 $signup_info->{'error'} = 'Unknown promotional code'
120 unless @{ $signup_info->{'part_pkg'} };
123 if ( $agentnum && ! @{ $signup_info->{'part_pkg'} } ) {
124 $signup_info->{'part_pkg'} = $signup_info->{'agentnum2part_pkg'}{$agentnum};
127 # delete $signup_info->{'part_pkg'};
131 my $agent_signup_info = { %$signup_info };
132 delete $agent_signup_info->{agentnum2part_pkg};
133 $agent_signup_info->{'agent'} = $session->{'agent'};
144 my $conf = new FS::Conf;
146 #things that aren't necessary in base class, but are for signup server
147 #return "Passwords don't match"
148 # if $hashref->{'_password'} ne $hashref->{'_password2'}
149 return { 'error' => gettext('empty_password') }
150 unless length($packet->{'_password'});
151 # a bit inefficient for large numbers of pops
152 return { 'error' => gettext('no_access_number_selected') }
153 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
156 if ( exists $packet->{'session_id'} ) {
157 my $cache = new Cache::SharedMemoryCache( {
158 'namespace' => 'FS::ClientAPI::Agent',
160 my $session = $cache->get($packet->{'session_id'});
162 $agentnum = $session->{'agentnum'};
164 return { 'error' => "Can't resume session" }; #better error message
167 $agentnum = $packet->{agentnum}
168 || $conf->config('signup_server-default_agentnum');
171 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
172 # common that are still here and library them.
173 my $cust_main = new FS::cust_main ( {
175 'agentnum' => $agentnum,
176 'refnum' => $packet->{refnum}
177 || $conf->config('signup_server-default_refnum'),
179 map { $_ => $packet->{$_} } qw(
181 last first ss company address1 address2
182 city county state zip country
185 ship_last ship_first ship_ss ship_company ship_address1 ship_address2
186 ship_city ship_county ship_state ship_zip ship_country
187 ship_daytime ship_night ship_fax
189 payby payinfo paycvv paydate payname referral_custnum comments
194 return { 'error' => "Illegal payment type" }
195 unless grep { $_ eq $packet->{'payby'} }
196 $conf->config('signup_server-payby');
198 $cust_main->payinfo($cust_main->daytime)
199 if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
201 my @invoicing_list = split( /\s*\,\s*/, $packet->{'invoicing_list'} );
203 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
205 return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
208 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
209 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
210 my $svcpart = $part_pkg->svcpart('svc_acct');
212 my $cust_pkg = new FS::cust_pkg ( {
213 #later#'custnum' => $custnum,
214 'pkgpart' => $packet->{'pkgpart'},
215 'promo_code' => $packet->{'promo_code'},
217 #my $error = $cust_pkg->check;
218 #return { 'error' => $error } if $error;
220 my $svc_acct = new FS::svc_acct ( {
221 'svcpart' => $svcpart,
222 map { $_ => $packet->{$_} }
223 qw( username _password sec_phrase popnum ),
228 while ( exists($packet->{"snarf_machine$snarfnum"})
229 && length($packet->{"snarf_machine$snarfnum"}) ) {
230 my $acct_snarf = new FS::acct_snarf ( {
231 'machine' => $packet->{"snarf_machine$snarfnum"},
232 'protocol' => $packet->{"snarf_protocol$snarfnum"},
233 'username' => $packet->{"snarf_username$snarfnum"},
234 '_password' => $packet->{"snarf_password$snarfnum"},
237 push @acct_snarf, $acct_snarf;
239 $svc_acct->child_objects( \@acct_snarf );
241 my $y = $svc_acct->setdefault; # arguably should be in new method
242 return { 'error' => $y } if $y && !ref($y);
244 #$error = $svc_acct->check;
245 #return { 'error' => $error } if $error;
247 #setup a job dependancy to delay provisioning
248 my $placeholder = new FS::queue ( {
249 'job' => 'FS::ClientAPI::Signup::__placeholder',
250 'status' => 'locked',
252 my $error = $placeholder->insert;
253 return { 'error' => $error } if $error;
256 tie my %hash, 'Tie::RefHash';
257 %hash = ( $cust_pkg => [ $svc_acct ] );
259 $error = $cust_main->insert(
262 'depend_jobnum' => $placeholder->jobnum,
265 my $perror = $placeholder->delete;
266 $error .= " (Additionally, error removing placeholder: $perror)" if $perror;
267 return { 'error' => $error };
270 if ( $conf->exists('signup_server-realtime') ) {
272 #warn "[fs_signup_server] Billing customer...\n" if $Debug;
274 my $bill_error = $cust_main->bill;
275 #warn "[fs_signup_server] error billing new customer: $bill_error"
278 $cust_main->apply_payments;
279 $cust_main->apply_credits;
281 $bill_error = $cust_main->collect;
282 #warn "[fs_signup_server] error collecting from new customer: $bill_error"
285 if ( $cust_main->balance > 0 ) {
287 #this makes sense. credit is "un-doing" the invoice
288 $cust_main->credit( $cust_main->balance, 'signup server decline' );
289 $cust_main->apply_credits;
291 #should check list for errors...
292 #$cust_main->suspend;
293 local $FS::svc_Common::noexport_hack = 1;
294 $cust_main->cancel('quiet'=>1);
296 my $perror = $placeholder->depended_delete;
297 warn "error removing provisioning jobs after decline: $perror" if $perror;
299 $perror = $placeholder->delete;
300 warn "error removing placeholder after decline: $perror" if $perror;
303 return { 'error' => '_decline' };
308 $error = $placeholder->delete;
309 return { 'error' => $error } if $error;
311 return { error => '' };