RT# 74435 - fixed errors in posting a echeck refund when no account is listed.
[freeside.git] / httemplate / edit / process / cust_refund.cgi
1 %if ( $error ) {
2 %  $cgi->param('error', $error);
3 <% $cgi->redirect(popurl(2). "cust_refund.cgi?". $cgi->query_string ) %>
4 %} else {
5 %
6 %  if ( $link eq 'popup' ) {
7 %
8 <& /elements/header-popup.html, 'Refund entered' &>
9     <SCRIPT TYPE="text/javascript">
10       topreload();
11     </SCRIPT>
12
13     </BODY></HTML>
14 %  } else {
15 <% $cgi->redirect(popurl(3). "view/cust_main.cgi?custnum=$custnum;show=payment_history") %>
16 %  }
17 %}
18 <%init>
19
20 die "access denied"
21   unless $FS::CurrentUser::CurrentUser->access_right('Refund payment')
22       || $FS::CurrentUser::CurrentUser->access_right('Post refund');
23
24 my $conf = new FS::Conf;
25
26 $cgi->param('custnum') =~ /^(\d*)$/ or die "Illegal custnum!";
27 my $custnum = $1;
28 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
29   or die "unknown custnum $custnum";
30
31 my $link    = $cgi->param('popup') ? 'popup' : '';
32
33 my $payby = $cgi->param('payby');
34
35 die "access denied"
36   unless $FS::CurrentUser::CurrentUser->refund_access_right($payby);
37
38 $cgi->param('reasonnum') =~ /^(-?\d+)$/ or die "Illegal reasonnum";
39 my ($reasonnum, $error) = $m->comp('/misc/process/elements/reason');
40 $cgi->param('reasonnum', $reasonnum) unless $error;
41
42 if ( $error ) {
43   # do nothing
44 } elsif ( $payby =~ /^(CARD|CHEK)$/ ) { 
45   my %options = ();
46   my $bop = $FS::payby::payby2bop{$1};
47
48   my %payby2fields = (
49   'CARD' => [ qw( address1 address2 city county state zip country ) ],
50   'CHEK' => [ qw( ss paytype paystate stateid stateid_state ) ],
51   );
52   my %type = ( 'CARD' => 'credit card',
53              'CHEK' => 'electronic check (ACH)',
54              );
55
56 my( $cust_payby, $payinfo, $paycvv, $month, $year, $payname );
57 my $paymask = '';
58 if ( (my $custpaybynum = scalar($cgi->param('custpaybynum'))) > 0 ) {
59
60   ##
61   # use stored cust_payby info
62   ##
63
64   $cust_payby = qsearchs('cust_payby', { custnum      => $custnum,
65                                             custpaybynum => $custpaybynum, } )
66     or die "unknown custpaybynum $custpaybynum";
67
68   # not needed for realtime_bop, but still needed for batch_card
69   $payinfo = $cust_payby->payinfo;
70   $paymask = $cust_payby->paymask;
71   $paycvv = $cust_payby->paycvv; # pass it if we got it, running a transaction will clear it
72   ( $month, $year ) = $cust_payby->paydate_mon_year;
73   $payname = $cust_payby->payname;
74   $cgi->param(-name=>"paytype", -value=>$cust_payby->paytype) unless $cgi->param("paytype");
75
76 } else {
77
78   ##
79   # use new info
80   ##
81
82   $cgi->param('year') =~ /^(\d+)$/
83     or errorpage("illegal year ". $cgi->param('year'));
84   $year = $1;
85
86   $cgi->param('month') =~ /^(\d+)$/
87     or errorpage("illegal month ". $cgi->param('month'));
88   $month = $1;
89
90   $cgi->param('payname') =~ /^([\w \,\.\-\']+)$/
91     or errorpage(gettext('illegal_name'). " payname: ". $cgi->param('payname'));
92   $payname = $1;
93
94   if ( $payby eq 'CHEK' ) {
95
96     $cgi->param('payinfo1') =~ /^(\d+)$/
97       or errorpage("Illegal account number ". $cgi->param('payinfo1'));
98     my $payinfo1 = $1;
99     $cgi->param('payinfo2') =~ /^(\d+)$/
100       or errorpage("Illegal ABA/routing number ". $cgi->param('payinfo2'));
101     my $payinfo2 = $1;
102     if ( $conf->config('echeck-country') eq 'CA' ) {
103       $cgi->param('payinfo3') =~ /^(\d{5})$/
104         or errorpage("Illegal branch number ". $cgi->param('payinfo2'));
105       $payinfo2 = "$1.$payinfo2";
106     }
107     $payinfo = $payinfo1 . '@'. $payinfo2;
108
109   } elsif ( $payby eq 'CARD' ) {
110
111     $payinfo = $cgi->param('payinfo');
112
113     $payinfo =~ s/\D//g;
114     $payinfo =~ /^(\d{13,19}|\d{8,9})$/
115       or errorpage(gettext('invalid_card'));
116     $payinfo = $1;
117     validate($payinfo)
118       or errorpage(gettext('invalid_card'));
119
120     unless ( $cust_main->tokenized($payinfo) ) { #token
121
122       my $cardtype = cardtype($payinfo);
123
124       errorpage(gettext('unknown_card_type'))
125         if $cardtype eq "Unknown";
126
127       my %bop_card_types = map { $_=>1 } values %{ card_types() };
128       errorpage("$cardtype not accepted") unless $bop_card_types{$cardtype};
129
130     }
131
132     if ( length($cgi->param('paycvv') ) ) {
133       if ( cardtype($payinfo) eq 'American Express card' ) {
134         $cgi->param('paycvv') =~ /^(\d{4})$/
135           or errorpage("CVV2 (CID) for American Express cards is four digits.");
136         $paycvv = $1;
137       } else {
138         $cgi->param('paycvv') =~ /^(\d{3})$/
139           or errorpage("CVV2 (CVC2/CID) is three digits.");
140         $paycvv = $1;
141       }
142     } elsif ( $conf->exists('backoffice-require_cvv') ){
143       errorpage("CVV2 is required");
144     }
145
146   } else {
147     die "unknown payby $payby";
148   }
149
150   # save first, for proper tokenization
151   if ( $cgi->param('save') ) {
152
153     my %saveopt;
154     if ( $payby eq 'CARD' ) {
155       my $bill_location = FS::cust_location->new;
156       $bill_location->set( $_ => scalar($cgi->param($_)) )
157         foreach @{$payby2fields{$payby}};
158       $saveopt{'bill_location'} = $bill_location;
159       $saveopt{'paycvv'} = $paycvv; # save_cust_payby contains conf logic for when to use this
160       $saveopt{'paydate'} = "$year-$month-01";
161     } else {
162       # ss/stateid/stateid_state won't be saved, but should be harmless to pass
163       %saveopt = map { $_ => scalar($cgi->param($_)) } @{$payby2fields{$payby}};
164     }
165
166     my $error = $cust_main->save_cust_payby(
167       'saved_cust_payby' => \$cust_payby,
168       'payment_payby' => $payby,
169       'auto'          => scalar($cgi->param('auto')),
170       'weight'        => scalar($cgi->param('weight')),
171       'payinfo'       => $payinfo,
172       'payname'       => $payname,
173       %saveopt
174     );
175
176     errorpage("error saving info, payment not processed: $error")
177       if $error;
178
179   } elsif ( $payby eq 'CARD' ) { # not saving
180
181     $paymask = FS::payinfo_Mixin->mask_payinfo('CARD',$payinfo); # for untokenized but tokenizable payinfo
182
183   }
184
185 }
186
187 ##
188 # now run the refund
189 ##
190
191   $cgi->param('refund') =~ /^(\d*)(\.\d{2})?$/
192     or die "illegal refund amount ". $cgi->param('refund');
193   my $refund = "$1$2";
194   $cgi->param('paynum') =~ /^(\d*)$/ or die "Illegal paynum!";
195   my $paynum = $1;
196   my $paydate;
197   if ($cust_payby->paydate) { $paydate = "$year-$month-01"; }
198   else { $paydate = "2037-12-01"; }
199
200   if ( $cgi->param('batch') ) {
201
202     $error ||= $cust_main->batch_card(
203                                      'payby'    => $payby,
204                                      'amount'   => $refund,
205                                      'payinfo'  => $payinfo,
206                                      'paydate'  => $paydate,
207                                      'payname'  => $payname,
208                                      'paycode'  => 'C',
209                                      map { $_ => scalar($cgi->param($_)) }
210                                        @{$payby2fields{$payby}}
211                                    );
212     errorpage($error) if $error;
213
214 #### post refund #####
215     my %hash = map {
216       $_, scalar($cgi->param($_))
217     } fields('cust_refund');
218     $paynum = $cgi->param('paynum');
219     $paynum =~ /^(\d*)$/ or die "Illegal paynum!";
220     if ($paynum) {
221       my $cust_pay = qsearchs('cust_pay',{ 'paynum' => $paynum });
222       die "Could not find paynum $paynum" unless $cust_pay;
223       $error = $cust_pay->refund(\%hash);
224     } else {
225       my $new = new FS::cust_refund ( \%hash );
226       $error = $new->insert;
227     }
228     # if not a batch refund run realtime.
229   } else {
230     $error = $cust_main->realtime_refund_bop( $bop, 'amount' => $refund,
231                                                   'paynum' => $paynum,
232                                                   'reasonnum' => scalar($cgi->param('reasonnum')),
233                                                   %options );
234   }
235 } else {
236   my %hash = map {
237     $_, scalar($cgi->param($_))
238   } fields('cust_refund');
239   my $paynum = $cgi->param('paynum');
240   $paynum =~ /^(\d*)$/ or die "Illegal paynum!";
241   if ($paynum) {
242     my $cust_pay = qsearchs('cust_pay',{ 'paynum' => $paynum });
243     die "Could not find paynum $paynum" unless $cust_pay;
244     $error = $cust_pay->refund(\%hash);
245   } else {
246     my $new = new FS::cust_refund ( \%hash );
247     $error = $new->insert;
248   }
249 }
250
251 </%init>