*** empty log message ***
[freeside.git] / site_perl / cust_pay_batch.pm
1 package FS::cust_pay_batch;
2
3 use strict;
4 use vars qw( @ISA );
5 use FS::Record;
6 use Business::CreditCard;
7
8 @ISA = qw( FS::Record );
9
10 =head1 NAME
11
12 FS::cust_pay_batch - Object methods for batch cards
13
14 =head1 SYNOPSIS
15
16   use FS::cust_pay_batch;
17
18   $record = new FS::cust_pay_batch \%hash;
19   $record = new FS::cust_pay_batch { 'column' => 'value' };
20
21   $error = $record->insert;
22
23   $error = $new_record->replace($old_record);
24
25   $error = $record->delete;
26
27   $error = $record->check;
28
29 =head1 DESCRIPTION
30
31 An FS::cust_pay_batch object represents a credit card transaction ready to be
32 batched (sent to a processor).  FS::cust_pay_batch inherits from FS::Record.  
33 Typically called by the collect method of an FS::cust_main object.  The
34 following fields are currently supported:
35
36 =over 4
37
38 =item trancode - 77 for charges
39
40 =item cardnum
41
42 =item exp - card expiration 
43
44 =item amount 
45
46 =item invnum - invoice
47
48 =item custnum - customer 
49
50 =item payname - name on card 
51
52 =item first - name 
53
54 =item last - name 
55
56 =item address1 
57
58 =item address2 
59
60 =item city 
61
62 =item state 
63
64 =item zip 
65
66 =item country 
67
68 =back
69
70 =head1 METHODS
71
72 =over 4
73
74 =item new HASHREF
75
76 Creates a new record.  To add the record to the database, see L<"insert">.
77
78 Note that this stores the hash reference, not a distinct copy of the hash it
79 points to.  You can ask the object for a copy with the I<hash> method.
80
81 =cut
82
83 sub table { 'cust_pay_batch'; }
84
85 =item insert
86
87 Adds this record to the database.  If there is an error, returns the error,
88 otherwise returns false.
89
90 =item delete
91
92 Delete this record from the database.  If there is an error, returns the error,
93 otherwise returns false.
94
95 =item replace OLD_RECORD
96
97 #inactive
98 #
99 #Replaces the OLD_RECORD with this one in the database.  If there is an error,
100 #returns the error, otherwise returns false.
101
102 =cut
103
104 sub replace {
105   return "Can't (yet?) replace batched transactions!";
106 }
107
108 =item check
109
110 Checks all fields to make sure this is a valid transaction.  If there is
111 an error, returns the error, otherwise returns false.  Called by the insert
112 and repalce methods.
113
114 =cut
115
116 sub check {
117   my $self = shift;
118
119   my $error = 
120       $self->ut_numbern('trancode')
121     || $self->ut_number('cardnum') 
122     || $self->ut_money('amount')
123     || $self->ut_number('invnum')
124     || $self->ut_number('custnum')
125     || $self->ut_text('address1')
126     || $self->ut_textn('address2')
127     || $self->ut_text('city')
128     || $self->ut_text('state')
129   ;
130
131   return $error if $error;
132
133   $self->getfield('last') =~ /^([\w \,\.\-\']+)$/ or return "Illegal last name";
134   $self->setfield('last',$1);
135
136   $self->first =~ /^([\w \,\.\-\']+)$/ or return "Illegal first name";
137   $self->first($1);
138
139   my $cardnum = $self->cardnum;
140   $cardnum =~ s/\D//g;
141   $cardnum =~ /^(\d{13,16})$/
142     or return "Illegal credit card number";
143   $cardnum = $1;
144   $self->cardnum($cardnum);
145   validate($cardnum) or return "Illegal credit card number";
146   return "Unknown card type" if cardtype($self->payinfo) eq "Unknown";
147
148   if ( $self->exp eq '' ) {
149     return "Expriation date required";
150     $self->exp('');
151   } else {
152     $self->exp =~ /^(\d{1,2})[\/\-](\d{2}(\d{2})?)$/
153       or return "Illegal expiration date";
154     if ( length($2) == 4 ) {
155       $self->exp("$2-$1-01");
156     } elsif ( $2 > 98 ) { #should pry change to check for "this year"
157       $self->exp("19$2-$1-01");
158     } else {
159       $self->exp("20$2-$1-01");
160     }
161   }
162
163   if ( $self->payname eq '' ) {
164     $self->payname( $self->first. " ". $self->getfield('last') );
165   } else {
166     $self->payname =~ /^([\w \,\.\-\']+)$/
167       or return "Illegal billing name";
168     $self->payname($1);
169   }
170
171   $self->zip =~ /^([\w\-]{10})$/ or return "Illegal zip";
172   $self->zip($1);
173
174   $self->country =~ /^(\w\w)$/ or return "Illegal \w\wy";
175   $self->country($1);
176
177   #check invnum, custnum, ?
178
179   ''; #no error
180 }
181
182 =back
183
184 =head1 VERSION
185
186 $Id: cust_pay_batch.pm,v 1.3 1998-12-29 11:59:44 ivan Exp $
187
188 =head1 BUGS
189
190 There should probably be a configuration file with a list of allowed credit
191 card types.
192
193 =head1 SEE ALSO
194
195 L<FS::cust_main>, L<FS::Record>
196
197 =head1 HISTORY
198
199 ivan@voicenet.com 97-jul-1
200
201 added hfields
202 ivan@sisd.com 97-nov-13
203
204 $Log: cust_pay_batch.pm,v $
205 Revision 1.3  1998-12-29 11:59:44  ivan
206 mostly properly OO, some work still to be done with svc_ stuff
207
208 Revision 1.2  1998/11/18 09:01:44  ivan
209 i18n! i18n!
210
211 Revision 1.1  1998/11/15 05:19:58  ivan
212 long overdue
213
214 Revision 1.3  1998/11/15 04:33:00  ivan
215 updates for newest versoin
216
217 Revision 1.2  1998/11/15 03:48:49  ivan
218 update for current version
219
220
221 =cut
222
223 1;
224