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;
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);
47 #die "my error\n".$error;
51 } elsif ( $payby =~ /^(CARD|CHEK)$/ ) {
53 my $bop = $FS::payby::payby2bop{$1};
56 'CARD' => [ qw( address1 address2 city county state zip country ) ],
57 'CHEK' => [ qw( ss paytype paystate stateid stateid_state ) ],
59 my %type = ( 'CARD' => 'credit card',
60 'CHEK' => 'electronic check (ACH)',
63 my( $cust_pay, $cust_payby, $payinfo, $paycvv, $month, $year, $payname, $paycardtype );
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 ",
76 if ( (my $custpaybynum = scalar($cgi->param('custpaybynum'))) > 0 ) {
79 # use stored cust_payby info
82 $cust_payby = qsearchs('cust_payby', { custnum => $custnum,
83 custpaybynum => $custpaybynum, } )
84 or die "unknown custpaybynum $custpaybynum";
86 # not needed for realtime_bop, but still needed for batch_card
87 $payinfo = $cust_payby->payinfo;
88 $paymask = $cust_payby->paymask;
89 $paycvv = $cust_payby->paycvv; # pass it if we got it, running a transaction will clear it
90 ( $month, $year ) = $cust_payby->paydate_mon_year;
91 $payname = $cust_payby->payname;
92 $cgi->param(-name=>"paytype", -value=>$cust_payby->paytype) unless $cgi->param("paytype");
94 } elsif ( $cgi->param('paynum') > 0) {
96 $payinfo = $cust_pay->payinfo;
97 $paycardtype = $cust_pay->paycardtype;
98 $payname = $cust_pay->payname;
106 $cgi->param('year') =~ /^(\d+)$/
107 or errorpage("illegal year ". $cgi->param('year'));
110 $cgi->param('month') =~ /^(\d+)$/
111 or errorpage("illegal month ". $cgi->param('month'));
114 $cgi->param('payname') =~ /^([\w \,\.\-\']+)$/
115 or errorpage(gettext('illegal_name'). " payname: ". $cgi->param('payname'));
118 if ( $payby eq 'CHEK' ) {
120 $cgi->param('payinfo1') =~ /^(\d+)$/
121 or errorpage("Illegal account number ". $cgi->param('payinfo1'));
123 $cgi->param('payinfo2') =~ /^(\d+)$/
124 or errorpage("Illegal ABA/routing number ". $cgi->param('payinfo2'));
126 if ( $conf->config('echeck-country') eq 'CA' ) {
127 $cgi->param('payinfo3') =~ /^(\d{5})$/
128 or errorpage("Illegal branch number ". $cgi->param('payinfo2'));
129 $payinfo2 = "$1.$payinfo2";
131 $payinfo = $payinfo1 . '@'. $payinfo2;
133 } elsif ( $payby eq 'CARD' ) {
135 $payinfo = $cgi->param('payinfo');
138 $payinfo =~ /^(\d{13,19}|\d{8,9})$/
139 or errorpage(gettext('invalid_card'));
142 or errorpage(gettext('invalid_card'));
144 unless ( $cust_main->tokenized($payinfo) ) { #token
146 my $cardtype = cardtype($payinfo);
148 errorpage(gettext('unknown_card_type'))
149 if $cardtype eq "Unknown";
151 my %bop_card_types = map { $_=>1 } values %{ card_types() };
152 errorpage("$cardtype not accepted") unless $bop_card_types{$cardtype};
156 if ( length($cgi->param('paycvv') ) ) {
157 if ( cardtype($payinfo) eq 'American Express card' ) {
158 $cgi->param('paycvv') =~ /^(\d{4})$/
159 or errorpage("CVV2 (CID) for American Express cards is four digits.");
162 $cgi->param('paycvv') =~ /^(\d{3})$/
163 or errorpage("CVV2 (CVC2/CID) is three digits.");
166 } elsif ( $conf->exists('backoffice-require_cvv') ){
167 errorpage("CVV2 is required");
171 die "unknown payby $payby";
174 # save first, for proper tokenization
175 if ( $cgi->param('save') ) {
178 if ( $payby eq 'CARD' ) {
179 my $bill_location = FS::cust_location->new;
180 $bill_location->set( $_ => scalar($cgi->param($_)) )
181 foreach @{$payby2fields{$payby}};
182 $saveopt{'bill_location'} = $bill_location;
183 $saveopt{'paycvv'} = $paycvv; # save_cust_payby contains conf logic for when to use this
184 $saveopt{'paydate'} = "$year-$month-01";
186 # ss/stateid/stateid_state won't be saved, but should be harmless to pass
187 %saveopt = map { $_ => scalar($cgi->param($_)) } @{$payby2fields{$payby}};
190 my $error = $cust_main->save_cust_payby(
191 'saved_cust_payby' => \$cust_payby,
192 'payment_payby' => $payby,
193 'auto' => scalar($cgi->param('auto')),
194 'weight' => scalar($cgi->param('weight')),
195 'payinfo' => $payinfo,
196 'payname' => $payname,
200 errorpage("error saving info, payment not processed: $error")
203 } elsif ( $payby eq 'CARD' ) { # not saving
205 $paymask = FS::payinfo_Mixin->mask_payinfo('CARD',$payinfo); # for untokenized but tokenizable payinfo
215 $cgi->param('refund') =~ /^(\d*)(\.\d{2})?$/
216 or die "illegal refund amount ". $cgi->param('refund');
218 $cgi->param('paynum') =~ /^(\d*)$/ or die "Illegal paynum!";
222 if ($cust_payby->paydate) { $paydate = "$year-$month-01"; }
223 else { $paydate = "2037-12-01"; }
226 if ( $cgi->param('batch') ) {
227 $paydate = "2037-12-01" unless $paydate;
228 $error ||= $cust_main->batch_card(
231 'payinfo' => $payinfo,
232 'paydate' => $paydate,
233 'payname' => $payname,
235 map { $_ => scalar($cgi->param($_)) }
236 @{$payby2fields{$payby}}
238 errorpage($error) if $error;
241 $_, scalar($cgi->param($_))
242 } fields('cust_refund');
244 $hash{'payinfo'} = $payinfo;
245 $hash{'paymask'} = $paymask;
246 $hash{'paycardtype'} = $paycardtype;
248 ## unapply payment before creating refund.
249 while ( $cust_pay && $cust_pay->unapplied < $refund ) {
250 my @cust_bill_pay = $cust_pay->cust_bill_pay;
251 last unless @cust_bill_pay;
252 my $cust_bill_pay = pop @cust_bill_pay;
253 my $error = $cust_bill_pay->delete;
257 my $new = new FS::cust_refund ( { 'paynum' => $paynum,
260 $error = $new->insert;
262 # if not a batch refund run realtime.
264 $error = $cust_main->realtime_refund_bop( $bop, 'amount' => $refund,
266 'reasonnum' => scalar($cgi->param('reasonnum')),
269 } else { # run cash refund.
271 $_, scalar($cgi->param($_))
272 } fields('cust_refund');
273 my $paynum = $cgi->param('paynum');
274 $paynum =~ /^(\d*)$/ or die "Illegal paynum!";
276 my $cust_pay = qsearchs('cust_pay',{ 'paynum' => $paynum });
277 die "Could not find paynum $paynum" unless $cust_pay;
278 $error = $cust_pay->refund(\%hash);
280 my $new = new FS::cust_refund ( \%hash );
281 $error = $new->insert;