RT# 74453 - V3 fixes for backport
[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 <% header('Refund entered') %>
9     <SCRIPT TYPE="text/javascript">
10       window.top.location.reload();
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_pay, $cust_payby, $payinfo, $paycvv, $month, $year, $payname );
57 my $paymask = '';
58
59 ## get cust pay info if paynum exists
60 if ( $cgi->param('paynum') > 0) {
61   $cust_pay = qsearchs({
62     'table'     => 'cust_pay',
63     'hashref'   => { 'paynum' => $cgi->param('paynum') },
64     'select'    => 'cust_pay.*, cust_pay_batch.payname ',
65     'addl_from' => "left join cust_pay_batch on cust_pay_batch.batchnum = cust_pay.batchnum and cust_pay_batch.custnum = $custnum ",
66   });
67 }
68
69 if ( $cgi->param('paynum') > 0) {
70
71   $payinfo = $cust_pay->payinfo;
72   $payname = $cust_pay->payname;
73
74 } else {
75
76   ##
77   # use new info
78   ##
79
80   $cgi->param('year') =~ /^(\d+)$/
81     or errorpage("illegal year ". $cgi->param('year'));
82   $year = $1;
83
84   $cgi->param('month') =~ /^(\d+)$/
85     or errorpage("illegal month ". $cgi->param('month'));
86   $month = $1;
87
88   $cgi->param('payname') =~ /^([\w \,\.\-\']+)$/
89     or errorpage(gettext('illegal_name'). " payname: ". $cgi->param('payname'));
90   $payname = $1;
91
92   if ( $payby eq 'CHEK' ) {
93
94     $cgi->param('payinfo1') =~ /^(\d+)$/
95       or errorpage("Illegal account number ". $cgi->param('payinfo1'));
96     my $payinfo1 = $1;
97     $cgi->param('payinfo2') =~ /^(\d+)$/
98       or errorpage("Illegal ABA/routing number ". $cgi->param('payinfo2'));
99     my $payinfo2 = $1;
100     if ( $conf->config('echeck-country') eq 'CA' ) {
101       $cgi->param('payinfo3') =~ /^(\d{5})$/
102         or errorpage("Illegal branch number ". $cgi->param('payinfo2'));
103       $payinfo2 = "$1.$payinfo2";
104     }
105     $payinfo = $payinfo1 . '@'. $payinfo2;
106
107   } elsif ( $payby eq 'CARD' ) {
108
109     $payinfo = $cgi->param('payinfo');
110
111     $payinfo =~ s/\D//g;
112     $payinfo =~ /^(\d{13,19}|\d{8,9})$/
113       or errorpage(gettext('invalid_card'));
114     $payinfo = $1;
115     validate($payinfo)
116       or errorpage(gettext('invalid_card'));
117
118     unless ( $cust_main->tokenized($payinfo) ) { #token
119
120       my $cardtype = cardtype($payinfo);
121
122       errorpage(gettext('unknown_card_type'))
123         if $cardtype eq "Unknown";
124
125       my %bop_card_types = map { $_=>1 } values %{ card_types() };
126       errorpage("$cardtype not accepted") unless $bop_card_types{$cardtype};
127
128     }
129
130     if ( length($cgi->param('paycvv') ) ) {
131       if ( cardtype($payinfo) eq 'American Express card' ) {
132         $cgi->param('paycvv') =~ /^(\d{4})$/
133           or errorpage("CVV2 (CID) for American Express cards is four digits.");
134         $paycvv = $1;
135       } else {
136         $cgi->param('paycvv') =~ /^(\d{3})$/
137           or errorpage("CVV2 (CVC2/CID) is three digits.");
138         $paycvv = $1;
139       }
140     } elsif ( $conf->exists('backoffice-require_cvv') ){
141       errorpage("CVV2 is required");
142     }
143
144   } else {
145     die "unknown payby $payby";
146   }
147
148   # save first, for proper tokenization
149   if ( $cgi->param('save') ) {
150
151     my %saveopt;
152     if ( $payby eq 'CARD' ) {
153       my $bill_location = FS::cust_location->new;
154       $bill_location->set( $_ => scalar($cgi->param($_)) )
155         foreach @{$payby2fields{$payby}};
156       $saveopt{'bill_location'} = $bill_location;
157       $saveopt{'paycvv'} = $paycvv; # save_cust_payby contains conf logic for when to use this
158       $saveopt{'paydate'} = "$year-$month-01";
159     } else {
160       # ss/stateid/stateid_state won't be saved, but should be harmless to pass
161       %saveopt = map { $_ => scalar($cgi->param($_)) } @{$payby2fields{$payby}};
162     }
163
164     my $error = $cust_main->save_cust_payby(
165       'saved_cust_payby' => \$cust_payby,
166       'payment_payby' => $payby,
167       'auto'          => scalar($cgi->param('auto')),
168       'weight'        => scalar($cgi->param('weight')),
169       'payinfo'       => $payinfo,
170       'payname'       => $payname,
171       %saveopt
172     );
173
174     errorpage("error saving info, payment not processed: $error")
175       if $error;
176
177   } elsif ( $payby eq 'CARD' ) { # not saving
178
179     $paymask = FS::payinfo_Mixin->mask_payinfo('CARD',$payinfo); # for untokenized but tokenizable payinfo
180
181   }
182
183 }
184
185 ##
186 # now run the refund
187 ##
188
189   $cgi->param('refund') =~ /^(\d*)(\.\d{2})?$/
190     or die "illegal refund amount ". $cgi->param('refund');
191   my $refund = "$1$2";
192   $cgi->param('paynum') =~ /^(\d*)$/ or die "Illegal paynum!";
193   my $paynum = $1;
194   my $paydate = $cgi->param('exp_year'). '-'. $cgi->param('exp_month'). '-01';
195
196   if ( $cgi->param('batch') ) {
197     $paydate = "2037-12-01" unless $paydate;
198     $error ||= $cust_main->batch_card(
199                                      'payby'    => $payby,
200                                      'amount'   => $refund,
201                                      #'payinfo'  => $payinfo,
202                                      #'paydate'  => $paydate,
203                                      #'payname'  => $payname,
204                                      'paycode'  => 'C',
205                                      map { $_ => scalar($cgi->param($_)) }
206                                        @{$payby2fields{$payby}}
207                                    );
208     errorpage($error) if $error;
209
210     my %hash = map {
211       $_, scalar($cgi->param($_))
212     } fields('cust_refund');
213
214     ## unapply payment before creating refund.
215     while ( $cust_pay && $cust_pay->unapplied < $refund ) {
216       my @cust_bill_pay = $cust_pay->cust_bill_pay;
217       last unless @cust_bill_pay;
218       my $cust_bill_pay = pop @cust_bill_pay;
219       my $error = $cust_bill_pay->delete;
220       last if $error;
221     }
222
223     my $new = new FS::cust_refund ( { 'paynum' => $paynum,
224                                       %hash,
225                                   } );
226     $error = $new->insert;
227
228   # if not a batch refund run realtime.
229   } else {
230     $options{'paydate'} = $paydate if $paydate =~ /^\d{2,4}-\d{1,2}-01$/;
231     $error = $cust_main->realtime_refund_bop( $bop, 'amount' => $refund,
232                                                   'paynum' => $paynum,
233                                                   'reasonnum' => $reasonnum,
234                                                   %options );
235   }
236 } else { # run cash refund.
237   my %hash = map {
238     $_, scalar($cgi->param($_))
239   } fields('cust_refund');
240   my $paynum = $cgi->param('paynum');
241   $paynum =~ /^(\d*)$/ or die "Illegal paynum!";
242   if ($paynum) {
243     my $cust_pay = qsearchs('cust_pay',{ 'paynum' => $paynum });
244     die "Could not find paynum $paynum" unless $cust_pay;
245     $error = $cust_pay->refund(\%hash);
246   } else {
247     my $new = new FS::cust_refund ( \%hash );
248     $error = $new->insert;
249   }
250 }
251
252 </%init>