f40a0497073fd4ce91694ee56edf420d73a1eb9f
[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 Exporter;
6 #use FS::UID qw(getotaker);
7 use FS::Record qw(hfields qsearch qsearchs);
8 use Business::CreditCard;
9
10 @ISA = qw(FS::Record);
11
12 =head1 NAME
13
14 FS::cust_pay_batdh - Object methods for batch cards
15
16 =head1 SYNOPSIS
17
18   use FS::cust_pay_batch;
19
20   $record = create FS::cust_pay_batch \%hash;
21   $record = create FS::cust_pay_batch { 'column' => 'value' };
22
23   $error = $record->insert;
24
25   $error = $new_record->replace($old_record);
26
27   $error = $record->delete;
28
29   $error = $record->check;
30
31 =head1 DESCRIPTION
32
33 An FS::cust_pay_batch object represents a credit card transaction ready to be
34 batched (sent to a processor).  FS::cust_pay_batch inherits from FS::Record.  
35 Typically called by the collect method of an FS::cust_main object.  The
36 following fields are currently supported:
37
38 =over 4
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 create 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 create {
86   my($proto,$hashref)=@_;
87
88   $proto->new('cust_pay_batch',$hashref);
89
90 }
91
92 =item insert
93
94 Adds this record to the database.  If there is an error, returns the error,
95 otherwise returns false.
96
97 =cut
98
99 sub insert {
100   my($self)=@_;
101
102   #local $SIG{HUP} = 'IGNORE';
103   #local $SIG{INT} = 'IGNORE';
104   #local $SIG{QUIT} = 'IGNORE';
105   #local $SIG{TERM} = 'IGNORE';
106   #local $SIG{TSTP} = 'IGNORE';
107
108   $self->check or
109   $self->add;
110 }
111
112 =item delete
113
114 #inactive
115 #
116 #Delete this record from the database.
117
118 =cut
119
120 sub delete {
121   my($self)=@_;
122
123   #$self->del;
124
125   return "Can't (yet?) delete batched transactions!";
126 }
127
128 =item replace OLD_RECORD
129
130 #inactive
131 #
132 #Replaces the OLD_RECORD with this one in the database.  If there is an error,
133 #returns the error, otherwise returns false.
134
135 =cut
136
137 sub replace {
138   my($new,$old)=@_;
139   #return "(Old) Not a table_name record!" unless $old->table eq "table_name";
140   #
141   #return "Can't change keyfield!"
142   #   unless $old->getfield('keyfield') eq $new->getfield('keyfield');
143   #
144   #$new->check or
145   #$new->rep($old);
146
147   return "Can't (yet?) replace batched transactions!";
148 }
149
150 =item check
151
152 Checks all fields to make sure this is a valid example.  If there is
153 an error, returns the error, otherwise returns false.  Called by the insert
154 and repalce methods.
155
156 =cut
157
158 sub check {
159   my($self)=@_;
160   return "Not a cust_pay_batch record!" unless $self->table eq "cust_pay_batch";
161
162   my $error = 
163       $self->ut_numbern('trancode')
164     or $self->ut_number('cardnum') 
165     or $self->ut_money('amount')
166     or $self->ut_number('invnum')
167     or $self->ut_number('custnum')
168     or $self->ut_text('address1')
169     or $self->ut_textn('address2')
170     or $self->ut_text('city')
171     or $self->ut_text('state')
172   ;
173
174   return $error if $error;
175
176   $self->getfield('last') =~ /^([\w \,\.\-\']+)$/ or return "Illegal last name";
177   $self->setfield('last',$1);
178
179   $self->first =~ /^([\w \,\.\-\']+)$/ or return "Illegal first name";
180   $self->first($1);
181
182   my $cardnum = $self->cardnum;
183   $cardnum =~ s/\D//g;
184   $cardnum =~ /^(\d{13,16})$/
185     or return "Illegal credit card number";
186   $cardnum = $1;
187   $self->cardnum($cardnum);
188   validate($cardnum) or return "Illegal credit card number";
189   my $type = cardtype($cardnum);
190   return "Unknown credit card type"
191     unless ( $type =~ /^VISA/ ||
192              $type =~ /^MasterCard/ ||
193              $type =~ /^American Express/ ||
194              $type =~ /^Discover/ );
195
196   if ( $self->exp eq '' ) {
197     return "Expriation date required";
198     $self->exp('');
199   } else {
200     $self->exp =~ /^(\d{1,2})[\/\-](\d{2}(\d{2})?)$/
201       or return "Illegal expiration date";
202     if ( length($2) == 4 ) {
203       $self->exp("$2-$1-01");
204     } elsif ( $2 > 98 ) { #should pry change to check for "this year"
205       $self->exp("19$2-$1-01");
206     } else {
207       $self->exp("20$2-$1-01");
208     }
209   }
210
211   if ( $self->payname eq '' ) {
212     $self->payname( $self->first. " ". $self->getfield('last') );
213   } else {
214     $self->payname =~ /^([\w \,\.\-\']+)$/
215       or return "Illegal billing name";
216     $self->payname($1);
217   }
218
219   $self->zip =~ /^([\w\-]{10})$/ or return "Illegal zip";
220   $self->zip($1);
221
222   $self->country =~ /^(\w\w)$/ or return "Illegal \w\wy";
223   $self->country($1);
224
225   #check invnum, custnum, ?
226
227   ''; #no error
228 }
229
230 =back
231
232 =head1 VERSION
233
234 $Id: cust_pay_batch.pm,v 1.2 1998-11-18 09:01:44 ivan Exp $
235
236 =head1 BUGS
237
238 =head1 SEE ALSO
239
240 L<FS::cust_main>, L<FS::Record>
241
242 =head1 HISTORY
243
244 ivan@voicenet.com 97-jul-1
245
246 added hfields
247 ivan@sisd.com 97-nov-13
248
249 $Log: cust_pay_batch.pm,v $
250 Revision 1.2  1998-11-18 09:01:44  ivan
251 i18n! i18n!
252
253 Revision 1.1  1998/11/15 05:19:58  ivan
254 long overdue
255
256 Revision 1.3  1998/11/15 04:33:00  ivan
257 updates for newest versoin
258
259 Revision 1.2  1998/11/15 03:48:49  ivan
260 update for current version
261
262
263 =cut
264
265 1;
266