RT# 83044 - fixed cc refund issues
[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 $error = "No batch download format configured that allows electronic refunds" unless (FS::pay_batch->can_handle_electronic_refunds && !$error);
43
44 if ( $error ) {
45   # do nothing
46 } elsif ( $payby =~ /^(CARD|CHEK)$/ ) { 
47   my %options = ();
48   my $bop = $FS::payby::payby2bop{$1};
49
50   my %payby2fields = (
51   'CARD' => [ qw( address1 address2 city county state zip country ) ],
52   'CHEK' => [ qw( ss paytype paystate stateid stateid_state ) ],
53   );
54   my %type = ( 'CARD' => 'credit card',
55              'CHEK' => 'electronic check (ACH)',
56              );
57
58 my( $cust_pay, $cust_payby, $payinfo, $paycvv, $month, $year, $payname, $paycardtype );
59 my $paymask = '';
60
61 ## get cust pay info if paynum exists
62 if ( $cgi->param('paynum') > 0) {
63   $cust_pay = qsearchs({
64     'table'     => 'cust_pay',
65     'hashref'   => { 'paynum' => $cgi->param('paynum') },
66     'select'    => 'cust_pay.*, cust_pay_batch.payname ',
67     'addl_from' => "left join cust_pay_batch on cust_pay_batch.batchnum = cust_pay.batchnum and cust_pay_batch.custnum = $custnum ",
68   });
69 }
70
71 if ( (my $custpaybynum = scalar($cgi->param('custpaybynum'))) > 0 ) {
72
73   ##
74   # use stored cust_payby info
75   ##
76
77   $cust_payby = qsearchs('cust_payby', { custnum      => $custnum,
78                                             custpaybynum => $custpaybynum, } )
79     or die "unknown custpaybynum $custpaybynum";
80
81   # not needed for realtime_bop, but still needed for batch_card
82   $payinfo = $cust_payby->payinfo;
83   $paymask = $cust_payby->paymask;
84   $paycvv = $cust_payby->paycvv; # pass it if we got it, running a transaction will clear it
85   ( $month, $year ) = $cust_payby->paydate_mon_year;
86   $payname = $cust_payby->payname;
87   $cgi->param(-name=>"paytype", -value=>$cust_payby->paytype) unless $cgi->param("paytype");
88
89 } elsif ( $cgi->param('paynum') > 0) {
90
91   $payinfo = $cust_pay->payinfo;
92   $paycardtype = $cust_pay->paycardtype;
93   $payname = $cust_pay->payname;
94
95 } else {
96
97   ##
98   # use new info
99   ##
100
101   $cgi->param('year') =~ /^(\d+)$/
102     or errorpage("illegal year ". $cgi->param('year'));
103   $year = $1;
104
105   $cgi->param('month') =~ /^(\d+)$/
106     or errorpage("illegal month ". $cgi->param('month'));
107   $month = $1;
108
109   $cgi->param('payname') =~ /^([\w \,\.\-\']+)$/
110     or errorpage(gettext('illegal_name'). " payname: ". $cgi->param('payname'));
111   $payname = $1;
112
113   if ( $payby eq 'CHEK' ) {
114
115     $cgi->param('payinfo1') =~ /^(\d+)$/
116       or errorpage("Illegal account number ". $cgi->param('payinfo1'));
117     my $payinfo1 = $1;
118     $cgi->param('payinfo2') =~ /^(\d+)$/
119       or errorpage("Illegal ABA/routing number ". $cgi->param('payinfo2'));
120     my $payinfo2 = $1;
121     if ( $conf->config('echeck-country') eq 'CA' ) {
122       $cgi->param('payinfo3') =~ /^(\d{5})$/
123         or errorpage("Illegal branch number ". $cgi->param('payinfo2'));
124       $payinfo2 = "$1.$payinfo2";
125     }
126     $payinfo = $payinfo1 . '@'. $payinfo2;
127
128   } elsif ( $payby eq 'CARD' ) {
129
130     $payinfo = $cgi->param('payinfo');
131
132     $payinfo =~ s/\D//g;
133     $payinfo =~ /^(\d{13,19}|\d{8,9})$/
134       or errorpage(gettext('invalid_card'));
135     $payinfo = $1;
136     validate($payinfo)
137       or errorpage(gettext('invalid_card'));
138
139     unless ( $cust_main->tokenized($payinfo) ) { #token
140
141       my $cardtype = cardtype($payinfo);
142
143       errorpage(gettext('unknown_card_type'))
144         if $cardtype eq "Unknown";
145
146       my %bop_card_types = map { $_=>1 } values %{ card_types() };
147       errorpage("$cardtype not accepted") unless $bop_card_types{$cardtype};
148
149     }
150
151     if ( length($cgi->param('paycvv') ) ) {
152       if ( cardtype($payinfo) eq 'American Express card' ) {
153         $cgi->param('paycvv') =~ /^(\d{4})$/
154           or errorpage("CVV2 (CID) for American Express cards is four digits.");
155         $paycvv = $1;
156       } else {
157         $cgi->param('paycvv') =~ /^(\d{3})$/
158           or errorpage("CVV2 (CVC2/CID) is three digits.");
159         $paycvv = $1;
160       }
161     } elsif ( $conf->exists('backoffice-require_cvv') ){
162       errorpage("CVV2 is required");
163     }
164
165   } else {
166     die "unknown payby $payby";
167   }
168
169   # save first, for proper tokenization
170   if ( $cgi->param('save') ) {
171
172     my %saveopt;
173     if ( $payby eq 'CARD' ) {
174       my $bill_location = FS::cust_location->new;
175       $bill_location->set( $_ => scalar($cgi->param($_)) )
176         foreach @{$payby2fields{$payby}};
177       $saveopt{'bill_location'} = $bill_location;
178       $saveopt{'paycvv'} = $paycvv; # save_cust_payby contains conf logic for when to use this
179       $saveopt{'paydate'} = "$year-$month-01";
180     } else {
181       # ss/stateid/stateid_state won't be saved, but should be harmless to pass
182       %saveopt = map { $_ => scalar($cgi->param($_)) } @{$payby2fields{$payby}};
183     }
184
185     my $error = $cust_main->save_cust_payby(
186       'saved_cust_payby' => \$cust_payby,
187       'payment_payby' => $payby,
188       'auto'          => scalar($cgi->param('auto')),
189       'weight'        => scalar($cgi->param('weight')),
190       'payinfo'       => $payinfo,
191       'payname'       => $payname,
192       %saveopt
193     );
194
195     errorpage("error saving info, payment not processed: $error")
196       if $error;
197
198   } elsif ( $payby eq 'CARD' ) { # not saving
199
200     $paymask = FS::payinfo_Mixin->mask_payinfo('CARD',$payinfo); # for untokenized but tokenizable payinfo
201
202   }
203
204 }
205
206 ##
207 # now run the refund
208 ##
209
210   $cgi->param('refund') =~ /^(\d*)(\.\d{2})?$/
211     or die "illegal refund amount ". $cgi->param('refund');
212   my $refund = "$1$2";
213   $cgi->param('paynum') =~ /^(\d*)$/ or die "Illegal paynum!";
214   my $paynum = $1;
215   my $paydate;
216   unless ($paynum) {
217     if ($cust_payby->paydate) { $paydate = "$year-$month-01"; }
218     else { $paydate = "2037-12-01"; }
219   }
220
221   if ( $cgi->param('batch') ) {
222     $paydate = "2037-12-01" unless $paydate;
223     $error ||= $cust_main->batch_card(
224                                      'payby'    => $payby,
225                                      'amount'   => $refund,
226                                      'payinfo'  => $payinfo,
227                                      'paydate'  => $paydate,
228                                      'payname'  => $payname,
229                                      'paycode'  => 'C',
230                                      map { $_ => scalar($cgi->param($_)) }
231                                        @{$payby2fields{$payby}}
232                                    );
233     errorpage($error) if $error;
234
235     my %hash = map {
236       $_, scalar($cgi->param($_))
237     } fields('cust_refund');
238
239     $hash{'payinfo'} = $payinfo;
240     $hash{'paymask'} = $paymask;
241     $hash{'paycardtype'} = $paycardtype;
242
243     ## unapply payment before creating refund.
244     while ( $cust_pay && $cust_pay->unapplied < $refund ) {
245       my @cust_bill_pay = $cust_pay->cust_bill_pay;
246       last unless @cust_bill_pay;
247       my $cust_bill_pay = pop @cust_bill_pay;
248       my $error = $cust_bill_pay->delete;
249       last if $error;
250     }
251
252     my $new = new FS::cust_refund ( { 'paynum' => $paynum,
253                                       %hash,
254                                   } );
255     $error = $new->insert;
256
257   # if not a batch refund run realtime.
258   } else {
259     $error = $cust_main->realtime_refund_bop( $bop, 'amount' => $refund,
260                                                   'paynum' => $paynum,
261                                                   'reasonnum' => scalar($cgi->param('reasonnum')),
262                                                   %options );
263   }
264 } else { # run cash refund.
265   my %hash = map {
266     $_, scalar($cgi->param($_))
267   } fields('cust_refund');
268   my $paynum = $cgi->param('paynum');
269   $paynum =~ /^(\d*)$/ or die "Illegal paynum!";
270   if ($paynum) {
271     my $cust_pay = qsearchs('cust_pay',{ 'paynum' => $paynum });
272     die "Could not find paynum $paynum" unless $cust_pay;
273     $error = $cust_pay->refund(\%hash);
274   } else {
275     my $new = new FS::cust_refund ( \%hash );
276     $error = $new->insert;
277   }
278 }
279
280 </%init>