RT# 83306 - fixed realtime refunds
[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 ( $cgi->param('batch') ) {
43   $error = "No batch download format configured that allows electronic refunds via batch processing."
44     unless (FS::pay_batch->can_handle_electronic_refunds && !$error);
45 }
46
47 #die "my error\n".$error;
48
49 if ( $error ) {
50   # do nothing
51 } elsif ( $payby =~ /^(CARD|CHEK)$/ ) { 
52   my %options = ();
53   my $bop = $FS::payby::payby2bop{$1};
54
55   my %payby2fields = (
56   'CARD' => [ qw( address1 address2 city county state zip country ) ],
57   'CHEK' => [ qw( ss paytype paystate stateid stateid_state ) ],
58   );
59   my %type = ( 'CARD' => 'credit card',
60              'CHEK' => 'electronic check (ACH)',
61              );
62
63 my( $cust_pay, $cust_payby, $payinfo, $paycvv, $month, $year, $payname, $paycardtype );
64 my $paymask = '';
65
66 ## get cust pay info if paynum exists
67 if ( $cgi->param('paynum') > 0) {
68   $cust_pay = qsearchs({
69     'table'     => 'cust_pay',
70     'hashref'   => { 'paynum' => $cgi->param('paynum') },
71     'select'    => 'cust_pay.*, cust_pay_batch.payname ',
72     'addl_from' => "left join cust_pay_batch on cust_pay_batch.batchnum = cust_pay.batchnum and cust_pay_batch.custnum = $custnum ",
73   });
74 }
75
76 ##
77 # now run the refund
78 ##
79
80   $cgi->param('refund') =~ /^(\d*)(\.\d{2})?$/
81     or die "illegal refund amount ". $cgi->param('refund');
82   my $refund = "$1$2";
83   $cgi->param('paynum') =~ /^(\d*)$/ or die "Illegal paynum!";
84   my $paynum = $1;
85   my $paydate = $cgi->param('exp_year'). '-'. $cgi->param('exp_month'). '-01';
86
87   if ( $cgi->param('batch') ) {
88     $paydate = "2037-12-01" unless $paydate;
89     $error ||= $cust_main->batch_card(
90                                      'payby'    => $payby,
91                                      'amount'   => $refund,
92                                      #'payinfo'  => $payinfo,
93                                      #'paydate'  => $paydate,
94                                      #'payname'  => $payname,
95                                      'paycode'  => 'C',
96                                      map { $_ => scalar($cgi->param($_)) }
97                                        @{$payby2fields{$payby}}
98                                    );
99     errorpage($error) if $error;
100
101     my %hash = map {
102       $_, scalar($cgi->param($_))
103     } fields('cust_refund');
104
105     $hash{'payinfo'} = $cust_main->payinfo;
106     $hash{'paymask'} = $cust_main->paymask;
107     $hash{'paycardtype'} = $cust_main->paycardtype;
108
109     ## unapply payment before creating refund.
110     while ( $cust_pay && $cust_pay->unapplied < $refund ) {
111       my @cust_bill_pay = $cust_pay->cust_bill_pay;
112       last unless @cust_bill_pay;
113       my $cust_bill_pay = pop @cust_bill_pay;
114       my $error = $cust_bill_pay->delete;
115       last if $error;
116     }
117
118     my $new = new FS::cust_refund ( { 'paynum' => $paynum,
119                                       %hash,
120                                   } );
121     $error = $new->insert;
122
123   # if not a batch refund run realtime.
124   } else {
125     $options{'paydate'} = $paydate if $paydate =~ /^\d{2,4}-\d{1,2}-01$/;
126     $error = $cust_main->realtime_refund_bop( $bop, 'amount' => $refund,
127                                                   'paynum' => $paynum,
128                                                   'reasonnum' => $reasonnum,
129                                                   %options );
130   }
131 } else { # run cash refund.
132   my %hash = map {
133     $_, scalar($cgi->param($_))
134   } fields('cust_refund');
135   my $paynum = $cgi->param('paynum');
136   $paynum =~ /^(\d*)$/ or die "Illegal paynum!";
137   if ($paynum) {
138     my $cust_pay = qsearchs('cust_pay',{ 'paynum' => $paynum });
139     die "Could not find paynum $paynum" unless $cust_pay;
140     $error = $cust_pay->refund(\%hash);
141   } else {
142     my $new = new FS::cust_refund ( \%hash );
143     $error = $new->insert;
144   }
145 }
146
147 </%init>