added on local at 2026-07-01 22:09:43
| 1 | #!/usr/bin/perl | |
| 2 | #====================================================================== | |
| 3 | # ShopCart - Pricing page (logged-out marketing) | |
| 4 | # | |
| 5 | # Pulls live plan rows from billing_plans so the same data drives this | |
| 6 | # page, the landing-page pricing block, and the per-feature gate in | |
| 7 | # profile.cgi. Falls back to a built-in 4-plan list if the billing | |
| 8 | # schema hasn't been migrated yet so the marketing page can still | |
| 9 | # render on a fresh install. | |
| 10 | #====================================================================== | |
| 11 | use strict; | |
| 12 | use warnings; | |
| 13 | ||
| 14 | use lib '/var/www/vhosts/3dshawn.com/shop.3dshawn.com'; | |
| 15 | use CGI; | |
| 16 | use MODS::Template; | |
| 17 | use MODS::DBConnect; | |
| 18 | use MODS::Login; | |
| 19 | use MODS::ShopCart::Config; | |
| 20 | use MODS::ShopCart::Wrapper; | |
| 21 | use MODS::ShopCart::Billing; | |
| 22 | use MODS::ShopCart::Promotions; | |
| 23 | ||
| 24 | my $q = CGI->new; | |
| 25 | my $auth = MODS::Login->new; | |
| 26 | my $wrap = MODS::ShopCart::Wrapper->new; | |
| 27 | my $tfile = MODS::Template->new; | |
| 28 | my $db = MODS::DBConnect->new; | |
| 29 | my $cfg = MODS::ShopCart::Config->new; | |
| 30 | my $bill = MODS::ShopCart::Billing->new; | |
| 31 | my $DB = $cfg->settings('database_name'); | |
| 32 | ||
| 33 | $|=1; | |
| 34 | my $userinfo = $auth->login_verify(); | |
| 35 | ||
| 36 | my $dbh = $db->db_connect(); | |
| 37 | my @plans = _build_plan_cards($db, $dbh, $DB, $bill); | |
| 38 | ||
| 39 | # Active plan-target promotion (if any) flows into every pricing card | |
| 40 | # below + into the hero banner so visitors see the deal before they | |
| 41 | # pick a tier. Falls back to "no promo" when there isn't an active row. | |
| 42 | my $promo_obj = MODS::ShopCart::Promotions->new; | |
| 43 | my $active_promo = $promo_obj->active_plan_promo($db, $dbh, $DB); | |
| 44 | my $promo_code = ($active_promo && $active_promo->{code}) ? $active_promo->{code} : ''; | |
| 45 | my $promo_label = $active_promo ? $promo_obj->marketing_label($active_promo) : ''; | |
| 46 | my $promo_name = ($active_promo && $active_promo->{name}) ? $active_promo->{name} : ''; | |
| 47 | ||
| 48 | # Per-card "with code WELCOME20 get 20% off your first invoice" line -- | |
| 49 | # pre-baked here so the template just emits a single field per card. | |
| 50 | # Only the plans that match the promo's eligibility set get the badge; | |
| 51 | # an empty eligibility set means "every paid plan", so all paid cards | |
| 52 | # light up. Free is always excluded since there's nothing to discount. | |
| 53 | if ($active_promo) { | |
| 54 | my @eligible = $promo_obj->eligible_plan_ids($db, $dbh, $DB, $active_promo->{id}); | |
| 55 | my %eligible_set = map { $_ => 1 } @eligible; | |
| 56 | my $all_paid = !scalar(@eligible); | |
| 57 | foreach my $c (@plans) { | |
| 58 | next if $c->{is_free}; | |
| 59 | next unless $all_paid || $eligible_set{ $c->{id} || 0 }; | |
| 60 | $c->{promo_active} = 1; | |
| 61 | $c->{promo_code} = $promo_code; | |
| 62 | $c->{promo_label} = $promo_label; | |
| 63 | $c->{promo_card_line} = $promo_label . ' on your first invoice with code <strong>' . $promo_code . '</strong>'; | |
| 64 | } | |
| 65 | } | |
| 66 | ||
| 67 | $db->db_disconnect($dbh); | |
| 68 | ||
| 69 | print "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"; | |
| 70 | ||
| 71 | my $tvars = { | |
| 72 | plans => \@plans, | |
| 73 | has_plans => scalar(@plans) ? 1 : 0, | |
| 74 | has_promo => $active_promo ? 1 : 0, | |
| 75 | promo_code => $promo_code, | |
| 76 | promo_label => $promo_label, | |
| 77 | promo_name => $promo_name, | |
| 78 | }; | |
| 79 | my $body = join('', $tfile->template('shopcart_pricing.html', $tvars, $userinfo)); | |
| 80 | $wrap->render({ userinfo => $userinfo, title => 'Pricing', body => $body }); | |
| 81 | ||
| 82 | #---------------------------------------------------------------------- | |
| 83 | # Each card needs: tier, display_name, price_display, period_label, | |
| 84 | # tagline, cta_label, cta_href, is_featured, and a list of feature | |
| 85 | # bullets (name + included flag). | |
| 86 | sub _build_plan_cards { | |
| 87 | my ($db, $dbh, $DB, $bill) = @_; | |
| 88 | ||
| 89 | return _fallback_plans() unless $bill->schema_ready($db, $dbh, $DB); | |
| 90 | ||
| 91 | my $entitle_ready = $bill->plan_features_ready($db, $dbh, $DB); | |
| 92 | my @catalog = $bill->feature_catalog; | |
| 93 | my %catalog_by_key = map { $_->{key} => $_ } @catalog; | |
| 94 | ||
| 95 | # Post-consolidation: one row per tier. Monthly price is the | |
| 96 | # headline; yearly comes from the same row via the yearly_* fields | |
| 97 | # (compute_yearly_price_cents resolves percent/dollars/explicit). | |
| 98 | my @rows = $bill->list_plans($db, $dbh, $DB); | |
| 99 | return _fallback_plans() unless scalar @rows; | |
| 100 | ||
| 101 | my @out; | |
| 102 | foreach my $r (@rows) { | |
| 103 | my @bullets = _parse_marketing_features($r->{features}); | |
| 104 | if (!scalar @bullets && $entitle_ready) { | |
| 105 | my $set = $bill->plan_feature_set($db, $dbh, $DB, $r->{id}); | |
| 106 | foreach my $f (@catalog) { | |
| 107 | push @bullets, { | |
| 108 | name => $f->{name} . ' — ' . $f->{blurb}, | |
| 109 | included => $set->{ $f->{key} } ? 1 : 0, | |
| 110 | }; | |
| 111 | } | |
| 112 | } | |
| 113 | ||
| 114 | my $yearly_cents = $bill->compute_yearly_price_cents($r); | |
| 115 | my $annual_note = ''; | |
| 116 | my $has_annual_note = 0; | |
| 117 | if ($yearly_cents > 0) { | |
| 118 | # Compute the buyer-facing "savings" tail. Months-free is | |
| 119 | # only meaningful when the yearly price is below 12 months | |
| 120 | # of the monthly price. | |
| 121 | my $monthly_12 = int($r->{price_cents} || 0) * 12; | |
| 122 | my $savings = $monthly_12 - $yearly_cents; | |
| 123 | my $months_free = $monthly_12 > 0 ? int($savings * 12 / $monthly_12) : 0; | |
| 124 | my $tail = ''; | |
| 125 | if ($months_free >= 2) { | |
| 126 | $tail = " ($months_free months free)"; | |
| 127 | } elsif ($savings > 0) { | |
| 128 | $tail = ' (save ' . _fmt_price_card($savings, $r->{currency}) . ')'; | |
| 129 | } | |
| 130 | $annual_note = 'or ' . _fmt_price_card($yearly_cents, $r->{currency}) . ' / yr' . $tail; | |
| 131 | $has_annual_note = 1; | |
| 132 | } elsif (!$r->{price_cents}) { | |
| 133 | $annual_note = 'forever · no card required'; | |
| 134 | $has_annual_note = 1; | |
| 135 | } | |
| 136 | ||
| 137 | push @out, { | |
| 138 | id => $r->{id}, | |
| 139 | tier => $r->{tier}, | |
| 140 | display_name => $r->{display_name} || ucfirst($r->{tier} || ''), | |
| 141 | tagline => $r->{tagline} || '', | |
| 142 | has_tagline => $r->{tagline} ? 1 : 0, | |
| 143 | price_display => _fmt_price_card($r->{price_cents}, $r->{currency}), | |
| 144 | is_free => ($r->{price_cents} || 0) == 0 ? 1 : 0, | |
| 145 | period_label => 'mo', | |
| 146 | annual_note => $annual_note, | |
| 147 | has_annual_note => $has_annual_note, | |
| 148 | cta_label => $r->{cta_label} || ($r->{price_cents} ? 'Start 14-day free trial' : 'Start free'), | |
| 149 | cta_href => $r->{price_cents} | |
| 150 | ? ('signup.cgi?plan=' . _safe_slug($r->{tier})) | |
| 151 | : 'signup.cgi', | |
| 152 | is_featured => $r->{is_featured} ? 1 : 0, | |
| 153 | not_featured => $r->{is_featured} ? 0 : 1, | |
| 154 | bullets_html => _render_bullets(\@bullets), | |
| 155 | }; | |
| 156 | } | |
| 157 | return @out; | |
| 158 | } | |
| 159 | ||
| 160 | # Pre-render bullet <li> HTML in Perl so the template can emit it as | |
| 161 | # a single variable -- side-steps the engine's nested-loop quirks. | |
| 162 | sub _render_bullets { | |
| 163 | my ($aref) = @_; | |
| 164 | my $html = ''; | |
| 165 | foreach my $b (@{ $aref || [] }) { | |
| 166 | my $name = defined $b->{name} ? $b->{name} : ''; | |
| 167 | if ($b->{included}) { | |
| 168 | $html .= '<li><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="3">' | |
| 169 | . '<polyline points="20 6 9 17 4 12"/></svg> ' . $name . '</li>'; | |
| 170 | } else { | |
| 171 | $html .= '<li class="dim"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="3">' | |
| 172 | . '<line x1="6" y1="6" x2="18" y2="18"/><line x1="6" y1="18" x2="18" y2="6"/></svg> ' | |
| 173 | . $name . '</li>'; | |
| 174 | } | |
| 175 | } | |
| 176 | return $html; | |
| 177 | } | |
| 178 | ||
| 179 | sub _parse_marketing_features { | |
| 180 | my ($raw) = @_; | |
| 181 | return () unless defined $raw && $raw =~ /\S/; | |
| 182 | # Minimal JSON: [{"name":"X","included":true},...]. We control the | |
| 183 | # writes, so a tolerant regex parse is enough -- no JSON dep needed. | |
| 184 | my @out; | |
| 185 | while ($raw =~ m/\{\s*"name"\s*:\s*"((?:\\.|[^"\\])*)"\s*,\s*"included"\s*:\s*(true|false|0|1)\s*\}/gi) { | |
| 186 | my $name = $1; my $inc = lc $2; | |
| 187 | $name =~ s/\\"/"/g; $name =~ s/\\\\/\\/g; | |
| 188 | push @out, { | |
| 189 | name => $name, | |
| 190 | included => ($inc eq 'true' || $inc eq '1') ? 1 : 0, | |
| 191 | }; | |
| 192 | } | |
| 193 | return @out; | |
| 194 | } | |
| 195 | ||
| 196 | sub _fmt_price_card { | |
| 197 | my ($cents, $currency) = @_; | |
| 198 | $cents = 0 unless defined $cents; | |
| 199 | # Plain '$'; the value is interpolated as data at eval time, NOT | |
| 200 | # re-parsed as template syntax, so no \-escape needed. | |
| 201 | if ($cents % 100 == 0) { | |
| 202 | return '$' . int($cents / 100); | |
| 203 | } | |
| 204 | return '$' . sprintf('%.2f', $cents / 100); | |
| 205 | } | |
| 206 | ||
| 207 | sub _safe_slug { | |
| 208 | my $s = lc(shift || ''); | |
| 209 | $s =~ s/[^a-z0-9_\-]//g; | |
| 210 | return $s; | |
| 211 | } | |
| 212 | ||
| 213 | # Used when billing tables haven't been migrated yet. Keeps the | |
| 214 | # marketing page rendering for fresh installs. | |
| 215 | sub _fallback_plans { | |
| 216 | return ( | |
| 217 | { tier=>'free', display_name=>'Free', tagline=>'', has_tagline=>0, | |
| 218 | price_display=>'$0', is_free=>1, period_label=>'mo', | |
| 219 | annual_note=>'forever · no card required', has_annual_note=>1, | |
| 220 | cta_label=>'Start free', cta_href=>'signup.cgi', | |
| 221 | is_featured=>0, not_featured=>1, | |
| 222 | bullets_html=>_render_bullets([ | |
| 223 | { name=>'Branded storefront with cart + checkout', included=>1 }, | |
| 224 | { name=>'Up to 50 products', included=>1 }, | |
| 225 | { name=>'Per-file upload up to 25 MB', included=>1 }, | |
| 226 | { name=>'1 team seat', included=>1 }, | |
| 227 | { name=>'1 active A/B test', included=>1 }, | |
| 228 | { name=>'Abandoned-cart recovery + built-in CRM', included=>1 }, | |
| 229 | { name=>'0% platform fee on every sale', included=>1 }, | |
| 230 | ]) }, | |
| 231 | { tier=>'pro', display_name=>'Pro', tagline=>'', has_tagline=>0, | |
| 232 | price_display=>'$29', is_free=>0, period_label=>'mo', | |
| 233 | annual_note=>'or $290 / yr (2 months free)', has_annual_note=>1, | |
| 234 | cta_label=>'Start 14-day free trial', cta_href=>'signup.cgi?plan=pro', | |
| 235 | is_featured=>1, not_featured=>0, | |
| 236 | bullets_html=>_render_bullets([ | |
| 237 | { name=>'<strong>Everything in Free</strong>', included=>1 }, | |
| 238 | { name=>'Unlimited products', included=>1 }, | |
| 239 | { name=>'Per-file upload up to 500 MB', included=>1 }, | |
| 240 | { name=>'5 team seats', included=>1 }, | |
| 241 | { name=>'10 active A/B tests + 3 MVT block tests',included=>1 }, | |
| 242 | { name=>'Van Westendorp price surveys', included=>1 }, | |
| 243 | { name=>'Custom domain', included=>1 }, | |
| 244 | { name=>'Priority support', included=>1 }, | |
| 245 | ]) }, | |
| 246 | { tier=>'business',display_name=>'Business',tagline=>'', has_tagline=>0, | |
| 247 | price_display=>'$79', is_free=>0, period_label=>'mo', | |
| 248 | annual_note=>'or $790 / yr (2 months free)', has_annual_note=>1, | |
| 249 | cta_label=>'Start 14-day free trial', cta_href=>'signup.cgi?plan=business', | |
| 250 | is_featured=>0, not_featured=>1, | |
| 251 | bullets_html=>_render_bullets([ | |
| 252 | { name=>'<strong>Everything in Pro</strong>', included=>1 }, | |
| 253 | { name=>'Per-file upload up to 5 GB', included=>1 }, | |
| 254 | { name=>'Unlimited team seats', included=>1 }, | |
| 255 | { name=>'Unlimited A/B + MVT', included=>1 }, | |
| 256 | { name=>'Price bandits', included=>1 }, | |
| 257 | { name=>'White-label checkout', included=>1 }, | |
| 258 | { name=>'Dedicated onboarding', included=>1 }, | |
| 259 | { name=>'SLA-backed support', included=>1 }, | |
| 260 | ]) }, | |
| 261 | ); | |
| 262 | } |