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 );
58 if ( (my $custpaybynum = scalar($cgi->param('custpaybynum'))) > 0 ) {
61 # use stored cust_payby info
64 $cust_payby = qsearchs('cust_payby', { custnum => $custnum,
65 custpaybynum => $custpaybynum, } )
66 or die "unknown custpaybynum $custpaybynum";
68 # not needed for realtime_bop, but still needed for batch_card
69 $payinfo = $cust_payby->payinfo;
70 $paymask = $cust_payby->paymask;
71 $paycvv = $cust_payby->paycvv; # pass it if we got it, running a transaction will clear it
72 ( $month, $year ) = $cust_payby->paydate_mon_year;
73 $payname = $cust_payby->payname;
74 $cgi->param(-name=>"paytype", -value=>$cust_payby->paytype) unless $cgi->param("paytype");
76 } elsif ( $cgi->param('paynum') > 0) {
78 $cust_pay = qsearchs({
79 'table' => 'cust_pay',
80 'hashref' => { 'paynum' => $cgi->param('paynum') },
81 'select' => 'cust_pay.*, cust_pay_batch.payname ',
82 'addl_from' => "left join cust_pay_batch on cust_pay_batch.batchnum = cust_pay.batchnum and cust_pay_batch.custnum = $custnum ",
84 $payinfo = $cust_pay->payinfo;
85 $payname = $cust_pay->payname;
93 $cgi->param('year') =~ /^(\d+)$/
94 or errorpage("illegal year ". $cgi->param('year'));
97 $cgi->param('month') =~ /^(\d+)$/
98 or errorpage("illegal month ". $cgi->param('month'));
101 $cgi->param('payname') =~ /^([\w \,\.\-\']+)$/
102 or errorpage(gettext('illegal_name'). " payname: ". $cgi->param('payname'));
105 if ( $payby eq 'CHEK' ) {
107 $cgi->param('payinfo1') =~ /^(\d+)$/
108 or errorpage("Illegal account number ". $cgi->param('payinfo1'));
110 $cgi->param('payinfo2') =~ /^(\d+)$/
111 or errorpage("Illegal ABA/routing number ". $cgi->param('payinfo2'));
113 if ( $conf->config('echeck-country') eq 'CA' ) {
114 $cgi->param('payinfo3') =~ /^(\d{5})$/
115 or errorpage("Illegal branch number ". $cgi->param('payinfo2'));
116 $payinfo2 = "$1.$payinfo2";
118 $payinfo = $payinfo1 . '@'. $payinfo2;
120 } elsif ( $payby eq 'CARD' ) {
122 $payinfo = $cgi->param('payinfo');
125 $payinfo =~ /^(\d{13,19}|\d{8,9})$/
126 or errorpage(gettext('invalid_card'));
129 or errorpage(gettext('invalid_card'));
131 unless ( $cust_main->tokenized($payinfo) ) { #token
133 my $cardtype = cardtype($payinfo);
135 errorpage(gettext('unknown_card_type'))
136 if $cardtype eq "Unknown";
138 my %bop_card_types = map { $_=>1 } values %{ card_types() };
139 errorpage("$cardtype not accepted") unless $bop_card_types{$cardtype};
143 if ( length($cgi->param('paycvv') ) ) {
144 if ( cardtype($payinfo) eq 'American Express card' ) {
145 $cgi->param('paycvv') =~ /^(\d{4})$/
146 or errorpage("CVV2 (CID) for American Express cards is four digits.");
149 $cgi->param('paycvv') =~ /^(\d{3})$/
150 or errorpage("CVV2 (CVC2/CID) is three digits.");
153 } elsif ( $conf->exists('backoffice-require_cvv') ){
154 errorpage("CVV2 is required");
158 die "unknown payby $payby";
161 # save first, for proper tokenization
162 if ( $cgi->param('save') ) {
165 if ( $payby eq 'CARD' ) {
166 my $bill_location = FS::cust_location->new;
167 $bill_location->set( $_ => scalar($cgi->param($_)) )
168 foreach @{$payby2fields{$payby}};
169 $saveopt{'bill_location'} = $bill_location;
170 $saveopt{'paycvv'} = $paycvv; # save_cust_payby contains conf logic for when to use this
171 $saveopt{'paydate'} = "$year-$month-01";
173 # ss/stateid/stateid_state won't be saved, but should be harmless to pass
174 %saveopt = map { $_ => scalar($cgi->param($_)) } @{$payby2fields{$payby}};
177 my $error = $cust_main->save_cust_payby(
178 'saved_cust_payby' => \$cust_payby,
179 'payment_payby' => $payby,
180 'auto' => scalar($cgi->param('auto')),
181 'weight' => scalar($cgi->param('weight')),
182 'payinfo' => $payinfo,
183 'payname' => $payname,
187 errorpage("error saving info, payment not processed: $error")
190 } elsif ( $payby eq 'CARD' ) { # not saving
192 $paymask = FS::payinfo_Mixin->mask_payinfo('CARD',$payinfo); # for untokenized but tokenizable payinfo
202 $cgi->param('refund') =~ /^(\d*)(\.\d{2})?$/
203 or die "illegal refund amount ". $cgi->param('refund');
205 $cgi->param('paynum') =~ /^(\d*)$/ or die "Illegal paynum!";
209 if ($cust_payby->paydate) { $paydate = "$year-$month-01"; }
210 else { $paydate = "2037-12-01"; }
213 if ( $cgi->param('batch') ) {
214 $paydate = "2037-12-01" unless $paydate;
215 $error ||= $cust_main->batch_card(
218 'payinfo' => $payinfo,
219 'paydate' => $paydate,
220 'payname' => $payname,
222 map { $_ => scalar($cgi->param($_)) }
223 @{$payby2fields{$payby}}
225 errorpage($error) if $error;
228 $_, scalar($cgi->param($_))
229 } fields('cust_refund');
231 my $new = new FS::cust_refund ( { 'paynum' => $paynum,
234 $error = $new->insert;
236 # if not a batch refund run realtime.
238 $error = $cust_main->realtime_refund_bop( $bop, 'amount' => $refund,
240 'reasonnum' => scalar($cgi->param('reasonnum')),
243 } else { # run cash refund.
245 $_, scalar($cgi->param($_))
246 } fields('cust_refund');
247 my $paynum = $cgi->param('paynum');
248 $paynum =~ /^(\d*)$/ or die "Illegal paynum!";
250 my $cust_pay = qsearchs('cust_pay',{ 'paynum' => $paynum });
251 die "Could not find paynum $paynum" unless $cust_pay;
252 $error = $cust_pay->refund(\%hash);
254 my $new = new FS::cust_refund ( \%hash );
255 $error = $new->insert;