2 % $cgi->param('error', $error);
3 <% $cgi->redirect(popurl(2). "recharge_svc.html?". $cgi->query_string ) %>
5 <% header("Package recharged") %>
6 <SCRIPT TYPE="text/javascript">
7 window.top.location.reload();
13 my $conf = new FS::Conf;
16 unless $FS::CurrentUser::CurrentUser->access_right('Recharge customer service');
19 my $svcnum = $cgi->param('svcnum');
20 $svcnum =~ /^(\d+)$/ || die "Illegal svcnum";
24 my $prepaid = $cgi->param('prepaid');
25 $prepaid =~ /^(\w*)$/;
29 my $payby = $cgi->param('payby');
30 $payby =~ /^([A-Z]*)$/;
34 my $svc_acct = qsearchs( 'svc_acct', {'svcnum'=>$svcnum} );
35 $error = "Can't recharge service $svcnum. " unless $svc_acct;
37 my $cust_main = $svc_acct->cust_svc->cust_pkg->cust_main;
39 my $oldAutoCommit = $FS::UID::AutoCommit;
40 local $FS::UID::AutoCommit = 0;
45 #should probably use payby.pm but whatever
46 if ($payby eq 'PREP') {
47 $error = $cust_main->recharge_prepay( $prepaid );
48 } elsif ( $payby =~ /^(CARD|DCRD|CHEK|DCHK|LECB|BILL|COMP)$/ ) {
49 my $part_pkg = $svc_acct->cust_svc->cust_pkg->part_pkg;
50 my $amount = $part_pkg->option('recharge_amount', 1);
51 my %rhash = map { $_ =~ /^recharge_(.*)$/; $1, $part_pkg->option($_) }
52 grep { $part_pkg->option($_, 1) }
53 qw ( recharge_seconds recharge_upbytes recharge_downbytes
54 recharge_totalbytes );
56 my $description = "Recharge";
57 $description .= " $rhash{seconds}s" if $rhash{seconds};
58 $description .= " $rhash{upbytes} up" if $rhash{upbytes};
59 $description .= " $rhash{downbytes} down" if $rhash{downbytes};
60 $description .= " $rhash{totalbytes} total" if $rhash{totalbytes};
62 $error = $cust_main->charge($amount, "Recharge " . $svc_acct->label,
63 $description, $part_pkg->taxclass);
65 $error ||= "invalid $_" foreach grep { $rhash{$_} !~ /^\d*$/ } keys %rhash;
66 if ($part_pkg->option('recharge_reset', 1)) {
67 $error ||= $svc_acct->set_usage(\%rhash, 'null' => 1);
69 $error ||= $svc_acct->recharge(\%rhash);
72 my $old_balance = $cust_main->balance;
73 $error ||= $cust_main->bill;
74 $error ||= $cust_main->apply_payments_and_credits;
75 my $bill_error = $cust_main->collect('realtime' => 1) unless $error;
76 $error ||= "Failed to collect - $bill_error"
77 if $cust_main->balance > $old_balance && $cust_main->balance > 0
81 $error = "fatal error - unknown payby: $payby";
87 $dbh->rollback if $oldAutoCommit;
89 $dbh->commit or die $dbh->errstr if $oldAutoCommit;