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