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">
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');
26 $prepaid =~ /^(\w*)$/;
30 my $payby = $cgi->param('payby');
31 $payby =~ /^([A-Z]*)$/;
35 my $svc_acct = qsearchs( 'svc_acct', {'svcnum'=>$svcnum} );
36 $error = "Can't recharge service $svcnum. " unless $svc_acct;
38 my $cust_main = $svc_acct->cust_svc->cust_pkg->cust_main;
40 my $oldAutoCommit = $FS::UID::AutoCommit;
41 local $FS::UID::AutoCommit = 0;
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 );
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};
63 $error = $cust_main->charge($amount, "Recharge " . $svc_acct->label,
64 $description, $part_pkg->taxclass);
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);
70 $error ||= $svc_acct->recharge(\%rhash);
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
82 $error = "fatal error - unknown payby: $payby";
88 $dbh->rollback if $oldAutoCommit;
90 $dbh->commit or die $dbh->errstr if $oldAutoCommit;