8 use Storable qw(nstore_fd fd_retrieve);
10 use Net::SSH qw(sshopen2);
11 use FS::UID qw(adminsuidsetup);
13 use FS::Record qw( qsearch qsearchs );
14 use FS::cust_main_county;
16 use FS::msgcat qw(gettext);
18 use vars qw( $opt $Debug );
22 my $user = shift or die &usage;
23 &adminsuidsetup( $user );
25 my $conf = new FS::Conf;
27 #my @payby = qw(CARD PREPAY);
28 my @payby = $conf->config('signup_server-payby');
29 my $smtpmachine = $conf->config('smtpmachine');
31 my $machine = shift or die &usage;
33 my $agentnum = shift or die &usage;
34 my $agent = qsearchs( 'agent', { 'agentnum' => $agentnum } ) or die &usage;
35 my $pkgpart_href = $agent->pkgpart_hashref;
37 my $refnum = shift or die &usage;
39 #causing trouble for some folks
40 #$SIG{CHLD} = sub { wait() };
42 my($fs_signupd)="/usr/local/sbin/fs_signupd";
45 my($reader,$writer)=(new IO::Handle, new IO::Handle);
46 #seems to be broken - calling ->flush explicitly# $writer->autoflush(1);
47 warn "[fs_signup_server] Connecting to $machine...\n" if $Debug;
48 sshopen2($machine,$reader,$writer,$fs_signupd);
50 my @pops = qsearch('svc_acct_pop',{} );
53 #'_protocol' => 'signup',
58 [ map { $_->hashref } qsearch('cust_main_county', {}) ],
63 grep { $_->svcpart('svc_acct') && $pkgpart_href->{ $_->pkgpart } }
64 qsearch( 'part_pkg', { 'disabled' => '' } )
67 'svc_acct_pop' => [ map { $_->hashref } @pops ],
69 'security_phrase' => $conf->exists('security_phrase'),
71 'payby' => [ $conf->config('signup_server-payby') ],
73 'msgcat' => { map { $_=>gettext($_) } qw(
74 passwords_dont_match invalid_card unknown_card_type not_a
79 warn "[fs_signup_server] Sending init data...\n" if $Debug;
80 nstore_fd($init_data, $writer) or die "can't send init data: $!";
83 warn "[fs_signup_server] Entering main loop...\n" if $Debug;
85 warn "[fs_signup_server] Reading (waiting for) signup data...\n" if $Debug;
86 my $signup_data = fd_retrieve($reader);
90 map { " $_ => ". $signup_data->{$_}. "\n" } keys %$signup_data );
93 warn "[fs_signup_server] Processing signup...\n" if $Debug;
97 #things that aren't necessary in base class, but are for signup server
98 #return "Passwords don't match"
99 # if $hashref->{'_password'} ne $hashref->{'_password2'}
100 $error ||= gettext('empty_password') unless $signup_data->{'_password'};
101 $error ||= gettext('no_access_number_selected')
102 unless $signup_data->{'popnum'} || !scalar(@pops);
104 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
105 # common that are still here and library them.
106 my $cust_main = new FS::cust_main ( {
108 'agentnum' => $agentnum,
111 map { $_ => $signup_data->{$_} } qw(
112 last first ss company address1 address2 city county state zip country
113 daytime night fax payby payinfo paydate payname referral_custnum
118 $error ||= "Illegal payment type"
119 unless grep { $_ eq $signup_data->{'payby'} } @payby;
121 my @invoicing_list = split( /\s*\,\s*/, $signup_data->{'invoicing_list'} );
124 qsearchs( 'part_pkg', { 'pkgpart' => $signup_data->{'pkgpart'} } )
125 or $error ||= "WARNING: unknown pkgpart ". $signup_data->{pkgpart};
126 my $svcpart = $part_pkg->svcpart unless $error;
128 # this should wind up in FS::cust_pkg!
129 my $agent = qsearchs( 'agent', { 'agentnum' => $agentnum } );
130 #my $pkgpart_href = $agent->pkgpart_hashref;
131 $error ||= "WARNING: agent $agentnum can't purchase pkgpart ".
132 $signup_data->{pkgpart}
133 unless $pkgpart_href->{ $signup_data->{pkgpart} };
135 my $cust_pkg = new FS::cust_pkg ( {
136 #later#'custnum' => $custnum,
137 'pkgpart' => $signup_data->{'pkgpart'},
139 $error ||= $cust_pkg->check;
141 my $svc_acct = new FS::svc_acct ( {
142 'svcpart' => $svcpart,
143 map { $_ => $signup_data->{$_} }
144 qw( username _password sec_phrase popnum ),
147 my $y = $svc_acct->setdefault; # arguably should be in new method
148 $error ||= $y unless ref($y);
150 $error ||= $svc_acct->check;
153 tie my %hash, 'Tie::RefHash';
154 %hash = ( $cust_pkg => [ $svc_acct ] );
155 $error ||= $cust_main->insert( \%hash, \@invoicing_list ); #msgcat
157 warn "[fs_signup_server] Sending results...\n" if $Debug;
158 print $writer $error, "\n";
160 if ( $conf->config('signup_server-realtime') ) {
162 my $bill_error = $cust_main->bill;
163 warn "[fs_signup_server] error billing new customer: $bill_error"
166 $cust_main->apply_payments;
167 $cust_main->apply_credits;
169 $bill_error = $cust_main->collect;
170 warn "[fs_signup_server] error collecting from new customer: $bill_error"
173 if ( $cust_main->balance ) {
174 #should check list for errors...
179 if ( $error && $conf->config('signup_server-email') ) {
180 warn "[fs_signup_server] Sending email...\n" if $Debug;
182 #false laziness w/FS::cust_bill::send & FS::cust_pay::delete
185 my $from = $conf->config('invoice_from'); #??? as good as any
186 $ENV{MAILADDRESS} = $from;
187 my $header = new Mail::Header ( [
189 "To: ". $conf->config('signup_server-email'),
192 "Date: ". time2str("%a, %d %b %Y %X %z", time),
193 "Subject: FREESIDE NOTIFICATION: Signup Server",
196 "This is an automatic message from your Freeside installation\n",
197 "informing you a customer has signed up via the signup server:\n",
199 'custnum: '. $cust_main->custnum. "\n",
200 'Name : '. $cust_main->last. ", ". $cust_main->first. "\n",
201 'Agent : '. $cust_main->agent->agent. "\n",
204 if ( $cust_main->balance ) {
206 "This customer has an outstanding balance and has been suspended.\n";
208 my $message = new Mail::Internet ( 'Header' => $header, 'Body' => $body );
210 $message->smtpsend( Host => $smtpmachine )
211 or $message->smtpsend( Host => $smtpmachine, Debug => 1 )
212 or warn "[fs_signup_server] can't send email to ".
213 $conf->config('signup_server-email').
214 " via server $smtpmachine with SMTP: $!";
221 warn "connection to $machine lost! waiting 60 seconds...\n";
223 warn "reconnecting...\n";
227 die "Usage:\n\n fs_signup_server user machine agentnum refnum\n";