RT# 76093 - Added ability to charge a processing fee when taking a payment on the...
[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 = $cust_main->save_cust_payby(
190       'saved_cust_payby' => \$cust_payby,
191       'payment_payby' => $payby,
192       'auto'          => scalar($cgi->param('auto')),
193       'weight'        => scalar($cgi->param('weight')),
194       'payinfo'       => $payinfo,
195       'payname'       => $payname,
196       %saveopt
197     );
198
199     errorpage("error saving info, payment not processed: $error")
200       if $error;        
201
202   } elsif ( $payby eq 'CARD' ) { # not saving
203
204     $paymask = FS::payinfo_Mixin->mask_payinfo('CARD',$payinfo); # for untokenized but tokenizable payinfo
205
206   }
207
208 }
209
210 ##
211 # now run the payment
212 ##
213
214 my $error = '';
215 my $paynum = '';
216
217 if ( $cgi->param('batch') ) {
218
219   $error = 'Prepayment discounts not supported with batched payments' 
220     if $discount_term;
221
222   # Invalid payment expire dates are replaced with 2037-12-01 (why?)
223   my $paydate = "${year}-${month}-01";
224   {
225     use DateTime;
226     local $@;
227     eval { DateTime->new({ year => $year, month => $month, day => 1 }) };
228     $paydate = '2037-12-01' if $@;
229   }
230
231   $error ||= $cust_main->batch_card(
232                                      'payby'    => $payby,
233                                      'amount'   => $amount,
234                                      'payinfo'  => $payinfo,
235                                      'paydate'  => $paydate,
236                                      'payname'  => $payname,
237                                      'invnum'   => $invoice,
238                                      'processing-fee' => $processing_fee,
239                                      map { $_ => scalar($cgi->param($_)) } 
240                                        @{$payby2fields{$payby}}
241                                    );
242   errorpage($error) if $error;
243
244 } else {
245
246   $error = $cust_main->realtime_bop( $FS::payby::payby2bop{$payby}, $amount,
247     'cust_payby' => $cust_payby, # if defined, will override passed payinfo, etc 
248     'quiet'      => 1,
249     'manual'     => 1,
250     'balance'    => $balance,
251     'payinfo'    => $payinfo,
252     'paymask'    => $paymask,
253     'paydate'    => "$year-$month-01",
254     'payname'    => $payname,
255     'payunique'  => $payunique,
256     'paycvv'     => $paycvv,
257     'paynum_ref' => \$paynum,
258     'discount_term' => $discount_term,
259     'no_auto_apply' => ($cgi->param('apply') eq 'never') ? 'Y' : '',
260     'no_invnum'     => 1,
261     'invnum'        => $invoice,
262     'processing-fee' => $processing_fee,
263     map { $_ => scalar($cgi->param($_)) } @{$payby2fields{$payby}}
264   );
265   errorpage($error) if $error;
266
267   #no error, so order the fee package if applicable...
268   if ( $cgi->param('fee_pkgpart') =~ /^(\d+)$/ ) {
269
270     my $cust_pkg = new FS::cust_pkg { 'pkgpart' => $1 };
271
272     my $error = $cust_main->order_pkg( 'cust_pkg' => $cust_pkg );
273     errorpage("payment processed successfully, but error ordering fee: $error")
274       if $error;
275
276     #and generate an invoice for it now too
277     $error = $cust_main->bill( 'pkg_list' => [ $cust_pkg ] );
278     errorpage("payment processed and fee ordered successfully, but error billing fee: $error")
279       if $error;
280
281   }
282
283   $cust_main->apply_payments if ($cgi->param('apply') eq 'yes');
284
285 }
286
287 ##
288 # success!  step 3: profit!
289 ##
290
291 </%init>