1 package FS::ClientAPI::Signup;
6 use FS::Record qw(qsearch qsearchs);
8 use FS::cust_main_county;
13 use FS::Msgcat qw(gettext);
15 use FS::ClientAPI; #hmm
16 FS::ClientAPI->register_handlers(
17 'Signup/signup_info' => \&signup_info,
18 'Signup/new_customer' => \&new_customer,
24 my $conf = new FS::Conf;
26 use vars qw($signup_info); #cache for performance;
30 [ map { $_->hashref } qsearch('cust_main_county', {}) ],
32 'agentnum2part_pkg' =>
35 my $href = $_->pkgpart_hashref;
38 map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
39 grep { $_->svcpart('svc_acct') && $href->{ $_->pkgpart } }
40 qsearch( 'part_pkg', { 'disabled' => '' } )
42 } qsearch('agent', {} )
45 'svc_acct_pop' => [ map { $_->hashref } qsearch('svc_acct_pop',{} ) ],
47 'security_phrase' => $conf->exists('security_phrase'),
49 'payby' => [ $conf->config('signup_server-payby') ],
51 'msgcat' => { map { $_=>gettext($_) } qw(
52 passwords_dont_match invalid_card unknown_card_type not_a
55 'statedefault' => $conf->config('statedefault') || 'CA',
57 'countrydefault' => $conf->config('countrydefault') || 'US',
62 $conf->config('signup_server-default_agentnum')
63 && !exists $signup_info->{'part_pkg'} #cache for performance
65 my $agentnum = $conf->config('signup_server-default_agentnum');
66 my $agent = qsearchs( 'agent', { 'agentnum' => $agentnum } )
67 or die "fatal: signup_server-default_agentnum $agentnum not found\n";
68 my $pkgpart_href = $agent->pkgpart_hashref;
70 $signup_info->{'part_pkg'} = [
72 map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
73 grep { $_->svcpart('svc_acct') && $pkgpart_href->{ $_->pkgpart } }
74 qsearch( 'part_pkg', { 'disabled' => '' } )
85 my $conf = new FS::Conf;
88 #things that aren't necessary in base class, but are for signup server
89 #return "Passwords don't match"
90 # if $hashref->{'_password'} ne $hashref->{'_password2'}
91 $error ||= gettext('empty_password') unless $packet->{'_password'};
92 # a bit inefficient for large numbers of pops
93 $error ||= gettext('no_access_number_selected')
94 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
96 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
97 # common that are still here and library them.
98 my $cust_main = new FS::cust_main ( {
100 'agentnum' => $packet->{agentnum}
101 || $conf->config('signup_server-default_agentnum'),
102 'refnum' => $packet->{refnum}
103 || $conf->config('signup_server-default_refnum'),
105 map { $_ => $packet->{$_} } qw(
106 last first ss company address1 address2 city county state zip country
107 daytime night fax payby payinfo paydate payname referral_custnum comments
112 $error ||= "Illegal payment type"
113 unless grep { $_ eq $packet->{'payby'} }
114 $conf->config('signup_server-payby');
116 $cust_main->payinfo($cust_main->daytime)
117 if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
119 my @invoicing_list = split( /\s*\,\s*/, $packet->{'invoicing_list'} );
121 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
123 $error ||= 'Please select a package' unless $pkgpart; #msgcat
126 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
127 or $error ||= "WARNING: unknown pkgpart: $pkgpart";
128 my $svcpart = $part_pkg->svcpart('svc_acct') unless $error;
130 my $cust_pkg = new FS::cust_pkg ( {
131 #later#'custnum' => $custnum,
132 'pkgpart' => $packet->{'pkgpart'},
134 $error ||= $cust_pkg->check;
136 my $svc_acct = new FS::svc_acct ( {
137 'svcpart' => $svcpart,
138 map { $_ => $packet->{$_} }
139 qw( username _password sec_phrase popnum ),
142 my $y = $svc_acct->setdefault; # arguably should be in new method
143 $error ||= $y unless ref($y);
145 $error ||= $svc_acct->check;
148 tie my %hash, 'Tie::RefHash';
149 %hash = ( $cust_pkg => [ $svc_acct ] );
151 $error ||= $cust_main->insert( \%hash, \@invoicing_list, 'noexport' => 1 );
153 if ( ! $error && $conf->exists('signup_server-realtime') ) {
155 #warn "[fs_signup_server] Billing customer...\n" if $Debug;
157 my $bill_error = $cust_main->bill;
158 #warn "[fs_signup_server] error billing new customer: $bill_error"
161 $cust_main->apply_payments;
162 $cust_main->apply_credits;
164 $bill_error = $cust_main->collect;
165 #warn "[fs_signup_server] error collecting from new customer: $bill_error"
168 if ( $cust_main->balance > 0 ) {
170 #this makes sense. credit is "un-doing" the invoice
171 $cust_main->credit( $cust_main->balance, 'signup server decline' );
172 $cust_main->apply_credits;
174 #should check list for errors...
175 #$cust_main->suspend;
176 local $FS::svc_Common::noexport_hack = 1;
177 $cust_main->cancel('quiet'=>1);
183 $cust_main->reexport unless $error;
185 return { error => $error };