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_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;
81 $cgi->param('year') =~ /^(\d+)$/
82 or errorpage("illegal year ". $cgi->param('year'));
85 $cgi->param('month') =~ /^(\d+)$/
86 or errorpage("illegal month ". $cgi->param('month'));
89 $cgi->param('payname') =~ /^([\w \,\.\-\']+)$/
90 or errorpage(gettext('illegal_name'). " payname: ". $cgi->param('payname'));
93 if ( $payby eq 'CHEK' ) {
95 $cgi->param('payinfo1') =~ /^(\d+)$/
96 or errorpage("Illegal account number ". $cgi->param('payinfo1'));
98 $cgi->param('payinfo2') =~ /^(\d+)$/
99 or errorpage("Illegal ABA/routing number ". $cgi->param('payinfo2'));
101 if ( $conf->config('echeck-country') eq 'CA' ) {
102 $cgi->param('payinfo3') =~ /^(\d{5})$/
103 or errorpage("Illegal branch number ". $cgi->param('payinfo2'));
104 $payinfo2 = "$1.$payinfo2";
106 $payinfo = $payinfo1 . '@'. $payinfo2;
108 } elsif ( $payby eq 'CARD' ) {
110 $payinfo = $cgi->param('payinfo');
113 $payinfo =~ /^(\d{13,19}|\d{8,9})$/
114 or errorpage(gettext('invalid_card'));
117 or errorpage(gettext('invalid_card'));
119 unless ( $cust_main->tokenized($payinfo) ) { #token
121 my $cardtype = cardtype($payinfo);
123 errorpage(gettext('unknown_card_type'))
124 if $cardtype eq "Unknown";
126 my %bop_card_types = map { $_=>1 } values %{ card_types() };
127 errorpage("$cardtype not accepted") unless $bop_card_types{$cardtype};
131 if ( length($cgi->param('paycvv') ) ) {
132 if ( cardtype($payinfo) eq 'American Express card' ) {
133 $cgi->param('paycvv') =~ /^(\d{4})$/
134 or errorpage("CVV2 (CID) for American Express cards is four digits.");
137 $cgi->param('paycvv') =~ /^(\d{3})$/
138 or errorpage("CVV2 (CVC2/CID) is three digits.");
141 } elsif ( $conf->exists('backoffice-require_cvv') ){
142 errorpage("CVV2 is required");
146 die "unknown payby $payby";
149 # save first, for proper tokenization
150 if ( $cgi->param('save') ) {
153 if ( $payby eq 'CARD' ) {
154 my $bill_location = FS::cust_location->new;
155 $bill_location->set( $_ => scalar($cgi->param($_)) )
156 foreach @{$payby2fields{$payby}};
157 $saveopt{'bill_location'} = $bill_location;
158 $saveopt{'paycvv'} = $paycvv; # save_cust_payby contains conf logic for when to use this
159 $saveopt{'paydate'} = "$year-$month-01";
161 # ss/stateid/stateid_state won't be saved, but should be harmless to pass
162 %saveopt = map { $_ => scalar($cgi->param($_)) } @{$payby2fields{$payby}};
165 my $error = $cust_main->save_cust_payby(
166 'saved_cust_payby' => \$cust_payby,
167 'payment_payby' => $payby,
168 'auto' => scalar($cgi->param('auto')),
169 'weight' => scalar($cgi->param('weight')),
170 'payinfo' => $payinfo,
171 'payname' => $payname,
175 errorpage("error saving info, payment not processed: $error")
178 } elsif ( $payby eq 'CARD' ) { # not saving
180 $paymask = FS::payinfo_Mixin->mask_payinfo('CARD',$payinfo); # for untokenized but tokenizable payinfo
190 $cgi->param('refund') =~ /^(\d*)(\.\d{2})?$/
191 or die "illegal refund amount ". $cgi->param('refund');
193 $cgi->param('paynum') =~ /^(\d*)$/ or die "Illegal paynum!";
195 my $paydate = $cgi->param('exp_year'). '-'. $cgi->param('exp_month'). '-01';
196 $options{'paydate'} = $paydate if $paydate =~ /^\d{2,4}-\d{1,2}-01$/;
198 if ( $cgi->param('batch') ) {
200 $error ||= $cust_main->batch_card(
203 'payinfo' => $payinfo,
204 'paydate' => "$year-$month-01",
205 'payname' => $payname,
207 map { $_ => scalar($cgi->param($_)) }
208 @{$payby2fields{$payby}}
210 errorpage($error) if $error;
212 #### post refund #####
214 $_, scalar($cgi->param($_))
215 } fields('cust_refund');
216 $paynum = $cgi->param('paynum');
217 $paynum =~ /^(\d*)$/ or die "Illegal paynum!";
219 my $cust_pay = qsearchs('cust_pay',{ 'paynum' => $paynum });
220 die "Could not find paynum $paynum" unless $cust_pay;
221 $error = $cust_pay->refund(\%hash);
223 my $new = new FS::cust_refund ( \%hash );
224 $error = $new->insert;
226 # if not a batch refund run realtime.
228 $error = $cust_main->realtime_refund_bop( $bop, 'amount' => $refund,
230 'reasonnum' => scalar($cgi->param('reasonnum')),
235 $_, scalar($cgi->param($_))
236 } fields('cust_refund');
237 my $paynum = $cgi->param('paynum');
238 $paynum =~ /^(\d*)$/ or die "Illegal paynum!";
240 my $cust_pay = qsearchs('cust_pay',{ 'paynum' => $paynum });
241 die "Could not find paynum $paynum" unless $cust_pay;
242 $error = $cust_pay->refund(\%hash);
244 my $new = new FS::cust_refund ( \%hash );
245 $error = $new->insert;