9 use Storable qw(nstore_fd fd_retrieve);
11 use Net::SSH qw(sshopen2);
12 use FS::UID qw(adminsuidsetup);
14 use FS::Record qw( qsearch qsearchs );
15 use FS::cust_main_county;
17 use FS::Msgcat qw(gettext);
19 use vars qw( $opt $Debug );
23 my $user = shift or die &usage;
24 &adminsuidsetup( $user );
26 my $conf = new FS::Conf;
28 #my @payby = qw(CARD PREPAY);
29 my @payby = $conf->config('signup_server-payby');
30 my $smtpmachine = $conf->config('smtpmachine');
32 my $machine = shift or die &usage;
34 my $agentnum = shift or die &usage;
35 my $agent = qsearchs( 'agent', { 'agentnum' => $agentnum } ) or die &usage;
36 my $pkgpart_href = $agent->pkgpart_hashref;
38 my $refnum = shift or die &usage;
40 #causing trouble for some folks
41 #$SIG{CHLD} = sub { wait() };
43 $SIG{HUP} = \&killssh;
44 $SIG{INT} = \&killssh;
45 $SIG{QUIT} = \&killssh;
46 $SIG{TERM} = \&killssh;
47 $SIG{PIPE} = \&killssh;
48 sub killssh { kill 'TERM', $pid if $pid; exit; };
50 my($fs_signupd)="/usr/local/sbin/fs_signupd";
53 my($reader,$writer)=(new IO::Handle, new IO::Handle);
54 #seems to be broken - calling ->flush explicitly# $writer->autoflush(1);
55 warn "[fs_signup_server] Connecting to $machine...\n" if $Debug;
56 $pid = sshopen2($machine,$reader,$writer,$fs_signupd);
58 my @pops = qsearch('svc_acct_pop',{} );
61 #'_protocol' => 'signup',
66 [ map { $_->hashref } qsearch('cust_main_county', {}) ],
71 map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
72 grep { $_->svcpart('svc_acct') && $pkgpart_href->{ $_->pkgpart } }
73 qsearch( 'part_pkg', { 'disabled' => '' } )
76 'agentnum2part_pkg' =>
79 my $href = $_->pkgpart_hashref;
82 map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
83 grep { $_->svcpart('svc_acct') && $href->{ $_->pkgpart } }
84 qsearch( 'part_pkg', { 'disabled' => '' } )
86 } qsearch('agent', {} )
89 'svc_acct_pop' => [ map { $_->hashref } @pops ],
91 'security_phrase' => $conf->exists('security_phrase'),
93 'payby' => [ $conf->config('signup_server-payby') ],
95 'msgcat' => { map { $_=>gettext($_) } qw(
96 passwords_dont_match invalid_card unknown_card_type not_a
101 warn "[fs_signup_server] Sending init data...\n" if $Debug;
102 nstore_fd($init_data, $writer) or die "can't send init data: $!";
105 warn "[fs_signup_server] Entering main loop...\n" if $Debug;
107 warn "[fs_signup_server] Reading (waiting for) signup data...\n" if $Debug;
108 my $signup_data = fd_retrieve($reader);
112 map { " $_ => ". $signup_data->{$_}. "\n" } keys %$signup_data );
115 warn "[fs_signup_server] Processing signup...\n" if $Debug;
119 #things that aren't necessary in base class, but are for signup server
120 #return "Passwords don't match"
121 # if $hashref->{'_password'} ne $hashref->{'_password2'}
122 $error ||= gettext('empty_password') unless $signup_data->{'_password'};
123 $error ||= gettext('no_access_number_selected')
124 unless $signup_data->{'popnum'} || !scalar(@pops);
126 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
127 # common that are still here and library them.
128 my $cust_main = new FS::cust_main ( {
130 'agentnum' => $signup_data->{agentnum} || $agentnum,
133 map { $_ => $signup_data->{$_} } qw(
134 last first ss company address1 address2 city county state zip country
135 daytime night fax payby payinfo paydate payname referral_custnum
140 $error ||= "Illegal payment type"
141 unless grep { $_ eq $signup_data->{'payby'} } @payby;
143 $cust_main->payinfo($cust_main->daytime)
144 if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
146 my @invoicing_list = split( /\s*\,\s*/, $signup_data->{'invoicing_list'} );
148 $signup_data->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
152 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
153 or $error ||= "WARNING: unknown pkgpart: $pkgpart";
154 my $svcpart = $part_pkg->svcpart unless $error;
156 my $cust_pkg = new FS::cust_pkg ( {
157 #later#'custnum' => $custnum,
158 'pkgpart' => $signup_data->{'pkgpart'},
160 $error ||= $cust_pkg->check;
162 my $svc_acct = new FS::svc_acct ( {
163 'svcpart' => $svcpart,
164 map { $_ => $signup_data->{$_} }
165 qw( username _password sec_phrase popnum ),
168 my $y = $svc_acct->setdefault; # arguably should be in new method
169 $error ||= $y unless ref($y);
171 $error ||= $svc_acct->check;
174 tie my %hash, 'Tie::RefHash';
175 %hash = ( $cust_pkg => [ $svc_acct ] );
176 $error ||= $cust_main->insert( \%hash, \@invoicing_list ); #msgcat
178 if ( ! $error && $conf->exists('signup_server-realtime') ) {
180 warn "[fs_signup_server] Billing customer...\n" if $Debug;
182 my $bill_error = $cust_main->bill;
183 warn "[fs_signup_server] error billing new customer: $bill_error"
186 $cust_main->apply_payments;
187 $cust_main->apply_credits;
189 $bill_error = $cust_main->collect;
190 warn "[fs_signup_server] error collecting from new customer: $bill_error"
193 if ( $cust_main->balance > 0 ) {
195 #this makes sense. credit is "un-doing" the invoice
196 $cust_main->credit( $cust_main->balance, 'signup server decline' );
197 $cust_main->apply_credits;
199 #should check list for errors...
200 #$cust_main->suspend;
207 warn "[fs_signup_server] Sending results...\n" if $Debug;
208 print $writer $error, "\n";
212 if ( $conf->config('signup_server-email') ) {
213 warn "[fs_signup_server] Sending email...\n" if $Debug;
215 #false laziness w/FS::cust_bill::send & FS::cust_pay::delete
217 use Mail::Internet 1.44;
219 my $from = $conf->config('invoice_from'); #??? as good as any
220 $ENV{MAILADDRESS} = $from;
221 my $header = new Mail::Header ( [
223 "To: ". $conf->config('signup_server-email'),
226 "Date: ". time2str("%a, %d %b %Y %X %z", time),
227 "Subject: FREESIDE NOTIFICATION: Signup Server",
230 "This is an automatic message from your Freeside installation\n",
231 "informing you a customer has signed up via the signup server:\n",
233 'custnum : '. $cust_main->custnum. "\n",
234 'Name : '. $cust_main->last. ", ". $cust_main->first. "\n",
235 'Agent : '. $cust_main->agent->agent. "\n",
236 'Package : '. $part_pkg->pkg. ' - '. $part_pkg->comment. "\n",
237 'Signup Date : '. time2str('%C', time). "\n",
238 'Username : '. $svc_acct->username. "\n",
239 #'Password : '. # config file to turn this on if noment insists
240 'Day phone : '. $cust_main->daytime. "\n",
241 'Night phone : '. $cust_main->night. "\n",
242 'Address : '. $cust_main->address1. "\n",
243 ( $cust_main->address2
244 ? ' '. $cust_main->address2. "\n"
246 ' '. $cust_main->city. ', '. $cust_main->state. ' '.
247 $cust_main->zip. "\n",
248 ( $cust_main->country eq 'US'
250 : ' '. $cust_main->country. "\n" ),
253 #if ( $cust_main->balance > 0 ) {
255 # "This customer has an outstanding balance and has been suspended.\n";
257 my $message = new Mail::Internet ( 'Header' => $header, 'Body' => $body );
259 $message->smtpsend( Host => $smtpmachine )
260 or $message->smtpsend( Host => $smtpmachine, Debug => 1 )
261 or warn "[fs_signup_server] can't send email to ".
262 $conf->config('signup_server-email').
263 " via server $smtpmachine with SMTP: $!";
270 warn "connection to $machine lost! waiting 60 seconds...\n";
272 warn "reconnecting...\n";
276 die "Usage:\n\n fs_signup_server user machine agentnum refnum\n";