0a3d550360c0a929ec36d66ec170ee0b608ac612
[freeside.git] / httemplate / edit / process / cust_refund.cgi
1 %if ( $error ) {
2 %  $cgi->param('error', $error);
3 <% $cgi->redirect(popurl(2). "cust_refund.cgi?". $cgi->query_string ) %>
4 %} else {
5 %
6 %  if ( $link eq 'popup' ) {
7 %
8 <& /elements/header-popup.html, 'Refund entered' &>
9     <SCRIPT TYPE="text/javascript">
10       topreload();
11     </SCRIPT>
12
13     </BODY></HTML>
14 %  } else {
15 <% $cgi->redirect(popurl(3). "view/cust_main.cgi?custnum=$custnum;show=payment_history") %>
16 %  }
17 %}
18 <%init>
19
20 die "access denied"
21   unless $FS::CurrentUser::CurrentUser->access_right('Refund payment')
22       || $FS::CurrentUser::CurrentUser->access_right('Post refund');
23
24 my $conf = new FS::Conf;
25
26 $cgi->param('custnum') =~ /^(\d*)$/ or die "Illegal custnum!";
27 my $custnum = $1;
28 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
29   or die "unknown custnum $custnum";
30
31 my $link    = $cgi->param('popup') ? 'popup' : '';
32
33 my $payby = $cgi->param('payby');
34
35 die "access denied"
36   unless $FS::CurrentUser::CurrentUser->refund_access_right($payby);
37
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;
41
42 if ( $error ) {
43   # do nothing
44 } elsif ( $payby =~ /^(CARD|CHEK)$/ ) { 
45   my %options = ();
46   my $bop = $FS::payby::payby2bop{$1};
47
48   my %payby2fields = (
49   'CARD' => [ qw( address1 address2 city county state zip country ) ],
50   'CHEK' => [ qw( ss paytype paystate stateid stateid_state ) ],
51   );
52   my %type = ( 'CARD' => 'credit card',
53              'CHEK' => 'electronic check (ACH)',
54              );
55
56 my( $cust_payby, $payinfo, $paycvv, $month, $year, $payname );
57 my $paymask = '';
58 if ( (my $custpaybynum = scalar($cgi->param('custpaybynum'))) > 0 ) {
59
60   ##
61   # use stored cust_payby info
62   ##
63
64   $cust_payby = qsearchs('cust_payby', { custnum      => $custnum,
65                                             custpaybynum => $custpaybynum, } )
66     or die "unknown custpaybynum $custpaybynum";
67
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
75 } else {
76
77   ##
78   # use new info
79   ##
80
81   $cgi->param('year') =~ /^(\d+)$/
82     or errorpage("illegal year ". $cgi->param('year'));
83   $year = $1;
84
85   $cgi->param('month') =~ /^(\d+)$/
86     or errorpage("illegal month ". $cgi->param('month'));
87   $month = $1;
88
89   $cgi->param('payname') =~ /^([\w \,\.\-\']+)$/
90     or errorpage(gettext('illegal_name'). " payname: ". $cgi->param('payname'));
91   $payname = $1;
92
93   if ( $payby eq 'CHEK' ) {
94
95     $cgi->param('payinfo1') =~ /^(\d+)$/
96       or errorpage("Illegal account number ". $cgi->param('payinfo1'));
97     my $payinfo1 = $1;
98     $cgi->param('payinfo2') =~ /^(\d+)$/
99       or errorpage("Illegal ABA/routing number ". $cgi->param('payinfo2'));
100     my $payinfo2 = $1;
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";
105     }
106     $payinfo = $payinfo1 . '@'. $payinfo2;
107
108   } elsif ( $payby eq 'CARD' ) {
109
110     $payinfo = $cgi->param('payinfo');
111
112     $payinfo =~ s/\D//g;
113     $payinfo =~ /^(\d{13,19}|\d{8,9})$/
114       or errorpage(gettext('invalid_card'));
115     $payinfo = $1;
116     validate($payinfo)
117       or errorpage(gettext('invalid_card'));
118
119     unless ( $cust_main->tokenized($payinfo) ) { #token
120
121       my $cardtype = cardtype($payinfo);
122
123       errorpage(gettext('unknown_card_type'))
124         if $cardtype eq "Unknown";
125
126       my %bop_card_types = map { $_=>1 } values %{ card_types() };
127       errorpage("$cardtype not accepted") unless $bop_card_types{$cardtype};
128
129     }
130
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.");
135         $paycvv = $1;
136       } else {
137         $cgi->param('paycvv') =~ /^(\d{3})$/
138           or errorpage("CVV2 (CVC2/CID) is three digits.");
139         $paycvv = $1;
140       }
141     } elsif ( $conf->exists('backoffice-require_cvv') ){
142       errorpage("CVV2 is required");
143     }
144
145   } else {
146     die "unknown payby $payby";
147   }
148
149   # save first, for proper tokenization
150   if ( $cgi->param('save') ) {
151
152     my %saveopt;
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";
160     } else {
161       # ss/stateid/stateid_state won't be saved, but should be harmless to pass
162       %saveopt = map { $_ => scalar($cgi->param($_)) } @{$payby2fields{$payby}};
163     }
164
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,
172       %saveopt
173     );
174
175     errorpage("error saving info, payment not processed: $error")
176       if $error;
177
178   } elsif ( $payby eq 'CARD' ) { # not saving
179
180     $paymask = FS::payinfo_Mixin->mask_payinfo('CARD',$payinfo); # for untokenized but tokenizable payinfo
181
182   }
183
184 }
185
186 ##
187 # now run the refund
188 ##
189
190   $cgi->param('refund') =~ /^(\d*)(\.\d{2})?$/
191     or die "illegal refund amount ". $cgi->param('refund');
192   my $refund = "$1$2";
193   $cgi->param('paynum') =~ /^(\d*)$/ or die "Illegal paynum!";
194   my $paynum = $1;
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$/;
197
198   if ( $cgi->param('batch') ) {
199
200     $error ||= $cust_main->batch_card(
201                                      'payby'    => $payby,
202                                      'amount'   => $refund,
203                                      'payinfo'  => $payinfo,
204                                      'paydate'  => "$year-$month-01",
205                                      'payname'  => $payname,
206                                      'paycode'  => 'C',
207                                      map { $_ => scalar($cgi->param($_)) }
208                                        @{$payby2fields{$payby}}
209                                    );
210     errorpage($error) if $error;
211
212 #### post refund #####
213     my %hash = map {
214       $_, scalar($cgi->param($_))
215     } fields('cust_refund');
216     $paynum = $cgi->param('paynum');
217     $paynum =~ /^(\d*)$/ or die "Illegal paynum!";
218     if ($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);
222     } else {
223       my $new = new FS::cust_refund ( \%hash );
224       $error = $new->insert;
225     }
226     # if not a batch refund run realtime.
227   } else {
228     $error = $cust_main->realtime_refund_bop( $bop, 'amount' => $refund,
229                                                   'paynum' => $paynum,
230                                                   'reasonnum' => scalar($cgi->param('reasonnum')),
231                                                   %options );
232   }
233 } else {
234   my %hash = map {
235     $_, scalar($cgi->param($_))
236   } fields('cust_refund');
237   my $paynum = $cgi->param('paynum');
238   $paynum =~ /^(\d*)$/ or die "Illegal paynum!";
239   if ($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);
243   } else {
244     my $new = new FS::cust_refund ( \%hash );
245     $error = $new->insert;
246   }
247 }
248
249 </%init>