source: Klonkt/src/routes/admin-paid.js@ c3d12a6

main
Last change on this file since c3d12a6 was c3d12a6, checked in by Robin <roboburr@…>, 7 weeks ago

Feature: a way to actually become a supporter, and a styled expired page

Bart's test surfaced two gaps in the visitor flow:

  • After "Allow" on Patreon, a non-supporter landed on "Nog geen supporter" with no way to actually pledge. The page now shows a primary "Word supporter op Patreon" button. The site owner sets their public Patreon page in Beheer -> Betaalde posts (new field); it's also linked from the gate itself ("Nog geen supporter? Word het op Patreon").
  • The invalid/expired callback replied with a raw res.send() plain-text line. It now renders the normal paid-result page (reason 'expired').

Changed files:
src/config/database.js

  • paid_patreon.patreon_url column (additive)

src/services/PaidPatreonService.js

  • patreonUrl in config/status/save (undefined keeps, empty clears) + patreonUrl(siteId) helper

src/routes/admin-paid.js

  • save patreon_url from the form

src/views/pages/admin-paid.ejs

  • "Openbare Patreon-pagina" field

src/routes/paid.js

  • expired callback renders paid-result; pass patronUrl to the result pages

src/routes/posts.js

  • pass pgPatronUrl to the gate

src/views/pages/paid-gate.ejs

  • "Word supporter" join line under the unlock button

src/views/pages/paid-result.ejs

  • "Word supporter op Patreon" primary button, 'expired' reason, ghost back button always has a label

test/paid-patron.test.js

  • patreonUrl set/keep/clear semantics

-robo
Co-Authored-By: Claude Opus 4.8 <noreply@…>

  • Property mode set to 100644
File size: 3.3 KB
RevLine 
[61e3daf]1/**
2 * Admin: Paid posts (premium module, god-only). Slice 1 of klonkt-demo-aki.
3 *
4 * GET /admin/paid -> the owner's Patreon config form + status
5 * POST /admin/paid -> save config (secret/token stored encrypted)
6 * POST /admin/paid/disconnect -> forget the config
7 *
8 * Premium-gated via premiumUnlocked(), like stats/downloads. This is the site
9 * owner's OWN Patreon campaign, separate from Klonkt Premium's license flow.
10 */
11import express from 'express';
12import { renderPage } from '../middleware/render.js';
13import { requireGod } from '../middleware/auth.js';
14import { premiumUnlocked } from '../services/PatreonService.js';
15import { cryptoBoxReady } from '../services/CryptoBox.js';
16import PaidPatreon from '../services/PaidPatreonService.js';
17
18const router = express.Router();
19
[603d246]20// The redirect URI the owner MUST whitelist in their Patreon client. Must match
21// exactly what paid.js sends, or Patreon shows its own error page (which we
22// cannot skin) instead of returning the visitor to us.
23const redirectUri = (req) =>
24 (process.env.PUBLIC_BASE_URL || `${req.protocol}://${req.get('host')}`).replace(/\/+$/, '') + '/paid/callback';
25
[61e3daf]26function gate(req, res) {
27 if (!premiumUnlocked()) {
28 res.status(403).send('Betaalde posts is een premium-functie: koppel Patreon in Beheer, Instellingen.');
29 return false;
30 }
31 if (!res.locals.site) { res.status(400).send('Geen site.'); return false; }
32 return true;
33}
34
35router.get('/', requireGod, (req, res) => {
36 if (!gate(req, res)) return;
37 renderPage(req, res, 'pages/admin-paid', {
38 pageTitle: 'Betaalde posts',
39 bodyClass: 'on-admin',
40 status: PaidPatreon.ownerStatus(res.locals.site.id),
41 secretReady: cryptoBoxReady(),
[603d246]42 redirectUri: redirectUri(req),
[61e3daf]43 saved: req.query.saved === '1',
44 error: req.query.error || null,
45 });
46});
47
48router.post('/', requireGod, (req, res) => {
49 if (!gate(req, res)) return;
50 if (!cryptoBoxReady()) return res.redirect('/admin/paid?error=' + encodeURIComponent('PAID_SECRET ontbreekt in de serverconfig; secrets kunnen niet versleuteld worden opgeslagen.'));
51 const b = req.body || {};
52 const eur = String(b.default_min_eur || '').replace(',', '.').trim();
53 const cents = eur ? Math.round(parseFloat(eur) * 100) : undefined;
54 try {
55 PaidPatreon.saveOwnerConfig(res.locals.site.id, {
56 clientId: (b.client_id || '').trim() || undefined,
57 // Empty secret/token fields keep the stored value (no re-paste needed).
58 clientSecret: (b.client_secret || '').trim() || undefined,
59 campaignId: (b.campaign_id || '').trim() || undefined,
60 accessToken: (b.access_token || '').trim() || undefined,
61 refreshToken: (b.refresh_token || '').trim() || undefined,
[c3d12a6]62 // Empty clears it (null), a value sets it. Unlike secrets, this is not
63 // sensitive and there's a clear "remove the link" intent.
64 patreonUrl: (b.patreon_url || '').trim() || null,
[61e3daf]65 defaultMinCents: Number.isFinite(cents) ? cents : undefined,
66 });
67 return res.redirect('/admin/paid?saved=1');
68 } catch (e) {
69 return res.redirect('/admin/paid?error=' + encodeURIComponent(e.message || 'Opslaan mislukt'));
70 }
71});
72
73router.post('/disconnect', requireGod, (req, res) => {
74 if (!gate(req, res)) return;
75 PaidPatreon.disconnect(res.locals.site.id);
76 res.redirect('/admin/paid?saved=1');
77});
78
79export default router;
Note: See TracBrowser for help on using the repository browser.