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
82 'statedefault' => $conf->config('statedefault') || 'CA',
84 'countrydefault' => $conf->config('countrydefault') || 'US',
86 'refnum' => $conf->config('signup_server-default_refnum'),
91 $conf->config('signup_server-default_agentnum')
92 && !exists $signup_info->{'part_pkg'} #cache for performance
94 my $agentnum = $conf->config('signup_server-default_agentnum');
95 my $agent = qsearchs( 'agent', { 'agentnum' => $agentnum } )
96 or die "fatal: signup_server-default_agentnum $agentnum not found\n";
97 my $pkgpart_href = $agent->pkgpart_hashref;
99 $signup_info->{'part_pkg'} = [
101 map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
102 grep { $_->svcpart('svc_acct') && $pkgpart_href->{ $_->pkgpart } }
103 qsearch( 'part_pkg', { 'disabled' => '' } )
114 my $conf = new FS::Conf;
116 #things that aren't necessary in base class, but are for signup server
117 #return "Passwords don't match"
118 # if $hashref->{'_password'} ne $hashref->{'_password2'}
119 return { 'error' => gettext('empty_password') }
120 unless $packet->{'_password'};
121 # a bit inefficient for large numbers of pops
122 return { 'error' => gettext('no_access_number_selected') }
123 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
125 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
126 # common that are still here and library them.
127 my $cust_main = new FS::cust_main ( {
129 'agentnum' => $packet->{agentnum}
130 || $conf->config('signup_server-default_agentnum'),
131 'refnum' => $packet->{refnum}
132 || $conf->config('signup_server-default_refnum'),
134 map { $_ => $packet->{$_} } qw(
135 last first ss company address1 address2 city county state zip country
136 daytime night fax payby payinfo paycvv paydate payname referral_custnum
142 return { 'error' => "Illegal payment type" }
143 unless grep { $_ eq $packet->{'payby'} }
144 $conf->config('signup_server-payby');
146 $cust_main->payinfo($cust_main->daytime)
147 if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
149 my @invoicing_list = split( /\s*\,\s*/, $packet->{'invoicing_list'} );
151 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
153 return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
156 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
157 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
158 my $svcpart = $part_pkg->svcpart('svc_acct');
160 my $cust_pkg = new FS::cust_pkg ( {
161 #later#'custnum' => $custnum,
162 'pkgpart' => $packet->{'pkgpart'},
164 my $error = $cust_pkg->check;
165 return { 'error' => $error } if $error;
167 my $svc_acct = new FS::svc_acct ( {
168 'svcpart' => $svcpart,
169 map { $_ => $packet->{$_} }
170 qw( username _password sec_phrase popnum ),
175 while ( exists($packet->{"snarf_machine$snarfnum"})
176 && length($packet->{"snarf_machine$snarfnum"}) ) {
177 my $acct_snarf = new FS::acct_snarf ( {
178 'machine' => $packet->{"snarf_machine$snarfnum"},
179 'protocol' => $packet->{"snarf_protocol$snarfnum"},
180 'username' => $packet->{"snarf_username$snarfnum"},
181 '_password' => $packet->{"snarf_password$snarfnum"},
184 push @acct_snarf, $acct_snarf;
186 $svc_acct->child_objects( \@acct_snarf );
188 my $y = $svc_acct->setdefault; # arguably should be in new method
189 return { 'error' => $y } if $y && !ref($y);
191 $error = $svc_acct->check;
192 return { 'error' => $error } if $error;
194 #setup a job dependancy to delay provisioning
195 my $placeholder = new FS::queue ( {
196 'job' => 'FS::ClientAPI::Signup::__placeholder',
197 'status' => 'locked',
199 $error = $placeholder->insert;
200 return { 'error' => $error } if $error;
203 tie my %hash, 'Tie::RefHash';
204 %hash = ( $cust_pkg => [ $svc_acct ] );
206 $error = $cust_main->insert(
209 'depend_jobnum' => $placeholder->jobnum,
212 my $perror = $placeholder->delete;
213 $error .= " (Additionally, error removing placeholder: $perror)" if $perror;
214 return { 'error' => $error };
217 if ( $conf->exists('signup_server-realtime') ) {
219 #warn "[fs_signup_server] Billing customer...\n" if $Debug;
221 my $bill_error = $cust_main->bill;
222 #warn "[fs_signup_server] error billing new customer: $bill_error"
225 $cust_main->apply_payments;
226 $cust_main->apply_credits;
228 $bill_error = $cust_main->collect;
229 #warn "[fs_signup_server] error collecting from new customer: $bill_error"
232 if ( $cust_main->balance > 0 ) {
234 #this makes sense. credit is "un-doing" the invoice
235 $cust_main->credit( $cust_main->balance, 'signup server decline' );
236 $cust_main->apply_credits;
238 #should check list for errors...
239 #$cust_main->suspend;
240 local $FS::svc_Common::noexport_hack = 1;
241 $cust_main->cancel('quiet'=>1);
243 my $perror = $placeholder->depended_delete;
244 warn "error removing provisioning jobs after decline: $perror" if $perror;
246 $perror = $placeholder->delete;
247 warn "error removing placeholder after decline: $perror" if $perror;
250 return { 'error' => '_decline' };
255 $error = $placeholder->delete;
256 return { 'error' => $error } if $error;
258 return { error => '' };