This commit was generated by cvs2svn to compensate for changes in r3241,
[freeside.git] / fs_signup / fs_signup_server
1 #!/usr/bin/perl -Tw
2 #
3 # fs_signup_server
4 #
5
6 use strict;
7 use vars qw($pid);
8 use IO::Handle;
9 use Storable qw(nstore_fd fd_retrieve);
10 use Tie::RefHash;
11 use Net::SSH qw(sshopen2);
12 use FS::UID qw(adminsuidsetup);
13 use FS::Conf;
14 use FS::Record qw( qsearch qsearchs );
15 use FS::cust_main_county;
16 use FS::cust_main;
17 use FS::cust_bill;
18 use FS::cust_pkg;
19 use FS::Msgcat qw(gettext);
20
21 use vars qw( $opt $Debug );
22
23 $Debug = 2;
24
25 my $user = shift or die &usage;
26 &adminsuidsetup( $user ); 
27
28 my $conf = new FS::Conf;
29
30 if ($conf->exists('signup_server-quiet')) {
31     $FS::cust_bill::quiet = 1;
32     $FS::cust_pkg::quiet = 1;
33 }
34
35 #my @payby = qw(CARD PREPAY);
36 my @payby = $conf->config('signup_server-payby');
37 my $smtpmachine = $conf->config('smtpmachine');
38
39 my $machine = shift or die &usage;
40
41 my $agentnum = shift or die &usage;
42 my $agent = qsearchs( 'agent', { 'agentnum' => $agentnum } ) or die &usage;
43 my $pkgpart_href = $agent->pkgpart_hashref;
44
45 my $refnum = shift or die &usage;
46
47 #causing trouble for some folks
48 #$SIG{CHLD} = sub { wait() };
49
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; };
56
57 my($fs_signupd)="/usr/local/sbin/fs_signupd";
58
59 while (1) {
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);
64
65   my @pops = qsearch('svc_acct_pop',{} );
66   my $init_data = {
67
68     #'_protocol' => 'signup',
69     #'_version' => '0.1',
70     #'_packet' => 'init'
71   
72     'cust_main_county' =>
73       [ map { $_->hashref } qsearch('cust_main_county', {}) ],
74       
75     'part_pkg' =>
76       [
77         #map { $_->hashref }
78         map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
79           grep { $_->svcpart('svc_acct') && $pkgpart_href->{ $_->pkgpart } }
80             qsearch( 'part_pkg', { 'disabled' => '' } )
81       ],
82
83     'agentnum2part_pkg' =>
84       {
85         map {
86           my $href = $_->pkgpart_hashref;
87           $_->agentnum =>
88             [
89               map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
90                 grep { $_->svcpart('svc_acct') && $href->{ $_->pkgpart } }
91                   qsearch( 'part_pkg', { 'disabled' => '' } )
92             ];
93         } qsearch('agent', {} )
94       },
95
96     'svc_acct_pop' => [ map { $_->hashref } @pops ],
97
98     'security_phrase' => $conf->exists('security_phrase'),
99
100     'payby' => [ $conf->config('signup_server-payby') ],
101
102     'msgcat' => { map { $_=>gettext($_) } qw(
103       passwords_dont_match invalid_card unknown_card_type not_a
104     ) },
105
106     'statedefault' => $conf->config('statedefault') || 'CA',
107
108     'countrydefault' => $conf->config('countrydefault') || 'US',
109
110   };
111
112   warn "[fs_signup_server] Sending init data...\n" if $Debug;
113   nstore_fd($init_data, $writer) or die "can't send init data: $!";
114   $writer->flush;
115
116   warn "[fs_signup_server] Entering main loop...\n" if $Debug;
117   while (1) {
118     warn "[fs_signup_server] Reading (waiting for) signup data...\n" if $Debug;
119     my $signup_data = fd_retrieve($reader);
120
121     if ( $Debug > 1 ) {
122       warn join('',
123         map { "  $_ => ". $signup_data->{$_}. "\n" } keys %$signup_data );
124     }
125
126     warn "[fs_signup_server] Processing signup...\n" if $Debug;
127
128     my $error = '';
129
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);
136
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 ( {
140       #'custnum'          => '',
141       'agentnum'         => $signup_data->{agentnum} || $agentnum,
142       'refnum'           => $refnum,
143
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
147       ),
148
149     } );
150
151     $error ||= "Illegal payment type"
152       unless grep { $_ eq $signup_data->{'payby'} } @payby;
153
154     $cust_main->payinfo($cust_main->daytime)
155       if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
156
157     my @invoicing_list = split( /\s*\,\s*/, $signup_data->{'invoicing_list'} );
158
159     $signup_data->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
160     my $pkgpart = $1;
161
162     my $part_pkg =
163       qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
164         or $error ||= "WARNING: unknown pkgpart: $pkgpart";
165     my $svcpart = $part_pkg->svcpart('svc_acct') unless $error;
166
167     my $cust_pkg = new FS::cust_pkg ( {
168       #later#'custnum' => $custnum,
169       'pkgpart' => $signup_data->{'pkgpart'},
170     } );
171     $error ||= $cust_pkg->check;
172
173     my $svc_acct = new FS::svc_acct ( {
174       'svcpart'   => $svcpart,
175       map { $_ => $signup_data->{$_} }
176         qw( username _password sec_phrase popnum ),
177     } );
178
179     my $y = $svc_acct->setdefault; # arguably should be in new method
180     $error ||= $y unless ref($y);
181
182     $error ||= $svc_acct->check;
183
184     use Tie::RefHash;
185     tie my %hash, 'Tie::RefHash';
186     %hash = ( $cust_pkg => [ $svc_acct ] );
187     $error ||= $cust_main->insert( \%hash, \@invoicing_list ); #msgcat
188
189     if ( ! $error && $conf->exists('signup_server-realtime') ) {
190
191       warn "[fs_signup_server] Billing customer...\n" if $Debug;
192
193       my $bill_error = $cust_main->bill;
194       warn "[fs_signup_server] error billing new customer: $bill_error"
195         if $bill_error;
196
197       $cust_main->apply_payments;
198       $cust_main->apply_credits;
199
200       $bill_error = $cust_main->collect;
201       warn "[fs_signup_server] error collecting from new customer: $bill_error"
202         if $bill_error;
203
204       if ( $cust_main->balance > 0 ) {
205
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;
209
210         #should check list for errors...
211         #$cust_main->suspend;
212         $cust_main->cancel;
213
214         $error = '_decline';
215       }
216     }
217
218     warn "[fs_signup_server] Sending results...\n" if $Debug;
219     print $writer $error, "\n";
220
221     next if $error;
222
223     if ( $conf->config('signup_server-email') ) {
224       warn "[fs_signup_server] Sending email...\n" if $Debug;
225
226       #false laziness w/FS::cust_bill::send & FS::cust_pay::delete
227       use Mail::Header;
228       use Mail::Internet 1.44;
229       use Date::Format;
230       my $from = $conf->config('invoice_from'); #??? as good as any
231       $ENV{MAILADDRESS} = $from;
232       my $header = new Mail::Header ( [
233         "From: $from",
234         "To: ". $conf->config('signup_server-email'),
235         "Sender: $from",
236         "Reply-To: $from",
237         "Date: ". time2str("%a, %d %b %Y %X %z", time),
238         "Subject: FREESIDE NOTIFICATION: Signup Server",
239       ] );
240       my $body = [
241         "This is an automatic message from your Freeside installation\n",
242         "informing you a customer has signed up via the signup server:\n",
243         "\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"
256             : ''                                           ),
257         '              '. $cust_main->city. ', '. $cust_main->state. '  '.
258                           $cust_main->zip. "\n",
259         ( $cust_main->country eq 'US'
260             ? ''
261             : '              '. $cust_main->country. "\n" ),
262         "\n",
263       ];
264       #if ( $cust_main->balance > 0 ) {
265       #  push @$body,
266       #    "This customer has an outstanding balance and has been suspended.\n";
267       #}
268       my $message = new Mail::Internet ( 'Header' => $header, 'Body' => $body );
269       $!=0;
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: $!";
275       #end-of-send mail
276     }
277
278   }
279   close $writer;
280   close $reader;
281   warn "connection to $machine lost!  waiting 60 seconds...\n";
282   sleep 60;
283   warn "reconnecting...\n";
284 }
285
286 sub usage {
287   die "Usage:\n\n  fs_signup_server user machine agentnum refnum\n";
288 }
289