communigate provisioning phase 2: add svc_domain.trailer -> communigate TrailerText...
[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     realtime  => 1,
52   },
53   'DCRD' => {
54     tinyname  => 'card',
55     shortname => 'Credit card',
56     longname  => 'Credit card (on-demand)',
57     cust_pay  => 'CARD', #this is a customer type only, payments are CARD...
58     realtime  => 1,
59   },
60   'CHEK' => {
61     tinyname  => 'check',
62     shortname => 'Electronic check',
63     longname  => 'Electronic check (automatic)',
64     realtime  => 1,
65   },
66   'DCHK' => {
67     tinyname  => 'check',
68     shortname => 'Electronic check',
69     longname  => 'Electronic check (on-demand)',
70     cust_pay  => 'CHEK', #this is a customer type only, payments are CHEK...
71     realtime  => 1,
72   },
73   'LECB' => {
74     tinyname  => 'phone bill',
75     shortname => 'Phone bill billing',
76     longname  => 'Phone bill billing',
77     realtime  => 1,
78   },
79   'BILL' => {
80     tinyname  => 'billing',
81     shortname => 'Billing',
82     payname   => 'Check',
83     longname  => 'Billing',
84   },
85   'PREP' => {
86     tinyname  => 'prepaid card',
87     shortname => 'Prepaid card',
88     longname  => 'Prepaid card',
89     cust_main => 'BILL', #this is a payment type only, customers go to BILL...
90   },
91   'CASH' => {
92     tinyname  => 'cash',
93     shortname => 'Cash', # initial payment, then billing
94     longname  => 'Cash',
95     cust_main => 'BILL', #this is a payment type only, customers go to BILL...
96   },
97   'WEST' => {
98     tinyname  => 'western union',
99     shortname => 'Western Union', # initial payment, then billing
100     longname  => 'Western Union',
101     cust_main => 'BILL', #this is a payment type only, customers go to BILL...
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 => 'BILL', #this is a payment type only, customers go to BILL...
108   },
109   'COMP' => {
110     tinyname  => 'comp',
111     shortname => 'Complimentary',
112     longname  => 'Complimentary',
113     cust_pay  => '', # (free) is depricated as a payment type in cust_pay
114   },
115   'CBAK' => {
116     tinyname  => 'chargeback',
117     shortname => 'Chargeback',
118     longname  => 'Chargeback',
119     cust_main => '', # not a customer type
120   },
121 ;
122
123 sub payby {
124   keys %hash;
125 }
126
127 sub can_payby {
128   my( $self, $table, $payby ) = @_;
129
130   #return "Illegal payby" unless $hash{$payby};
131   return 0 unless $hash{$payby};
132
133   $table = 'cust_pay' if $table =~ /^cust_(pay_pending|pay_batch|pay_void|refund)$/;
134   return 0 if exists( $hash{$payby}->{$table} );
135
136   return 1;
137 }
138
139 sub realtime {  # can use realtime payment facilities
140   my( $self, $payby ) = @_;
141
142   return 0 unless $hash{$payby};
143   return 0 unless exists( $hash{$payby}->{realtime} );
144
145   return $hash{$payby}->{realtime};
146 }
147
148 sub payby2longname {
149   my $self = shift;
150   map { $_ => $hash{$_}->{longname} } $self->payby;
151 }
152
153 sub shortname {
154   my( $self, $payby ) = @_;
155   $hash{$payby}->{shortname};
156 }
157
158 sub payname {
159   my( $self, $payby ) = @_;
160   #$hash{$payby}->{payname} || $hash{$payby}->{shortname};
161   exists($hash{$payby}->{payname})
162     ? $hash{$payby}->{payname}
163     : $hash{$payby}->{shortname};
164 }
165
166 sub longname {
167   my( $self, $payby ) = @_;
168   $hash{$payby}->{longname};
169 }
170
171 %payby2bop = (
172   'CARD' => 'CC',
173   'CHEK' => 'ECHECK',
174   'MCRD' => 'CC',
175 );
176
177 sub payby2bop {
178   my( $self, $payby ) = @_;
179   $payby2bop{ $self->payby2payment($payby) };
180 }
181
182 sub payby2payment {
183   my( $self, $payby ) = @_;
184   $hash{$payby}{'cust_pay'} || $payby;
185 }
186
187 sub cust_payby {
188   my $self = shift;
189   grep { ! exists $hash{$_}->{cust_main} } $self->payby;
190 }
191
192 sub cust_payby2longname {
193   my $self = shift;
194   map { $_ => $hash{$_}->{longname} } $self->cust_payby;
195 }
196
197 =back
198
199 =head1 BUGS
200
201 This should eventually be an actual database table, and all tables that
202 currently have a char payby field should have a foreign key into here instead.
203
204 =head1 SEE ALSO
205
206 =cut
207
208 1;
209