9175eb1367b3f2bd752ced9744afabd6f4242d9a
[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
59 ## get cust pay info if paynum exists
60 if ( $cgi->param('paynum') > 0) {
61   $cust_pay = qsearchs({
62     'table'     => 'cust_pay',
63     'hashref'   => { 'paynum' => $cgi->param('paynum') },
64     'select'    => 'cust_pay.*, cust_pay_batch.payname ',
65     'addl_from' => "left join cust_pay_batch on cust_pay_batch.batchnum = cust_pay.batchnum and cust_pay_batch.custnum = $custnum ",
66   });
67 }
68
69 if ( (my $custpaybynum = scalar($cgi->param('custpaybynum'))) > 0 ) {
70
71   ##
72   # use stored cust_payby info
73   ##
74
75   $cust_payby = qsearchs('cust_payby', { custnum      => $custnum,
76                                             custpaybynum => $custpaybynum, } )
77     or die "unknown custpaybynum $custpaybynum";
78
79   # not needed for realtime_bop, but still needed for batch_card
80   $payinfo = $cust_payby->payinfo;
81   $paymask = $cust_payby->paymask;
82   $paycvv = $cust_payby->paycvv; # pass it if we got it, running a transaction will clear it
83   ( $month, $year ) = $cust_payby->paydate_mon_year;
84   $payname = $cust_payby->payname;
85   $cgi->param(-name=>"paytype", -value=>$cust_payby->paytype) unless $cgi->param("paytype");
86
87 } elsif ( $cgi->param('paynum') > 0) {
88
89   $payinfo = $cust_pay->payinfo;
90   $payname = $cust_pay->payname;
91
92 } else {
93
94   ##
95   # use new info
96   ##
97
98   $cgi->param('year') =~ /^(\d+)$/
99     or errorpage("illegal year ". $cgi->param('year'));
100   $year = $1;
101
102   $cgi->param('month') =~ /^(\d+)$/
103     or errorpage("illegal month ". $cgi->param('month'));
104   $month = $1;
105
106   $cgi->param('payname') =~ /^([\w \,\.\-\']+)$/
107     or errorpage(gettext('illegal_name'). " payname: ". $cgi->param('payname'));
108   $payname = $1;
109
110   if ( $payby eq 'CHEK' ) {
111
112     $cgi->param('payinfo1') =~ /^(\d+)$/
113       or errorpage("Illegal account number ". $cgi->param('payinfo1'));
114     my $payinfo1 = $1;
115     $cgi->param('payinfo2') =~ /^(\d+)$/
116       or errorpage("Illegal ABA/routing number ". $cgi->param('payinfo2'));
117     my $payinfo2 = $1;
118     if ( $conf->config('echeck-country') eq 'CA' ) {
119       $cgi->param('payinfo3') =~ /^(\d{5})$/
120         or errorpage("Illegal branch number ". $cgi->param('payinfo2'));
121       $payinfo2 = "$1.$payinfo2";
122     }
123     $payinfo = $payinfo1 . '@'. $payinfo2;
124
125   } elsif ( $payby eq 'CARD' ) {
126
127     $payinfo = $cgi->param('payinfo');
128
129     $payinfo =~ s/\D//g;
130     $payinfo =~ /^(\d{13,19}|\d{8,9})$/
131       or errorpage(gettext('invalid_card'));
132     $payinfo = $1;
133     validate($payinfo)
134       or errorpage(gettext('invalid_card'));
135
136     unless ( $cust_main->tokenized($payinfo) ) { #token
137
138       my $cardtype = cardtype($payinfo);
139
140       errorpage(gettext('unknown_card_type'))
141         if $cardtype eq "Unknown";
142
143       my %bop_card_types = map { $_=>1 } values %{ card_types() };
144       errorpage("$cardtype not accepted") unless $bop_card_types{$cardtype};
145
146     }
147
148     if ( length($cgi->param('paycvv') ) ) {
149       if ( cardtype($payinfo) eq 'American Express card' ) {
150         $cgi->param('paycvv') =~ /^(\d{4})$/
151           or errorpage("CVV2 (CID) for American Express cards is four digits.");
152         $paycvv = $1;
153       } else {
154         $cgi->param('paycvv') =~ /^(\d{3})$/
155           or errorpage("CVV2 (CVC2/CID) is three digits.");
156         $paycvv = $1;
157       }
158     } elsif ( $conf->exists('backoffice-require_cvv') ){
159       errorpage("CVV2 is required");
160     }
161
162   } else {
163     die "unknown payby $payby";
164   }
165
166   # save first, for proper tokenization
167   if ( $cgi->param('save') ) {
168
169     my %saveopt;
170     if ( $payby eq 'CARD' ) {
171       my $bill_location = FS::cust_location->new;
172       $bill_location->set( $_ => scalar($cgi->param($_)) )
173         foreach @{$payby2fields{$payby}};
174       $saveopt{'bill_location'} = $bill_location;
175       $saveopt{'paycvv'} = $paycvv; # save_cust_payby contains conf logic for when to use this
176       $saveopt{'paydate'} = "$year-$month-01";
177     } else {
178       # ss/stateid/stateid_state won't be saved, but should be harmless to pass
179       %saveopt = map { $_ => scalar($cgi->param($_)) } @{$payby2fields{$payby}};
180     }
181
182     my $error = $cust_main->save_cust_payby(
183       'saved_cust_payby' => \$cust_payby,
184       'payment_payby' => $payby,
185       'auto'          => scalar($cgi->param('auto')),
186       'weight'        => scalar($cgi->param('weight')),
187       'payinfo'       => $payinfo,
188       'payname'       => $payname,
189       %saveopt
190     );
191
192     errorpage("error saving info, payment not processed: $error")
193       if $error;
194
195   } elsif ( $payby eq 'CARD' ) { # not saving
196
197     $paymask = FS::payinfo_Mixin->mask_payinfo('CARD',$payinfo); # for untokenized but tokenizable payinfo
198
199   }
200
201 }
202
203 ##
204 # now run the refund
205 ##
206
207   $cgi->param('refund') =~ /^(\d*)(\.\d{2})?$/
208     or die "illegal refund amount ". $cgi->param('refund');
209   my $refund = "$1$2";
210   $cgi->param('paynum') =~ /^(\d*)$/ or die "Illegal paynum!";
211   my $paynum = $1;
212   my $paydate;
213   unless ($paynum) {
214     if ($cust_payby->paydate) { $paydate = "$year-$month-01"; }
215     else { $paydate = "2037-12-01"; }
216   }
217
218   if ( $cgi->param('batch') ) {
219     $paydate = "2037-12-01" unless $paydate;
220     $error ||= $cust_main->batch_card(
221                                      'payby'    => $payby,
222                                      'amount'   => $refund,
223                                      'payinfo'  => $payinfo,
224                                      'paydate'  => $paydate,
225                                      'payname'  => $payname,
226                                      'paycode'  => 'C',
227                                      map { $_ => scalar($cgi->param($_)) }
228                                        @{$payby2fields{$payby}}
229                                    );
230     errorpage($error) if $error;
231
232     my %hash = map {
233       $_, scalar($cgi->param($_))
234     } fields('cust_refund');
235
236     ## unapply payment before creating refund.
237     while ( $cust_pay && $cust_pay->unapplied < $refund ) {
238       my @cust_bill_pay = $cust_pay->cust_bill_pay;
239       last unless @cust_bill_pay;
240       my $cust_bill_pay = pop @cust_bill_pay;
241       my $error = $cust_bill_pay->delete;
242       last if $error;
243     }
244
245     my $new = new FS::cust_refund ( { 'paynum' => $paynum,
246                                       %hash,
247                                   } );
248     $error = $new->insert;
249
250   # if not a batch refund run realtime.
251   } else {
252     $error = $cust_main->realtime_refund_bop( $bop, 'amount' => $refund,
253                                                   'paynum' => $paynum,
254                                                   'reasonnum' => scalar($cgi->param('reasonnum')),
255                                                   %options );
256   }
257 } else { # run cash refund.
258   my %hash = map {
259     $_, scalar($cgi->param($_))
260   } fields('cust_refund');
261   my $paynum = $cgi->param('paynum');
262   $paynum =~ /^(\d*)$/ or die "Illegal paynum!";
263   if ($paynum) {
264     my $cust_pay = qsearchs('cust_pay',{ 'paynum' => $paynum });
265     die "Could not find paynum $paynum" unless $cust_pay;
266     $error = $cust_pay->refund(\%hash);
267   } else {
268     my $new = new FS::cust_refund ( \%hash );
269     $error = $new->insert;
270   }
271 }
272
273 </%init>