Diff -- /var/www/vhosts/webstls.com/httpdocs/my_subscription.cgi
Diff

/var/www/vhosts/webstls.com/httpdocs/my_subscription.cgi

added on WebSTLs (webstls.com) at 2026-07-01 22:26:43

Added
+100
lines
Removed
-0
lines
Context
0
unchanged
Blobs
from
to 66dc776146ed
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's "My Subscriptions" page (one per storefront).
4#======================================================================
5use strict;
6use warnings;
7
8use lib '/var/www/vhosts/webstls.com/httpdocs';
9use CGI;
10use MODS::Template;
11use MODS::DBConnect;
12use MODS::WebSTLs::Config;
13use MODS::WebSTLs::Themes;
14use MODS::WebSTLs::BuyerAuth;
15use MODS::WebSTLs::Subscriptions;
16
17$| = 1;
18
19my $q = CGI->new;
20my $form = $q->Vars;
21my $tfile = MODS::Template->new;
22my $db = MODS::DBConnect->new;
23my $cfg = MODS::WebSTLs::Config->new;
24my $themes= MODS::WebSTLs::Themes->new;
25my $bauth = MODS::WebSTLs::BuyerAuth->new;
26my $sub = MODS::WebSTLs::Subscriptions->new;
27my $DB = $cfg->settings('database_name');
28
29my $sid = $form->{id} || $form->{storefront_id} || 0;
30$sid =~ s/[^0-9]//g;
31unless ($sid) { print "Status: 302 Found\nLocation: /\n\n"; exit; }
32
33my $dbh = $db->db_connect();
34my $buyer = $bauth->verify($q, $db, $dbh, $DB);
35unless ($buyer && $buyer->{id}) {
36 $db->db_disconnect($dbh);
37 print "Status: 302 Found\nLocation: /buyer_login.cgi?id=$sid\n\n"; exit;
38}
39
40# POST: cancel.
41if (($ENV{REQUEST_METHOD} || '') eq 'POST' && ($form->{_act} || '') eq 'cancel') {
42 my $sub_id = $form->{sub_id}; $sub_id =~ s/[^0-9]//g;
43 $sub->cancel_subscription($db, $dbh, $DB, $sub_id, $buyer->{id});
44 $db->db_disconnect($dbh);
45 print "Status: 302 Found\nLocation: /my_subscription.cgi?id=$sid&canceled=1\n\n"; exit;
46}
47
48my $store = $db->db_readwrite($dbh, qq~
49 SELECT id, name, subdomain, theme_id FROM `${DB}`.storefronts WHERE id='$sid' LIMIT 1
50~, $ENV{SCRIPT_NAME}, __LINE__);
51my $css = $themes->css_vars($themes->by_id($store->{theme_id}));
52
53# All buyer's subs at this storefront.
54my $subs = $sub->buyer_subscriptions_for($db, $dbh, $DB, $buyer->{id});
55my @rows;
56foreach my $s (@$subs) {
57 next unless ($s->{seller_id} || 0);
58 # Match storefront by joining the plan
59 my $owner = $db->db_readwrite($dbh, qq~
60 SELECT storefront_id FROM `${DB}`.subscription_plans WHERE id='$s->{plan_id}' LIMIT 1
61 ~, $ENV{SCRIPT_NAME}, __LINE__);
62 next unless $owner && ($owner->{storefront_id} || 0) eq $sid;
63 push @rows, {
64 id => $s->{id},
65 plan_name => _h($s->{plan_name}),
66 kind => $s->{plan_kind},
67 kind_label => $s->{plan_kind} eq 'ship_monthly' ? 'Ship monthly' : 'Download quota',
68 status => $s->{status},
69 is_active => $s->{status} eq 'active' ? 1 : 0,
70 is_canceled => $s->{status} eq 'canceled' ? 1 : 0,
71 price_label => '$' . sprintf('%.2f', ($s->{price_cents} || 0) / 100),
72 cadence => $s->{cadence_days},
73 quota => $s->{quota_per_period},
74 used => $s->{downloads_used_this_period} || 0,
75 remaining => ($s->{quota_per_period} || 0) - ($s->{downloads_used_this_period} || 0),
76 period_end => $s->{current_period_end} || '',
77 };
78}
79
80$db->db_disconnect($dbh);
81
82my $tvars = {
83 theme_css_vars => $css,
84 store_id => $sid,
85 store_name => _h($store->{name} || $store->{subdomain}),
86 has_subs => scalar(@rows) ? 1 : 0,
87 subs => \@rows,
88 canceled_flash => defined $form->{canceled} ? 1 : 0,
89};
90
91print "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";
92print join('', $tfile->template('webstls_my_subscription.html', $tvars, undef));
93exit;
94
95sub _h {
96 my $s = shift // '';
97 $s =~ s/&/&amp;/g; $s =~ s/</&lt;/g; $s =~ s/>/&gt;/g;
98 $s =~ s/"/&quot;/g; $s =~ s/'/&#39;/g;
99 return $s;
100}
Keyboard: j next diff k previous diff g top G bottom r restore c compile-check ? help