echeck options to hide routing number and add account owner name, RT#13885
[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 #some false laziness w/MyAccount::process_payment
25
26 $cgi->param('custnum') =~ /^(\d+)$/
27   or die "illegal custnum ". $cgi->param('custnum');
28 my $custnum = $1;
29
30 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } );
31 die "unknown custnum $custnum" unless $cust_main;
32
33 $cgi->param('amount') =~ /^\s*(\d*(\.\d\d)?)\s*$/
34   or errorpage("illegal amount ". $cgi->param('amount'));
35 my $amount = $1;
36 errorpage("amount <= 0") unless $amount > 0;
37
38 if ( $cgi->param('fee') =~ /^\s*(\d*(\.\d\d)?)\s*$/ ) {
39   my $fee = $1;
40   $amount = sprintf('%.2f', $amount + $fee);
41 }
42
43 $cgi->param('year') =~ /^(\d+)$/
44   or errorpage("illegal year ". $cgi->param('year'));
45 my $year = $1;
46
47 $cgi->param('month') =~ /^(\d+)$/
48   or errorpage("illegal month ". $cgi->param('month'));
49 my $month = $1;
50
51 $cgi->param('payby') =~ /^(CARD|CHEK)$/
52   or errorpage("illegal payby ". $cgi->param('payby'));
53 my $payby = $1;
54 my %payby2fields = (
55   'CARD' => [ qw( address1 address2 city county state zip country ) ],
56   'CHEK' => [ qw( ss paytype paystate stateid stateid_state ) ],
57 );
58 my %type = ( 'CARD' => 'credit card',
59              'CHEK' => 'electronic check (ACH)',
60            );
61
62 $cgi->param('payname') =~ /^([\w \,\.\-\']+)$/
63   or errorpage(gettext('illegal_name'). " payname: ". $cgi->param('payname'));
64 my $payname = $1;
65
66 $cgi->param('payunique') =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
67   or errorpage(gettext('illegal_text'). " payunique: ". $cgi->param('payunique'));
68 my $payunique = $1;
69
70 $cgi->param('balance') =~ /^\s*(\-?\s*\d*(\.\d\d)?)\s*$/
71   or errorpage("illegal balance");
72 my $balance = $1;
73
74 my $payinfo;
75 my $paycvv = '';
76 if ( $payby eq 'CHEK' ) {
77
78   if ($cgi->param('payinfo1') =~ /xx/i || $cgi->param('payinfo2') =~ /xx/i ) {
79     $payinfo = $cust_main->payinfo;
80   } else {
81     $cgi->param('payinfo1') =~ /^(\d+)$/
82       or errorpage("illegal account number ". $cgi->param('payinfo1'));
83     my $payinfo1 = $1;
84     if ( $conf->exists('echeck-no_routing') ) {
85       $cgi->param('payinfo2') =~ /^(\d*)$/
86         or errorpage("illegal ABA/routing number ". $cgi->param('payinfo2'));
87     } else {
88       $cgi->param('payinfo2') =~ /^(\d+)$/
89         or errorpage("illegal ABA/routing number ". $cgi->param('payinfo2'));
90     }
91     my $payinfo2 = $1;
92     $payinfo = $payinfo1. '@'. $payinfo2;
93   }
94
95 } elsif ( $payby eq 'CARD' ) {
96
97   $payinfo = $cgi->param('payinfo');
98   if ($payinfo eq $cust_main->paymask) {
99     $payinfo = $cust_main->payinfo;
100   }
101   $payinfo =~ s/\D//g;
102   $payinfo =~ /^(\d{13,16}|\d{8,9})$/
103     or errorpage(gettext('invalid_card')); # . ": ". $self->payinfo;
104   $payinfo = $1;
105   validate($payinfo)
106     or errorpage(gettext('invalid_card')); # . ": ". $self->payinfo;
107
108   errorpage(gettext('unknown_card_type'))
109     if $payinfo !~ /^99\d{14}$/ #token
110     && cardtype($payinfo) eq "Unknown";
111
112   if ( defined $cust_main->dbdef_table->column('paycvv') ) {
113     if ( length($cgi->param('paycvv') ) ) {
114       if ( cardtype($payinfo) eq 'American Express card' ) {
115         $cgi->param('paycvv') =~ /^(\d{4})$/
116           or errorpage("CVV2 (CID) for American Express cards is four digits.");
117         $paycvv = $1;
118       } else {
119         $cgi->param('paycvv') =~ /^(\d{3})$/
120           or errorpage("CVV2 (CVC2/CID) is three digits.");
121         $paycvv = $1;
122       }
123     }
124   }
125
126 } else {
127   die "unknown payby $payby";
128 }
129
130 $cgi->param('discount_term') =~ /^\d*$/
131   or errorpage("illegal discount_term");
132 my $discount_term = $1;
133
134 my $error = '';
135 my $paynum = '';
136 if ( $cgi->param('batch') ) {
137
138   $error = 'Prepayment discounts not supported with batched payments' 
139     if $discount_term;
140
141   $error ||= $cust_main->batch_card(
142                                      'payby'    => $payby,
143                                      'amount'   => $amount,
144                                      'payinfo'  => $payinfo,
145                                      'paydate'  => "$year-$month-01",
146                                      'payname'  => $payname,
147                                      map { $_ => $cgi->param($_) } 
148                                        @{$payby2fields{$payby}}
149                                    );
150   errorpage($error) if $error;
151
152 } else {
153
154   $error = $cust_main->realtime_bop( $FS::payby::payby2bop{$payby}, $amount,
155     'quiet'      => 1,
156     'manual'     => 1,
157     'balance'    => $balance,
158     'payinfo'    => $payinfo,
159     'paydate'    => "$year-$month-01",
160     'payname'    => $payname,
161     'payunique'  => $payunique,
162     'paycvv'     => $paycvv,
163     'paynum_ref' => \$paynum,
164     'discount_term' => $discount_term,
165     map { $_ => $cgi->param($_) } @{$payby2fields{$payby}}
166   );
167   errorpage($error) if $error;
168
169   #no error, so order the fee package if applicable...
170   if ( $cgi->param('fee_pkgpart') =~ /^(\d+)$/ ) {
171
172     my $cust_pkg = new FS::cust_pkg { 'pkgpart' => $1 };
173
174     my $error = $cust_main->order_pkg( 'cust_pkg' => $cust_pkg );
175     errorpage("payment processed successfully, but error ordering fee: $error")
176       if $error;
177
178     #and generate an invoice for it now too
179     $error = $cust_main->bill( 'pkg_list' => [ $cust_pkg ] );
180     errorpage("payment processed and fee ordered sucessfully, but error billing fee: $error")
181       if $error;
182
183   }
184
185   $cust_main->apply_payments;
186
187 }
188
189 if ( $cgi->param('save') ) {
190   my $new = new FS::cust_main { $cust_main->hash };
191   if ( $payby eq 'CARD' ) { 
192     $new->set( 'payby' => ( $cgi->param('auto') ? 'CARD' : 'DCRD' ) );
193   } elsif ( $payby eq 'CHEK' ) {
194     $new->set( 'payby' => ( $cgi->param('auto') ? 'CHEK' : 'DCHK' ) );
195   } else {
196     die "unknown payby $payby";
197   }
198   $new->set( 'payinfo' => $cust_main->card_token || $payinfo );
199   $new->set( 'paydate' => "$year-$month-01" );
200   $new->set( 'payname' => $payname );
201
202   #false laziness w/FS:;cust_main::realtime_bop - check both to make sure
203   # working correctly
204   my $conf = new FS::Conf;
205   if ( $payby eq 'CARD' &&
206        grep { $_ eq cardtype($payinfo) } $conf->config('cvv-save') ) {
207     $new->set( 'paycvv' => $paycvv );
208   } else {
209     $new->set( 'paycvv' => '');
210   }
211
212   $new->set( $_ => $cgi->param($_) ) foreach @{$payby2fields{$payby}};
213
214   my $error = $new->replace($cust_main);
215   errorpage("payment processed successfully, but error saving info: $error")
216     if $error;
217   $cust_main = $new;
218 }
219
220 #success!
221
222 </%init>