RT# 81616 Mask CVV on Process Card Payment page
[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 #some false laziness w/MyAccount::process_payment
27
28 $cgi->param('custnum') =~ /^(\d+)$/
29   or die "illegal custnum ". $cgi->param('custnum');
30 my $custnum = $1;
31
32 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } );
33 die "unknown custnum $custnum" unless $cust_main;
34
35 $cgi->param('amount') =~ /^\s*(\d*(\.\d\d)?)\s*$/
36   or errorpage("illegal amount ". $cgi->param('amount'));
37 my $amount = $1;
38 errorpage("amount <= 0") unless $amount > 0;
39
40 if ( $cgi->param('fee') =~ /^\s*(\d*(\.\d\d)?)\s*$/ ) {
41   my $fee = $1;
42   $amount = sprintf('%.2f', $amount + $fee);
43 }
44
45 $cgi->param('year') =~ /^(\d{4})/
46   or errorpage("illegal year ". $cgi->param('year'));
47 my $year = $1;
48
49 $cgi->param('month') =~ /^(\d{2})/
50   or errorpage("illegal month ". $cgi->param('month'));
51 my $month = $1;
52
53 $cgi->param('payby') =~ /^(CARD|CHEK)$/
54   or errorpage("illegal payby ". $cgi->param('payby'));
55 my $payby = $1;
56 my %payby2fields = (
57   'CARD' => [ qw( address1 address2 city county state zip country ) ],
58   'CHEK' => [ qw( ss paytype paystate stateid stateid_state ) ],
59 );
60 my %type = ( 'CARD' => 'credit card',
61              'CHEK' => 'electronic check (ACH)',
62            );
63
64 $cgi->param('payname') =~ /^([\w \,\.\-\']+)$/
65   or errorpage(gettext('illegal_name'). " payname: ". $cgi->param('payname'));
66 my $payname = $1;
67
68 $cgi->param('payunique') =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
69   or errorpage(gettext('illegal_text'). " payunique: ". $cgi->param('payunique'));
70 my $payunique = $1;
71
72 $cgi->param('balance') =~ /^\s*(\-?\s*\d*(\.\d\d)?)\s*$/
73   or errorpage("illegal balance");
74 my $balance = $1;
75
76 my $payinfo;
77 my $paymask; # override only used by loaded cust payinfo, only implemented for realtime processing
78 my $paycvv = '';
79 if ( $payby eq 'CHEK' ) {
80
81   if ($cgi->param('payinfo1') =~ /xx/i || $cgi->param('payinfo2') =~ /xx/i ) {
82     $payinfo = $cust_main->payinfo;
83     $paymask = $cust_main->paymask;
84   } else {
85     $cgi->param('payinfo1') =~ /^(\d+)$/
86       or errorpage("Illegal account number ". $cgi->param('payinfo1'));
87     my $payinfo1 = $1;
88     $cgi->param('payinfo2') =~ /^(\d+)$/
89       or errorpage("Illegal ABA/routing number ". $cgi->param('payinfo2'));
90     my $payinfo2 = $1;
91     if ( $conf->config('echeck-country') eq 'CA' ) {
92       $cgi->param('payinfo3') =~ /^(\d{5})$/
93         or errorpage("Illegal branch number ". $cgi->param('payinfo2'));
94       $payinfo2 = "$1.$payinfo2";
95     }
96     $payinfo = $payinfo1. '@'. $payinfo2;
97   }
98
99 } elsif ( $payby eq 'CARD' ) {
100
101   $payinfo = $cgi->param('payinfo');
102   if ($payinfo eq $cust_main->paymask) {
103     $payinfo = $cust_main->payinfo;
104     $paymask = $cust_main->paymask;
105   }
106   $payinfo =~ s/\D//g;
107   $payinfo =~ /^(\d{13,16}|\d{8,9})$/
108     or errorpage(gettext('invalid_card'));
109   $payinfo = $1;
110   validate($payinfo)
111     or errorpage(gettext('invalid_card'));
112
113   unless ( $payinfo =~ /^99\d{14}$/ ) { #token
114
115     my $cardtype = cardtype($payinfo);
116
117     errorpage(gettext('unknown_card_type'))
118       if $cardtype eq "Unknown";
119
120     my %bop_card_types = map { $_=>1 } values %{ card_types() };
121     errorpage("$cardtype not accepted") unless $bop_card_types{$cardtype};
122
123   }
124
125   if ( defined $cust_main->dbdef_table->column('paycvv') ) {
126     if ( length($cgi->param('paycvv') ) ) {
127       if ( $cgi->param('paycvv') =~ /^\*+$/ ) {
128         $paycvv = $cust_main->paycvv;
129       } elsif ( cardtype($payinfo) eq 'American Express card' ) {
130         $cgi->param('paycvv') =~ /^(\d{4})$/
131           or errorpage("CVV2 (CID) for American Express cards is four digits.");
132         $paycvv = $1;
133       } else {
134         $cgi->param('paycvv') =~ /^(\d{3})$/
135           or errorpage("CVV2 (CVC2/CID) is three digits.");
136         $paycvv = $1;
137       }
138     }elsif( $conf->exists('backoffice-require_cvv') ){
139       errorpage("CVV2 is required");
140     }
141   }
142
143 } else {
144   die "unknown payby $payby";
145 }
146
147 $cgi->param('discount_term') =~ /^(\d*)$/
148   or errorpage("illegal discount_term");
149 my $discount_term = $1;
150
151 # save first, for proper tokenization later
152 if ( $cgi->param('save') ) {
153   my $new = new FS::cust_main { $cust_main->hash };
154   if ( $payby eq 'CARD' ) { 
155     $new->set( 'payby' => ( $cgi->param('auto') ? 'CARD' : 'DCRD' ) );
156   } elsif ( $payby eq 'CHEK' ) {
157     $new->set( 'payby' => ( $cgi->param('auto') ? 'CHEK' : 'DCHK' ) );
158   } else {
159     die "unknown payby $payby";
160   }
161   $new->payinfo($payinfo);             # sets default paymask, but not if it's already tokenized
162   $new->paymask($paymask) if $paymask; # in case it's been tokenized, override with loaded paymask
163   $new->set( 'paydate' => "$year-$month-01" );
164   $new->set( 'payname' => $payname );
165
166   #false laziness w/FS:;cust_main::realtime_bop - check both to make sure
167   # working correctly
168   if ( $payby eq 'CARD' &&
169        ( (grep { $_ eq cardtype($payinfo) } $conf->config('cvv-save')) 
170          || $conf->exists('business-onlinepayment-verification') 
171        )
172   ) {
173     $new->set( 'paycvv' => $paycvv );
174   } else {
175     $new->set( 'paycvv' => '');
176   }
177
178   if ( $payby eq 'CARD' ) {
179     my $bill_location = FS::cust_location->new;
180     $bill_location->set( $_ => scalar($cgi->param($_)) )
181       foreach @{$payby2fields{$payby}};
182     $new->set('bill_location' => $bill_location);
183     # will do nothing if the fields are all unchanged
184   } else {
185     $new->set( $_ => scalar($cgi->param($_)) ) foreach @{$payby2fields{$payby}};
186   }
187
188   my $error = $new->replace($cust_main);
189   errorpage("error saving info, payment not processed: $error")
190     if $error;
191   $cust_main = $new;
192 }
193
194 my $error = '';
195 my $paynum = '';
196 if ( $cgi->param('batch') ) {
197
198   $error = 'Prepayment discounts not supported with batched payments' 
199     if $discount_term;
200
201   $error ||= $cust_main->batch_card(
202                                      'payby'    => $payby,
203                                      'amount'   => $amount,
204                                      'payinfo'  => $payinfo,
205                                      'paydate'  => "$year-$month-01",
206                                      'payname'  => $payname,
207                                      map { $_ => scalar($cgi->param($_)) } 
208                                        @{$payby2fields{$payby}}
209                                    );
210   errorpage($error) if $error;
211
212 } else {
213
214   $error = $cust_main->realtime_bop( $FS::payby::payby2bop{$payby}, $amount,
215     'quiet'      => 1,
216     'manual'     => 1,
217     'balance'    => $balance,
218     'payinfo'    => $payinfo,
219     'paymask'    => $paymask,
220     'paydate'    => "$year-$month-01",
221     'payname'    => $payname,
222     'payunique'  => $payunique,
223     'paycvv'     => $paycvv,
224     'paynum_ref' => \$paynum,
225     'discount_term' => $discount_term,
226     'no_auto_apply' => ($cgi->param('apply') eq 'never') ? 'Y' : '',
227     'no_invnum'     => 1,
228     map { $_ => scalar($cgi->param($_)) } @{$payby2fields{$payby}}
229   );
230   errorpage($error) if $error;
231
232   #no error, so order the fee package if applicable...
233   if ( $cgi->param('fee_pkgpart') =~ /^(\d+)$/ ) {
234
235     my $cust_pkg = new FS::cust_pkg { 'pkgpart' => $1 };
236
237     my $error = $cust_main->order_pkg( 'cust_pkg' => $cust_pkg );
238     errorpage("payment processed successfully, but error ordering fee: $error")
239       if $error;
240
241     #and generate an invoice for it now too
242     $error = $cust_main->bill( 'pkg_list' => [ $cust_pkg ] );
243     errorpage("payment processed and fee ordered successfully, but error billing fee: $error")
244       if $error;
245
246   }
247
248   $cust_main->apply_payments if ($cgi->param('apply') eq 'yes');
249
250 }
251
252 #success!
253
254 </%init>