add menu items for credit card batching, debug last-minute changes to payby.pm, add...
[freeside.git] / FS / FS / payby.pm
1 package FS::payby;
2
3 use strict;
4 use vars qw(%hash %payby2bop);
5 use Tie::IxHash;
6 use Business::CreditCard;
7
8
9 =head1 NAME
10
11 FS::payby - Object methods for payment type records
12
13 =head1 SYNOPSIS
14
15   use FS::payby;
16
17   #for now...
18
19   my @payby = FS::payby->payby;
20
21   tie my %payby, 'Tie::IxHash', FS::payby->payby2longname
22
23   my @cust_payby = FS::payby->cust_payby;
24
25   tie my %payby, 'Tie::IxHash', FS::payby->cust_payby2longname
26
27 =head1 DESCRIPTION
28
29 Payment types.
30
31 =head1 METHODS
32
33 =over 4 
34
35 =item
36
37 =cut
38
39 # paybys can be any/all of:
40 # - a customer payment type (cust_main.payby)
41 # - a payment or refund type (cust_pay.payby)
42 # - an event type (part_bill_event.payby)
43
44 tie %hash, 'Tie::IxHash',
45   'CARD' => {
46     tinyname  => 'card',
47     shortname => 'Credit card',
48     longname  => 'Credit card (automatic)',
49   },
50   'DCRD' => {
51     tinyname  => 'card',
52     shortname => 'Credit card',
53     longname  => 'Credit card (on-demand)',
54     cust_pay  => 'CARD', #this is a customer type only, payments are CARD...
55   },
56   'CHEK' => {
57     tinyname  => 'check',
58     shortname => 'Electronic check',
59     longname  => 'Electronic check (automatic)',
60   },
61   'DCHK' => {
62     tinyname  => 'check',
63     shortname => 'Electronic check',
64     longname  => 'Electronic check (on-demand)',
65     cust_pay  => 'CHEK', #this is a customer type only, payments are CHEK...
66   },
67   'LECB' => {
68     tinyname  => 'phone bill',
69     shortname => 'Phone bill billing',
70     longname  => 'Phone bill billing',
71   },
72   'BILL' => {
73     tinyname  => 'billing',
74     shortname => 'Billing',
75     longname  => 'Billing',
76   },
77   'CASH' => {
78     tinyname  => 'cash',
79     shortname => 'Cash', # initial payment, then billing
80     longname  => 'Cash',
81     cust_main => 'BILL', #this is a payment type only, customers go to BILL...
82   },
83   'WEST' => {
84     tinyname  => 'western union',
85     shortname => 'Western Union', # initial payment, then billing
86     longname  => 'Western Union',
87     cust_main => 'BILL', #this is a payment type only, customers go to BILL...
88   },
89   'MCRD' => { #not the same as DCRD
90     tinyname  => 'card',
91     shortname => 'Manual credit card', # initial payment, then billing
92     longname  => 'Manual credit card', 
93     cust_main => 'BILL', #this is a payment type only, customers go to BILL...
94   },
95   'COMP' => {
96     tinyname  => 'comp',
97     shortname => 'Complimentary',
98     longname  => 'Complimentary',
99   },
100   'DCLN' => {  # This is only an event.
101     tinyname  => 'declined',
102     shortname => 'Batch declined payment',
103     longname  => 'Batch declined payment',
104
105     #its neither of these..
106     #cust_main => '',
107     cust_pay  => '',
108
109   },
110 ;
111
112 sub payby {
113   keys %hash;
114 }
115
116 sub payby2longname {
117   my $self = shift;
118   map { $_ => $hash{$_}->{longname} } $self->payby;
119 }
120
121 sub shortname {
122   my( $self, $payby ) = @_;
123   $hash{$payby}->{shortname};
124 }
125
126 sub longname {
127   my( $self, $payby ) = @_;
128   $hash{$payby}->{longname};
129 }
130
131 %payby2bop = (
132   'CARD' => 'CC',
133   'CHEK' => 'ECHECK',
134 );
135
136 sub payby2bop {
137   my( $self, $payby ) = @_;
138   $payby2bop{ $self->payby2payment($payby) };
139 }
140
141 sub payby2payment {
142   my( $self, $payby ) = @_;
143   $hash{$payby}{'cust_pay'} || $payby;
144 }
145
146 sub cust_payby {
147   my $self = shift;
148   grep { ! exists $hash{$_}->{cust_main} } $self->payby;
149 }
150
151 sub cust_payby2longname {
152   my $self = shift;
153   map { $_ => $hash{$_}->{longname} } $self->cust_payby;
154 }
155
156 sub payinfo_check{
157   my($payby, $payinforef) = @_;
158
159   if ($payby eq 'CARD') {
160     $$payinforef =~ s/\D//g;
161     if ($$payinforef){
162       $$payinforef =~ /^(\d{13,16})$/
163         or return "Illegal (mistyped?) credit card number (payinfo)";
164       $$payinforef = $1;
165       validate($$payinforef) or return "Illegal credit card number";
166       return "Unknown card type" if cardtype($$payinforef) eq "Unknown";
167     } else {
168       $$payinforef="N/A";
169     }
170   } else {
171     $$payinforef =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
172     or return "Illegal text (payinfo)";
173     $$payinforef = $1;
174   }
175   '';
176 }
177
178 =back
179
180 =head1 BUGS
181
182 This should eventually be an actual database table, and all tables that
183 currently have a char payby field should have a foreign key into here instead.
184
185 =head1 SEE ALSO
186
187 =cut
188
189 1;
190