RT# 74435 - fixed errors in posting a echeck refund when no account is listed.
[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 $cgi->param('amount') =~ /^\s*(\d*(\.\d\d)?)\s*$/
43   or errorpage("illegal amount ". $cgi->param('amount'));
44 my $amount = $1;
45 errorpage("amount <= 0") unless $amount > 0;
46
47 if ( $cgi->param('fee') =~ /^\s*(\d*(\.\d\d)?)\s*$/ ) {
48   my $fee = $1;
49   $amount = sprintf('%.2f', $amount + $fee);
50 }
51
52 $cgi->param('payby') =~ /^(CARD|CHEK)$/
53   or errorpage("illegal payby ". $cgi->param('payby'));
54 my $payby = $1;
55 my %payby2fields = (
56   'CARD' => [ qw( address1 address2 city county state zip country ) ],
57   'CHEK' => [ qw( ss paytype paystate stateid stateid_state ) ],
58 );
59 my %type = ( 'CARD' => 'credit card',
60              'CHEK' => 'electronic check (ACH)',
61            );
62
63 $cgi->param('payunique') =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
64   or errorpage(gettext('illegal_text'). " payunique: ". $cgi->param('payunique'));
65 my $payunique = $1;
66
67 $cgi->param('balance') =~ /^\s*(\-?\s*\d*(\.\d\d)?)\s*$/
68   or errorpage("illegal balance");
69 my $balance = $1;
70
71 $cgi->param('discount_term') =~ /^(\d*)$/
72   or errorpage("illegal discount_term");
73 my $discount_term = $1;
74
75 my( $cust_payby, $payinfo, $paycvv, $month, $year, $payname );
76 my $paymask = '';
77 if ( (my $custpaybynum = scalar($cgi->param('custpaybynum'))) > 0 ) {
78
79   ##
80   # use stored cust_payby info
81   ##
82
83   $cust_payby = qsearchs('cust_payby', { custnum      => $custnum,
84                                             custpaybynum => $custpaybynum, } )
85     or die "unknown custpaybynum $custpaybynum";
86
87   # not needed for realtime_bop, but still needed for batch_card
88   $payinfo = $cust_payby->payinfo;
89   $paymask = $cust_payby->paymask;
90   $paycvv = $cust_payby->paycvv; # pass it if we got it, running a transaction will clear it
91   ( $month, $year ) = $cust_payby->paydate_mon_year;
92   $payname = $cust_payby->payname;
93   $cgi->param(-name=>"paytype", -value=>$cust_payby->paytype) unless $cgi->param("paytype");
94
95 } else {
96
97   ##
98   # use new info
99   ##
100
101   $cgi->param('year') =~ /^(\d+)$/
102     or errorpage("illegal year ". $cgi->param('year'));
103   $year = $1;
104
105   $cgi->param('month') =~ /^(\d+)$/
106     or errorpage("illegal month ". $cgi->param('month'));
107   $month = $1;
108
109   $cgi->param('payname') =~ /^([\w \,\.\-\']+)$/
110     or errorpage(gettext('illegal_name'). " payname: ". $cgi->param('payname'));
111   $payname = $1;
112
113   if ( $payby eq 'CHEK' ) {
114
115     $cgi->param('payinfo1') =~ /^(\d+)$/
116       or errorpage("Illegal account number ". $cgi->param('payinfo1'));
117     my $payinfo1 = $1;
118     $cgi->param('payinfo2') =~ /^(\d+)$/
119       or errorpage("Illegal ABA/routing number ". $cgi->param('payinfo2'));
120     my $payinfo2 = $1;
121     if ( $conf->config('echeck-country') eq 'CA' ) {
122       $cgi->param('payinfo3') =~ /^(\d{5})$/
123         or errorpage("Illegal branch number ". $cgi->param('payinfo2'));
124       $payinfo2 = "$1.$payinfo2";
125     }
126     $payinfo = $payinfo1. '@'. $payinfo2;
127
128   } elsif ( $payby eq 'CARD' ) {
129
130     $payinfo = $cgi->param('payinfo');
131
132     $payinfo =~ s/\D//g;
133     $payinfo =~ /^(\d{13,19}|\d{8,9})$/
134       or errorpage(gettext('invalid_card'));
135     $payinfo = $1;
136     validate($payinfo)
137       or errorpage(gettext('invalid_card'));
138
139     unless ( $cust_main->tokenized($payinfo) ) { #token
140
141       my $cardtype = cardtype($payinfo);
142
143       errorpage(gettext('unknown_card_type'))
144         if $cardtype eq "Unknown";
145
146       my %bop_card_types = map { $_=>1 } values %{ card_types() };
147       errorpage("$cardtype not accepted") unless $bop_card_types{$cardtype};
148
149     }
150
151     if ( length($cgi->param('paycvv') ) ) {
152       if ( cardtype($payinfo) eq 'American Express card' ) {
153         $cgi->param('paycvv') =~ /^(\d{4})$/
154           or errorpage("CVV2 (CID) for American Express cards is four digits.");
155         $paycvv = $1;
156       } else {
157         $cgi->param('paycvv') =~ /^(\d{3})$/
158           or errorpage("CVV2 (CVC2/CID) is three digits.");
159         $paycvv = $1;
160       }
161     } elsif ( $conf->exists('backoffice-require_cvv') ){
162       errorpage("CVV2 is required");
163     }
164
165   } else {
166     die "unknown payby $payby";
167   }
168
169   # save first, for proper tokenization
170   if ( $cgi->param('save') ) {
171
172     my %saveopt;
173     if ( $payby eq 'CARD' ) {
174       my $bill_location = FS::cust_location->new;
175       $bill_location->set( $_ => scalar($cgi->param($_)) )
176         foreach @{$payby2fields{$payby}};
177       $saveopt{'bill_location'} = $bill_location;
178       $saveopt{'paycvv'} = $paycvv; # save_cust_payby contains conf logic for when to use this
179       $saveopt{'paydate'} = "$year-$month-01";
180     } else {
181       # ss/stateid/stateid_state won't be saved, but should be harmless to pass
182       %saveopt = map { $_ => scalar($cgi->param($_)) } @{$payby2fields{$payby}};
183     }
184
185     my $error = $cust_main->save_cust_payby(
186       'saved_cust_payby' => \$cust_payby,
187       'payment_payby' => $payby,
188       'auto'          => scalar($cgi->param('auto')),
189       'weight'        => scalar($cgi->param('weight')),
190       'payinfo'       => $payinfo,
191       'payname'       => $payname,
192       %saveopt
193     );
194
195     errorpage("error saving info, payment not processed: $error")
196       if $error;        
197
198   } elsif ( $payby eq 'CARD' ) { # not saving
199
200     $paymask = FS::payinfo_Mixin->mask_payinfo('CARD',$payinfo); # for untokenized but tokenizable payinfo
201
202   }
203
204 }
205
206 ##
207 # now run the payment
208 ##
209
210 my $error = '';
211 my $paynum = '';
212 my $paydate;
213 if ($cust_payby->paydate) { $paydate = "$year-$month-01"; }
214 else { $paydate = "2037-12-01"; }
215
216 if ( $cgi->param('batch') ) {
217
218   $error = 'Prepayment discounts not supported with batched payments' 
219     if $discount_term;
220
221   $error ||= $cust_main->batch_card(
222                                      'payby'    => $payby,
223                                      'amount'   => $amount,
224                                      'payinfo'  => $payinfo,
225                                      'paydate'  => $paydate,
226                                      'payname'  => $payname,
227                                      map { $_ => scalar($cgi->param($_)) } 
228                                        @{$payby2fields{$payby}}
229                                    );
230   errorpage($error) if $error;
231
232 } else {
233
234   $error = $cust_main->realtime_bop( $FS::payby::payby2bop{$payby}, $amount,
235     'cust_payby' => $cust_payby, # if defined, will override passed payinfo, etc 
236     'quiet'      => 1,
237     'manual'     => 1,
238     'balance'    => $balance,
239     'payinfo'    => $payinfo,
240     'paymask'    => $paymask,
241     'paydate'    => "$year-$month-01",
242     'payname'    => $payname,
243     'payunique'  => $payunique,
244     'paycvv'     => $paycvv,
245     'paynum_ref' => \$paynum,
246     'discount_term' => $discount_term,
247     'no_auto_apply' => ($cgi->param('apply') eq 'never') ? 'Y' : '',
248     'no_invnum'     => 1,
249     map { $_ => scalar($cgi->param($_)) } @{$payby2fields{$payby}}
250   );
251   errorpage($error) if $error;
252
253   #no error, so order the fee package if applicable...
254   if ( $cgi->param('fee_pkgpart') =~ /^(\d+)$/ ) {
255
256     my $cust_pkg = new FS::cust_pkg { 'pkgpart' => $1 };
257
258     my $error = $cust_main->order_pkg( 'cust_pkg' => $cust_pkg );
259     errorpage("payment processed successfully, but error ordering fee: $error")
260       if $error;
261
262     #and generate an invoice for it now too
263     $error = $cust_main->bill( 'pkg_list' => [ $cust_pkg ] );
264     errorpage("payment processed and fee ordered successfully, but error billing fee: $error")
265       if $error;
266
267   }
268
269   $cust_main->apply_payments if ($cgi->param('apply') eq 'yes');
270
271 }
272
273 ##
274 # success!  step 3: profit!
275 ##
276
277 </%init>