671cd710ab7e0950da35c83ff28a667730d0ec33
[freeside.git] / FS / FS / 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 paybatchnum - primary key (automatically assigned)
39
40 =item trancode - 77 for charges
41
42 =item cardnum
43
44 =item exp - card expiration 
45
46 =item amount 
47
48 =item invnum - invoice
49
50 =item custnum - customer 
51
52 =item payname - name on card 
53
54 =item first - name 
55
56 =item last - name 
57
58 =item address1 
59
60 =item address2 
61
62 =item city 
63
64 =item state 
65
66 =item zip 
67
68 =item country 
69
70 =back
71
72 =head1 METHODS
73
74 =over 4
75
76 =item new HASHREF
77
78 Creates a new record.  To add the record to the database, see L<"insert">.
79
80 Note that this stores the hash reference, not a distinct copy of the hash it
81 points to.  You can ask the object for a copy with the I<hash> method.
82
83 =cut
84
85 sub table { 'cust_pay_batch'; }
86
87 =item insert
88
89 Adds this record to the database.  If there is an error, returns the error,
90 otherwise returns false.
91
92 =item delete
93
94 Delete this record from the database.  If there is an error, returns the error,
95 otherwise returns false.
96
97 =item replace OLD_RECORD
98
99 #inactive
100 #
101 #Replaces the OLD_RECORD with this one in the database.  If there is an error,
102 #returns the error, otherwise returns false.
103
104 =cut
105
106 sub replace {
107   return "Can't (yet?) replace batched transactions!";
108 }
109
110 =item check
111
112 Checks all fields to make sure this is a valid transaction.  If there is
113 an error, returns the error, otherwise returns false.  Called by the insert
114 and repalce methods.
115
116 =cut
117
118 sub check {
119   my $self = shift;
120
121   my $error = 
122       $self->ut_numbern('paybatchnum')
123     || $self->ut_numbern('trancode')
124     || $self->ut_number('cardnum') 
125     || $self->ut_money('amount')
126     || $self->ut_number('invnum')
127     || $self->ut_number('custnum')
128     || $self->ut_text('address1')
129     || $self->ut_textn('address2')
130     || $self->ut_text('city')
131     || $self->ut_text('state')
132   ;
133
134   return $error if $error;
135
136   $self->getfield('last') =~ /^([\w \,\.\-\']+)$/ or return "Illegal last name";
137   $self->setfield('last',$1);
138
139   $self->first =~ /^([\w \,\.\-\']+)$/ or return "Illegal first name";
140   $self->first($1);
141
142   my $cardnum = $self->cardnum;
143   $cardnum =~ s/\D//g;
144   $cardnum =~ /^(\d{13,16})$/
145     or return "Illegal credit card number";
146   $cardnum = $1;
147   $self->cardnum($cardnum);
148   validate($cardnum) or return "Illegal credit card number";
149   return "Unknown card type" if cardtype($cardnum) eq "Unknown";
150
151   if ( $self->exp eq '' ) {
152     return "Expriation date required"; #unless 
153     $self->exp('');
154   } else {
155     if ( $self->exp =~ /^(\d{4})[\/\-](\d{1,2})[\/\-](\d{1,2})$/ ) {
156       $self->exp("$1-$2-$3");
157     } elsif ( $self->exp =~ /^(\d{1,2})[\/\-](\d{2}(\d{2})?)$/ ) {
158       if ( length($2) == 4 ) {
159         $self->exp("$2-$1-01");
160       } elsif ( $2 > 98 ) { #should pry change to check for "this year"
161         $self->exp("19$2-$1-01");
162       } else {
163         $self->exp("20$2-$1-01");
164       }
165     } else {
166       return "Illegal expiration date";
167     }
168   }
169
170   if ( $self->payname eq '' ) {
171     $self->payname( $self->first. " ". $self->getfield('last') );
172   } else {
173     $self->payname =~ /^([\w \,\.\-\']+)$/
174       or return "Illegal billing name";
175     $self->payname($1);
176   }
177
178   $self->zip =~ /^\s*(\w[\w\-\s]{3,8}\w)\s*$/
179     or return "Illegal zip: ". $self->zip;
180   $self->zip($1);
181
182   $self->country =~ /^(\w\w)$/ or return "Illegal country: ". $self->country;
183   $self->country($1);
184
185   #check invnum, custnum, ?
186
187   ''; #no error
188 }
189
190 =back
191
192 =head1 VERSION
193
194 $Id: cust_pay_batch.pm,v 1.3 2001-10-02 16:00:30 jeff Exp $
195
196 =head1 BUGS
197
198 There should probably be a configuration file with a list of allowed credit
199 card types.
200
201 =head1 SEE ALSO
202
203 L<FS::cust_main>, L<FS::Record>
204
205 =cut
206
207 1;
208