Diff -- /var/www/vhosts/3dshawn.com/affiliate.3dshawn.com/my_credit.cgi

O Operator
Diff

/var/www/vhosts/3dshawn.com/affiliate.3dshawn.com/my_credit.cgi

added on local at 2026-07-01 13:47:35

Added
+117
lines
Removed
-0
lines
Context
0
unchanged
Blobs
from
to c300969878dc
Restore this content →
Unified diff
Naive LCS-based line diff. Additions in emerald, deletions in rose. Both sides decompressed live from BLOB_STORE.
1#!/usr/bin/perl
2#======================================================================
3# WebSTLs -- buyer-facing store-credit history.
4#
5# GET /my_credit.cgi?id=<storefront_id>
6# Shows the signed-in buyer's current store-credit balance + the
7# ledger rows that produced it (grants, applies, reversals). Auth is
8# the buyer session cookie; visitors are bounced to /buyer_login.cgi.
9#======================================================================
10use strict;
11use warnings;
12
13use lib '/var/www/vhosts/3dshawn.com/affiliate.3dshawn.com';
14use CGI;
15use MODS::Template;
16use MODS::DBConnect;
17use MODS::AffSoft::Config;
18use MODS::AffSoft::BuyerAuth;
19use MODS::AffSoft::Themes;
20use MODS::AffSoft::BuyerCredits;
21
22my $q = CGI->new;
23my $form = $q->Vars;
24my $tfile = MODS::Template->new;
25my $db = MODS::DBConnect->new;
26my $cfg = MODS::AffSoft::Config->new;
27my $auth = MODS::AffSoft::BuyerAuth->new;
28my $themes = MODS::AffSoft::Themes->new;
29my $bc = MODS::AffSoft::BuyerCredits->new;
30my $DB = $cfg->settings('database_name');
31
32$| = 1;
33
34my $sid = $form->{id} || $form->{storefront_id} || 0;
35$sid =~ s/[^0-9]//g;
36
37my $dbh = $db->db_connect();
38
39my $buyer = $auth->verify($q, $db, $dbh, $DB);
40unless ($buyer && $buyer->{id}) {
41 $db->db_disconnect($dbh);
42 my $target = $sid ? "/buyer_login.cgi?id=$sid" : '/';
43 print "Status: 302 Found\nLocation: $target\n\n";
44 exit;
45}
46
47$sid ||= $buyer->{storefront_id};
48
49my $store = $db->db_readwrite($dbh, qq~
50 SELECT id, name, subdomain, theme_id FROM `${DB}`.storefronts
51 WHERE id='$sid' LIMIT 1
52~, $ENV{SCRIPT_NAME}, __LINE__);
53unless ($store && $store->{id}) {
54 $db->db_disconnect($dbh);
55 print "Status: 302 Found\nLocation: /\n\n";
56 exit;
57}
58
59my $balance = $bc->balance_cents($db, $dbh, $DB, $store->{id},
60 { buyer_account_id => $buyer->{id} });
61
62my @hist = $bc->history($db, $dbh, $DB, $store->{id},
63 { buyer_account_id => $buyer->{id} }, 100);
64my @rows;
65my %label = (
66 manual_grant => 'Store credit',
67 order_refund => 'Order refund credit',
68 promotional => 'Promotional credit',
69 order_apply => 'Used on order',
70 reversal => 'Order refunded',
71);
72foreach my $h (@hist) {
73 my $a = $h->{amount_cents} || 0;
74 push @rows, {
75 amount_dollars => ($a < 0 ? '-' : '+') . '$' . sprintf('%.2f', abs($a)/100),
76 is_positive => $a > 0 ? 1 : 0,
77 is_negative => $a < 0 ? 1 : 0,
78 reason => $h->{reason},
79 reason_label => $label{$h->{reason}} || $h->{reason},
80 related_order_id => $h->{related_order_id} ? ($h->{related_order_id} + 0) : 0,
81 has_related => $h->{related_order_id} ? 1 : 0,
82 note => _h($h->{note} || ''),
83 created_at => $h->{created_at} || '-',
84 };
85}
86
87my $theme = $themes->by_id($store->{theme_id});
88my $css_vars = $themes->css_vars($theme);
89
90$db->db_disconnect($dbh);
91
92my $tvars = {
93 theme_css_vars => $css_vars,
94 store_id => $store->{id},
95 store_name => _h($store->{name} || $store->{subdomain}),
96 buyer_email => _h($buyer->{email}),
97 buyer_name => _h($buyer->{display_name} || $buyer->{email}),
98 balance_dollars => '$' . sprintf('%.2f', $balance/100),
99 balance_cents => $balance,
100 has_credit => $balance > 0 ? 1 : 0,
101 rows => \@rows,
102 has_rows => scalar(@rows) ? 1 : 0,
103 no_rows => scalar(@rows) ? 0 : 1,
104 my_orders_href => "/my_orders.cgi?id=$store->{id}",
105 back_href => "/store.cgi?id=$store->{id}",
106 logout_href => "/buyer_logout.cgi?id=$store->{id}",
107};
108
109print "Content-Type: text/html; charset=utf-8\nCache-Control: no-store, no-cache, must-revalidate, max-age=0\nPragma: no-cache\nExpires: 0\n\n";
110print join('', $tfile->template('webstls_my_credit.html', $tvars, undef));
111
112sub _h {
113 my $s = shift; $s = '' unless defined $s;
114 $s =~ s/&/&amp;/g; $s =~ s/</&lt;/g; $s =~ s/>/&gt;/g;
115 $s =~ s/"/&quot;/g; $s =~ s/'/&#39;/g;
116 return $s;
117}