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;
19 use FS::Msgcat qw(gettext);
21 use vars qw( $opt $Debug );
25 my $user = shift or die &usage;
26 &adminsuidsetup( $user );
28 my $conf = new FS::Conf;
30 if ($conf->exists('signup_server-quiet')) {
31 $FS::cust_bill::quiet = 1;
32 $FS::cust_pkg::quiet = 1;
35 #my @payby = qw(CARD PREPAY);
36 my @payby = $conf->config('signup_server-payby');
37 my $smtpmachine = $conf->config('smtpmachine');
39 my $machine = shift or die &usage;
41 my $agentnum = shift or die &usage;
42 my $agent = qsearchs( 'agent', { 'agentnum' => $agentnum } ) or die &usage;
43 my $pkgpart_href = $agent->pkgpart_hashref;
45 my $refnum = shift or die &usage;
47 #causing trouble for some folks
48 #$SIG{CHLD} = sub { wait() };
50 $SIG{HUP} = \&killssh;
51 $SIG{INT} = \&killssh;
52 $SIG{QUIT} = \&killssh;
53 $SIG{TERM} = \&killssh;
54 $SIG{PIPE} = \&killssh;
55 sub killssh { kill 'TERM', $pid if $pid; exit; };
57 my($fs_signupd)="/usr/local/sbin/fs_signupd";
60 my($reader,$writer)=(new IO::Handle, new IO::Handle);
61 #seems to be broken - calling ->flush explicitly# $writer->autoflush(1);
62 warn "[fs_signup_server] Connecting to $machine...\n" if $Debug;
63 $pid = sshopen2($machine,$reader,$writer,$fs_signupd);
65 my @pops = qsearch('svc_acct_pop',{} );
68 #'_protocol' => 'signup',
73 [ map { $_->hashref } qsearch('cust_main_county', {}) ],
78 map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
79 grep { $_->svcpart('svc_acct') && $pkgpart_href->{ $_->pkgpart } }
80 qsearch( 'part_pkg', { 'disabled' => '' } )
83 'agentnum2part_pkg' =>
86 my $href = $_->pkgpart_hashref;
89 map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
90 grep { $_->svcpart('svc_acct') && $href->{ $_->pkgpart } }
91 qsearch( 'part_pkg', { 'disabled' => '' } )
93 } qsearch('agent', {} )
96 'svc_acct_pop' => [ map { $_->hashref } @pops ],
98 'security_phrase' => $conf->exists('security_phrase'),
100 'payby' => [ $conf->config('signup_server-payby') ],
102 'msgcat' => { map { $_=>gettext($_) } qw(
103 passwords_dont_match invalid_card unknown_card_type not_a
106 'statedefault' => $conf->config('statedefault') || 'CA',
108 'countrydefault' => $conf->config('countrydefault') || 'US',
112 warn "[fs_signup_server] Sending init data...\n" if $Debug;
113 nstore_fd($init_data, $writer) or die "can't send init data: $!";
116 warn "[fs_signup_server] Entering main loop...\n" if $Debug;
118 warn "[fs_signup_server] Reading (waiting for) signup data...\n" if $Debug;
119 my $signup_data = fd_retrieve($reader);
123 map { " $_ => ". $signup_data->{$_}. "\n" } keys %$signup_data );
126 warn "[fs_signup_server] Processing signup...\n" if $Debug;
130 #things that aren't necessary in base class, but are for signup server
131 #return "Passwords don't match"
132 # if $hashref->{'_password'} ne $hashref->{'_password2'}
133 $error ||= gettext('empty_password') unless $signup_data->{'_password'};
134 $error ||= gettext('no_access_number_selected')
135 unless $signup_data->{'popnum'} || !scalar(@pops);
137 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
138 # common that are still here and library them.
139 my $cust_main = new FS::cust_main ( {
141 'agentnum' => $signup_data->{agentnum} || $agentnum,
144 map { $_ => $signup_data->{$_} } qw(
145 last first ss company address1 address2 city county state zip country
146 daytime night fax payby payinfo paydate payname referral_custnum comments
151 $error ||= "Illegal payment type"
152 unless grep { $_ eq $signup_data->{'payby'} } @payby;
154 $cust_main->payinfo($cust_main->daytime)
155 if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
157 my @invoicing_list = split( /\s*\,\s*/, $signup_data->{'invoicing_list'} );
159 $signup_data->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
163 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
164 or $error ||= "WARNING: unknown pkgpart: $pkgpart";
165 my $svcpart = $part_pkg->svcpart('svc_acct') unless $error;
167 my $cust_pkg = new FS::cust_pkg ( {
168 #later#'custnum' => $custnum,
169 'pkgpart' => $signup_data->{'pkgpart'},
171 $error ||= $cust_pkg->check;
173 my $svc_acct = new FS::svc_acct ( {
174 'svcpart' => $svcpart,
175 map { $_ => $signup_data->{$_} }
176 qw( username _password sec_phrase popnum ),
179 my $y = $svc_acct->setdefault; # arguably should be in new method
180 $error ||= $y unless ref($y);
182 $error ||= $svc_acct->check;
185 tie my %hash, 'Tie::RefHash';
186 %hash = ( $cust_pkg => [ $svc_acct ] );
187 $error ||= $cust_main->insert( \%hash, \@invoicing_list ); #msgcat
189 if ( ! $error && $conf->exists('signup_server-realtime') ) {
191 warn "[fs_signup_server] Billing customer...\n" if $Debug;
193 my $bill_error = $cust_main->bill;
194 warn "[fs_signup_server] error billing new customer: $bill_error"
197 $cust_main->apply_payments;
198 $cust_main->apply_credits;
200 $bill_error = $cust_main->collect;
201 warn "[fs_signup_server] error collecting from new customer: $bill_error"
204 if ( $cust_main->balance > 0 ) {
206 #this makes sense. credit is "un-doing" the invoice
207 $cust_main->credit( $cust_main->balance, 'signup server decline' );
208 $cust_main->apply_credits;
210 #should check list for errors...
211 #$cust_main->suspend;
218 warn "[fs_signup_server] Sending results...\n" if $Debug;
219 print $writer $error, "\n";
223 if ( $conf->config('signup_server-email') ) {
224 warn "[fs_signup_server] Sending email...\n" if $Debug;
226 #false laziness w/FS::cust_bill::send & FS::cust_pay::delete
228 use Mail::Internet 1.44;
230 my $from = $conf->config('invoice_from'); #??? as good as any
231 $ENV{MAILADDRESS} = $from;
232 my $header = new Mail::Header ( [
234 "To: ". $conf->config('signup_server-email'),
237 "Date: ". time2str("%a, %d %b %Y %X %z", time),
238 "Subject: FREESIDE NOTIFICATION: Signup Server",
241 "This is an automatic message from your Freeside installation\n",
242 "informing you a customer has signed up via the signup server:\n",
244 'custnum : '. $cust_main->custnum. "\n",
245 'Name : '. $cust_main->last. ", ". $cust_main->first. "\n",
246 'Agent : '. $cust_main->agent->agent. "\n",
247 'Package : '. $part_pkg->pkg. ' - '. $part_pkg->comment. "\n",
248 'Signup Date : '. time2str('%C', time). "\n",
249 'Username : '. $svc_acct->username. "\n",
250 #'Password : '. # config file to turn this on if noment insists
251 'Day phone : '. $cust_main->daytime. "\n",
252 'Night phone : '. $cust_main->night. "\n",
253 'Address : '. $cust_main->address1. "\n",
254 ( $cust_main->address2
255 ? ' '. $cust_main->address2. "\n"
257 ' '. $cust_main->city. ', '. $cust_main->state. ' '.
258 $cust_main->zip. "\n",
259 ( $cust_main->country eq 'US'
261 : ' '. $cust_main->country. "\n" ),
264 #if ( $cust_main->balance > 0 ) {
266 # "This customer has an outstanding balance and has been suspended.\n";
268 my $message = new Mail::Internet ( 'Header' => $header, 'Body' => $body );
270 $message->smtpsend( Host => $smtpmachine )
271 or $message->smtpsend( Host => $smtpmachine, Debug => 1 )
272 or warn "[fs_signup_server] can't send email to ".
273 $conf->config('signup_server-email').
274 " via server $smtpmachine with SMTP: $!";
281 warn "connection to $machine lost! waiting 60 seconds...\n";
283 warn "reconnecting...\n";
287 die "Usage:\n\n fs_signup_server user machine agentnum refnum\n";