1 # Copyright 2004-2007 Interchange Development Group and others
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version. See the LICENSE file for details.
8 # $Id: pay_cert_redeem.tag,v 1.4 2007-08-09 13:40:53 pajamian Exp $
10 UserTag pay-cert-redeem Order certs
11 UserTag pay-cert-redeem addAttr
12 UserTag pay-cert-redeem Routine <<EOR
14 my ($certs, $opt) = @_;
16 my $ctab = $opt->{table} || 'pay_certs';
17 my $cdb = dbref($ctab)
18 or die errmsg("No payment cert table '%s'", $ctab);
21 $opt->{set_scratch} = 'amount_remaining' unless defined $opt->{set_scratch};
23 my $svar = $opt->{set_scratch};
28 $certs ||= $::Scratch->{pay_certs_to_capture};
30 my @certs = split /[\s,\0]+/, $certs;
32 foreach my $code (@certs) {
33 my $success = $Tag->pay_cert({ capture => 1, tid => $code });
45 "Voided capture tid %s due to capture error on %s",
54 my $total_cost = round_to_frac_digits($Tag->total_cost( { noformat => 1 }));
55 my $remaining = $total_cost;
57 $certs ||= $::Values->{use_pay_cert} || $::Scratch->{pay_cert_code};
58 return $remaining unless $certs;
59 my @certs = split /[\s,\0]+/, $certs;
61 foreach my $code (@certs) {
62 last if $remaining <= 0;
63 my $this = $cdb->field($code, 'amount');
65 if($this < $remaining) {
78 my $tid = $Tag->pay_cert($o);
81 #::logDebug("authorized pay_cert=$code amount=$amount tid=$tid");
84 #::logDebug("failed to auth pay_cert=$code amount=$amount tid=$tid");
92 "Voided authorization tid %s due to auth error on %s",
98 die errmsg("failed to authorize pay_cert %s", $code)
104 $::Scratch->{pay_certs_to_capture} = join ",", @tid;
105 if($opt->{set_scratch}) {
106 $::Scratch->{$svar} = $remaining;
108 return $opt->{success} if $opt->{success};