@deverjak/tenantkit-payments-stripe 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 +21 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +77 -0
- package/package.json +32 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Chytré Digital
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `PaymentProvider` port → Stripe. Used by the `payments` plugin (docs/09 §5). Two checkout flows — tenant
|
|
3
|
+
* billing (subscription → drives `core.tenants.tier`) and course payment (one-off → `payments.orders`) — plus
|
|
4
|
+
* refunds and webhook verification that maps Stripe events to the kernel's VENDOR-NEUTRAL `PaymentEvent` union,
|
|
5
|
+
* so the plugin never sees a Stripe type. Swap this package for `@tenantkit/payments-gopay` and nothing else changes.
|
|
6
|
+
*/
|
|
7
|
+
import Stripe from 'stripe';
|
|
8
|
+
export function createStripePayments(opts) {
|
|
9
|
+
const stripe = new Stripe(opts.secretKey);
|
|
10
|
+
return {
|
|
11
|
+
async createSubscriptionCheckout({ tenantId, plan, returnUrl }) {
|
|
12
|
+
const s = await stripe.checkout.sessions.create({
|
|
13
|
+
mode: 'subscription',
|
|
14
|
+
line_items: [{ price: plan, quantity: 1 }],
|
|
15
|
+
success_url: returnUrl,
|
|
16
|
+
cancel_url: returnUrl,
|
|
17
|
+
metadata: { tenantId, kind: 'tenant_subscription' },
|
|
18
|
+
});
|
|
19
|
+
return { url: s.url ?? returnUrl };
|
|
20
|
+
},
|
|
21
|
+
async createPaymentCheckout({ tenantId, enrollmentId, amountMinor, currency, returnUrl }) {
|
|
22
|
+
const s = await stripe.checkout.sessions.create({
|
|
23
|
+
mode: 'payment',
|
|
24
|
+
line_items: [
|
|
25
|
+
{ price_data: { currency, unit_amount: amountMinor, product_data: { name: 'Course fee' } }, quantity: 1 },
|
|
26
|
+
],
|
|
27
|
+
success_url: returnUrl,
|
|
28
|
+
cancel_url: returnUrl,
|
|
29
|
+
metadata: { tenantId, enrollmentId, kind: 'course_payment' },
|
|
30
|
+
});
|
|
31
|
+
return { url: s.url ?? returnUrl };
|
|
32
|
+
},
|
|
33
|
+
async refund({ paymentRef, amountMinor }) {
|
|
34
|
+
const r = await stripe.refunds.create({ payment_intent: paymentRef, amount: amountMinor });
|
|
35
|
+
return { refundRef: r.id };
|
|
36
|
+
},
|
|
37
|
+
async verifyWebhook(req, secret) {
|
|
38
|
+
const sig = req.headers.get('stripe-signature') ?? '';
|
|
39
|
+
const body = await req.text();
|
|
40
|
+
const evt = stripe.webhooks.constructEvent(body, sig, secret || opts.webhookSecret);
|
|
41
|
+
return mapEvent(evt);
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/** Stripe event → the kernel's neutral PaymentEvent (ports/index.ts §4). Unknown events collapse to `ignored`. */
|
|
46
|
+
function mapEvent(evt) {
|
|
47
|
+
switch (evt.type) {
|
|
48
|
+
case 'customer.subscription.updated':
|
|
49
|
+
case 'customer.subscription.created': {
|
|
50
|
+
const sub = evt.data.object;
|
|
51
|
+
return {
|
|
52
|
+
type: 'subscription.updated',
|
|
53
|
+
tenantId: String(sub.metadata['tenantId'] ?? ''),
|
|
54
|
+
tier: String(sub.metadata['tier'] ?? 'free'),
|
|
55
|
+
status: sub.status,
|
|
56
|
+
// Stripe API 2025-03-31+ (SDK v18+) moved `current_period_end` off the Subscription onto each
|
|
57
|
+
// SubscriptionItem; the subscription's period end is the first item's. (Was `sub.current_period_end`.)
|
|
58
|
+
currentPeriodEnd: (sub.items.data[0]?.current_period_end ?? 0) * 1000,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
case 'checkout.session.completed': {
|
|
62
|
+
const s = evt.data.object;
|
|
63
|
+
return {
|
|
64
|
+
type: 'payment.succeeded',
|
|
65
|
+
enrollmentId: String(s.metadata?.['enrollmentId'] ?? ''),
|
|
66
|
+
paymentRef: String(s.payment_intent ?? ''),
|
|
67
|
+
amountMinor: s.amount_total ?? 0,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
case 'charge.refunded': {
|
|
71
|
+
const c = evt.data.object;
|
|
72
|
+
return { type: 'refund.succeeded', paymentRef: String(c.payment_intent ?? ''), refundRef: c.id };
|
|
73
|
+
}
|
|
74
|
+
default:
|
|
75
|
+
return { type: 'ignored' };
|
|
76
|
+
}
|
|
77
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@deverjak/tenantkit-payments-stripe",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Stripe PaymentProvider adapter for the @deverjak/tenantkit-kernel ports — subscription + course checkout, refunds, neutral webhook events.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
18
|
+
},
|
|
19
|
+
"peerDependencies": {
|
|
20
|
+
"@deverjak/tenantkit-kernel": "0.2.0"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"stripe": "^22.3.0"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"typescript": "^5.9.3"
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"typecheck": "tsc --noEmit",
|
|
30
|
+
"build": "tsc -p tsconfig.json --outDir dist --declaration"
|
|
31
|
+
}
|
|
32
|
+
}
|