Merge branch 'master' of git.freeside.biz:/home/git/freeside
[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_pay, $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   $cgi->param(-name=>"paytype", -value=>$cust_payby->paytype) unless $cgi->param("paytype");
75
76 } elsif ( $cgi->param('paynum') > 0) {
77
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 ",
83   });
84   $payinfo = $cust_pay->payinfo;
85   $payname = $cust_pay->payname;
86
87 } else {
88
89   ##
90   # use new info
91   ##
92
93   $cgi->param('year') =~ /^(\d+)$/
94     or errorpage("illegal year ". $cgi->param('year'));
95   $year = $1;
96
97   $cgi->param('month') =~ /^(\d+)$/
98     or errorpage("illegal month ". $cgi->param('month'));
99   $month = $1;
100
101   $cgi->param('payname') =~ /^([\w \,\.\-\']+)$/
102     or errorpage(gettext('illegal_name'). " payname: ". $cgi->param('payname'));
103   $payname = $1;
104
105   if ( $payby eq 'CHEK' ) {
106
107     $cgi->param('payinfo1') =~ /^(\d+)$/
108       or errorpage("Illegal account number ". $cgi->param('payinfo1'));
109     my $payinfo1 = $1;
110     $cgi->param('payinfo2') =~ /^(\d+)$/
111       or errorpage("Illegal ABA/routing number ". $cgi->param('payinfo2'));
112     my $payinfo2 = $1;
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";
117     }
118     $payinfo = $payinfo1 . '@'. $payinfo2;
119
120   } elsif ( $payby eq 'CARD' ) {
121
122     $payinfo = $cgi->param('payinfo');
123
124     $payinfo =~ s/\D//g;
125     $payinfo =~ /^(\d{13,19}|\d{8,9})$/
126       or errorpage(gettext('invalid_card'));
127     $payinfo = $1;
128     validate($payinfo)
129       or errorpage(gettext('invalid_card'));
130
131     unless ( $cust_main->tokenized($payinfo) ) { #token
132
133       my $cardtype = cardtype($payinfo);
134
135       errorpage(gettext('unknown_card_type'))
136         if $cardtype eq "Unknown";
137
138       my %bop_card_types = map { $_=>1 } values %{ card_types() };
139       errorpage("$cardtype not accepted") unless $bop_card_types{$cardtype};
140
141     }
142
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.");
147         $paycvv = $1;
148       } else {
149         $cgi->param('paycvv') =~ /^(\d{3})$/
150           or errorpage("CVV2 (CVC2/CID) is three digits.");
151         $paycvv = $1;
152       }
153     } elsif ( $conf->exists('backoffice-require_cvv') ){
154       errorpage("CVV2 is required");
155     }
156
157   } else {
158     die "unknown payby $payby";
159   }
160
161   # save first, for proper tokenization
162   if ( $cgi->param('save') ) {
163
164     my %saveopt;
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";
172     } else {
173       # ss/stateid/stateid_state won't be saved, but should be harmless to pass
174       %saveopt = map { $_ => scalar($cgi->param($_)) } @{$payby2fields{$payby}};
175     }
176
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,
184       %saveopt
185     );
186
187     errorpage("error saving info, payment not processed: $error")
188       if $error;
189
190   } elsif ( $payby eq 'CARD' ) { # not saving
191
192     $paymask = FS::payinfo_Mixin->mask_payinfo('CARD',$payinfo); # for untokenized but tokenizable payinfo
193
194   }
195
196 }
197
198 ##
199 # now run the refund
200 ##
201
202   $cgi->param('refund') =~ /^(\d*)(\.\d{2})?$/
203     or die "illegal refund amount ". $cgi->param('refund');
204   my $refund = "$1$2";
205   $cgi->param('paynum') =~ /^(\d*)$/ or die "Illegal paynum!";
206   my $paynum = $1;
207   my $paydate;
208   unless ($paynum) {
209     if ($cust_payby->paydate) { $paydate = "$year-$month-01"; }
210     else { $paydate = "2037-12-01"; }
211   }
212
213   if ( $cgi->param('batch') ) {
214     $paydate = "2037-12-01" unless $paydate;
215     $error ||= $cust_main->batch_card(
216                                      'payby'    => $payby,
217                                      'amount'   => $refund,
218                                      'payinfo'  => $payinfo,
219                                      'paydate'  => $paydate,
220                                      'payname'  => $payname,
221                                      'paycode'  => 'C',
222                                      map { $_ => scalar($cgi->param($_)) }
223                                        @{$payby2fields{$payby}}
224                                    );
225     errorpage($error) if $error;
226
227     my %hash = map {
228       $_, scalar($cgi->param($_))
229     } fields('cust_refund');
230
231     my $new = new FS::cust_refund ( { 'paynum' => $paynum,
232                                       %hash,
233                                   } );
234     $error = $new->insert;
235
236   # if not a batch refund run realtime.
237   } else {
238     $error = $cust_main->realtime_refund_bop( $bop, 'amount' => $refund,
239                                                   'paynum' => $paynum,
240                                                   'reasonnum' => scalar($cgi->param('reasonnum')),
241                                                   %options );
242   }
243 } else { # run cash refund.
244   my %hash = map {
245     $_, scalar($cgi->param($_))
246   } fields('cust_refund');
247   my $paynum = $cgi->param('paynum');
248   $paynum =~ /^(\d*)$/ or die "Illegal paynum!";
249   if ($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);
253   } else {
254     my $new = new FS::cust_refund ( \%hash );
255     $error = $new->insert;
256   }
257 }
258
259 </%init>