remove echeck-no_routing, never should have been there in the first place
[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 $paycvv = '';
78 if ( $payby eq 'CHEK' ) {
79
80   if ($cgi->param('payinfo1') =~ /xx/i || $cgi->param('payinfo2') =~ /xx/i ) {
81     $payinfo = $cust_main->payinfo;
82   } else {
83     $cgi->param('payinfo1') =~ /^(\d+)$/
84       or errorpage("illegal account number ". $cgi->param('payinfo1'));
85     my $payinfo1 = $1;
86     $cgi->param('payinfo2') =~ /^(\d+)$/
87       or errorpage("illegal ABA/routing number ". $cgi->param('payinfo2'));
88     my $payinfo2 = $1;
89     $payinfo = $payinfo1. '@'. $payinfo2;
90   }
91
92 } elsif ( $payby eq 'CARD' ) {
93
94   $payinfo = $cgi->param('payinfo');
95   if ($payinfo eq $cust_main->paymask) {
96     $payinfo = $cust_main->payinfo;
97   }
98   $payinfo =~ s/\D//g;
99   $payinfo =~ /^(\d{13,16}|\d{8,9})$/
100     or errorpage(gettext('invalid_card')); # . ": ". $self->payinfo;
101   $payinfo = $1;
102   validate($payinfo)
103     or errorpage(gettext('invalid_card')); # . ": ". $self->payinfo;
104
105   errorpage(gettext('unknown_card_type'))
106     if $payinfo !~ /^99\d{14}$/ #token
107     && cardtype($payinfo) eq "Unknown";
108
109   if ( defined $cust_main->dbdef_table->column('paycvv') ) {
110     if ( length($cgi->param('paycvv') ) ) {
111       if ( cardtype($payinfo) eq 'American Express card' ) {
112         $cgi->param('paycvv') =~ /^(\d{4})$/
113           or errorpage("CVV2 (CID) for American Express cards is four digits.");
114         $paycvv = $1;
115       } else {
116         $cgi->param('paycvv') =~ /^(\d{3})$/
117           or errorpage("CVV2 (CVC2/CID) is three digits.");
118         $paycvv = $1;
119       }
120     }
121   }
122
123 } else {
124   die "unknown payby $payby";
125 }
126
127 $cgi->param('discount_term') =~ /^\d*$/
128   or errorpage("illegal discount_term");
129 my $discount_term = $1;
130
131 my $error = '';
132 my $paynum = '';
133 if ( $cgi->param('batch') ) {
134
135   $error = 'Prepayment discounts not supported with batched payments' 
136     if $discount_term;
137
138   $error ||= $cust_main->batch_card(
139                                      'payby'    => $payby,
140                                      'amount'   => $amount,
141                                      'payinfo'  => $payinfo,
142                                      'paydate'  => "$year-$month-01",
143                                      'payname'  => $payname,
144                                      map { $_ => $cgi->param($_) } 
145                                        @{$payby2fields{$payby}}
146                                    );
147   errorpage($error) if $error;
148
149 } else {
150
151   $error = $cust_main->realtime_bop( $FS::payby::payby2bop{$payby}, $amount,
152     'quiet'      => 1,
153     'manual'     => 1,
154     'balance'    => $balance,
155     'payinfo'    => $payinfo,
156     'paydate'    => "$year-$month-01",
157     'payname'    => $payname,
158     'payunique'  => $payunique,
159     'paycvv'     => $paycvv,
160     'paynum_ref' => \$paynum,
161     'discount_term' => $discount_term,
162     map { $_ => $cgi->param($_) } @{$payby2fields{$payby}}
163   );
164   errorpage($error) if $error;
165
166   #no error, so order the fee package if applicable...
167   if ( $cgi->param('fee_pkgpart') =~ /^(\d+)$/ ) {
168
169     my $cust_pkg = new FS::cust_pkg { 'pkgpart' => $1 };
170
171     my $error = $cust_main->order_pkg( 'cust_pkg' => $cust_pkg );
172     errorpage("payment processed successfully, but error ordering fee: $error")
173       if $error;
174
175     #and generate an invoice for it now too
176     $error = $cust_main->bill( 'pkg_list' => [ $cust_pkg ] );
177     errorpage("payment processed and fee ordered sucessfully, but error billing fee: $error")
178       if $error;
179
180   }
181
182   $cust_main->apply_payments;
183
184 }
185
186 if ( $cgi->param('save') ) {
187   my $new = new FS::cust_main { $cust_main->hash };
188   if ( $payby eq 'CARD' ) { 
189     $new->set( 'payby' => ( $cgi->param('auto') ? 'CARD' : 'DCRD' ) );
190   } elsif ( $payby eq 'CHEK' ) {
191     $new->set( 'payby' => ( $cgi->param('auto') ? 'CHEK' : 'DCHK' ) );
192   } else {
193     die "unknown payby $payby";
194   }
195   $new->set( 'payinfo' => $cust_main->card_token || $payinfo );
196   $new->set( 'paydate' => "$year-$month-01" );
197   $new->set( 'payname' => $payname );
198
199   #false laziness w/FS:;cust_main::realtime_bop - check both to make sure
200   # working correctly
201   if ( $payby eq 'CARD' &&
202        grep { $_ eq cardtype($payinfo) } $conf->config('cvv-save') ) {
203     $new->set( 'paycvv' => $paycvv );
204   } else {
205     $new->set( 'paycvv' => '');
206   }
207
208   $new->set( $_ => $cgi->param($_) ) foreach @{$payby2fields{$payby}};
209
210   my $error = $new->replace($cust_main);
211   errorpage("payment processed successfully, but error saving info: $error")
212     if $error;
213   $cust_main = $new;
214 }
215
216 #success!
217
218 </%init>