multiple payment options, RT#23741
[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 =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   my $bool = FS::payby->can_payby('cust_main', 'CARD');
21
22   tie my %payby, 'Tie::IxHash', FS::payby->payby2longname
23
24   my @cust_payby = FS::payby->cust_payby;
25
26   tie my %payby, 'Tie::IxHash', FS::payby->cust_payby2longname
27
28 =head1 DESCRIPTION
29
30 Payment types.
31
32 =head1 METHODS
33
34 =over 4 
35
36 =item
37
38 =cut
39
40 # paybys can be any/all of:
41 # - a customer saved payment type (cust_payby.payby)
42 # - a payment or refund type (cust_pay.payby, cust_pay_batch.payby, cust_refund.payby)
43
44 tie %hash, 'Tie::IxHash',
45   'CARD' => {
46     tinyname  => 'card',
47     shortname => 'Credit card',
48     longname  => 'Credit card (automatic)',
49     realtime  => 1,
50   },
51   'DCRD' => {
52     tinyname  => 'card',
53     shortname => 'Credit card',
54     longname  => 'Credit card (on-demand)',
55     cust_pay  => 'CARD', #this is a customer type only, payments are CARD...
56     realtime  => 1,
57   },
58   'CHEK' => {
59     tinyname  => 'check',
60     shortname => 'Electronic check',
61     longname  => 'Electronic check (automatic)',
62     realtime  => 1,
63   },
64   'DCHK' => {
65     tinyname  => 'check',
66     shortname => 'Electronic check',
67     longname  => 'Electronic check (on-demand)',
68     cust_pay  => 'CHEK', #this is a customer type only, payments are CHEK...
69     realtime  => 1,
70   },
71   'BILL' => {
72     tinyname  => 'billing',
73     shortname => 'Billing',
74     payname   => 'Check',
75     longname  => 'Billing',
76     cust_main => '', #no longer a customer type
77   },
78   'PPAL' => {
79     tinyname  => 'PayPal',
80     shortname => 'PayPal',
81     longname  => 'PayPal',
82     cust_main => '', #not yet a customer type, but could be once we can do
83                      # invoice presentment via paypal
84   },
85   'PREP' => {
86     tinyname  => 'prepaid card',
87     shortname => 'Prepaid card',
88     longname  => 'Prepaid card',
89     cust_main => '', #this is a payment type only
90   },
91   'CASH' => {
92     tinyname  => 'cash',
93     shortname => 'Cash', # initial payment, then billing
94     longname  => 'Cash',
95     cust_main => '', #this is a payment type only
96   },
97   'WEST' => {
98     tinyname  => 'western union',
99     shortname => 'Western Union', # initial payment, then billing
100     longname  => 'Western Union',
101     cust_main => '', #this is a payment type only
102   },
103   'MCRD' => { #not the same as DCRD
104     tinyname  => 'card',
105     shortname => 'Manual credit card', # initial payment, then billing
106     longname  => 'Manual credit card', 
107     cust_main => '', #this is a payment type only
108   },
109   'MCHK' => { #not the same as DCHK
110     tinyname  => 'card',
111     shortname => 'Manual electronic check', # initial payment, then billing
112     longname  => 'Manual electronic check', 
113     cust_main => '', #this is a payment type only
114   },
115   'APPL' => {
116     tinyname  => 'apple store',
117     shortname => 'Apple Store',
118     longname  => 'Apple Store',
119     cust_main => '', #this is a payment type only
120   },
121   'ANRD' => {
122     tinyname  => 'android market',
123     shortname => 'Android Market',
124     longname  => 'Android Market',
125     cust_main => '', #this is a payment type only
126   },
127   'EDI' => {
128     tinyname  => 'EDI',
129     shortname => 'Electronic Debit (EDI)',
130     longname  => 'Electronic Debit (EDI)',
131     cust_main => '', #not a customer type
132   },
133   'WIRE' => {
134     tinyname  => 'Wire',
135     shortname => 'Wire transfer',
136     longname  => 'Wire transfer',
137     cust_main => '', #not a customer type
138   },
139   'CBAK' => {
140     tinyname  => 'chargeback',
141     shortname => 'Chargeback',
142     longname  => 'Chargeback',
143     cust_main => '', # not a customer type
144   },
145 ;
146
147 sub payby {
148   keys %hash;
149 }
150
151 sub can_payby {
152   my( $self, $table, $payby ) = @_;
153
154   #return "Illegal payby" unless $hash{$payby};
155   return 0 unless $hash{$payby};
156
157   $table = 'cust_pay' if $table =~ /^cust_(pay_pending|pay_batch|pay_void|refund)$/;
158   return 0 if exists( $hash{$payby}->{$table} );
159
160   return 1;
161 }
162
163 sub realtime {  # can use realtime payment facilities
164   my( $self, $payby ) = @_;
165
166   return 0 unless $hash{$payby};
167   return 0 unless exists( $hash{$payby}->{realtime} );
168
169   return $hash{$payby}->{realtime};
170 }
171
172 sub payby2shortname {
173   my $self = shift;
174   map { $_ => $hash{$_}->{shortname} } $self->payby;
175 }
176
177 sub payby2longname {
178   my $self = shift;
179   map { $_ => $hash{$_}->{longname} } $self->payby;
180 }
181
182 sub shortname {
183   my( $self, $payby ) = @_;
184   $hash{$payby}->{shortname};
185 }
186
187 sub payname {
188   my( $self, $payby ) = @_;
189   #$hash{$payby}->{payname} || $hash{$payby}->{shortname};
190   exists($hash{$payby}->{payname})
191     ? $hash{$payby}->{payname}
192     : $hash{$payby}->{shortname};
193 }
194
195 sub longname {
196   my( $self, $payby ) = @_;
197   $hash{$payby}->{longname};
198 }
199
200 %payby2bop = (
201   'CARD' => 'CC',
202   'CHEK' => 'ECHECK',
203   'MCRD' => 'CC', #?  but doesn't MCRD mean _offline_ card?  i think it got
204                   # overloaded for third-party card payments -- but no one is
205                   # doing those other than paypal now
206   'PPAL' => 'PAYPAL',
207 );
208
209 sub payby2bop {
210   my( $self, $payby ) = @_;
211   $payby2bop{ $self->payby2payment($payby) };
212 }
213
214 sub payby2payment {
215   my( $self, $payby ) = @_;
216   $hash{$payby}{'cust_pay'} || $payby;
217 }
218
219 sub cust_payby {
220   my $self = shift;
221   grep { ! exists $hash{$_}->{cust_main} } $self->payby;
222 }
223
224 sub cust_payby2shortname {
225   my $self = shift;
226   map { $_ => $hash{$_}->{shortname} } $self->cust_payby;
227 }
228
229 sub cust_payby2longname {
230   my $self = shift;
231   map { $_ => $hash{$_}->{longname} } $self->cust_payby;
232 }
233
234 =item payment_payby
235
236 Returns all values of payby that can be used by payments.
237
238 =cut
239
240 sub payment_payby {
241   my $self = shift;
242   grep { ! exists $hash{$_}->{cust_pay} } $self->payby;
243 }
244
245 =item payment_payby2longname
246
247 Returns hash, keys are L</payment_payby> types, values are payby longname.
248
249 =cut
250
251 sub payment_payby2longname {
252   my $self = shift;
253   map { $_ => $hash{$_}->{longname} } $self->payment_payby;
254 }
255
256 =item payment_payby2payname
257
258 Returns hash, keys are L</payment_payby> types, values are payby payname.
259
260 =cut
261
262 sub payment_payby2payname {
263   my $self = shift;
264   map { $_ => $self->payname($_) } $self->payment_payby;
265 }
266
267 =back
268
269 =head1 BUGS
270
271 This should eventually be an actual database table, and all tables that
272 currently have a char payby field should have a foreign key into here instead.
273
274 =head1 SEE ALSO
275
276 =cut
277
278 1;
279