RT#38363: use cust_payby when saving cards during payments
[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+)$/
46   or errorpage("illegal year ". $cgi->param('year'));
47 my $year = $1;
48
49 $cgi->param('month') =~ /^(\d+)$/
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 my $loaded_cust_payby;
80 if ( $payby eq 'CHEK' ) {
81
82   if ($cgi->param('payinfo1') =~ /xx/i || $cgi->param('payinfo2') =~ /xx/i ) {
83
84     my $search_paymask = $cgi->param('payinfo1') . '@' . $cgi->param('payinfo2');
85     $search_paymask .= '.' . $cgi->param('payinfo3')
86       if $conf->config('echeck-country') eq 'CA';
87
88     #paymask might not be saved in database, need to run paymask method for any potential match
89     foreach my $search_cust_payby ($cust_main->cust_payby('CHEK','DCHK')) {
90       if ($search_paymask eq $search_cust_payby->paymask) {
91         # if there are multiple matches, assume for now that it's the first one returned,
92         # since that's what auto-fills; it's unlikely a masked number would be entered by hand,
93         # but it's very likely users will just click-through what's been auto-filled
94         $loaded_cust_payby = $search_cust_payby;
95         last;
96       }
97     }
98     errorpage("Masked payinfo not found") unless $loaded_cust_payby;
99     $payinfo = $loaded_cust_payby->payinfo;
100     $paymask = $loaded_cust_payby->paymask;
101
102   } else {
103     $cgi->param('payinfo1') =~ /^(\d+)$/
104       or errorpage("Illegal account number ". $cgi->param('payinfo1'));
105     my $payinfo1 = $1;
106     $cgi->param('payinfo2') =~ /^(\d+)$/
107       or errorpage("Illegal ABA/routing number ". $cgi->param('payinfo2'));
108     my $payinfo2 = $1;
109     if ( $conf->config('echeck-country') eq 'CA' ) {
110       $cgi->param('payinfo3') =~ /^(\d{5})$/
111         or errorpage("Illegal branch number ". $cgi->param('payinfo2'));
112       $payinfo2 = "$1.$payinfo2";
113     }
114     $payinfo = $payinfo1. '@'. $payinfo2;
115   }
116
117 } elsif ( $payby eq 'CARD' ) {
118
119   $payinfo = $cgi->param('payinfo');
120   if ($payinfo =~ /xx/i) {
121
122     #paymask might not be saved in database, need to run paymask method for any potential match
123     foreach my $search_cust_payby ($cust_main->cust_payby('CARD','DCRD')) {
124       if ($payinfo eq $search_cust_payby->paymask) {
125         $loaded_cust_payby = $search_cust_payby;
126         last;
127       }
128     }
129
130     errorpage("Masked payinfo not found") unless $loaded_cust_payby;
131     $payinfo = $loaded_cust_payby->payinfo;
132     $paymask = $loaded_cust_payby->paymask;
133
134   }
135
136   $payinfo =~ s/\D//g;
137   $payinfo =~ /^(\d{13,16}|\d{8,9})$/
138     or errorpage(gettext('invalid_card')); # . ": ". $self->payinfo;
139   $payinfo = $1;
140   validate($payinfo)
141     or errorpage(gettext('invalid_card')); # . ": ". $self->payinfo;
142
143   errorpage(gettext('unknown_card_type'))
144     if $payinfo !~ /^99\d{14}$/ #token
145     && cardtype($payinfo) eq "Unknown";
146
147   if ( defined $cust_main->dbdef_table->column('paycvv') ) { #is this test necessary anymore?
148     if ( length($cgi->param('paycvv') ) ) {
149       if ( cardtype($payinfo) eq 'American Express card' ) {
150         $cgi->param('paycvv') =~ /^(\d{4})$/
151           or errorpage("CVV2 (CID) for American Express cards is four digits.");
152         $paycvv = $1;
153       } else {
154         $cgi->param('paycvv') =~ /^(\d{3})$/
155           or errorpage("CVV2 (CVC2/CID) is three digits.");
156         $paycvv = $1;
157       }
158     }elsif( $conf->exists('backoffice-require_cvv') ){
159       errorpage("CVV2 is required");
160     }
161   }
162
163 } else {
164   die "unknown payby $payby";
165 }
166
167 $cgi->param('discount_term') =~ /^(\d*)$/
168   or errorpage("illegal discount_term");
169 my $discount_term = $1;
170
171 # save first, for proper tokenization later
172 if ( $cgi->param('save') ) {
173
174   my %saveopt;
175   if ( $payby eq 'CARD' ) {
176     my $bill_location = FS::cust_location->new;
177     $bill_location->set( $_ => $cgi->param($_) )
178       foreach @{$payby2fields{$payby}};
179     $saveopt{'bill_location'} = $bill_location;
180     $saveopt{'paycvv'} = $paycvv; # save_cust_payby contains conf logic for when to use this
181     $saveopt{'paydate'} = "$year-$month-01";
182   } else {
183     # ss/stateid/stateid_state won't be saved, but should be harmless to pass
184     %saveopt = map { $_ => scalar($cgi->param($_)) } @{$payby2fields{$payby}};
185   }
186
187   my $error = $cust_main->save_cust_payby(
188     'payment_payby' => $payby,
189     'auto'          => scalar($cgi->param('auto')),
190     'payinfo'       => $payinfo,
191     'paymask'       => $paymask,
192     'payname'       => $payname,
193     %saveopt
194   );
195
196   errorpage("error saving info, payment not processed: $error")
197     if $error;  
198 }
199
200 my $error = '';
201 my $paynum = '';
202 if ( $cgi->param('batch') ) {
203
204   $error = 'Prepayment discounts not supported with batched payments' 
205     if $discount_term;
206
207   $error ||= $cust_main->batch_card(
208                                      'payby'    => $payby,
209                                      'amount'   => $amount,
210                                      'payinfo'  => $payinfo,
211                                      'paydate'  => "$year-$month-01",
212                                      'payname'  => $payname,
213                                      map { $_ => $cgi->param($_) } 
214                                        @{$payby2fields{$payby}}
215                                    );
216   errorpage($error) if $error;
217
218 } else {
219
220   $error = $cust_main->realtime_bop( $FS::payby::payby2bop{$payby}, $amount,
221     'quiet'      => 1,
222     'manual'     => 1,
223     'balance'    => $balance,
224     'payinfo'    => $payinfo,
225     'paymask'    => $paymask,
226     'paydate'    => "$year-$month-01",
227     'payname'    => $payname,
228     'payunique'  => $payunique,
229     'paycvv'     => $paycvv,
230     'paynum_ref' => \$paynum,
231     'discount_term' => $discount_term,
232     map { $_ => $cgi->param($_) } @{$payby2fields{$payby}}
233   );
234   errorpage($error) if $error;
235
236   #no error, so order the fee package if applicable...
237   if ( $cgi->param('fee_pkgpart') =~ /^(\d+)$/ ) {
238
239     my $cust_pkg = new FS::cust_pkg { 'pkgpart' => $1 };
240
241     my $error = $cust_main->order_pkg( 'cust_pkg' => $cust_pkg );
242     errorpage("payment processed successfully, but error ordering fee: $error")
243       if $error;
244
245     #and generate an invoice for it now too
246     $error = $cust_main->bill( 'pkg_list' => [ $cust_pkg ] );
247     errorpage("payment processed and fee ordered sucessfully, but error billing fee: $error")
248       if $error;
249
250   }
251
252   $cust_main->apply_payments;
253
254 }
255
256 #success!
257
258 </%init>