replace FS::CGI::header function with header-popup component, related to #71249
[freeside.git] / httemplate / misc / process / recharge_svc.html
1 %if ($error) {
2 %  $cgi->param('error', $error);
3 <% $cgi->redirect(popurl(2). "recharge_svc.html?". $cgi->query_string ) %>
4 %} else {
5 <& /elements/header-popup.html, "Package recharged" &>
6   <SCRIPT TYPE="text/javascript">
7     topreload();
8   </SCRIPT>
9   </BODY></HTML>
10 %}
11 <%init>
12
13 my $conf = new FS::Conf;
14
15 die "access denied"
16   unless $FS::CurrentUser::CurrentUser->access_right('Recharge customer service');
17
18 #untaint svcnum
19 my $svcnum = $cgi->param('svcnum');
20 $svcnum =~ /^(\d+)$/ || die "Illegal svcnum";
21 $svcnum = $1;
22
23 #untaint prepaid
24 my $prepaid = $cgi->param('prepaid');
25 $prepaid =~ s/\W//g;
26 $prepaid =~ /^(\w*)$/;
27 $prepaid = $1;
28
29 #untaint payby
30 my $payby = $cgi->param('payby');
31 $payby =~ /^([A-Z]*)$/;
32 $payby = $1;
33
34 my $error = '';
35 my $svc_acct = qsearchs( 'svc_acct', {'svcnum'=>$svcnum} );
36 $error = "Can't recharge service $svcnum. " unless $svc_acct;
37
38 my $cust_main = $svc_acct->cust_svc->cust_pkg->cust_main;
39
40 my $oldAutoCommit = $FS::UID::AutoCommit;
41 local $FS::UID::AutoCommit = 0;
42 my $dbh = dbh;
43
44 unless ($error) {
45
46   #should probably use payby.pm but whatever
47   if ($payby eq 'PREP') {
48     $error = $cust_main->recharge_prepay( $prepaid );
49   } elsif ( $payby =~ /^(CARD|DCRD|CHEK|DCHK|LECB|BILL|COMP)$/ ) {
50     my $part_pkg = $svc_acct->cust_svc->cust_pkg->part_pkg;
51     my $amount = $part_pkg->option('recharge_amount', 1);
52     my %rhash = map { $_ =~ /^recharge_(.*)$/; $1, $part_pkg->option($_) }
53       grep { $part_pkg->option($_, 1) }
54       qw ( recharge_seconds recharge_upbytes recharge_downbytes
55            recharge_totalbytes );
56
57     my $description = "Recharge";
58     $description .= " $rhash{seconds}s" if $rhash{seconds};
59     $description .= " $rhash{upbytes} up" if $rhash{upbytes};
60     $description .= " $rhash{downbytes} down" if $rhash{downbytes};
61     $description .= " $rhash{totalbytes} total" if $rhash{totalbytes};
62
63     $error = $cust_main->charge($amount, "Recharge " . $svc_acct->label,
64                                 $description, $part_pkg->taxclass);
65
66     $error ||= "invalid $_" foreach grep { $rhash{$_} !~ /^\d*$/ } keys %rhash;
67     if ($part_pkg->option('recharge_reset', 1)) {
68       $error ||= $svc_acct->set_usage(\%rhash, 'null' => 1);
69     }else{
70       $error ||= $svc_acct->recharge(\%rhash);
71     }
72
73     my $old_balance = $cust_main->balance;
74     $error ||= $cust_main->bill;
75     $error ||= $cust_main->apply_payments_and_credits;
76     my $bill_error = $cust_main->collect('realtime' => 1) unless $error;
77     $error ||= "Failed to collect - $bill_error"
78       if $cust_main->balance > $old_balance && $cust_main->balance > 0
79           && $payby ne 'BILL';
80
81   } else {
82     $error = "fatal error - unknown payby: $payby";
83   }
84
85 }
86
87 if ($error) {
88   $dbh->rollback if $oldAutoCommit;
89 } else {
90   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
91 }
92
93 </%init>