fae26baae0b706feef2de4df31efc9a27059608f
[freeside.git] / fs_selfservice / FS-SelfService / SelfService.pm
1 package FS::SelfService;
2
3 use strict;
4 use vars qw($VERSION @ISA @EXPORT_OK $socket %autoload $tag);
5 use Exporter;
6 use Socket;
7 use FileHandle;
8 #use IO::Handle;
9 use IO::Select;
10 use Storable qw(nstore_fd fd_retrieve);
11
12 $VERSION = '0.03';
13
14 @ISA = qw( Exporter );
15
16 $socket =  "/usr/local/freeside/selfservice_socket";
17 $socket .= '.'.$tag if defined $tag && length($tag);
18
19 #maybe should ask ClientAPI for this list
20 %autoload = (
21   'passwd'          => 'passwd/passwd',
22   'chfn'            => 'passwd/passwd',
23   'chsh'            => 'passwd/passwd',
24   'login'           => 'MyAccount/login',
25   'customer_info'   => 'MyAccount/customer_info',
26   'invoice'         => 'MyAccount/invoice',
27   'cancel'          => 'MyAccount/cancel',
28   'list_pkgs'       => 'MyAccount/list_pkgs',
29   'order_pkg'       => 'MyAccount/order_pkg',
30   'cancel_pkg'      => 'MyAccount/cancel_pkg',
31   'signup_info'     => 'Signup/signup_info',
32   'new_customer'    => 'Signup/new_customer',
33 );
34 @EXPORT_OK = keys %autoload;
35
36 $ENV{'PATH'} ='/usr/bin:/usr/ucb:/bin';
37 $ENV{'SHELL'} = '/bin/sh';
38 $ENV{'IFS'} = " \t\n";
39 $ENV{'CDPATH'} = '';
40 $ENV{'ENV'} = '';
41 $ENV{'BASH_ENV'} = '';
42
43 my $freeside_uid = scalar(getpwnam('freeside'));
44 die "not running as the freeside user\n" if $> != $freeside_uid;
45
46 foreach my $autoload ( keys %autoload ) {
47
48   my $eval =
49   "sub $autoload { ". '
50                    my $param;
51                    if ( ref($_[0]) ) {
52                      $param = shift;
53                    } else {
54                      $param = { @_ };
55                    }
56
57                    $param->{_packet} = \''. $autoload{$autoload}. '\';
58
59                    simple_packet($param);
60                  }';
61
62   eval $eval;
63   die $@ if $@;
64
65 }
66
67 sub simple_packet {
68   my $packet = shift;
69   socket(SOCK, PF_UNIX, SOCK_STREAM, 0) or die "socket: $!";
70   connect(SOCK, sockaddr_un($socket)) or die "connect: $!";
71   nstore_fd($packet, \*SOCK) or die "can't send packet: $!";
72   SOCK->flush;
73
74   #shoudl trap: Magic number checking on storable file failed at blib/lib/Storable.pm (autosplit into blib/lib/auto/Storable/fd_retrieve.al) line 337, at /usr/local/share/perl/5.6.1/FS/SelfService.pm line 71
75
76   #block until there is a message on socket
77 #  my $w = new IO::Select;
78 #  $w->add(\*SOCK);
79 #  my @wait = $w->can_read;
80   my $return = fd_retrieve(\*SOCK) or die "error reading result: $!";
81   die $return->{'_error'} if defined $return->{_error} && $return->{_error};
82
83   $return;
84 }
85
86 =head1 NAME
87
88 FS::SelfService - Freeside self-service API
89
90 =head1 SYNOPSIS
91
92   # password and shell account changes
93   use FS::SelfService qw(passwd chfn chsh);
94
95   # "my account" functionality
96   use FS::SelfService qw( login customer_info invoice cancel payment_info process_payment );
97
98   my $rv = login( { 'username' => $username,
99                     'domain'   => $domain,
100                     'password' => $password,
101                   }
102                 );
103
104   if ( $rv->{'error'} ) {
105     #handle login error...
106   } else {
107     #successful login
108     my $session_id = $rv->{'session_id'};
109   }
110
111   my $customer_info = customer_info( { 'session_id' => $session_id } );
112
113   #payment_info and process_payment are available in 1.5+ only
114   my $payment_info = payment_info) { 'session_id' => $session_id } );
115
116   #!!! process_payment example
117
118   #!!! list_pkgs example
119
120   #!!! order_pkg example
121
122   #!!! cancel_pkg example
123
124   # signup functionality
125   use FS::SelfService qw( signup_info new_customer );
126
127   my $signup_info = signup_info;
128
129   $rv = new_customer( {
130                         'first'            => $first,
131                         'last'             => $last,
132                         'company'          => $company,
133                         'address1'         => $address1,
134                         'address2'         => $address2,
135                         'city'             => $city,
136                         'state'            => $state,
137                         'zip'              => $zip,
138                         'country'          => $country,
139                         'daytime'          => $daytime,
140                         'night'            => $night,
141                         'fax'              => $fax,
142                         'payby'            => $payby,
143                         'payinfo'          => $payinfo,
144                         'paycvv'           => $paycvv,
145                         'paydate'          => $paydate,
146                         'payname'          => $payname,
147                         'invoicing_list'   => $invoicing_list,
148                         'referral_custnum' => $referral_custnum,
149                         'pkgpart'          => $pkgpart,
150                         'username'         => $username,
151                         '_password'        => $password,
152                         'popnum'           => $popnum,
153                         'agentnum'         => $agentnum,
154                       }
155                     );
156   
157   my $error = $rv->{'error'};
158   if ( $error eq '_decline' ) {
159     print_decline();
160   } elsif ( $error ) {
161     reprint_signup();
162   } else {
163     print_success();
164   }
165
166 =head1 DESCRIPTION
167
168 Use this API to implement your own client "self-service" module.
169
170 If you just want to customize the look of the existing "self-service" module,
171 see XXXX instead.
172
173 =head1 PASSWORD, GECOS, SHELL CHANGING FUNCTIONS
174
175 =over 4
176
177 =item passwd
178
179 =item chfn
180
181 =item chsh
182
183 =back
184
185 =head1 "MY ACCOUNT" FUNCTIONS
186
187 =over 4
188
189 =item login HASHREF
190
191 Creates a user session.  Takes a hash reference as parameter with the
192 following keys:
193
194 =over 4
195
196 =item username
197
198 =item domain
199
200 =item password
201
202 =back
203
204 Returns a hash reference with the following keys:
205
206 =over 4
207
208 =item error
209
210 Empty on success, or an error message on errors.
211
212 =item session_id
213
214 Session identifier for successful logins
215
216 =back
217
218 =item customer_info HASHREF
219
220 Returns general customer information.
221
222 Takes a hash reference as parameter with a single key: B<session_id>
223
224 Returns a hash reference with the following keys:
225
226 =over 4
227
228 =item name
229
230 Customer name
231
232 =item balance
233
234 Balance owed
235
236 =item open
237
238 Array reference of hash references of open inoices.  Each hash reference has
239 the following keys: invnum, date, owed
240
241 =item small_custview
242
243 An HTML fragment containing shipping and billing addresses.
244
245 =back
246
247 =item invoice HASHREF
248
249 Returns an invoice.  Takes a hash reference as parameter with two keys:
250 session_id and invnum
251
252 Returns a hash reference with the following keys:
253
254 =over 4
255
256 =item error
257
258 Empty on success, or an error message on errors
259
260 =item invnum
261
262 Invoice number
263
264 =item invoice_text
265
266 Invoice text
267
268 =back
269
270 =item cancel HASHREF
271
272 Cancels this customer.
273
274 Takes a hash reference as parameter with a single key: B<session_id>
275
276 Returns a hash reference with a single key, B<error>, which is empty on
277 success or an error message on errors.
278
279 =item payment_info HASHREF
280
281 Returns information that may be useful in displaying a payment page.
282
283 Takes a hash reference as parameter with a single key: B<session_id>.
284
285 Returns a hash reference with the following keys:
286
287 =over 4
288
289 =item error
290
291 Empty on success, or an error message on errors
292
293 =item balance
294
295 Balance owed
296
297 =item payname
298
299 Exact name on credit card (CARD/DCRD)
300
301 =item address1
302
303 =item address2
304
305 =item city
306
307 =item state
308
309 =item zip
310
311 =item payby
312
313 Customer's current default payment type.
314
315 =item card_type
316
317 For CARD/DCRD payment types, the card type (Visa card, MasterCard, Discover card, American Express card, etc.)
318
319 =item payinfo
320
321 For CARD/DCRD payment types, the card number
322
323 =item month
324
325 For CARD/DCRD payment types, expiration month
326
327 =item year
328
329 For CARD/DCRD payment types, expiration year
330
331 =item cust_main_county
332
333 County/state/country data - array reference of hash references, each of which has the fields of a cust_main_county record (see L<FS::cust_main_county>).  Note these are not FS::cust_main_county objects, but hash references of columns and values.
334
335 =item states
336
337 Array reference of all states in the current default country.
338
339 =item card_types
340
341 Hash reference of card types; keys are card types, values are the exact strings
342 passed to the process_payment function
343
344 =item paybatch
345
346 Unique transaction identifier (prevents multiple charges), passed to the
347 process_payment function
348
349 =back
350
351 =item process_payment HASHREF
352
353 Processes a payment and possible change of address or payment type.  Takes a
354 hash reference as parameter with the following keys:
355
356 =over 4
357
358 =item session_id
359
360 =item save
361
362 If true, address and card information entered will be saved for subsequent
363 transactions.
364
365 =item auto
366
367 If true, future credit card payments will be done automatically (sets payby to
368 CARD).  If false, future credit card payments will be done on-demand (sets
369 payby to DCRD).  This option only has meaning if B<save> is set true.  
370
371 =item payname
372
373 =item address1
374
375 =item address2
376
377 =item city
378
379 =item state
380
381 =item zip
382
383 =item payinfo
384
385 Card number
386
387 =item month
388
389 Card expiration month
390
391 =item year
392
393 Card expiration year
394
395 =item paybatch
396
397 Unique transaction identifier, returned from the payment_info function.
398 Prevents multiple charges.
399
400 =back
401
402 Returns a hash reference with a single key, B<error>, empty on success, or an
403 error message on errors
404
405 =item list_pkgs
406
407 Returns package information for this customer.
408
409 Takes a hash reference as parameter with a single key: B<session_id>
410
411 Returns a hash reference containing customer package information.  The hash reference contains the following keys:
412
413 =over 4
414
415 =item cust_pkg HASHREF
416
417 Array reference of hash references, each of which has the fields of a cust_pkg record (see L<FS::cust_pkg>).  Note these are not FS::cust_pkg objects, but hash references of columns and values.
418
419 =back
420
421 =item order_pkg
422
423 Orders a package for this customer.
424
425 Takes a hash reference as parameter with the following keys:
426
427 =over 4
428
429 =item session_id
430
431 =item pkgpart
432
433 =item svcpart
434
435 optional svcpart, required only if the package definition does not contain
436 one svc_acct service definition with quantity 1 (it may contain others with
437 quantity >1)
438
439 =item username
440
441 =item _password
442
443 =item sec_phrase
444
445 =item popnum
446
447 =back
448
449 Returns a hash reference with a single key, B<error>, empty on success, or an
450 error message on errors.  The special error '_decline' is returned for
451 declined transactions.
452
453 =item cancel_pkg
454
455 Cancels a package for this customer.
456
457 Takes a hash reference as parameter with the following keys:
458
459 =over 4
460
461 =item session_id
462
463 =item pkgpart
464
465 =back
466
467 Returns a hash reference with a single key, B<error>, empty on success, or an
468 error message on errors.
469
470 =back
471
472 =head1 SIGNUP FUNCTIONS
473
474 =over 4
475
476 =item signup_info
477
478 Returns a hash reference containing information that may be useful in
479 displaying a signup page.  The hash reference contains the following keys:
480
481 =over 4
482
483 =item cust_main_county
484
485 County/state/country data - array reference of hash references, each of which has the fields of a cust_main_county record (see L<FS::cust_main_county>).  Note these are not FS::cust_main_county objects, but hash references of columns and values.
486
487 =item part_pkg
488
489 Available packages - array reference of hash references, each of which has the fields of a part_pkg record (see L<FS::part_pkg>).  Each hash reference also has an additional 'payby' field containing an array reference of acceptable payment types specific to this package (see below and L<FS::part_pkg/payby>).  Note these are not FS::part_pkg objects, but hash references of columns and values.  Requires the 'signup_server-default_agentnum' configuration value to be set.
490
491 =item agent
492
493 Array reference of hash references, each of which has the fields of an agent record (see L<FS::agent>).  Note these are not FS::agent objects, but hash references of columns and values.
494
495 =item agentnum2part_pkg
496
497 Hash reference; keys are agentnums, values are array references of available packages for that agent, in the same format as the part_pkg arrayref above.
498
499 =item svc_acct_pop
500
501 Access numbers - array reference of hash references, each of which has the fields of an svc_acct_pop record (see L<FS::svc_acct_pop>).  Note these are not FS::svc_acct_pop objects, but hash references of columns and values.
502
503 =item security_phrase
504
505 True if the "security_phrase" feature is enabled
506
507 =item payby
508
509 Array reference of acceptable payment types for signup
510
511 =over 4
512
513 =item CARD (credit card - automatic)
514
515 =item DCRD (credit card - on-demand - version 1.5+ only)
516
517 =item CHEK (electronic check - automatic)
518
519 =item DCHK (electronic check - on-demand - version 1.5+ only)
520
521 =item LECB (Phone bill billing)
522
523 =item BILL (billing, not recommended for signups)
524
525 =item COMP (free, definately not recommended for signups)
526
527 =item PREPAY (special billing type: applies a credit (see FS::prepay_credit) and sets billing type to BILL)
528
529 =back
530
531 =item cvv_enabled
532
533 True if CVV features are available (1.5+ or 1.4.2 with CVV schema patch)
534
535 =item msgcat
536
537 Hash reference of message catalog values, to support error message customization.  Currently available keys are: passwords_dont_match, invalid_card, unknown_card_type, and not_a (as in "Not a Discover card").  Values are configured in the web interface under "View/Edit message catalog".
538
539 =item statedefault
540
541 Default state
542
543 =item countrydefault
544
545 Default country
546
547 =back
548
549 =item new_customer HASHREF
550
551 Creates a new customer.  Takes a hash reference as parameter with the
552 following keys:
553
554 =over 4
555
556 =item first - first name (required)
557
558 =item last - last name (required)
559
560 =item ss (not typically collected; mostly used for ACH transactions)
561
562 =item company
563
564 =item address1 (required)
565
566 =item address2
567
568 =item city (required)
569
570 =item county
571
572 =item state (required)
573
574 =item zip (required)
575
576 =item daytime - phone
577
578 =item night - phone
579
580 =item fax - phone
581
582 =item payby - CARD, DCRD, CHEK, DCHK, LECB, BILL, COMP or PREPAY (see L</signup_info> (required)
583
584 =item payinfo - Card number for CARD/DCRD, account_number@aba_number for CHEK/DCHK, prepaid "pin" for PREPAY, purchase order number for BILL
585
586 =item paycvv - Credit card CVV2 number (1.5+ or 1.4.2 with CVV schema patch)
587
588 =item paydate - Expiration date for CARD/DCRD
589
590 =item payname - Exact name on credit card for CARD/DCRD, bank name for CHEK/DCHK
591
592 =item invoicing_list - comma-separated list of email addresses for email invoices.  The special value 'POST' is used to designate postal invoicing (it may be specified alone or in addition to email addresses),
593
594 =item referral_custnum - referring customer number
595
596 =item pkgpart - pkgpart of initial package
597
598 =item username
599
600 =item _password
601
602 =item sec_phrase - security phrase
603
604 =item popnum - access number (index, not the literal number)
605
606 =item agentnum - agent number
607
608 =back
609
610 Returns a hash reference with the following keys:
611
612 =over 4
613
614 =item error Empty on success, or an error message on errors.  The special error '_decline' is returned for declined transactions; other error messages should be suitable for display to the user (and are customizable in under Sysadmin | View/Edit message catalog)
615
616 =back
617
618
619 =back
620
621 =head1 BUGS
622
623 =head1 SEE ALSO
624
625 L<freeside-selfservice-clientd>, L<freeside-selfservice-server>
626
627 =cut
628
629 1;
630