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