-if ( $error ) {
- $cgi->param('error', $error);
- print $cgi->redirect(popurl(2). "cust_refund.cgi?". $cgi->query_string );
-} else {
- print $cgi->redirect(popurl(3). "view/cust_main.cgi?$custnum");
+ my %hash = map {
+ $_, scalar($cgi->param($_))
+ } fields('cust_refund');
+
+ my $new = new FS::cust_refund ( { 'paynum' => $paynum,
+ %hash,
+ } );
+ $error = $new->insert;
+
+ # if not a batch refund run realtime.
+ } else {
+ $options{'paydate'} = $paydate if $paydate =~ /^\d{2,4}-\d{1,2}-01$/;
+ $error = $cust_main->realtime_refund_bop( $bop, 'amount' => $refund,
+ 'paynum' => $paynum,
+ 'reasonnum' => $reasonnum,
+ %options );
+ }
+} else { # run cash refund.
+ my %hash = map {
+ $_, scalar($cgi->param($_))
+ } fields('cust_refund');
+ my $paynum = $cgi->param('paynum');
+ $paynum =~ /^(\d*)$/ or die "Illegal paynum!";
+ if ($paynum) {
+ my $cust_pay = qsearchs('cust_pay',{ 'paynum' => $paynum });
+ die "Could not find paynum $paynum" unless $cust_pay;
+ $error = $cust_pay->refund(\%hash);
+ } else {
+ my $new = new FS::cust_refund ( \%hash );
+ $error = $new->insert;
+ }