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