@zeyolabs/shopping 2026.11.39 → 2026.11.40
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/checkout/payments-worker.ts +16 -0
- package/checkout/webhook.server.ts +3 -39
- package/checkout/webhook.ts +51 -0
- package/package.json +1 -1
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { WorkerRouter } from '@zeyolabs/kit/cloudflare/workers';
|
|
2
|
+
|
|
3
|
+
import { StripeWebhook } from './webhook';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A Worker that only takes Stripe's webhook (`POST /stripe/webhook`), on a host of its own, so
|
|
7
|
+
* the URL Stripe calls never moves when the shop's domain or deploys do. The app's Worker imports
|
|
8
|
+
* its shopping config first (which registers it) and passes the secret names it reads.
|
|
9
|
+
*/
|
|
10
|
+
export const PaymentsWorker = {
|
|
11
|
+
create(secretNames: ReadonlySet<string>): ExportedHandler<{ SENTRY_DSN: string }> {
|
|
12
|
+
return WorkerRouter.withSentry(secretNames)
|
|
13
|
+
.post('/stripe/webhook', ({ req }) => StripeWebhook.handle(req))
|
|
14
|
+
.build();
|
|
15
|
+
},
|
|
16
|
+
};
|
|
@@ -1,42 +1,6 @@
|
|
|
1
1
|
import type { RequestHandler } from '@sveltejs/kit';
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
import { logger } from '@zeyolabs/kit/server/logger';
|
|
3
|
+
import { StripeWebhook } from './webhook';
|
|
5
4
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
import { secret } from '../config';
|
|
9
|
-
import { OrderManager } from '../orders/manager.server';
|
|
10
|
-
|
|
11
|
-
/** Stripe calls this after payment. Unsigned or mis-signed requests are rejected before any work. */
|
|
12
|
-
export const POST: RequestHandler = async ({ request }) => {
|
|
13
|
-
const signature = request.headers.get('stripe-signature');
|
|
14
|
-
if (!signature) error(400, 'Missing signature');
|
|
15
|
-
|
|
16
|
-
let event;
|
|
17
|
-
try {
|
|
18
|
-
event = await ShopClients.stripe().webhooks.constructEventAsync(
|
|
19
|
-
await request.text(),
|
|
20
|
-
signature,
|
|
21
|
-
secret('stripeWebhookSecret'),
|
|
22
|
-
undefined,
|
|
23
|
-
StripeClient.webhookCrypto
|
|
24
|
-
);
|
|
25
|
-
} catch {
|
|
26
|
-
error(400, 'Invalid signature');
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
if (
|
|
30
|
-
event.type === 'checkout.session.completed' ||
|
|
31
|
-
event.type === 'checkout.session.async_payment_succeeded'
|
|
32
|
-
) {
|
|
33
|
-
try {
|
|
34
|
-
await OrderManager.fulfil(event.data.object.id);
|
|
35
|
-
} catch (err) {
|
|
36
|
-
logger.error(err, { eventId: event.id, op: 'stripeWebhook' });
|
|
37
|
-
// 500 makes Stripe retry; fulfilment is idempotent.
|
|
38
|
-
error(500, 'Fulfilment failed');
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
return Response.json({ received: true });
|
|
42
|
-
};
|
|
5
|
+
/** Stripe's webhook as a SvelteKit route: `export { POST } from` this in the app. */
|
|
6
|
+
export const POST: RequestHandler = ({ request }) => StripeWebhook.handle(request);
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { logger } from '@zeyolabs/kit/server/logger';
|
|
2
|
+
|
|
3
|
+
import { ShopClients } from '../clients/shop.server';
|
|
4
|
+
import { StripeClient } from '../clients/stripe';
|
|
5
|
+
import { secret } from '../config';
|
|
6
|
+
import { OrderManager } from '../orders/manager.server';
|
|
7
|
+
|
|
8
|
+
const FULFIL_ON = new Set([
|
|
9
|
+
'checkout.session.async_payment_succeeded',
|
|
10
|
+
'checkout.session.completed',
|
|
11
|
+
]);
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Stripe's webhook, independent of any framework: the shop's SvelteKit route and the standalone
|
|
15
|
+
* payments Worker both answer with it. Runs inside `runWithContext`.
|
|
16
|
+
*/
|
|
17
|
+
export const StripeWebhook = {
|
|
18
|
+
/**
|
|
19
|
+
* Unsigned or mis-signed requests get a 400 before any work. A paid Checkout session is
|
|
20
|
+
* fulfilled; a failed fulfilment answers 500 so Stripe retries (fulfilment is idempotent).
|
|
21
|
+
* Every other event is acknowledged and ignored.
|
|
22
|
+
*/
|
|
23
|
+
async handle(request: Request): Promise<Response> {
|
|
24
|
+
const signature = request.headers.get('stripe-signature');
|
|
25
|
+
if (!signature) return new Response('Missing signature', { status: 400 });
|
|
26
|
+
|
|
27
|
+
let event;
|
|
28
|
+
try {
|
|
29
|
+
event = await ShopClients.stripe().webhooks.constructEventAsync(
|
|
30
|
+
await request.text(),
|
|
31
|
+
signature,
|
|
32
|
+
secret('stripeWebhookSecret'),
|
|
33
|
+
undefined,
|
|
34
|
+
StripeClient.webhookCrypto
|
|
35
|
+
);
|
|
36
|
+
} catch {
|
|
37
|
+
return new Response('Invalid signature', { status: 400 });
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (FULFIL_ON.has(event.type)) {
|
|
41
|
+
const session = event.data.object as { id: string };
|
|
42
|
+
try {
|
|
43
|
+
await OrderManager.fulfil(session.id);
|
|
44
|
+
} catch (err) {
|
|
45
|
+
logger.error(err, { eventId: event.id, op: 'stripeWebhook' });
|
|
46
|
+
return new Response('Fulfilment failed', { status: 500 });
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return Response.json({ received: true });
|
|
50
|
+
},
|
|
51
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zeyolabs/shopping",
|
|
3
|
-
"version": "2026.11.
|
|
3
|
+
"version": "2026.11.40",
|
|
4
4
|
"description": "Shop and order fulfilment for SvelteKit 3 on Cloudflare: catalogue, basket, Stripe Checkout, orders, Royal Mail Click & Drop, order emails, packing, pages and admin.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "UNLICENSED",
|