2 % $cgi->param('error', $error);
3 <% $cgi->redirect(popurl(2). "cust_refund.cgi?". $cgi->query_string ) %>
6 % if ( $link eq 'popup' ) {
8 <& /elements/header-popup.html, 'Refund entered' &>
9 <SCRIPT TYPE="text/javascript">
15 <% $cgi->redirect(popurl(3). "view/cust_main.cgi?custnum=$custnum;show=payment_history") %>
21 unless $FS::CurrentUser::CurrentUser->access_right('Refund payment')
22 || $FS::CurrentUser::CurrentUser->access_right('Post refund');
24 my $conf = new FS::Conf;
26 $cgi->param('custnum') =~ /^(\d*)$/ or die "Illegal custnum!";
28 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
29 or die "unknown custnum $custnum";
31 my $link = $cgi->param('popup') ? 'popup' : '';
33 my $payby = $cgi->param('payby');
36 unless $FS::CurrentUser::CurrentUser->refund_access_right($payby);
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;
44 } elsif ( $payby =~ /^(CARD|CHEK)$/ ) {
46 my $bop = $FS::payby::payby2bop{$1};
49 'CARD' => [ qw( address1 address2 city county state zip country ) ],
50 'CHEK' => [ qw( ss paytype paystate stateid stateid_state ) ],
52 my %type = ( 'CARD' => 'credit card',
53 'CHEK' => 'electronic check (ACH)',
56 my( $cust_pay, $cust_payby, $payinfo, $paycvv, $month, $year, $payname );
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 ",
69 if ( (my $custpaybynum = scalar($cgi->param('custpaybynum'))) > 0 ) {
72 # use stored cust_payby info
75 $cust_payby = qsearchs('cust_payby', { custnum => $custnum,
76 custpaybynum => $custpaybynum, } )
77 or die "unknown custpaybynum $custpaybynum";
79 # not needed for realtime_bop, but still needed for batch_card
80 $payinfo = $cust_payby->payinfo;
81 $paymask = $cust_payby->paymask;
82 $paycvv = $cust_payby->paycvv; # pass it if we got it, running a transaction will clear it
83 ( $month, $year ) = $cust_payby->paydate_mon_year;
84 $payname = $cust_payby->payname;
85 $cgi->param(-name=>"paytype", -value=>$cust_payby->paytype) unless $cgi->param("paytype");
87 } elsif ( $cgi->param('paynum') > 0) {
89 $payinfo = $cust_pay->payinfo;
90 $payname = $cust_pay->payname;
98 $cgi->param('year') =~ /^(\d+)$/
99 or errorpage("illegal year ". $cgi->param('year'));
102 $cgi->param('month') =~ /^(\d+)$/
103 or errorpage("illegal month ". $cgi->param('month'));
106 $cgi->param('payname') =~ /^([\w \,\.\-\']+)$/
107 or errorpage(gettext('illegal_name'). " payname: ". $cgi->param('payname'));
110 if ( $payby eq 'CHEK' ) {
112 $cgi->param('payinfo1') =~ /^(\d+)$/
113 or errorpage("Illegal account number ". $cgi->param('payinfo1'));
115 $cgi->param('payinfo2') =~ /^(\d+)$/
116 or errorpage("Illegal ABA/routing number ". $cgi->param('payinfo2'));
118 if ( $conf->config('echeck-country') eq 'CA' ) {
119 $cgi->param('payinfo3') =~ /^(\d{5})$/
120 or errorpage("Illegal branch number ". $cgi->param('payinfo2'));
121 $payinfo2 = "$1.$payinfo2";
123 $payinfo = $payinfo1 . '@'. $payinfo2;
125 } elsif ( $payby eq 'CARD' ) {
127 $payinfo = $cgi->param('payinfo');
130 $payinfo =~ /^(\d{13,19}|\d{8,9})$/
131 or errorpage(gettext('invalid_card'));
134 or errorpage(gettext('invalid_card'));
136 unless ( $cust_main->tokenized($payinfo) ) { #token
138 my $cardtype = cardtype($payinfo);
140 errorpage(gettext('unknown_card_type'))
141 if $cardtype eq "Unknown";
143 my %bop_card_types = map { $_=>1 } values %{ card_types() };
144 errorpage("$cardtype not accepted") unless $bop_card_types{$cardtype};
148 if ( length($cgi->param('paycvv') ) ) {
149 if ( cardtype($payinfo) eq 'American Express card' ) {
150 $cgi->param('paycvv') =~ /^(\d{4})$/
151 or errorpage("CVV2 (CID) for American Express cards is four digits.");
154 $cgi->param('paycvv') =~ /^(\d{3})$/
155 or errorpage("CVV2 (CVC2/CID) is three digits.");
158 } elsif ( $conf->exists('backoffice-require_cvv') ){
159 errorpage("CVV2 is required");
163 die "unknown payby $payby";
166 # save first, for proper tokenization
167 if ( $cgi->param('save') ) {
170 if ( $payby eq 'CARD' ) {
171 my $bill_location = FS::cust_location->new;
172 $bill_location->set( $_ => scalar($cgi->param($_)) )
173 foreach @{$payby2fields{$payby}};
174 $saveopt{'bill_location'} = $bill_location;
175 $saveopt{'paycvv'} = $paycvv; # save_cust_payby contains conf logic for when to use this
176 $saveopt{'paydate'} = "$year-$month-01";
178 # ss/stateid/stateid_state won't be saved, but should be harmless to pass
179 %saveopt = map { $_ => scalar($cgi->param($_)) } @{$payby2fields{$payby}};
182 my $error = $cust_main->save_cust_payby(
183 'saved_cust_payby' => \$cust_payby,
184 'payment_payby' => $payby,
185 'auto' => scalar($cgi->param('auto')),
186 'weight' => scalar($cgi->param('weight')),
187 'payinfo' => $payinfo,
188 'payname' => $payname,
192 errorpage("error saving info, payment not processed: $error")
195 } elsif ( $payby eq 'CARD' ) { # not saving
197 $paymask = FS::payinfo_Mixin->mask_payinfo('CARD',$payinfo); # for untokenized but tokenizable payinfo
207 $cgi->param('refund') =~ /^(\d*)(\.\d{2})?$/
208 or die "illegal refund amount ". $cgi->param('refund');
210 $cgi->param('paynum') =~ /^(\d*)$/ or die "Illegal paynum!";
214 if ($cust_payby->paydate) { $paydate = "$year-$month-01"; }
215 else { $paydate = "2037-12-01"; }
218 if ( $cgi->param('batch') ) {
219 $paydate = "2037-12-01" unless $paydate;
220 $error ||= $cust_main->batch_card(
223 'payinfo' => $payinfo,
224 'paydate' => $paydate,
225 'payname' => $payname,
227 map { $_ => scalar($cgi->param($_)) }
228 @{$payby2fields{$payby}}
230 errorpage($error) if $error;
233 $_, scalar($cgi->param($_))
234 } fields('cust_refund');
236 ## unapply payment before creating refund.
237 while ( $cust_pay && $cust_pay->unapplied < $refund ) {
238 my @cust_bill_pay = $cust_pay->cust_bill_pay;
239 last unless @cust_bill_pay;
240 my $cust_bill_pay = pop @cust_bill_pay;
241 my $error = $cust_bill_pay->delete;
245 my $new = new FS::cust_refund ( { 'paynum' => $paynum,
248 $error = $new->insert;
250 # if not a batch refund run realtime.
252 $error = $cust_main->realtime_refund_bop( $bop, 'amount' => $refund,
254 'reasonnum' => scalar($cgi->param('reasonnum')),
257 } else { # run cash refund.
259 $_, scalar($cgi->param($_))
260 } fields('cust_refund');
261 my $paynum = $cgi->param('paynum');
262 $paynum =~ /^(\d*)$/ or die "Illegal paynum!";
264 my $cust_pay = qsearchs('cust_pay',{ 'paynum' => $paynum });
265 die "Could not find paynum $paynum" unless $cust_pay;
266 $error = $cust_pay->refund(\%hash);
268 my $new = new FS::cust_refund ( \%hash );
269 $error = $new->insert;