clean up payinfo_Mixin to use payby.pm for payby info and have card masking full...
[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   my $bool = FS::payby->can_payby('cust_main', 'CARD');
22
23   tie my %payby, 'Tie::IxHash', FS::payby->payby2longname
24
25   my @cust_payby = FS::payby->cust_payby;
26
27   tie my %payby, 'Tie::IxHash', FS::payby->cust_payby2longname
28
29 =head1 DESCRIPTION
30
31 Payment types.
32
33 =head1 METHODS
34
35 =over 4 
36
37 =item
38
39 =cut
40
41 # paybys can be any/all of:
42 # - a customer payment type (cust_main.payby)
43 # - a payment or refund type (cust_pay.payby, cust_pay_batch.payby, cust_refund.payby)
44 # - an event type (part_bill_event.payby)
45
46 tie %hash, 'Tie::IxHash',
47   'CARD' => {
48     tinyname  => 'card',
49     shortname => 'Credit card',
50     longname  => 'Credit card (automatic)',
51   },
52   'DCRD' => {
53     tinyname  => 'card',
54     shortname => 'Credit card',
55     longname  => 'Credit card (on-demand)',
56     cust_pay  => 'CARD', #this is a customer type only, payments are CARD...
57   },
58   'CHEK' => {
59     tinyname  => 'check',
60     shortname => 'Electronic check',
61     longname  => 'Electronic check (automatic)',
62   },
63   'DCHK' => {
64     tinyname  => 'check',
65     shortname => 'Electronic check',
66     longname  => 'Electronic check (on-demand)',
67     cust_pay  => 'CHEK', #this is a customer type only, payments are CHEK...
68   },
69   'LECB' => {
70     tinyname  => 'phone bill',
71     shortname => 'Phone bill billing',
72     longname  => 'Phone bill billing',
73   },
74   'BILL' => {
75     tinyname  => 'billing',
76     shortname => 'Billing',
77     longname  => 'Billing',
78   },
79   'CASH' => {
80     tinyname  => 'cash',
81     shortname => 'Cash', # initial payment, then billing
82     longname  => 'Cash',
83     cust_main => 'BILL', #this is a payment type only, customers go to BILL...
84   },
85   'WEST' => {
86     tinyname  => 'western union',
87     shortname => 'Western Union', # initial payment, then billing
88     longname  => 'Western Union',
89     cust_main => 'BILL', #this is a payment type only, customers go to BILL...
90   },
91   'MCRD' => { #not the same as DCRD
92     tinyname  => 'card',
93     shortname => 'Manual credit card', # initial payment, then billing
94     longname  => 'Manual credit card', 
95     cust_main => 'BILL', #this is a payment type only, customers go to BILL...
96   },
97   'COMP' => {
98     tinyname  => 'comp',
99     shortname => 'Complimentary',
100     longname  => 'Complimentary',
101     cust_pay  => '', # (free) is depricated as a payment type in cust_pay
102   },
103   'CBAK' => {
104     tinyname  => 'chargeback',
105     shortname => 'Chargeback',
106     longname  => 'Chargeback',
107     cust_main => '', # not a customer type
108   },
109   'DCLN' => {  # This is only an event.
110     tinyname  => 'declined',
111     shortname => 'Batch declined payment',
112     longname  => 'Batch declined payment',
113
114     #its neither of these..
115     cust_main => '',
116     cust_pay  => '',
117
118   },
119 ;
120
121 sub payby {
122   keys %hash;
123 }
124
125 sub can_payby {
126   my( $self, $table, $payby ) = @_;
127
128   #return "Illegal payby" unless $hash{$payby};
129   return 0 unless $hash{$payby};
130
131   $table = 'cust_pay' if $table eq 'cust_pay_batch' || $table eq 'cust_refund';
132   return 0 if exists( $hash{$payby}->{$table} );
133
134   return 1;
135 }
136
137 sub payby2longname {
138   my $self = shift;
139   map { $_ => $hash{$_}->{longname} } $self->payby;
140 }
141
142 sub shortname {
143   my( $self, $payby ) = @_;
144   $hash{$payby}->{shortname};
145 }
146
147 sub longname {
148   my( $self, $payby ) = @_;
149   $hash{$payby}->{longname};
150 }
151
152 %payby2bop = (
153   'CARD' => 'CC',
154   'CHEK' => 'ECHECK',
155 );
156
157 sub payby2bop {
158   my( $self, $payby ) = @_;
159   $payby2bop{ $self->payby2payment($payby) };
160 }
161
162 sub payby2payment {
163   my( $self, $payby ) = @_;
164   $hash{$payby}{'cust_pay'} || $payby;
165 }
166
167 sub cust_payby {
168   my $self = shift;
169   grep { ! exists $hash{$_}->{cust_main} } $self->payby;
170 }
171
172 sub cust_payby2longname {
173   my $self = shift;
174   map { $_ => $hash{$_}->{longname} } $self->cust_payby;
175 }
176
177 =back
178
179 =head1 BUGS
180
181 This should eventually be an actual database table, and all tables that
182 currently have a char payby field should have a foreign key into here instead.
183
184 =head1 SEE ALSO
185
186 =cut
187
188 1;
189