respect card-types config in backend (one-off payments), RT#39861
[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'));
139   $payinfo = $1;
140   validate($payinfo)
141     or errorpage(gettext('invalid_card'));
142
143   unless ( $self->payinfo =~ /^99\d{14}$/ ) { #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 ( defined $cust_main->dbdef_table->column('paycvv') ) { #is this test necessary anymore?
156     if ( length($cgi->param('paycvv') ) ) {
157       if ( cardtype($payinfo) eq 'American Express card' ) {
158         $cgi->param('paycvv') =~ /^(\d{4})$/
159           or errorpage("CVV2 (CID) for American Express cards is four digits.");
160         $paycvv = $1;
161       } else {
162         $cgi->param('paycvv') =~ /^(\d{3})$/
163           or errorpage("CVV2 (CVC2/CID) is three digits.");
164         $paycvv = $1;
165       }
166     }elsif( $conf->exists('backoffice-require_cvv') ){
167       errorpage("CVV2 is required");
168     }
169   }
170
171 } else {
172   die "unknown payby $payby";
173 }
174
175 $cgi->param('discount_term') =~ /^(\d*)$/
176   or errorpage("illegal discount_term");
177 my $discount_term = $1;
178
179 # save first, for proper tokenization later
180 if ( $cgi->param('save') ) {
181
182   my %saveopt;
183   if ( $payby eq 'CARD' ) {
184     my $bill_location = FS::cust_location->new;
185     $bill_location->set( $_ => $cgi->param($_) )
186       foreach @{$payby2fields{$payby}};
187     $saveopt{'bill_location'} = $bill_location;
188     $saveopt{'paycvv'} = $paycvv; # save_cust_payby contains conf logic for when to use this
189     $saveopt{'paydate'} = "$year-$month-01";
190   } else {
191     # ss/stateid/stateid_state won't be saved, but should be harmless to pass
192     %saveopt = map { $_ => scalar($cgi->param($_)) } @{$payby2fields{$payby}};
193   }
194
195   my $error = $cust_main->save_cust_payby(
196     'payment_payby' => $payby,
197     'auto'          => scalar($cgi->param('auto')),
198     'payinfo'       => $payinfo,
199     'paymask'       => $paymask,
200     'payname'       => $payname,
201     'paytype        => $paytype,
202     %saveopt
203   );
204
205   errorpage("error saving info, payment not processed: $error")
206     if $error;  
207 }
208
209 my $error = '';
210 my $paynum = '';
211 if ( $cgi->param('batch') ) {
212
213   $error = 'Prepayment discounts not supported with batched payments' 
214     if $discount_term;
215
216   $error ||= $cust_main->batch_card(
217                                      'payby'    => $payby,
218                                      'amount'   => $amount,
219                                      'payinfo'  => $payinfo,
220                                      'paydate'  => "$year-$month-01",
221                                      'payname'  => $payname,
222                                      map { $_ => $cgi->param($_) } 
223                                        @{$payby2fields{$payby}}
224                                    );
225   errorpage($error) if $error;
226
227 } else {
228
229   $error = $cust_main->realtime_bop( $FS::payby::payby2bop{$payby}, $amount,
230     'quiet'      => 1,
231     'manual'     => 1,
232     'balance'    => $balance,
233     'payinfo'    => $payinfo,
234     'paymask'    => $paymask,
235     'paydate'    => "$year-$month-01",
236     'payname'    => $payname,
237     'payunique'  => $payunique,
238     'paycvv'     => $paycvv,
239     'paynum_ref' => \$paynum,
240     'discount_term' => $discount_term,
241     map { $_ => $cgi->param($_) } @{$payby2fields{$payby}}
242   );
243   errorpage($error) if $error;
244
245   #no error, so order the fee package if applicable...
246   if ( $cgi->param('fee_pkgpart') =~ /^(\d+)$/ ) {
247
248     my $cust_pkg = new FS::cust_pkg { 'pkgpart' => $1 };
249
250     my $error = $cust_main->order_pkg( 'cust_pkg' => $cust_pkg );
251     errorpage("payment processed successfully, but error ordering fee: $error")
252       if $error;
253
254     #and generate an invoice for it now too
255     $error = $cust_main->bill( 'pkg_list' => [ $cust_pkg ] );
256     errorpage("payment processed and fee ordered sucessfully, but error billing fee: $error")
257       if $error;
258
259   }
260
261   $cust_main->apply_payments;
262
263 }
264
265 #success!
266
267 </%init>