qarpeo 0.2.0 → 0.3.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/dist/chunks/tools.mjs +993 -31
- package/dist/cli/index.mjs +1102 -31
- package/dist/fate/index.d.ts +9 -1
- package/dist/fate/index.mjs +20 -3
- package/dist/module.mjs +296 -753
- package/dist/runtime/browser.mjs +2 -2
- package/dist/runtime/comments-server.d.ts +3 -0
- package/dist/runtime/comments-server.mjs +2 -0
- package/dist/runtime/comments.d.ts +0 -2
- package/dist/runtime/comments.mjs +0 -2
- package/dist/runtime/db.d.ts +10 -9
- package/dist/runtime/db.mjs +79 -8
- package/dist/runtime/drizzle.mjs +21 -6
- package/dist/runtime/index.d.ts +4 -0
- package/dist/runtime/realtime-server.d.ts +4 -0
- package/dist/runtime/realtime-server.mjs +13 -0
- package/dist/runtime/realtime.d.ts +1 -4
- package/dist/runtime/realtime.mjs +0 -13
- package/dist/runtime/replica.d.ts +27 -0
- package/dist/runtime/replica.mjs +229 -0
- package/dist/runtime/server/durable-objects/agent.mjs +14 -2
- package/dist/runtime/server/durable-objects/room.d.ts +10 -0
- package/dist/runtime/server/durable-objects/room.mjs +119 -28
- package/dist/runtime/server/middleware/devtools.mjs +1 -1
- package/dist/runtime/server/middleware/tenancy.mjs +7 -4
- package/dist/runtime/server/plugins/store.mjs +1 -1
- package/dist/runtime/server/routes/__qarpeo/mail/track/click/[id].get.mjs +57 -17
- package/dist/runtime/server/routes/agents/[agent]/chat.post.mjs +19 -12
- package/dist/runtime/server/routes/agents/[agent]/clear.post.mjs +8 -9
- package/dist/runtime/server/routes/agents/[agent]/history.get.mjs +8 -9
- package/dist/runtime/server/routes/analytics/events.get.mjs +12 -2
- package/dist/runtime/server/routes/analytics/pageviews.get.mjs +10 -1
- package/dist/runtime/server/routes/analytics/summary.get.mjs +10 -1
- package/dist/runtime/server/routes/auth/2fa/disable.post.mjs +11 -2
- package/dist/runtime/server/routes/auth/2fa/setup.post.mjs +7 -0
- package/dist/runtime/server/routes/auth/2fa/verify.post.mjs +11 -2
- package/dist/runtime/server/routes/auth/account.delete.mjs +7 -0
- package/dist/runtime/server/routes/auth/magic-link/verify.get.mjs +5 -3
- package/dist/runtime/server/routes/auth/organization/sso/index.post.mjs +10 -0
- package/dist/runtime/server/routes/auth/otp/verify.post.mjs +5 -3
- package/dist/runtime/server/routes/auth/sso/saml/acs.post.mjs +18 -0
- package/dist/runtime/server/routes/auth/webauthn/login/options.post.d.ts +1 -1
- package/dist/runtime/server/routes/auth/webauthn/login/options.post.mjs +11 -2
- package/dist/runtime/server/routes/auth/webauthn/login/verify.post.mjs +10 -1
- package/dist/runtime/server/routes/auth/webauthn/register/options.post.d.ts +1 -1
- package/dist/runtime/server/routes/auth/webauthn/register/options.post.mjs +10 -1
- package/dist/runtime/server/routes/auth/webauthn/register/verify.post.mjs +10 -1
- package/dist/runtime/server/routes/browser/crawl.post.mjs +13 -0
- package/dist/runtime/server/routes/browser/invoice.post.mjs +15 -1
- package/dist/runtime/server/routes/browser/og-image.post.mjs +13 -0
- package/dist/runtime/server/routes/browser/pdf.post.mjs +15 -1
- package/dist/runtime/server/routes/browser/screenshot.post.mjs +13 -0
- package/dist/runtime/server/routes/cms/[collection]/[id]/preview-token.post.mjs +23 -8
- package/dist/runtime/server/routes/cms/[collection]/[id]/publish.post.mjs +2 -1
- package/dist/runtime/server/routes/cms/[collection]/[id]/review.post.mjs +2 -1
- package/dist/runtime/server/routes/cms/[collection]/[id]/revisions/[revId]/restore.post.mjs +2 -1
- package/dist/runtime/server/routes/cms/[collection]/[id].delete.mjs +2 -1
- package/dist/runtime/server/routes/cms/[collection]/[id].put.mjs +2 -1
- package/dist/runtime/server/routes/cms/[collection]/index.post.mjs +2 -1
- package/dist/runtime/server/routes/cms/categories/[id].delete.mjs +2 -1
- package/dist/runtime/server/routes/cms/categories/[id].put.mjs +2 -1
- package/dist/runtime/server/routes/cms/categories.post.mjs +2 -1
- package/dist/runtime/server/routes/cms/collections/[id].delete.mjs +2 -1
- package/dist/runtime/server/routes/cms/collections/[id].put.mjs +2 -1
- package/dist/runtime/server/routes/cms/collections/index.post.mjs +2 -1
- package/dist/runtime/server/routes/cms/export.post.mjs +1 -1
- package/dist/runtime/server/routes/cms/subscribers/[id].delete.mjs +2 -1
- package/dist/runtime/server/routes/cms/subscribers/[id].patch.mjs +2 -1
- package/dist/runtime/server/routes/cms/tags/[id].delete.mjs +2 -1
- package/dist/runtime/server/routes/cms/tags.post.mjs +2 -1
- package/dist/runtime/server/routes/comments/[id]/react.post.mjs +7 -3
- package/dist/runtime/server/routes/comments/[id]/report.post.mjs +3 -0
- package/dist/runtime/server/routes/comments/[id].delete.mjs +1 -1
- package/dist/runtime/server/routes/comments/[id].get.mjs +13 -0
- package/dist/runtime/server/routes/comments/[id].patch.mjs +1 -1
- package/dist/runtime/server/routes/fate.d.ts +6 -0
- package/dist/runtime/server/routes/fate.mjs +32 -3
- package/dist/runtime/server/routes/mail/bounce.post.mjs +14 -2
- package/dist/runtime/server/routes/mail/unsubscribe.mjs +2 -1
- package/dist/runtime/server/routes/mail/webhooks.post.mjs +18 -4
- package/dist/runtime/server/routes/notifications/[id].get.mjs +3 -4
- package/dist/runtime/server/routes/notifications/index.get.mjs +4 -2
- package/dist/runtime/server/routes/notifications/push/subscribe.post.mjs +3 -1
- package/dist/runtime/server/routes/notifications/push/unsubscribe.post.mjs +5 -3
- package/dist/runtime/server/routes/notifications/read-all.post.mjs +6 -2
- package/dist/runtime/server/routes/notifications/send.post.mjs +13 -2
- package/dist/runtime/server/routes/payments/cancel.post.mjs +1 -1
- package/dist/runtime/server/routes/payments/change-plan.post.mjs +1 -1
- package/dist/runtime/server/routes/payments/checkout.post.mjs +1 -1
- package/dist/runtime/server/routes/payments/pause.post.mjs +1 -1
- package/dist/runtime/server/routes/payments/portal.post.mjs +1 -1
- package/dist/runtime/server/routes/payments/subscription.get.mjs +1 -1
- package/dist/runtime/server/routes/payments/usage.get.mjs +1 -1
- package/dist/runtime/server/routes/payments/usage.post.mjs +1 -1
- package/dist/runtime/server/routes/payments/webhook.post.mjs +8 -1
- package/dist/runtime/server/routes/realtime/broadcast.post.mjs +3 -2
- package/dist/runtime/server/routes/realtime/history.delete.mjs +2 -2
- package/dist/runtime/server/routes/realtime/history.get.mjs +2 -2
- package/dist/runtime/server/routes/realtime/presence.get.mjs +3 -3
- package/dist/runtime/server/routes/realtime/state.get.mjs +3 -3
- package/dist/runtime/server/routes/realtime/state.post.mjs +13 -4
- package/dist/runtime/server/routes/realtime/ws.get.mjs +2 -2
- package/dist/runtime/server/routes/storage/file/[...key].delete.mjs +1 -1
- package/dist/runtime/server/routes/storage/file/[...key].get.mjs +7 -0
- package/dist/runtime/server/routes/storage/files.get.d.ts +1 -1
- package/dist/runtime/server/routes/storage/files.get.mjs +24 -4
- package/dist/runtime/server/routes/storage/multipart/abort.post.mjs +16 -1
- package/dist/runtime/server/routes/storage/multipart/complete.post.d.ts +1 -1
- package/dist/runtime/server/routes/storage/multipart/complete.post.mjs +15 -1
- package/dist/runtime/server/routes/storage/multipart/create.post.mjs +7 -0
- package/dist/runtime/server/routes/storage/multipart/part.put.mjs +15 -1
- package/dist/runtime/server/routes/storage/presigned-r2.post.mjs +8 -1
- package/dist/runtime/server/routes/storage/presigned.post.d.ts +1 -1
- package/dist/runtime/server/routes/storage/presigned.post.mjs +8 -1
- package/dist/runtime/server/routes/storage/sign.post.mjs +7 -0
- package/dist/runtime/server/routes/storage/upload.post.d.ts +2 -2
- package/dist/runtime/server/routes/store/cart/index.delete.mjs +3 -4
- package/dist/runtime/server/routes/store/cart/index.get.mjs +3 -10
- package/dist/runtime/server/routes/store/cart/items/[id].delete.mjs +6 -4
- package/dist/runtime/server/routes/store/cart/items/[id].patch.mjs +12 -4
- package/dist/runtime/server/routes/store/cart/items.post.mjs +9 -10
- package/dist/runtime/server/routes/store/categories/index.get.mjs +4 -2
- package/dist/runtime/server/routes/store/checkout.post.mjs +13 -3
- package/dist/runtime/server/routes/store/coupons/apply.post.mjs +6 -5
- package/dist/runtime/server/routes/store/orders/[id].get.mjs +2 -2
- package/dist/runtime/server/routes/store/orders/download/[token].get.mjs +6 -3
- package/dist/runtime/server/routes/store/orders/index.get.mjs +1 -1
- package/dist/runtime/server/routes/store/products/[slug].get.mjs +2 -2
- package/dist/runtime/server/routes/store/products/index.get.mjs +1 -1
- package/dist/runtime/server/routes/store/tax/calculate.post.mjs +10 -0
- package/dist/runtime/server/routes/tenants/[tenant]/domains/[domain]/index.delete.mjs +26 -25
- package/dist/runtime/server/routes/tenants/[tenant]/domains/[domain]/verify.post.mjs +26 -25
- package/dist/runtime/server/utils/ai.d.ts +6 -0
- package/dist/runtime/server/utils/ai.mjs +12 -2
- package/dist/runtime/server/utils/analytics-query.mjs +8 -4
- package/dist/runtime/server/utils/browser-utils.d.ts +9 -0
- package/dist/runtime/server/utils/browser-utils.mjs +135 -0
- package/dist/runtime/server/utils/cms/cache.d.ts +13 -0
- package/dist/runtime/server/utils/cms/cache.mjs +95 -0
- package/dist/runtime/server/utils/cms/common.mjs +1 -1
- package/dist/runtime/server/utils/cms/markdown.mjs +38 -8
- package/dist/runtime/server/utils/cms/mutations.d.ts +26 -0
- package/dist/runtime/server/utils/cms/mutations.mjs +569 -0
- package/dist/runtime/server/utils/cms/path.d.ts +8 -0
- package/dist/runtime/server/utils/cms/path.mjs +23 -0
- package/dist/runtime/server/utils/cms/queries.d.ts +27 -0
- package/dist/runtime/server/utils/cms/queries.mjs +366 -0
- package/dist/runtime/server/utils/cms/references.d.ts +48 -0
- package/dist/runtime/server/utils/cms/references.mjs +309 -0
- package/dist/runtime/server/utils/cms/revisions.d.ts +7 -0
- package/dist/runtime/server/utils/cms/revisions.mjs +60 -0
- package/dist/runtime/server/utils/cms/sync.mjs +3 -3
- package/dist/runtime/server/utils/cms.d.ts +5 -82
- package/dist/runtime/server/utils/cms.mjs +28 -1367
- package/dist/runtime/server/utils/comments.d.ts +11 -0
- package/dist/runtime/server/utils/comments.mjs +65 -17
- package/dist/runtime/server/utils/crypto.mjs +1 -1
- package/dist/runtime/server/utils/domains.mjs +9 -0
- package/dist/runtime/server/utils/drizzle.d.ts +26 -1
- package/dist/runtime/server/utils/drizzle.mjs +78 -19
- package/dist/runtime/server/utils/email-inbound.mjs +9 -1
- package/dist/runtime/server/utils/impersonation.mjs +58 -1
- package/dist/runtime/server/utils/lock.d.ts +11 -0
- package/dist/runtime/server/utils/lock.mjs +4 -0
- package/dist/runtime/server/utils/mail-suppression.mjs +48 -13
- package/dist/runtime/server/utils/mail.d.ts +2 -1
- package/dist/runtime/server/utils/mail.mjs +17 -11
- package/dist/runtime/server/utils/notifications.d.ts +13 -4
- package/dist/runtime/server/utils/notifications.mjs +20 -4
- package/dist/runtime/server/utils/organization.mjs +36 -1
- package/dist/runtime/server/utils/passwordless.mjs +8 -0
- package/dist/runtime/server/utils/payments/checkout.d.ts +34 -0
- package/dist/runtime/server/utils/payments/checkout.mjs +158 -0
- package/dist/runtime/server/utils/payments/common.d.ts +45 -0
- package/dist/runtime/server/utils/payments/common.mjs +234 -0
- package/dist/runtime/server/utils/payments/index.d.ts +9 -0
- package/dist/runtime/server/utils/payments/index.mjs +21 -0
- package/dist/runtime/server/utils/payments/lemonsqueezy.d.ts +7 -0
- package/dist/runtime/server/utils/payments/lemonsqueezy.mjs +55 -0
- package/dist/runtime/server/utils/payments/paddle.d.ts +7 -0
- package/dist/runtime/server/utils/payments/paddle.mjs +92 -0
- package/dist/runtime/server/utils/payments/polar.d.ts +11 -0
- package/dist/runtime/server/utils/payments/polar.mjs +66 -0
- package/dist/runtime/server/utils/payments/stripe.d.ts +7 -0
- package/dist/runtime/server/utils/payments/stripe.mjs +159 -0
- package/dist/runtime/server/utils/payments/subscriptions.d.ts +89 -0
- package/dist/runtime/server/utils/payments/subscriptions.mjs +236 -0
- package/dist/runtime/server/utils/payments/usage.d.ts +12 -0
- package/dist/runtime/server/utils/payments/usage.mjs +143 -0
- package/dist/runtime/server/utils/payments/webhooks.d.ts +10 -0
- package/dist/runtime/server/utils/payments/webhooks.mjs +200 -0
- package/dist/runtime/server/utils/payments.d.ts +1 -158
- package/dist/runtime/server/utils/payments.mjs +1 -1289
- package/dist/runtime/server/utils/queue.d.ts +2 -0
- package/dist/runtime/server/utils/queue.mjs +34 -21
- package/dist/runtime/server/utils/rag.mjs +30 -17
- package/dist/runtime/server/utils/realtime.d.ts +5 -1
- package/dist/runtime/server/utils/realtime.mjs +43 -7
- package/dist/runtime/server/utils/session.mjs +16 -3
- package/dist/runtime/server/utils/sso.d.ts +3 -0
- package/dist/runtime/server/utils/sso.mjs +43 -0
- package/dist/runtime/server/utils/storage.mjs +12 -1
- package/dist/runtime/server/utils/store/cart.d.ts +63 -0
- package/dist/runtime/server/utils/store/cart.mjs +542 -0
- package/dist/runtime/server/utils/store/checkout.d.ts +14 -0
- package/dist/runtime/server/utils/store/checkout.mjs +118 -0
- package/dist/runtime/server/utils/store/common.d.ts +29 -0
- package/dist/runtime/server/utils/store/common.mjs +479 -0
- package/dist/runtime/server/utils/store/index.d.ts +7 -0
- package/dist/runtime/server/utils/store/index.mjs +30 -0
- package/dist/runtime/server/utils/store/inventory.d.ts +25 -0
- package/dist/runtime/server/utils/store/inventory.mjs +190 -0
- package/dist/runtime/server/utils/store/orders.d.ts +60 -0
- package/dist/runtime/server/utils/store/orders.mjs +590 -0
- package/dist/runtime/server/utils/store/products.d.ts +35 -0
- package/dist/runtime/server/utils/store/products.mjs +461 -0
- package/dist/runtime/server/utils/store/vendor.d.ts +18 -0
- package/dist/runtime/server/utils/store/vendor.mjs +68 -0
- package/dist/runtime/server/utils/store.d.ts +1 -207
- package/dist/runtime/server/utils/store.mjs +1 -2227
- package/dist/runtime/server/utils/string.d.ts +1 -1
- package/dist/runtime/server/utils/string.mjs +12 -4
- package/dist/runtime/server/utils/tenant.mjs +19 -5
- package/dist/runtime/server/utils/tracker.mjs +2 -0
- package/dist/runtime/server.d.ts +3 -3
- package/dist/runtime/server.mjs +4 -4
- package/dist/runtime/storage-server.d.ts +2 -0
- package/dist/runtime/storage-server.mjs +1 -0
- package/dist/runtime/storage.d.ts +1 -1
- package/dist/runtime/storage.mjs +0 -1
- package/dist/runtime/store.d.ts +0 -1
- package/dist/runtime/store.mjs +0 -1
- package/dist/runtime/test-utils.d.ts +4 -0
- package/dist/runtime/test-utils.mjs +54 -0
- package/dist/runtime/types/d1.d.ts +43 -0
- package/dist/runtime/types/drizzle.d.ts +6 -0
- package/dist/runtime/types/store.d.ts +1 -1
- package/dist/types/analytics.d.ts +80 -0
- package/dist/types/auth.d.ts +282 -0
- package/dist/types/cache.d.ts +55 -0
- package/dist/types/comments.d.ts +130 -0
- package/dist/types/db.d.ts +29 -0
- package/dist/types/email.d.ts +182 -0
- package/dist/types/index.d.ts +22 -0
- package/dist/types/jobs.d.ts +58 -0
- package/dist/types/notifications.d.ts +77 -0
- package/dist/types/options.d.ts +363 -0
- package/dist/types/payments.d.ts +145 -0
- package/dist/types/ratelimit.d.ts +71 -0
- package/dist/types/realtime.d.ts +136 -0
- package/dist/types/site.d.ts +121 -0
- package/dist/types/storage.d.ts +164 -0
- package/dist/types/store-options.d.ts +199 -0
- package/dist/types/tenant.d.ts +100 -0
- package/dist/types.d.ts +1 -2174
- package/dist/utils/migration-lock.d.ts +139 -0
- package/dist/utils/pkg.d.ts +14 -1
- package/dist/utils/schema-diff.d.ts +39 -0
- package/package.json +55 -17
- package/dist/chunks/auth-schema.mjs +0 -364
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { createError, getRequestHeader } from "nuxt/server";
|
|
2
|
+
import { getKVBinding } from "../../../kv.mjs";
|
|
3
|
+
import {
|
|
4
|
+
customerTbl,
|
|
5
|
+
subscriptionTbl,
|
|
6
|
+
webhookEventTbl,
|
|
7
|
+
getPaymentsDb,
|
|
8
|
+
getPaymentsConfig,
|
|
9
|
+
ensurePaymentsTables,
|
|
10
|
+
rowToSubscription,
|
|
11
|
+
notifyPaymentWebhookListeners
|
|
12
|
+
} from "./common.mjs";
|
|
13
|
+
import { verifyStripeSignature, handleStripeWebhookEvent } from "./stripe.mjs";
|
|
14
|
+
import { verifyLemonSqueezySignature, handleLemonSqueezyWebhookEvent } from "./lemonsqueezy.mjs";
|
|
15
|
+
import { verifyPolarSignature, handlePolarWebhookEvent } from "./polar.mjs";
|
|
16
|
+
import { verifyPaddleSignature, handlePaddleWebhookEvent } from "./paddle.mjs";
|
|
17
|
+
export async function processPaymentWebhook(event, rawBody) {
|
|
18
|
+
await ensurePaymentsTables(event);
|
|
19
|
+
const db = getPaymentsDb(event);
|
|
20
|
+
const config = getPaymentsConfig(event);
|
|
21
|
+
const provider = config.provider;
|
|
22
|
+
const ct = customerTbl(event);
|
|
23
|
+
const st = subscriptionTbl(event);
|
|
24
|
+
const wt = webhookEventTbl(event);
|
|
25
|
+
let isValid = false;
|
|
26
|
+
if (provider === "stripe") {
|
|
27
|
+
const sig = getRequestHeader(event, "stripe-signature");
|
|
28
|
+
isValid = await verifyStripeSignature(rawBody, sig, config.webhookSecret);
|
|
29
|
+
} else if (provider === "lemonsqueezy") {
|
|
30
|
+
const sig = getRequestHeader(event, "x-signature");
|
|
31
|
+
isValid = await verifyLemonSqueezySignature(rawBody, sig, config.webhookSecret);
|
|
32
|
+
} else if (provider === "polar") {
|
|
33
|
+
const sig = getRequestHeader(event, "webhook-signature");
|
|
34
|
+
const ts = getRequestHeader(event, "webhook-timestamp");
|
|
35
|
+
const webhookId = getRequestHeader(event, "webhook-id");
|
|
36
|
+
isValid = await verifyPolarSignature(rawBody, sig, config.webhookSecret, {
|
|
37
|
+
timestamp: ts,
|
|
38
|
+
webhookId
|
|
39
|
+
});
|
|
40
|
+
} else if (provider === "paddle") {
|
|
41
|
+
const sig = getRequestHeader(event, "paddle-signature");
|
|
42
|
+
isValid = await verifyPaddleSignature(rawBody, sig, config.webhookSecret);
|
|
43
|
+
}
|
|
44
|
+
if (!isValid) {
|
|
45
|
+
console.warn(`[qarpeo:payments] Invalid webhook signature from provider: ${provider}`);
|
|
46
|
+
return { success: false, error: "invalid_signature" };
|
|
47
|
+
}
|
|
48
|
+
let payload;
|
|
49
|
+
try {
|
|
50
|
+
payload = JSON.parse(rawBody);
|
|
51
|
+
} catch {
|
|
52
|
+
throw createError({
|
|
53
|
+
statusCode: 400,
|
|
54
|
+
statusMessage: "Bad Request",
|
|
55
|
+
message: "Invalid JSON payload"
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
const now = Date.now();
|
|
59
|
+
let eventType = "";
|
|
60
|
+
let eventData = null;
|
|
61
|
+
let affectedSubId = null;
|
|
62
|
+
let eventId = "";
|
|
63
|
+
if (provider === "stripe") {
|
|
64
|
+
eventId = payload.id || "";
|
|
65
|
+
} else if (provider === "lemonsqueezy") {
|
|
66
|
+
eventId = String(payload.meta?.event_id || payload.meta?.id || payload.data?.id || "");
|
|
67
|
+
} else if (provider === "polar") {
|
|
68
|
+
eventId = payload.id || "";
|
|
69
|
+
} else if (provider === "paddle") {
|
|
70
|
+
eventId = payload.event_id || payload.id || "";
|
|
71
|
+
}
|
|
72
|
+
if (provider === "lemonsqueezy") {
|
|
73
|
+
eventType = payload.meta?.event_name || "";
|
|
74
|
+
} else if (provider === "paddle") {
|
|
75
|
+
eventType = payload.event_type || payload.type || "";
|
|
76
|
+
} else {
|
|
77
|
+
eventType = payload.type || "";
|
|
78
|
+
}
|
|
79
|
+
let kv = null;
|
|
80
|
+
try {
|
|
81
|
+
kv = getKVBinding("KV", event);
|
|
82
|
+
} catch {
|
|
83
|
+
}
|
|
84
|
+
const kvKey = `payments:idempotency:${provider}:${eventId}`;
|
|
85
|
+
if (kv && eventId) {
|
|
86
|
+
try {
|
|
87
|
+
const cached = await kv.get(kvKey);
|
|
88
|
+
if (cached === "completed") {
|
|
89
|
+
return { received: true, skipped: true, reason: "duplicate" };
|
|
90
|
+
}
|
|
91
|
+
} catch {
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (eventId) {
|
|
95
|
+
const dedupRes = await db.prepare(
|
|
96
|
+
`INSERT OR IGNORE INTO ${wt} (event_id, provider, event_type, processed_at) VALUES (?, ?, ?, ?)`
|
|
97
|
+
).bind(eventId, provider, eventType || null, now).run();
|
|
98
|
+
if (!dedupRes?.meta?.changes) {
|
|
99
|
+
if (kv) {
|
|
100
|
+
try {
|
|
101
|
+
await kv.put(kvKey, "completed", { expirationTtl: 604800 });
|
|
102
|
+
} catch {
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return { received: true, skipped: true, reason: "duplicate" };
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
try {
|
|
109
|
+
if (provider === "stripe") {
|
|
110
|
+
;
|
|
111
|
+
({ eventType, eventData, affectedSubId } = await handleStripeWebhookEvent(
|
|
112
|
+
db,
|
|
113
|
+
ct,
|
|
114
|
+
st,
|
|
115
|
+
payload,
|
|
116
|
+
now
|
|
117
|
+
));
|
|
118
|
+
} else if (provider === "lemonsqueezy") {
|
|
119
|
+
;
|
|
120
|
+
({ eventType, eventData, affectedSubId } = await handleLemonSqueezyWebhookEvent(
|
|
121
|
+
db,
|
|
122
|
+
ct,
|
|
123
|
+
st,
|
|
124
|
+
payload,
|
|
125
|
+
now
|
|
126
|
+
));
|
|
127
|
+
} else if (provider === "polar") {
|
|
128
|
+
;
|
|
129
|
+
({ eventType, eventData, affectedSubId } = await handlePolarWebhookEvent(
|
|
130
|
+
db,
|
|
131
|
+
st,
|
|
132
|
+
payload,
|
|
133
|
+
now
|
|
134
|
+
));
|
|
135
|
+
} else if (provider === "paddle") {
|
|
136
|
+
;
|
|
137
|
+
({ eventType, eventData, affectedSubId } = await handlePaddleWebhookEvent(
|
|
138
|
+
db,
|
|
139
|
+
ct,
|
|
140
|
+
st,
|
|
141
|
+
payload,
|
|
142
|
+
now
|
|
143
|
+
));
|
|
144
|
+
}
|
|
145
|
+
let syncedSubscription = null;
|
|
146
|
+
if (affectedSubId) {
|
|
147
|
+
try {
|
|
148
|
+
const subRow = await db.prepare(`SELECT * FROM ${st} WHERE subscription_id = ? LIMIT 1`).bind(affectedSubId).first();
|
|
149
|
+
if (subRow) {
|
|
150
|
+
syncedSubscription = rowToSubscription(subRow);
|
|
151
|
+
}
|
|
152
|
+
} catch {
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
await notifyPaymentWebhookListeners({
|
|
156
|
+
event,
|
|
157
|
+
provider,
|
|
158
|
+
type: eventType,
|
|
159
|
+
data: eventData,
|
|
160
|
+
payload,
|
|
161
|
+
subscription: syncedSubscription
|
|
162
|
+
});
|
|
163
|
+
try {
|
|
164
|
+
const nitroApp = globalThis.useNitroApp?.();
|
|
165
|
+
await nitroApp?.hooks?.callHook("qarpeo:payments:webhook", {
|
|
166
|
+
event,
|
|
167
|
+
type: eventType,
|
|
168
|
+
data: eventData,
|
|
169
|
+
subscription: syncedSubscription,
|
|
170
|
+
provider
|
|
171
|
+
});
|
|
172
|
+
} catch (hookErr) {
|
|
173
|
+
console.warn(
|
|
174
|
+
"[qarpeo:payments] Error calling qarpeo:payments:webhook hook:",
|
|
175
|
+
hookErr?.message || hookErr
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
if (kv && eventId) {
|
|
179
|
+
try {
|
|
180
|
+
await kv.put(kvKey, "completed", { expirationTtl: 604800 });
|
|
181
|
+
} catch {
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return { success: true, eventType };
|
|
185
|
+
} catch (processingErr) {
|
|
186
|
+
if (eventId) {
|
|
187
|
+
try {
|
|
188
|
+
await db.prepare(`DELETE FROM ${wt} WHERE event_id = ?`).bind(eventId).run();
|
|
189
|
+
} catch {
|
|
190
|
+
}
|
|
191
|
+
if (kv) {
|
|
192
|
+
try {
|
|
193
|
+
await kv.delete(kvKey);
|
|
194
|
+
} catch {
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
throw processingErr;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
@@ -1,158 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import type { PaymentsOptions, PaymentProvider, QarpeoSubscription, SubscriptionStatus, CreateCheckoutOptions, MeterRecordOptions, MeterUsageResult, UsageSummary } from "../../../types";
|
|
3
|
-
export declare function getPaymentsConfig(event?: RequestEvent): PaymentsOptions & {
|
|
4
|
-
provider: PaymentProvider;
|
|
5
|
-
secretKey: string;
|
|
6
|
-
webhookSecret: string;
|
|
7
|
-
};
|
|
8
|
-
export declare function ensurePaymentsTables(event?: RequestEvent): Promise<void>;
|
|
9
|
-
/** Verifies Stripe webhook signature: Stripe-Signature header contains: `t=timestamp,v1=signature` */
|
|
10
|
-
export declare function verifyStripeSignature(rawBody: string, signatureHeader: string | null | undefined, secret: string, toleranceSec?: number): Promise<boolean>;
|
|
11
|
-
/** Verifies Lemon Squeezy webhook signature (X-Signature header) */
|
|
12
|
-
export declare function verifyLemonSqueezySignature(rawBody: string, signatureHeader: string | null | undefined, secret: string): Promise<boolean>;
|
|
13
|
-
/** Verifies Polar webhook signature (webhook-signature header with optional Standard Webhooks headers) */
|
|
14
|
-
export declare function verifyPolarSignature(rawBody: string, signatureHeader: string | null | undefined, secret: string, options?: {
|
|
15
|
-
timestamp?: string | null;
|
|
16
|
-
webhookId?: string | null;
|
|
17
|
-
toleranceSec?: number;
|
|
18
|
-
}): Promise<boolean>;
|
|
19
|
-
/** Verifies Paddle Billing webhook signature (Paddle-Signature header: `ts=TIMESTAMP;h1=HMAC_HEX`) with replay protection */
|
|
20
|
-
export declare function verifyPaddleSignature(rawBody: string, signatureHeader: string | null | undefined, secret: string, toleranceSec?: number): Promise<boolean>;
|
|
21
|
-
export declare function processPaymentWebhook(event: RequestEvent, rawBody: string): Promise<{
|
|
22
|
-
success: boolean;
|
|
23
|
-
eventType?: string;
|
|
24
|
-
error?: string;
|
|
25
|
-
} | {
|
|
26
|
-
received: true;
|
|
27
|
-
skipped: true;
|
|
28
|
-
reason: "duplicate";
|
|
29
|
-
}>;
|
|
30
|
-
/**
|
|
31
|
-
* Creates a Stripe checkout session via Stripe REST API. Runs 100% edge-native on Cloudflare
|
|
32
|
-
* Workers using standard `fetch`.
|
|
33
|
-
*
|
|
34
|
-
* @example
|
|
35
|
-
* ```ts
|
|
36
|
-
* const { url } = await createCheckoutSession(event, {
|
|
37
|
-
* planId: "pro_monthly",
|
|
38
|
-
* successUrl: "https://example.com/billing?success=true",
|
|
39
|
-
* cancelUrl: "https://example.com/pricing",
|
|
40
|
-
* })
|
|
41
|
-
* ```
|
|
42
|
-
*/
|
|
43
|
-
export declare function createCheckoutSession(event: RequestEvent, options: CreateCheckoutOptions): Promise<{
|
|
44
|
-
id: string;
|
|
45
|
-
url: string;
|
|
46
|
-
}>;
|
|
47
|
-
/**
|
|
48
|
-
* Creates a Customer Billing Portal session for the current user.
|
|
49
|
-
*
|
|
50
|
-
* @example
|
|
51
|
-
* ```ts
|
|
52
|
-
* const { url } = await createCustomerPortalSession(event, {
|
|
53
|
-
* returnUrl: "https://example.com/settings/billing",
|
|
54
|
-
* })
|
|
55
|
-
* ```
|
|
56
|
-
*/
|
|
57
|
-
export declare function createCustomerPortalSession(event: RequestEvent, options?: {
|
|
58
|
-
returnUrl?: string;
|
|
59
|
-
}): Promise<{
|
|
60
|
-
url: string;
|
|
61
|
-
}>;
|
|
62
|
-
/**
|
|
63
|
-
* Cancels a Stripe subscription at the end of the current billing period. Calls Stripe POST
|
|
64
|
-
* /v1/subscriptions/{id} with `cancel_at_period_end: true` and marks the D1 row as canceled.
|
|
65
|
-
*/
|
|
66
|
-
export declare function cancelSubscription(event: RequestEvent, subscriptionId: string): Promise<{
|
|
67
|
-
success: boolean;
|
|
68
|
-
subscriptionId: string;
|
|
69
|
-
status: string;
|
|
70
|
-
}>;
|
|
71
|
-
/**
|
|
72
|
-
* Pauses collection on a Stripe subscription. Calls Stripe POST /v1/subscriptions/{id} with
|
|
73
|
-
* `pause_collection: { behavior: 'void', resumes_at: null }` and marks the D1 row as paused.
|
|
74
|
-
*/
|
|
75
|
-
export declare function pauseSubscription(event: RequestEvent, subscriptionId: string): Promise<{
|
|
76
|
-
success: boolean;
|
|
77
|
-
subscriptionId: string;
|
|
78
|
-
status: string;
|
|
79
|
-
}>;
|
|
80
|
-
/**
|
|
81
|
-
* Changes a Stripe subscription to a new price/plan with proration. Fetches the current
|
|
82
|
-
* subscription item id, calls Stripe POST /v1/subscriptions/{id} to swap the item price, and
|
|
83
|
-
* updates the D1 plan_id/price_id.
|
|
84
|
-
*/
|
|
85
|
-
export declare function changePlan(event: RequestEvent, subscriptionId: string, newPriceId: string): Promise<{
|
|
86
|
-
success: boolean;
|
|
87
|
-
subscriptionId: string;
|
|
88
|
-
planId: string;
|
|
89
|
-
}>;
|
|
90
|
-
/**
|
|
91
|
-
* Gets the current active subscription for a user or organization from D1.
|
|
92
|
-
*
|
|
93
|
-
* @example
|
|
94
|
-
* ```ts
|
|
95
|
-
* const sub = await getSubscription(event)
|
|
96
|
-
* if (sub?.status === "active") {
|
|
97
|
-
* console.log("Plan:", sub.planId)
|
|
98
|
-
* }
|
|
99
|
-
* ```
|
|
100
|
-
*/
|
|
101
|
-
export declare function getSubscription(event: RequestEvent, options?: {
|
|
102
|
-
userId?: string;
|
|
103
|
-
orgId?: string;
|
|
104
|
-
}): Promise<QarpeoSubscription | null>;
|
|
105
|
-
export interface RequireSubscriptionOptions {
|
|
106
|
-
tier?: string | string[];
|
|
107
|
-
plan?: string | string[];
|
|
108
|
-
status?: SubscriptionStatus[];
|
|
109
|
-
message?: string;
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* Server guard: throws 402 Payment Required if caller has no active subscription.
|
|
113
|
-
*
|
|
114
|
-
* @example
|
|
115
|
-
* ```ts
|
|
116
|
-
* const sub = await requireSubscription(event, { plan: "pro" })
|
|
117
|
-
* ```
|
|
118
|
-
*/
|
|
119
|
-
export declare function requireSubscription(event: RequestEvent, options?: RequireSubscriptionOptions): Promise<QarpeoSubscription>;
|
|
120
|
-
/**
|
|
121
|
-
* Checks whether the current user or organization's active subscription plan includes a specific
|
|
122
|
-
* feature flag.
|
|
123
|
-
*
|
|
124
|
-
* @example
|
|
125
|
-
* ```ts
|
|
126
|
-
* if (!(await hasFeature(event, "custom_domain"))) {
|
|
127
|
-
* throw createError({ statusCode: 403, message: "Custom domains require Pro plan" })
|
|
128
|
-
* }
|
|
129
|
-
* ```
|
|
130
|
-
*/
|
|
131
|
-
export declare function hasFeature(event: RequestEvent, feature: string): Promise<boolean>;
|
|
132
|
-
/**
|
|
133
|
-
* Checks whether a numerical usage limit defined on the active plan configuration has been
|
|
134
|
-
* exceeded.
|
|
135
|
-
*
|
|
136
|
-
* @example
|
|
137
|
-
* ```ts
|
|
138
|
-
* const { allowed, limit, current } = await checkLimit(event, "max_projects", projectCount)
|
|
139
|
-
* if (!allowed) {
|
|
140
|
-
* throw createError({ statusCode: 403, message: `Project limit (${limit}) reached` })
|
|
141
|
-
* }
|
|
142
|
-
* ```
|
|
143
|
-
*/
|
|
144
|
-
export declare function checkLimit(event: RequestEvent, limitKey: string, currentUsage: number): Promise<{
|
|
145
|
-
allowed: boolean;
|
|
146
|
-
limit: number;
|
|
147
|
-
current: number;
|
|
148
|
-
}>;
|
|
149
|
-
/** Records a metered usage event in D1 and reports to upstream provider (e.g. Stripe Meters API). */
|
|
150
|
-
export declare function reportUsage(event: RequestEvent, options: MeterRecordOptions): Promise<MeterUsageResult>;
|
|
151
|
-
/** Queries metered usage summary and records from D1 for a user or organization. */
|
|
152
|
-
export declare function getUsageSummary(event: RequestEvent, options: {
|
|
153
|
-
eventName?: string;
|
|
154
|
-
userId?: string;
|
|
155
|
-
orgId?: string;
|
|
156
|
-
since?: number;
|
|
157
|
-
until?: number;
|
|
158
|
-
}): Promise<UsageSummary>;
|
|
1
|
+
export * from "./payments/index";
|