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 'msgcat' => { map { $_=>gettext($_) } qw(
79 passwords_dont_match invalid_card unknown_card_type not_a empty_password
82 'statedefault' => $conf->config('statedefault') || 'CA',
84 'countrydefault' => $conf->config('countrydefault') || 'US',
86 'refnum' => $conf->config('signup_server-default_refnum'),
90 my $agentnum = $conf->config('signup_server-default_agentnum');
93 if ( exists $packet->{'session_id'} ) {
94 my $cache = new Cache::SharedMemoryCache( {
95 'namespace' => 'FS::ClientAPI::Agent',
97 $session = $cache->get($packet->{'session_id'});
99 $agentnum = $session->{'agentnum'};
101 return { 'error' => "Can't resume session" }; #better error message
106 $signup_info->{'part_pkg'} = $signup_info->{'agentnum2part_pkg'}{$agentnum};
108 delete $signup_info->{'part_pkg'};
112 my $agent_signup_info = { %$signup_info };
113 delete $agent_signup_info->{agentnum2part_pkg};
114 $agent_signup_info->{'agent'} = $session->{'agent'};
125 my $conf = new FS::Conf;
127 #things that aren't necessary in base class, but are for signup server
128 #return "Passwords don't match"
129 # if $hashref->{'_password'} ne $hashref->{'_password2'}
130 return { 'error' => gettext('empty_password') }
131 unless length($packet->{'_password'});
132 # a bit inefficient for large numbers of pops
133 return { 'error' => gettext('no_access_number_selected') }
134 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
137 if ( exists $packet->{'session_id'} ) {
138 my $cache = new Cache::SharedMemoryCache( {
139 'namespace' => 'FS::ClientAPI::Agent',
141 my $session = $cache->get($packet->{'session_id'});
143 $agentnum = $session->{'agentnum'};
145 return { 'error' => "Can't resume session" }; #better error message
148 $agentnum = $packet->{agentnum}
149 || $conf->config('signup_server-default_agentnum');
152 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
153 # common that are still here and library them.
154 my $cust_main = new FS::cust_main ( {
156 'agentnum' => $agentnum,
157 'refnum' => $packet->{refnum}
158 || $conf->config('signup_server-default_refnum'),
160 map { $_ => $packet->{$_} } qw(
161 last first ss company address1 address2 city county state zip country
162 daytime night fax payby payinfo paycvv paydate payname referral_custnum
168 return { 'error' => "Illegal payment type" }
169 unless grep { $_ eq $packet->{'payby'} }
170 $conf->config('signup_server-payby');
172 $cust_main->payinfo($cust_main->daytime)
173 if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
175 my @invoicing_list = split( /\s*\,\s*/, $packet->{'invoicing_list'} );
177 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
179 return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
182 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
183 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
184 my $svcpart = $part_pkg->svcpart('svc_acct');
186 my $cust_pkg = new FS::cust_pkg ( {
187 #later#'custnum' => $custnum,
188 'pkgpart' => $packet->{'pkgpart'},
190 my $error = $cust_pkg->check;
191 return { 'error' => $error } if $error;
193 my $svc_acct = new FS::svc_acct ( {
194 'svcpart' => $svcpart,
195 map { $_ => $packet->{$_} }
196 qw( username _password sec_phrase popnum ),
201 while ( exists($packet->{"snarf_machine$snarfnum"})
202 && length($packet->{"snarf_machine$snarfnum"}) ) {
203 my $acct_snarf = new FS::acct_snarf ( {
204 'machine' => $packet->{"snarf_machine$snarfnum"},
205 'protocol' => $packet->{"snarf_protocol$snarfnum"},
206 'username' => $packet->{"snarf_username$snarfnum"},
207 '_password' => $packet->{"snarf_password$snarfnum"},
210 push @acct_snarf, $acct_snarf;
212 $svc_acct->child_objects( \@acct_snarf );
214 my $y = $svc_acct->setdefault; # arguably should be in new method
215 return { 'error' => $y } if $y && !ref($y);
217 $error = $svc_acct->check;
218 return { 'error' => $error } if $error;
220 #setup a job dependancy to delay provisioning
221 my $placeholder = new FS::queue ( {
222 'job' => 'FS::ClientAPI::Signup::__placeholder',
223 'status' => 'locked',
225 $error = $placeholder->insert;
226 return { 'error' => $error } if $error;
229 tie my %hash, 'Tie::RefHash';
230 %hash = ( $cust_pkg => [ $svc_acct ] );
232 $error = $cust_main->insert(
235 'depend_jobnum' => $placeholder->jobnum,
238 my $perror = $placeholder->delete;
239 $error .= " (Additionally, error removing placeholder: $perror)" if $perror;
240 return { 'error' => $error };
243 if ( $conf->exists('signup_server-realtime') ) {
245 #warn "[fs_signup_server] Billing customer...\n" if $Debug;
247 my $bill_error = $cust_main->bill;
248 #warn "[fs_signup_server] error billing new customer: $bill_error"
251 $cust_main->apply_payments;
252 $cust_main->apply_credits;
254 $bill_error = $cust_main->collect;
255 #warn "[fs_signup_server] error collecting from new customer: $bill_error"
258 if ( $cust_main->balance > 0 ) {
260 #this makes sense. credit is "un-doing" the invoice
261 $cust_main->credit( $cust_main->balance, 'signup server decline' );
262 $cust_main->apply_credits;
264 #should check list for errors...
265 #$cust_main->suspend;
266 local $FS::svc_Common::noexport_hack = 1;
267 $cust_main->cancel('quiet'=>1);
269 my $perror = $placeholder->depended_delete;
270 warn "error removing provisioning jobs after decline: $perror" if $perror;
272 $perror = $placeholder->delete;
273 warn "error removing placeholder after decline: $perror" if $perror;
276 return { 'error' => '_decline' };
281 $error = $placeholder->delete;
282 return { 'error' => $error } if $error;
284 return { error => '' };