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