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