@meith/plugin-dues 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +165 -0
- package/README.md +182 -0
- package/package.json +31 -0
- package/src/codes.ts +47 -0
- package/src/config.ts +153 -0
- package/src/definition.tsx +292 -0
- package/src/demo.ts +526 -0
- package/src/entitlement.ts +371 -0
- package/src/handlers-admin.ts +405 -0
- package/src/handlers.ts +397 -0
- package/src/index.ts +18 -0
- package/src/money.ts +42 -0
- package/src/period.ts +76 -0
- package/src/plans.ts +183 -0
- package/src/schema.ts +153 -0
- package/src/store.ts +966 -0
- package/src/stripe/client.ts +303 -0
- package/src/stripe/events.ts +189 -0
- package/src/stripe/webhook.ts +64 -0
- package/src/tasks.ts +156 -0
- package/src/ui/admin.tsx +925 -0
- package/src/ui/pages.tsx +557 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
|
2
|
+
Version 3, 29 June 2007
|
|
3
|
+
|
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
|
6
|
+
of this license document, but changing it is not allowed.
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
|
11
|
+
License, supplemented by the additional permissions listed below.
|
|
12
|
+
|
|
13
|
+
0. Additional Definitions.
|
|
14
|
+
|
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
|
17
|
+
General Public License.
|
|
18
|
+
|
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
|
20
|
+
other than an Application or a Combined Work as defined below.
|
|
21
|
+
|
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
|
25
|
+
of using an interface provided by the Library.
|
|
26
|
+
|
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
|
28
|
+
Application with the Library. The particular version of the Library
|
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
|
30
|
+
Version".
|
|
31
|
+
|
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
|
35
|
+
based on the Application, and not on the Linked Version.
|
|
36
|
+
|
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
|
38
|
+
object code and/or source code for the Application, including any data
|
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
|
41
|
+
|
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
|
43
|
+
|
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
|
46
|
+
|
|
47
|
+
2. Conveying Modified Versions.
|
|
48
|
+
|
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
|
51
|
+
that uses the facility (other than as an argument passed when the
|
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
|
53
|
+
version:
|
|
54
|
+
|
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
|
56
|
+
ensure that, in the event an Application does not supply the
|
|
57
|
+
function or data, the facility still operates, and performs
|
|
58
|
+
whatever part of its purpose remains meaningful, or
|
|
59
|
+
|
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
|
61
|
+
this License applicable to that copy.
|
|
62
|
+
|
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
|
64
|
+
|
|
65
|
+
The object code form of an Application may incorporate material from
|
|
66
|
+
a header file that is part of the Library. You may convey such object
|
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
|
68
|
+
material is not limited to numerical parameters, data structure
|
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
|
71
|
+
|
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
|
73
|
+
Library is used in it and that the Library and its use are
|
|
74
|
+
covered by this License.
|
|
75
|
+
|
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
|
77
|
+
document.
|
|
78
|
+
|
|
79
|
+
4. Combined Works.
|
|
80
|
+
|
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
|
82
|
+
taken together, effectively do not restrict modification of the
|
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
|
85
|
+
the following:
|
|
86
|
+
|
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
|
88
|
+
the Library is used in it and that the Library and its use are
|
|
89
|
+
covered by this License.
|
|
90
|
+
|
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
|
92
|
+
document.
|
|
93
|
+
|
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
|
95
|
+
execution, include the copyright notice for the Library among
|
|
96
|
+
these notices, as well as a reference directing the user to the
|
|
97
|
+
copies of the GNU GPL and this license document.
|
|
98
|
+
|
|
99
|
+
d) Do one of the following:
|
|
100
|
+
|
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
|
102
|
+
License, and the Corresponding Application Code in a form
|
|
103
|
+
suitable for, and under terms that permit, the user to
|
|
104
|
+
recombine or relink the Application with a modified version of
|
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
|
107
|
+
Corresponding Source.
|
|
108
|
+
|
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
|
111
|
+
a copy of the Library already present on the user's computer
|
|
112
|
+
system, and (b) will operate properly with a modified version
|
|
113
|
+
of the Library that is interface-compatible with the Linked
|
|
114
|
+
Version.
|
|
115
|
+
|
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
|
117
|
+
be required to provide such information under section 6 of the
|
|
118
|
+
GNU GPL, and only to the extent that such information is
|
|
119
|
+
necessary to install and execute a modified version of the
|
|
120
|
+
Combined Work produced by recombining or relinking the
|
|
121
|
+
Application with a modified version of the Linked Version. (If
|
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
|
126
|
+
for conveying Corresponding Source.)
|
|
127
|
+
|
|
128
|
+
5. Combined Libraries.
|
|
129
|
+
|
|
130
|
+
You may place library facilities that are a work based on the
|
|
131
|
+
Library side by side in a single library together with other library
|
|
132
|
+
facilities that are not Applications and are not covered by this
|
|
133
|
+
License, and convey such a combined library under terms of your
|
|
134
|
+
choice, if you do both of the following:
|
|
135
|
+
|
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
|
137
|
+
on the Library, uncombined with any other library facilities,
|
|
138
|
+
conveyed under the terms of this License.
|
|
139
|
+
|
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
|
141
|
+
is a work based on the Library, and explaining where to find the
|
|
142
|
+
accompanying uncombined form of the same work.
|
|
143
|
+
|
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
|
145
|
+
|
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
|
148
|
+
versions will be similar in spirit to the present version, but may
|
|
149
|
+
differ in detail to address new problems or concerns.
|
|
150
|
+
|
|
151
|
+
Each version is given a distinguishing version number. If the
|
|
152
|
+
Library as you received it specifies that a certain numbered version
|
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
|
154
|
+
applies to it, you have the option of following the terms and
|
|
155
|
+
conditions either of that published version or of any later version
|
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
|
160
|
+
|
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
|
164
|
+
permanent authorization for you to choose that version for the
|
|
165
|
+
Library.
|
package/README.md
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
# Dues
|
|
2
|
+
|
|
3
|
+
Sells **membership of a usergroup** on a Meith board, through Stripe — a
|
|
4
|
+
monthly or yearly subscription, a fixed-term pass from a day to two years, or
|
|
5
|
+
a lifetime membership — each a plan an administrator shapes in the panel:
|
|
6
|
+
name, price, currency, and length are the operator's call. A pass or a
|
|
7
|
+
lifetime plan can be bought for yourself **or gifted to another member by
|
|
8
|
+
name**.
|
|
9
|
+
|
|
10
|
+
What a paying member gets is whatever the group carries: forum access, a
|
|
11
|
+
badge, a name colour. Dues never makes a permission decision — it decides who
|
|
12
|
+
is in a group and until when, through the board's own
|
|
13
|
+
[timed-grant capability](../../docs/plugin-api.md#timed-group-grants), and
|
|
14
|
+
every guarantee that capability makes applies here. The load-bearing one:
|
|
15
|
+
**a grant always expires on its own.** If this plugin is removed, its tasks
|
|
16
|
+
stop, or Stripe closes the account, every sold membership drains away at its
|
|
17
|
+
own boundary and the board is left correct.
|
|
18
|
+
|
|
19
|
+
**Try it before you read any of this**: [demo.meith.dev](https://demo.meith.dev)
|
|
20
|
+
runs this plugin against a Stripe that is not Stripe — a shop with a year of
|
|
21
|
+
history behind it, and a checkout a visitor can actually go through. See
|
|
22
|
+
[demo mode](../../docs/demo-mode.md#the-shop-and-a-stripe-that-is-not-stripe).
|
|
23
|
+
|
|
24
|
+
## Setting up a board
|
|
25
|
+
|
|
26
|
+
1. **Register the plugin** where plugins are registered:
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
// apps/community/community.plugins.ts (this repository's board uses
|
|
30
|
+
// DUES_TEST_BOARD=1 to switch a test-shaped version of this on)
|
|
31
|
+
import { dues } from '@meith/plugin-dues'
|
|
32
|
+
|
|
33
|
+
export const INSTALLED_PLUGINS = [
|
|
34
|
+
{ key: 'dues', plugin: dues({ currency: 'gbp', graceDays: 7 }) },
|
|
35
|
+
]
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
`plans` may also be declared here as **seeds** — they populate the plan
|
|
39
|
+
table on the board's first run and are ignored once it has rows. After
|
|
40
|
+
that, the panel owns the plans.
|
|
41
|
+
|
|
42
|
+
2. **On the board**: create the group a plan will grant (its permissions,
|
|
43
|
+
badge and colour are the product), then tick **may be granted by plugins**
|
|
44
|
+
on its screen under Admin → Groups. Staff, system and power-carrying
|
|
45
|
+
groups refuse the tick, on purpose.
|
|
46
|
+
3. **Keys**: set `DUES_STRIPE_SECRET_KEY` and `DUES_STRIPE_WEBHOOK_SECRET` in
|
|
47
|
+
the environment, or fill them under Admin → Plugins → Dues. Environment
|
|
48
|
+
wins, and the settings screen says which source is in force.
|
|
49
|
+
4. **Migrations**: run `community upgrade`.
|
|
50
|
+
5. **Make the plans** under Admin → Plugins → Dues → plans — see
|
|
51
|
+
[Plans](#plans) below.
|
|
52
|
+
6. **The webhook**: in Stripe, add an endpoint at
|
|
53
|
+
`https://your.board/api/plugins/dues/hook/stripe` subscribed to the events
|
|
54
|
+
the status page lists, and put its `whsec_…` in the settings.
|
|
55
|
+
7. **Prove it**: the status page (Admin → Plugins → Dues → status) should read
|
|
56
|
+
green; buy a pass yourself in test mode before turning the live key on.
|
|
57
|
+
|
|
58
|
+
## Plans
|
|
59
|
+
|
|
60
|
+
A plan is made and edited in the panel. It has a permanent key, a name, a
|
|
61
|
+
description, the group it grants, a price in minor units, its own three-letter
|
|
62
|
+
currency, and one of three billing shapes:
|
|
63
|
+
|
|
64
|
+
- **A pass** — one payment for a fixed stretch, one day to two years
|
|
65
|
+
(including the grace window; the board caps a plugin grant at two years,
|
|
66
|
+
and a pass respects the cap rather than pretending otherwise).
|
|
67
|
+
- **A subscription** — renews monthly or yearly until cancelled. It bills
|
|
68
|
+
against a real Stripe price: leave the box empty and the plugin mints a
|
|
69
|
+
product and price to match the form, or paste a `price_…` id made in the
|
|
70
|
+
Stripe dashboard. Subscriptions are never giftable.
|
|
71
|
+
- **Lifetime** — one payment, no renewal, no end date. Under the hood the
|
|
72
|
+
board still only ever grants a bounded window (about 23 months), and the
|
|
73
|
+
hourly sweep re-issues it long before it drains — so if the plugin is ever
|
|
74
|
+
removed, access ends at the window's edge instead of leaving unaccountable
|
|
75
|
+
permanent rows. Holding lifetime makes further purchases for that group
|
|
76
|
+
refuse; holding a subscription blocks buying lifetime until the renewal is
|
|
77
|
+
cancelled.
|
|
78
|
+
|
|
79
|
+
Editing is safe by construction: **every order snapshots its plan** — name,
|
|
80
|
+
price, currency, length — so a change never rewrites what anyone already
|
|
81
|
+
bought. A subscription price change mints a new Stripe price; running
|
|
82
|
+
subscriptions keep billing what they signed up for, and only the next buyer
|
|
83
|
+
sees the new number. Plans are never deleted — **archiving** takes one off
|
|
84
|
+
sale while everyone who holds it keeps it. The plan key is permanent because
|
|
85
|
+
it is how orders, memberships and the ledger refer to the plan forever.
|
|
86
|
+
|
|
87
|
+
## How it decides things
|
|
88
|
+
|
|
89
|
+
- **A pass stacks.** Buying a fixed pass while holding one adds to the end —
|
|
90
|
+
paying early never wastes time. Subscriptions refuse a second subscription
|
|
91
|
+
for the same group, lifetime refuses everything after it — there is nothing
|
|
92
|
+
left to sell that member.
|
|
93
|
+
- **Gifts are one-off purchases only** — passes and lifetime. An
|
|
94
|
+
auto-renewing gift would charge the buyer's card forever for someone
|
|
95
|
+
else's membership; that is a support disaster by design, so it is refused
|
|
96
|
+
when the plan is made. The recipient is notified the moment their gift is
|
|
97
|
+
paid for — the board's own bell and e-mail, honouring their notification
|
|
98
|
+
preferences.
|
|
99
|
+
- **Checkout is rate-limited** — ten starts a minute per member, enforced by
|
|
100
|
+
the host before the handler runs — so a stuck retry loop or a hostile
|
|
101
|
+
script cannot mint orders and Stripe sessions at wire speed.
|
|
102
|
+
- **Nothing is granted from a redirect.** The return page waits; only the
|
|
103
|
+
signature-verified webhook (or the reconcile task reading Stripe's own
|
|
104
|
+
record) turns money into membership. Amounts must match the order exactly —
|
|
105
|
+
a mismatch grants nothing and flags the order for an administrator.
|
|
106
|
+
- **A failed renewal means grace, not the door.** Access holds for
|
|
107
|
+
`graceDays` past the period; Stripe retries on its own schedule; the member
|
|
108
|
+
is told through the board's own notifications — bell and e-mail, per their
|
|
109
|
+
preferences — and sees what happened on their manage page.
|
|
110
|
+
- **A refund or chargeback revokes immediately** — the only path that takes
|
|
111
|
+
access away early — and lands in the ledger as a negative amount.
|
|
112
|
+
- **Cancelling keeps what was paid for.** Cancel-at-period-end, always.
|
|
113
|
+
- **Lost webhooks are ordinary.** The five-minute reconcile task settles
|
|
114
|
+
pending orders from Stripe's records and replays any stored event that
|
|
115
|
+
failed; the hourly sweep tidies lapsed rows the expiry already handled.
|
|
116
|
+
|
|
117
|
+
## Discount codes
|
|
118
|
+
|
|
119
|
+
Minted under Admin → Plugins → Dues → discount codes: a percentage off,
|
|
120
|
+
optionally locked to one plan, capped to a number of redemptions, or given an
|
|
121
|
+
expiry date. Members type the code in the box on any plan card.
|
|
122
|
+
|
|
123
|
+
- **A code discounts what Stripe charges, not what the order records** — the
|
|
124
|
+
order snapshots the discounted amount, so the exact-match rule between
|
|
125
|
+
payment and order still holds.
|
|
126
|
+
- **On a pass or lifetime plan** the whole price drops. **On a
|
|
127
|
+
subscription** the discount
|
|
128
|
+
becomes a real Stripe coupon (created on the code's first use, `duration:
|
|
129
|
+
once`), so the first invoice is discounted and renewals bill in full — both
|
|
130
|
+
numbers come from Stripe's own arithmetic.
|
|
131
|
+
- **A 100% code on a pass or lifetime plan is a comp.** Stripe is never
|
|
132
|
+
contacted: the order settles on the spot for zero and the recipient belongs
|
|
133
|
+
immediately. This is the intended way to comp a member — including a
|
|
134
|
+
lifetime comp.
|
|
135
|
+
- **Redemptions count at settlement**, not at checkout, so an abandoned
|
|
136
|
+
checkout never burns a use. Two people racing the last redemption of a
|
|
137
|
+
capped code can both succeed — the count is honest about money that
|
|
138
|
+
actually moved.
|
|
139
|
+
- **Codes are never deleted.** Switch one off and it refuses from that moment;
|
|
140
|
+
its history stays.
|
|
141
|
+
- Stripe refuses charges under its per-currency minimum (about £0.30), so a
|
|
142
|
+
deep discount on a cheap plan can fail at Stripe's page. 100%-off passes
|
|
143
|
+
are exempt — they never reach Stripe.
|
|
144
|
+
|
|
145
|
+
## The admin desk
|
|
146
|
+
|
|
147
|
+
Admin → Plugins → Dues → memberships acts on any sold membership:
|
|
148
|
+
|
|
149
|
+
- **Extend** adds days to the period end and moves the group grant with it —
|
|
150
|
+
a grant, not a charge, so the ledger is untouched.
|
|
151
|
+
- **Cancel renewal** is the same cancel-at-period-end a member can do from
|
|
152
|
+
their manage page, for when someone asks support instead.
|
|
153
|
+
- **Revoke now** removes access on the spot and tells Stripe to stop billing
|
|
154
|
+
a subscription. It does not move money — issue any refund in the Stripe
|
|
155
|
+
dashboard, and the refund webhook records it (and would have revoked on its
|
|
156
|
+
own).
|
|
157
|
+
- **Clear the flag** acknowledges a needs-attention row once you have looked
|
|
158
|
+
at it; flagged orders are cleared from the status screen.
|
|
159
|
+
|
|
160
|
+
Every one of these actions lands in the board's admin action log as
|
|
161
|
+
`plugin.route`, alongside everything else administrators do.
|
|
162
|
+
|
|
163
|
+
## What the operator owns
|
|
164
|
+
|
|
165
|
+
VAT and invoicing, the refund policy and statutory cancellation rights, and
|
|
166
|
+
what your jurisdiction requires around selling recurring services — prices
|
|
167
|
+
here are simply what the member pays. Refunds are issued in the Stripe
|
|
168
|
+
dashboard and flow back as webhooks.
|
|
169
|
+
|
|
170
|
+
## Testing
|
|
171
|
+
|
|
172
|
+
- `pnpm vitest run plugins/dues` — the unit suites: money, periods,
|
|
173
|
+
configuration refusals, the Stripe client against a scripted fetch, webhook
|
|
174
|
+
signatures, event mapping across Stripe API versions.
|
|
175
|
+
- `pnpm vitest run tests/dues-lifecycle.test.ts` — the whole life of a
|
|
176
|
+
membership against real SQL: purchase, gift, stacking, replay, mismatch,
|
|
177
|
+
renewal, grace, cancel, refund-revoke, reconcile, sweep, discount codes,
|
|
178
|
+
and the admin desk.
|
|
179
|
+
- `pnpm test:e2e e2e/dues-purchase-no-js.spec.ts` — a real browser buys a
|
|
180
|
+
pass, gifts one, and redeems a minted code on the test board
|
|
181
|
+
(`DUES_TEST_BOARD=1`), against a fake Stripe on loopback, with the suite
|
|
182
|
+
signing webhooks the way Stripe would.
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@meith/plugin-dues",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Membership dues for a Meith board: Stripe-backed subscriptions as a contained plugin.",
|
|
5
|
+
"license": "LGPL-3.0-or-later",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/meith-dev/meith.git",
|
|
9
|
+
"directory": "plugins/dues"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"main": "./src/index.ts",
|
|
13
|
+
"types": "./src/index.ts",
|
|
14
|
+
"files": [
|
|
15
|
+
"src",
|
|
16
|
+
"!src/**/*.test.*",
|
|
17
|
+
"!src/**/*.type-test.*"
|
|
18
|
+
],
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@meith/plugin-kit": "^0.1.0"
|
|
24
|
+
},
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"react": "^19.2.0"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@types/react": "^19.2.3"
|
|
30
|
+
}
|
|
31
|
+
}
|
package/src/codes.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { randomBytes } from 'node:crypto'
|
|
2
|
+
|
|
3
|
+
export const CODE_ALPHABET = 'ABCDEFGHJKMNPQRSTUVWXYZ23456789'
|
|
4
|
+
const CODE_SHAPE = /^[A-Z0-9][A-Z0-9-]{2,31}$/
|
|
5
|
+
|
|
6
|
+
export function generateCode(): string {
|
|
7
|
+
const bytes = randomBytes(8)
|
|
8
|
+
let out = ''
|
|
9
|
+
for (let index = 0; index < 8; index += 1) {
|
|
10
|
+
if (index === 4) out += '-'
|
|
11
|
+
out += CODE_ALPHABET[bytes[index]! % CODE_ALPHABET.length]
|
|
12
|
+
}
|
|
13
|
+
return out
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function normalizeCode(input: string): string {
|
|
17
|
+
return input.trim().toUpperCase()
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function validCodeShape(code: string): boolean {
|
|
21
|
+
return CODE_SHAPE.test(code)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function discountedPrice(priceMinor: number, percentOff: number): number {
|
|
25
|
+
return Math.max(0, Math.round((priceMinor * (100 - percentOff)) / 100))
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface UsableCode {
|
|
29
|
+
readonly percentOff: number
|
|
30
|
+
readonly planKey: string | null
|
|
31
|
+
readonly maxRedemptions: number | null
|
|
32
|
+
readonly redeemedCount: number
|
|
33
|
+
readonly expiresAt: Date | null
|
|
34
|
+
readonly disabled: boolean
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export type CodeProblem = 'disabled' | 'expired' | 'exhausted' | 'wrong-plan'
|
|
38
|
+
|
|
39
|
+
export function codeProblem(code: UsableCode, planKey: string, now: Date): CodeProblem | null {
|
|
40
|
+
if (code.disabled) return 'disabled'
|
|
41
|
+
if (code.expiresAt !== null && code.expiresAt <= now) return 'expired'
|
|
42
|
+
if (code.maxRedemptions !== null && code.redeemedCount >= code.maxRedemptions) {
|
|
43
|
+
return 'exhausted'
|
|
44
|
+
}
|
|
45
|
+
if (code.planKey !== null && code.planKey !== planKey) return 'wrong-plan'
|
|
46
|
+
return null
|
|
47
|
+
}
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { isCurrencyCode, isValidMinorAmount } from './money'
|
|
2
|
+
import { parsePeriod, periodCeilingDays, type Period } from './period'
|
|
3
|
+
|
|
4
|
+
export interface DuesPlanInput {
|
|
5
|
+
readonly key: string
|
|
6
|
+
readonly name: string
|
|
7
|
+
readonly description?: string
|
|
8
|
+
readonly group: string
|
|
9
|
+
readonly price: number
|
|
10
|
+
readonly billing:
|
|
11
|
+
| { readonly mode: 'auto'; readonly interval: 'month' | 'year'; readonly stripePriceId: string }
|
|
12
|
+
| { readonly mode: 'fixed'; readonly period: string }
|
|
13
|
+
readonly giftable?: boolean
|
|
14
|
+
readonly hidden?: boolean
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface DuesConfigInput {
|
|
18
|
+
readonly currency: string
|
|
19
|
+
readonly graceDays?: number
|
|
20
|
+
readonly label?: string
|
|
21
|
+
readonly plans?: readonly DuesPlanInput[]
|
|
22
|
+
readonly extraRedirectHosts?: readonly string[]
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface DuesPlan {
|
|
26
|
+
readonly key: string
|
|
27
|
+
readonly name: string
|
|
28
|
+
readonly description: string | null
|
|
29
|
+
readonly group: string
|
|
30
|
+
readonly price: number
|
|
31
|
+
readonly billing:
|
|
32
|
+
| { readonly mode: 'auto'; readonly interval: 'month' | 'year'; readonly stripePriceId: string }
|
|
33
|
+
| { readonly mode: 'fixed'; readonly period: string; readonly parsed: Period }
|
|
34
|
+
readonly giftable: boolean
|
|
35
|
+
readonly hidden: boolean
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface DuesConfig {
|
|
39
|
+
readonly currency: string
|
|
40
|
+
readonly graceDays: number
|
|
41
|
+
readonly label: string
|
|
42
|
+
readonly seedPlans: readonly DuesPlan[]
|
|
43
|
+
readonly extraRedirectHosts: readonly string[]
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const PLAN_KEY = /^[a-z][a-z0-9-]{0,39}$/
|
|
47
|
+
const MAX_GRANTABLE_DAYS = 2 * 366
|
|
48
|
+
|
|
49
|
+
function refuse(message: string): never {
|
|
50
|
+
throw new Error(`dues plugin configuration: ${message}`)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function parseDuesConfig(input: DuesConfigInput): DuesConfig {
|
|
54
|
+
if (!isCurrencyCode(input.currency)) {
|
|
55
|
+
refuse(`"${input.currency}" is not a three-letter ISO 4217 currency code.`)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const graceDays = input.graceDays ?? 7
|
|
59
|
+
if (!Number.isInteger(graceDays) || graceDays < 0 || graceDays > 30) {
|
|
60
|
+
refuse(
|
|
61
|
+
`graceDays is ${String(input.graceDays)}. It is the window a lapsed payment keeps ` +
|
|
62
|
+
'access for, 0 to 30 days.',
|
|
63
|
+
)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const label = (input.label ?? 'Membership').trim()
|
|
67
|
+
if (label === '') refuse('label must not be empty.')
|
|
68
|
+
|
|
69
|
+
const seen = new Set<string>()
|
|
70
|
+
const plans = (input.plans ?? []).map((plan): DuesPlan => {
|
|
71
|
+
const where = `plan "${plan.key}"`
|
|
72
|
+
|
|
73
|
+
if (!PLAN_KEY.test(plan.key)) {
|
|
74
|
+
refuse(`${where}: keys are lower-case letters, digits and hyphens.`)
|
|
75
|
+
}
|
|
76
|
+
if (seen.has(plan.key)) refuse(`${where} is declared twice.`)
|
|
77
|
+
seen.add(plan.key)
|
|
78
|
+
|
|
79
|
+
if (plan.name.trim() === '') refuse(`${where}: needs a name members will read.`)
|
|
80
|
+
if (!PLAN_KEY.test(plan.group)) {
|
|
81
|
+
refuse(`${where}: "${plan.group}" is not a valid group key.`)
|
|
82
|
+
}
|
|
83
|
+
if (!isValidMinorAmount(plan.price)) {
|
|
84
|
+
refuse(
|
|
85
|
+
`${where}: price must be a positive whole number of minor units — 500 is £5.00. ` +
|
|
86
|
+
'A decimal here is almost always a hundredfold mistake.',
|
|
87
|
+
)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (plan.billing.mode === 'auto') {
|
|
91
|
+
if (!plan.billing.stripePriceId.startsWith('price_')) {
|
|
92
|
+
refuse(
|
|
93
|
+
`${where}: an auto-renewing plan needs the Stripe price id (price_…) it bills ` +
|
|
94
|
+
'against. Create the price in the Stripe dashboard and paste its id.',
|
|
95
|
+
)
|
|
96
|
+
}
|
|
97
|
+
if (plan.giftable === true) {
|
|
98
|
+
refuse(
|
|
99
|
+
`${where}: an auto-renewing plan cannot be a gift. It would charge the buyer's ` +
|
|
100
|
+
"card forever for someone else's membership — sell a fixed-term pass instead.",
|
|
101
|
+
)
|
|
102
|
+
}
|
|
103
|
+
return {
|
|
104
|
+
key: plan.key,
|
|
105
|
+
name: plan.name.trim(),
|
|
106
|
+
description: plan.description?.trim() || null,
|
|
107
|
+
group: plan.group,
|
|
108
|
+
price: plan.price,
|
|
109
|
+
billing: plan.billing,
|
|
110
|
+
giftable: false,
|
|
111
|
+
hidden: plan.hidden === true,
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const parsed = parsePeriod(plan.billing.period)
|
|
116
|
+
if (parsed === null) {
|
|
117
|
+
refuse(
|
|
118
|
+
`${where}: "${plan.billing.period}" is not an ISO-8601 period like P90D, P1M or P1Y.`,
|
|
119
|
+
)
|
|
120
|
+
}
|
|
121
|
+
if (periodCeilingDays(parsed) + graceDays > MAX_GRANTABLE_DAYS) {
|
|
122
|
+
refuse(
|
|
123
|
+
`${where}: the period plus grace can reach past two years, and the board caps a ` +
|
|
124
|
+
'plugin grant at two years. Sell a shorter pass.',
|
|
125
|
+
)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return {
|
|
129
|
+
key: plan.key,
|
|
130
|
+
name: plan.name.trim(),
|
|
131
|
+
description: plan.description?.trim() || null,
|
|
132
|
+
group: plan.group,
|
|
133
|
+
price: plan.price,
|
|
134
|
+
billing: { ...plan.billing, parsed },
|
|
135
|
+
giftable: plan.giftable !== false,
|
|
136
|
+
hidden: plan.hidden === true,
|
|
137
|
+
}
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
for (const host of input.extraRedirectHosts ?? []) {
|
|
141
|
+
if (!/^[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*$/.test(host)) {
|
|
142
|
+
refuse(`"${host}" is not a plain host name.`)
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return {
|
|
147
|
+
currency: input.currency.toLowerCase(),
|
|
148
|
+
graceDays,
|
|
149
|
+
label,
|
|
150
|
+
seedPlans: plans,
|
|
151
|
+
extraRedirectHosts: input.extraRedirectHosts ?? [],
|
|
152
|
+
}
|
|
153
|
+
}
|