1 package FS::ClientAPI::Signup;
6 use FS::Record qw(qsearch qsearchs dbdef);
8 use FS::cust_main_county;
15 use FS::Msgcat qw(gettext);
17 use FS::ClientAPI; #hmm
18 FS::ClientAPI->register_handlers(
19 'Signup/signup_info' => \&signup_info,
20 'Signup/new_customer' => \&new_customer,
26 my $conf = new FS::Conf;
28 use vars qw($signup_info); #cache for performance;
32 [ map { $_->hashref } qsearch('cust_main_county', {}) ],
37 qsearch('agent', dbdef->table('agent')->column('disabled')
38 ? { 'disabled' => '' }
43 'agentnum2part_pkg' =>
46 my $href = $_->pkgpart_hashref;
49 map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
50 grep { $_->svcpart('svc_acct') && $href->{ $_->pkgpart } }
51 qsearch( 'part_pkg', { 'disabled' => '' } )
53 } qsearch('agent', dbdef->table('agent')->column('disabled')
54 ? { 'disabled' => '' }
59 'svc_acct_pop' => [ map { $_->hashref } qsearch('svc_acct_pop',{} ) ],
61 'security_phrase' => $conf->exists('security_phrase'),
63 'payby' => [ $conf->config('signup_server-payby') ],
65 'cvv_enabled' => defined dbdef->table('cust_main')->column('paycvv'),
67 'msgcat' => { map { $_=>gettext($_) } qw(
68 passwords_dont_match invalid_card unknown_card_type not_a
71 'statedefault' => $conf->config('statedefault') || 'CA',
73 'countrydefault' => $conf->config('countrydefault') || 'US',
78 $conf->config('signup_server-default_agentnum')
79 && !exists $signup_info->{'part_pkg'} #cache for performance
81 my $agentnum = $conf->config('signup_server-default_agentnum');
82 my $agent = qsearchs( 'agent', { 'agentnum' => $agentnum } )
83 or die "fatal: signup_server-default_agentnum $agentnum not found\n";
84 my $pkgpart_href = $agent->pkgpart_hashref;
86 $signup_info->{'part_pkg'} = [
88 map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
89 grep { $_->svcpart('svc_acct') && $pkgpart_href->{ $_->pkgpart } }
90 qsearch( 'part_pkg', { 'disabled' => '' } )
101 my $conf = new FS::Conf;
103 #things that aren't necessary in base class, but are for signup server
104 #return "Passwords don't match"
105 # if $hashref->{'_password'} ne $hashref->{'_password2'}
106 return { 'error' => gettext('empty_password') }
107 unless $packet->{'_password'};
108 # a bit inefficient for large numbers of pops
109 return { 'error' => gettext('no_access_number_selected') }
110 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
112 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
113 # common that are still here and library them.
114 my $cust_main = new FS::cust_main ( {
116 'agentnum' => $packet->{agentnum}
117 || $conf->config('signup_server-default_agentnum'),
118 'refnum' => $packet->{refnum}
119 || $conf->config('signup_server-default_refnum'),
121 map { $_ => $packet->{$_} } qw(
122 last first ss company address1 address2 city county state zip country
123 daytime night fax payby payinfo paycvv paydate payname referral_custnum
129 return { 'error' => "Illegal payment type" }
130 unless grep { $_ eq $packet->{'payby'} }
131 $conf->config('signup_server-payby');
133 $cust_main->payinfo($cust_main->daytime)
134 if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
136 my @invoicing_list = split( /\s*\,\s*/, $packet->{'invoicing_list'} );
138 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
140 return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
143 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
144 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
145 my $svcpart = $part_pkg->svcpart('svc_acct');
147 my $cust_pkg = new FS::cust_pkg ( {
148 #later#'custnum' => $custnum,
149 'pkgpart' => $packet->{'pkgpart'},
151 my $error = $cust_pkg->check;
152 return { 'error' => $error } if $error;
154 my $svc_acct = new FS::svc_acct ( {
155 'svcpart' => $svcpart,
156 map { $_ => $packet->{$_} }
157 qw( username _password sec_phrase popnum ),
162 while ( length($packet->{"snarf_machine$snarfnum"}) ) {
163 my $acct_snarf = new FS::acct_snarf ( {
164 'machine' => $packet->{"snarf_machine$snarfnum"},
165 'protocol' => $packet->{"snarf_protocol$snarfnum"},
166 'username' => $packet->{"snarf_username$snarfnum"},
167 '_password' => $packet->{"snarf_password$snarfnum"},
170 push @acct_snarf, $acct_snarf;
172 $svc_acct->child_objects( \@acct_snarf );
174 my $y = $svc_acct->setdefault; # arguably should be in new method
175 return { 'error' => $y } if $y && !ref($y);
177 $error = $svc_acct->check;
178 return { 'error' => $error } if $error;
181 tie my %hash, 'Tie::RefHash';
182 %hash = ( $cust_pkg => [ $svc_acct ] );
184 $error = $cust_main->insert( \%hash, \@invoicing_list, 'noexport' => 1 );
185 return { 'error' => $error } if $error;
187 if ( $conf->exists('signup_server-realtime') ) {
189 #warn "[fs_signup_server] Billing customer...\n" if $Debug;
191 my $bill_error = $cust_main->bill;
192 #warn "[fs_signup_server] error billing new customer: $bill_error"
195 $cust_main->apply_payments;
196 $cust_main->apply_credits;
198 $bill_error = $cust_main->collect;
199 #warn "[fs_signup_server] error collecting from new customer: $bill_error"
202 if ( $cust_main->balance > 0 ) {
204 #this makes sense. credit is "un-doing" the invoice
205 $cust_main->credit( $cust_main->balance, 'signup server decline' );
206 $cust_main->apply_credits;
208 #should check list for errors...
209 #$cust_main->suspend;
210 local $FS::svc_Common::noexport_hack = 1;
211 $cust_main->cancel('quiet'=>1);
213 return { 'error' => '_decline' };
217 $cust_main->reexport;
219 return { error => '' };