default to a session cookie instead of setting an explicit timeout, weird timezone...
[freeside.git] / httemplate / misc / process / payment.cgi
1 % if ( $cgi->param('batch') ) {
2
3   <% include( '/elements/header.html', ucfirst($type{$payby}). ' processing successful',
4                  include('/elements/menubar.html'),
5
6             )
7   %>
8
9   <% include( '/elements/small_custview.html', $cust_main, '', '', popurl(3). "view/cust_main.cgi" ) %>
10
11   <% include('/elements/footer.html') %>
12
13 % #2.5/2.7?# } elsif ( $curuser->access_right('View payments') ) {
14 % } elsif ( $curuser->access_right(['View invoices', 'View payments']) ) {
15 <% $cgi->redirect(popurl(3). "view/cust_pay.html?paynum=$paynum" ) %>
16 % } else {
17 <% $cgi->redirect(popurl(3). "view/cust_main.html?custnum=$custnum" ) %>
18 % }
19 <%init>
20
21 my $curuser = $FS::CurrentUser::CurrentUser;
22 die "access denied" unless $curuser->access_right('Process payment');
23
24 my $conf = new FS::Conf;
25
26 ##
27 # info for all payments, stored or unstored
28 ##
29
30 #some false laziness w/MyAccount::process_payment
31
32 $cgi->param('custnum') =~ /^(\d+)$/
33   or die "illegal custnum ". $cgi->param('custnum');
34 my $custnum = $1;
35
36 my $cust_main = qsearchs({
37   'table'     => 'cust_main',
38   'hashref'   => { 'custnum' => $custnum },
39   'extra_sql' => ' AND '. $curuser->agentnums_sql,
40 }) or die "unknown custnum $custnum";
41
42 my $invoice = ($cgi->param('invoice') =~ /^(\d+)$/) ? $cgi->param('invoice') : '';
43
44 my $processing_fee = $cgi->param('processing_fee') ? $cgi->param('processing_fee') : '';
45
46 $cgi->param('amount') =~ /^\s*(\d*(\.\d\d)?)\s*$/
47   or errorpage("illegal amount ". $cgi->param('amount'));
48 my $amount = $1;
49 errorpage("amount <= 0") unless $amount > 0;
50
51 if ( $cgi->param('fee') =~ /^\s*(\d*(\.\d\d)?)\s*$/ ) {
52   my $fee = $1;
53   $amount = sprintf('%.2f', $amount + $fee);
54 }
55
56 $cgi->param('payby') =~ /^(CARD|CHEK)$/
57   or errorpage("illegal payby ". $cgi->param('payby'));
58 my $payby = $1;
59 my %payby2fields = (
60   'CARD' => [ qw( address1 address2 city county state zip country ) ],
61   'CHEK' => [ qw( ss paytype paystate stateid stateid_state ) ],
62 );
63 my %type = ( 'CARD' => 'credit card',
64              'CHEK' => 'electronic check (ACH)',
65            );
66
67 $cgi->param('payunique') =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
68   or errorpage(gettext('illegal_text'). " payunique: ". $cgi->param('payunique'));
69 my $payunique = $1;
70
71 $cgi->param('balance') =~ /^\s*(\-?\s*\d*(\.\d\d)?)\s*$/
72   or errorpage("illegal balance");
73 my $balance = $1;
74
75 $cgi->param('discount_term') =~ /^(\d*)$/
76   or errorpage("illegal discount_term");
77 my $discount_term = $1;
78
79 my( $cust_payby, $payinfo, $paycvv, $month, $year, $payname );
80 my $paymask = '';
81 if ( (my $custpaybynum = scalar($cgi->param('custpaybynum'))) > 0 ) {
82
83   ##
84   # use stored cust_payby info
85   ##
86
87   $cust_payby = qsearchs('cust_payby', { custnum      => $custnum,
88                                             custpaybynum => $custpaybynum, } )
89     or die "unknown custpaybynum $custpaybynum";
90
91   # not needed for realtime_bop, but still needed for batch_card
92   $payinfo = $cust_payby->payinfo;
93   $paymask = $cust_payby->paymask;
94   $paycvv = $cust_payby->paycvv; # pass it if we got it, running a transaction will clear it
95   ( $month, $year ) = $cust_payby->paydate_mon_year;
96   $payname = $cust_payby->payname;
97   $cgi->param(-name=>"paytype", -value=>$cust_payby->paytype) unless $cgi->param("paytype");
98
99 } else {
100
101   ##
102   # use new info
103   ##
104
105   $cgi->param('year') =~ /^(\d{4})/
106     or errorpage("illegal year ". $cgi->param('year'));
107   $year = $1;
108
109   $cgi->param('month') =~ /^(\d{2})/
110     or errorpage("illegal month ". $cgi->param('month'));
111   $month = $1;
112
113   $cgi->param('payname') =~ /^([\w \,\.\-\']+)$/
114     or errorpage(gettext('illegal_name'). " payname: ". $cgi->param('payname'));
115   $payname = $1;
116
117   if ( $payby eq 'CHEK' ) {
118
119     $cgi->param('payinfo1') =~ /^(\d+)$/
120       or errorpage("Illegal account number ". $cgi->param('payinfo1'));
121     my $payinfo1 = $1;
122     $cgi->param('payinfo2') =~ /^(\d+)$/
123       or errorpage("Illegal ABA/routing number ". $cgi->param('payinfo2'));
124     my $payinfo2 = $1;
125     if ( $conf->config('echeck-country') eq 'CA' ) {
126       $cgi->param('payinfo3') =~ /^(\d{5})$/
127         or errorpage("Illegal branch number ". $cgi->param('payinfo2'));
128       $payinfo2 = "$1.$payinfo2";
129     }
130     $payinfo = $payinfo1. '@'. $payinfo2;
131
132   } elsif ( $payby eq 'CARD' ) {
133
134     $payinfo = $cgi->param('payinfo');
135
136     $payinfo =~ s/\D//g;
137     $payinfo =~ /^(\d{13,19}|\d{8,9})$/
138       or errorpage(gettext('invalid_card'));
139     $payinfo = $1;
140     validate($payinfo)
141       or errorpage(gettext('invalid_card'));
142
143     unless ( $cust_main->tokenized($payinfo) ) { #token
144
145       my $cardtype = cardtype($payinfo);
146
147       errorpage(gettext('unknown_card_type'))
148         if $cardtype eq "Unknown";
149
150       my %bop_card_types = map { $_=>1 } values %{ card_types() };
151       errorpage("$cardtype not accepted") unless $bop_card_types{$cardtype};
152
153     }
154
155     if ( length($cgi->param('paycvv') ) ) {
156       if ( cardtype($payinfo) eq 'American Express card' ) {
157         $cgi->param('paycvv') =~ /^(\d{4})$/
158           or errorpage("CVV2 (CID) for American Express cards is four digits.");
159         $paycvv = $1;
160       } else {
161         $cgi->param('paycvv') =~ /^(\d{3})$/
162           or errorpage("CVV2 (CVC2/CID) is three digits.");
163         $paycvv = $1;
164       }
165     } elsif ( $conf->exists('backoffice-require_cvv') ){
166       errorpage("CVV2 is required");
167     }
168
169   } else {
170     die "unknown payby $payby";
171   }
172
173   # save first, for proper tokenization
174   if ( $cgi->param('save') ) {
175
176     my %saveopt;
177     if ( $payby eq 'CARD' ) {
178       my $bill_location = FS::cust_location->new;
179       $bill_location->set( $_ => scalar($cgi->param($_)) )
180         foreach @{$payby2fields{$payby}};
181       $saveopt{'bill_location'} = $bill_location;
182       $saveopt{'paycvv'} = $paycvv; # save_cust_payby contains conf logic for when to use this
183       $saveopt{'paydate'} = "$year-$month-01";
184     } else {
185       # ss/stateid/stateid_state won't be saved, but should be harmless to pass
186       %saveopt = map { $_ => scalar($cgi->param($_)) } @{$payby2fields{$payby}};
187     }
188
189     my $error;
190     {
191       local $@;
192       eval {
193         $error = $cust_main->save_cust_payby(
194           'saved_cust_payby' => \$cust_payby,
195           'payment_payby' => $payby,
196           'auto'          => scalar($cgi->param('auto')),
197           'weight'        => scalar($cgi->param('weight')),
198           'payinfo'       => $payinfo,
199           'payname'       => $payname,
200           %saveopt
201         );
202       };
203       $error ||= $@;
204     }
205
206     errorpage("error saving info, payment not processed: $error")
207       if $error;        
208
209   } elsif ( $payby eq 'CARD' ) { # not saving
210
211     $paymask = FS::payinfo_Mixin->mask_payinfo('CARD',$payinfo); # for untokenized but tokenizable payinfo
212
213   }
214
215 }
216
217 ##
218 # now run the payment
219 ##
220
221 my $error = '';
222 my $paynum = '';
223
224 if ( $cgi->param('batch') ) {
225
226   $error = 'Prepayment discounts not supported with batched payments' 
227     if $discount_term;
228
229   # Invalid payment expire dates are replaced with 2037-12-01 (why?)
230   my $paydate = "${year}-${month}-01";
231   {
232     use DateTime;
233     local $@;
234     eval { DateTime->new({ year => $year, month => $month, day => 1 }) };
235     $paydate = '2037-12-01' if $@;
236   }
237
238   $error ||= $cust_main->batch_card(
239                                      'payby'    => $payby,
240                                      'amount'   => $amount,
241                                      'payinfo'  => $payinfo,
242                                      'paydate'  => $paydate,
243                                      'payname'  => $payname,
244                                      'invnum'   => $invoice,
245                                      'processing-fee' => $processing_fee,
246                                      map { $_ => scalar($cgi->param($_)) } 
247                                        @{$payby2fields{$payby}}
248                                    );
249   errorpage($error) if $error;
250
251 } else {
252
253   $error = $cust_main->realtime_bop( $FS::payby::payby2bop{$payby}, $amount,
254     'cust_payby' => $cust_payby, # if defined, will override passed payinfo, etc 
255     'quiet'      => 1,
256     'manual'     => 1,
257     'balance'    => $balance,
258     'payinfo'    => $payinfo,
259     'paymask'    => $paymask,
260     'paydate'    => "$year-$month-01",
261     'payname'    => $payname,
262     'payunique'  => $payunique,
263     'paycvv'     => $paycvv,
264     'paynum_ref' => \$paynum,
265     'discount_term' => $discount_term,
266     'no_auto_apply' => ($cgi->param('apply') eq 'never') ? 'Y' : '',
267     'no_invnum'     => 1,
268     'invnum'        => $invoice,
269     'processing-fee' => $processing_fee,
270     map { $_ => scalar($cgi->param($_)) } @{$payby2fields{$payby}}
271   );
272   errorpage($error) if $error;
273
274   #no error, so order the fee package if applicable...
275   if ( $cgi->param('fee_pkgpart') =~ /^(\d+)$/ ) {
276
277     my $cust_pkg = new FS::cust_pkg { 'pkgpart' => $1 };
278
279     my $error = $cust_main->order_pkg( 'cust_pkg' => $cust_pkg );
280     errorpage("payment processed successfully, but error ordering fee: $error")
281       if $error;
282
283     #and generate an invoice for it now too
284     $error = $cust_main->bill( 'pkg_list' => [ $cust_pkg ] );
285     errorpage("payment processed and fee ordered successfully, but error billing fee: $error")
286       if $error;
287
288   }
289
290   $cust_main->apply_payments if ($cgi->param('apply') eq 'yes');
291
292 }
293
294 ##
295 # success!  step 3: profit!
296 ##
297
298 </%init>