qarpeo 0.1.34 → 0.2.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/README.md +114 -26
- package/dist/apps/cms.d.ts +4 -0
- package/dist/apps/comments.d.ts +4 -0
- package/dist/apps/payments.d.ts +4 -0
- package/dist/apps/store.d.ts +4 -0
- package/dist/chunks/auth-schema.mjs +169 -30
- package/dist/chunks/tools.mjs +1680 -0
- package/dist/cli/index.mjs +1256 -221
- package/dist/core/aliases.d.ts +5 -0
- package/dist/core/context.d.ts +10 -0
- package/dist/core/cross-drive.d.ts +2 -0
- package/dist/core/d1-migrate.d.ts +4 -0
- package/dist/core/devtools.d.ts +4 -0
- package/dist/core/nitro.d.ts +4 -0
- package/dist/core/schema.d.ts +4 -0
- package/dist/core/server-utils.d.ts +4 -0
- package/dist/core/wrangler.d.ts +4 -0
- package/dist/dev/wrangler.d.ts +1 -0
- package/dist/fate/index.d.ts +8 -8
- package/dist/features/agents.d.ts +4 -0
- package/dist/features/auth.d.ts +4 -0
- package/dist/features/bots.d.ts +4 -0
- package/dist/features/fate.d.ts +4 -0
- package/dist/features/mail.d.ts +4 -0
- package/dist/features/mcp.d.ts +4 -0
- package/dist/features/multitenant.d.ts +4 -0
- package/dist/features/notifications.d.ts +4 -0
- package/dist/features/realtime.d.ts +4 -0
- package/dist/module.d.ts +10 -2
- package/dist/module.mjs +6313 -5114
- package/dist/primitives/ai.d.ts +4 -0
- package/dist/primitives/analytics.d.ts +4 -0
- package/dist/primitives/blob.d.ts +4 -0
- package/dist/primitives/browser.d.ts +4 -0
- package/dist/primitives/cache.d.ts +4 -0
- package/dist/primitives/d1.d.ts +4 -0
- package/dist/primitives/do.d.ts +4 -0
- package/dist/primitives/email.d.ts +4 -0
- package/dist/primitives/hyperdrive.d.ts +4 -0
- package/dist/primitives/kv.d.ts +4 -0
- package/dist/primitives/queue.d.ts +4 -0
- package/dist/primitives/ratelimit.d.ts +4 -0
- package/dist/primitives/vectorize.d.ts +4 -0
- package/dist/primitives/workflows.d.ts +4 -0
- package/dist/runtime/ai.d.ts +3 -3
- package/dist/runtime/ai.mjs +8 -1
- package/dist/runtime/app/components/RealtimeCursors.vue +2 -2
- package/dist/runtime/app/composables/ai.d.ts +56 -29
- package/dist/runtime/app/composables/analytics.d.ts +26 -106
- package/dist/runtime/app/composables/api-tokens.d.ts +31 -34
- package/dist/runtime/app/composables/auth.d.ts +147 -51
- package/dist/runtime/app/composables/auth.mjs +0 -7
- package/dist/runtime/app/composables/chat.d.ts +38 -35
- package/dist/runtime/app/composables/cms-preview.d.ts +30 -25
- package/dist/runtime/app/composables/cms.d.ts +57 -282
- package/dist/runtime/app/composables/cms.mjs +0 -35
- package/dist/runtime/app/composables/comments.d.ts +32 -5
- package/dist/runtime/app/composables/comments.mjs +29 -1
- package/dist/runtime/app/composables/docs-version.d.ts +25 -28
- package/dist/runtime/app/composables/impersonation.d.ts +22 -5
- package/dist/runtime/app/composables/organization.d.ts +38 -91
- package/dist/runtime/app/composables/passkey.d.ts +22 -6
- package/dist/runtime/app/composables/passwordless.d.ts +24 -7
- package/dist/runtime/app/composables/payments.d.ts +50 -34
- package/dist/runtime/app/composables/payments.mjs +1 -0
- package/dist/runtime/app/composables/sse.d.ts +76 -0
- package/dist/runtime/app/composables/sse.mjs +152 -0
- package/dist/runtime/app/composables/storage.d.ts +53 -65
- package/dist/runtime/app/composables/storage.mjs +17 -0
- package/dist/runtime/app/composables/store.d.ts +198 -0
- package/dist/runtime/app/composables/store.mjs +414 -0
- package/dist/runtime/app/composables/tenant.d.ts +31 -72
- package/dist/runtime/app/composables/turnstile.d.ts +16 -3
- package/dist/runtime/app/composables/two-factor.d.ts +23 -6
- package/dist/runtime/app/pages/devtools/notifications.vue +30 -27
- package/dist/runtime/app/pages/devtools/realtime.vue +19 -31
- package/dist/runtime/app/pages/devtools.vue +0 -7
- package/dist/runtime/auth.d.ts +9 -9
- package/dist/runtime/blob.d.ts +10 -1
- package/dist/runtime/blob.mjs +83 -0
- package/dist/runtime/cache.d.ts +48 -0
- package/dist/runtime/cache.mjs +92 -0
- package/dist/runtime/comments.d.ts +4 -0
- package/dist/runtime/comments.mjs +2 -0
- package/dist/runtime/db.d.ts +21 -1
- package/dist/runtime/db.mjs +54 -0
- package/dist/runtime/drizzle.d.ts +4 -2
- package/dist/runtime/drizzle.mjs +68 -1
- package/dist/runtime/durable-object.d.ts +2 -0
- package/dist/runtime/durable-object.mjs +2 -0
- package/dist/runtime/email.d.ts +1 -1
- package/dist/runtime/email.mjs +1 -1
- package/dist/runtime/kv.mjs +50 -1
- package/dist/runtime/mcp.d.ts +2 -0
- package/dist/runtime/mcp.mjs +2 -0
- package/dist/runtime/queue.d.ts +12 -2
- package/dist/runtime/queue.mjs +19 -1
- package/dist/runtime/ratelimit.d.ts +1 -0
- package/dist/runtime/ratelimit.mjs +7 -0
- package/dist/runtime/schema.d.ts +313 -0
- package/dist/runtime/schema.mjs +259 -0
- package/dist/runtime/server/db/cms-schema.d.ts +1225 -171
- package/dist/runtime/server/db/cms-schema.mjs +69 -70
- package/dist/runtime/server/db/store-schema.d.ts +4344 -0
- package/dist/runtime/server/db/store-schema.mjs +266 -0
- package/dist/runtime/server/durable-objects/agent.mjs +35 -0
- package/dist/runtime/server/durable-objects/room.mjs +0 -1
- package/dist/runtime/server/middleware/ratelimit.mjs +10 -8
- package/dist/runtime/server/plugins/comments.d.ts +7 -0
- package/dist/runtime/server/plugins/comments.mjs +7 -0
- package/dist/runtime/server/plugins/email-inbound.d.ts +5 -0
- package/dist/runtime/server/plugins/email-inbound.mjs +17 -0
- package/dist/runtime/server/plugins/store.d.ts +7 -0
- package/dist/runtime/server/plugins/store.mjs +7 -0
- package/dist/runtime/server/routes/__qarpeo/db/tables.get.mjs +2 -2
- package/dist/runtime/server/routes/atom.xml.get.mjs +3 -1
- package/dist/runtime/server/routes/auth/2fa/disable.post.mjs +13 -2
- package/dist/runtime/server/routes/auth/2fa/setup.post.mjs +9 -3
- package/dist/runtime/server/routes/auth/2fa/verify.post.mjs +11 -3
- package/dist/runtime/server/routes/auth/account.delete.mjs +2 -1
- package/dist/runtime/server/routes/auth/audit-logs.get.mjs +1 -1
- package/dist/runtime/server/routes/auth/magic-link/send.post.d.ts +1 -9
- package/dist/runtime/server/routes/auth/magic-link/send.post.mjs +8 -0
- package/dist/runtime/server/routes/auth/organization/sso/[id].delete.mjs +1 -1
- package/dist/runtime/server/routes/auth/organization/sso/index.get.mjs +1 -1
- package/dist/runtime/server/routes/auth/organization/sso/index.post.mjs +1 -1
- package/dist/runtime/server/routes/auth/otp/send.post.d.ts +1 -9
- package/dist/runtime/server/routes/auth/otp/send.post.mjs +8 -0
- package/dist/runtime/server/routes/auth/sso/login.get.mjs +3 -3
- package/dist/runtime/server/routes/auth/sso/oidc/callback.get.mjs +1 -1
- package/dist/runtime/server/routes/auth/sso/saml/acs.post.mjs +2 -2
- package/dist/runtime/server/routes/auth/webauthn/login/options.post.mjs +1 -1
- package/dist/runtime/server/routes/auth/webauthn/login/verify.post.mjs +1 -1
- package/dist/runtime/server/routes/auth/webauthn/register/options.post.mjs +2 -2
- package/dist/runtime/server/routes/auth/webauthn/register/verify.post.mjs +1 -1
- package/dist/runtime/server/routes/cms/[collection]/[id]/preview-token.post.mjs +1 -1
- package/dist/runtime/server/routes/cms/[collection]/atom.xml.get.mjs +3 -1
- package/dist/runtime/server/routes/cms/[collection]/feed.xml.get.mjs +3 -1
- package/dist/runtime/server/routes/cms/bookmarks/[id].delete.mjs +1 -1
- package/dist/runtime/server/routes/cms/bookmarks/check.post.mjs +1 -1
- package/dist/runtime/server/routes/cms/bookmarks/index.get.mjs +1 -1
- package/dist/runtime/server/routes/cms/bookmarks/index.post.mjs +1 -1
- package/dist/runtime/server/routes/cms/deploy.post.mjs +1 -1
- package/dist/runtime/server/routes/cms/export.post.mjs +4 -3
- package/dist/runtime/server/routes/cms/search.get.mjs +7 -5
- package/dist/runtime/server/routes/cms/sync.post.mjs +2 -1
- package/dist/runtime/server/routes/comments/[id]/react.post.mjs +1 -2
- package/dist/runtime/server/routes/comments/[id]/report.post.d.ts +1 -1
- package/dist/runtime/server/routes/comments/[id]/report.post.mjs +0 -2
- package/dist/runtime/server/routes/comments/[id].delete.mjs +8 -4
- package/dist/runtime/server/routes/comments/[id].get.d.ts +1 -1
- package/dist/runtime/server/routes/comments/[id].get.mjs +1 -2
- package/dist/runtime/server/routes/comments/[id].patch.d.ts +1 -1
- package/dist/runtime/server/routes/comments/[id].patch.mjs +6 -3
- package/dist/runtime/server/routes/comments/batch.post.d.ts +2 -0
- package/dist/runtime/server/routes/comments/batch.post.mjs +33 -0
- package/dist/runtime/server/routes/comments/index.get.d.ts +1 -1
- package/dist/runtime/server/routes/comments/index.get.mjs +6 -4
- package/dist/runtime/server/routes/comments/index.post.d.ts +1 -1
- package/dist/runtime/server/routes/comments/index.post.mjs +6 -2
- package/dist/runtime/server/routes/feed.xml.get.mjs +3 -1
- package/dist/runtime/server/routes/sitemap.xml.get.mjs +3 -1
- package/dist/runtime/server/routes/storage/presigned-r2.post.d.ts +17 -0
- package/dist/runtime/server/routes/storage/presigned-r2.post.mjs +29 -0
- package/dist/runtime/server/routes/store/cart/index.delete.d.ts +6 -0
- package/dist/runtime/server/routes/store/cart/index.delete.mjs +11 -0
- package/dist/runtime/server/routes/store/cart/index.get.d.ts +5 -0
- package/dist/runtime/server/routes/store/cart/index.get.mjs +14 -0
- package/dist/runtime/server/routes/store/cart/items/[id].delete.d.ts +6 -0
- package/dist/runtime/server/routes/store/cart/items/[id].delete.mjs +25 -0
- package/dist/runtime/server/routes/store/cart/items/[id].patch.d.ts +6 -0
- package/dist/runtime/server/routes/store/cart/items/[id].patch.mjs +29 -0
- package/dist/runtime/server/routes/store/cart/items.post.d.ts +6 -0
- package/dist/runtime/server/routes/store/cart/items.post.mjs +36 -0
- package/dist/runtime/server/routes/store/categories/index.get.d.ts +2 -0
- package/dist/runtime/server/routes/store/categories/index.get.mjs +8 -0
- package/dist/runtime/server/routes/store/checkout.post.d.ts +6 -0
- package/dist/runtime/server/routes/store/checkout.post.mjs +21 -0
- package/dist/runtime/server/routes/store/coupons/apply.post.d.ts +7 -0
- package/dist/runtime/server/routes/store/coupons/apply.post.mjs +18 -0
- package/dist/runtime/server/routes/store/currencies.get.d.ts +15 -0
- package/dist/runtime/server/routes/store/currencies.get.mjs +13 -0
- package/dist/runtime/server/routes/store/orders/[id].get.d.ts +2 -0
- package/dist/runtime/server/routes/store/orders/[id].get.mjs +26 -0
- package/dist/runtime/server/routes/store/orders/download/[token].get.d.ts +7 -0
- package/dist/runtime/server/routes/store/orders/download/[token].get.mjs +57 -0
- package/dist/runtime/server/routes/store/orders/index.get.d.ts +5 -0
- package/dist/runtime/server/routes/store/orders/index.get.mjs +30 -0
- package/dist/runtime/server/routes/store/products/[slug].get.d.ts +2 -0
- package/dist/runtime/server/routes/store/products/[slug].get.mjs +18 -0
- package/dist/runtime/server/routes/store/products/index.get.d.ts +5 -0
- package/dist/runtime/server/routes/store/products/index.get.mjs +33 -0
- package/dist/runtime/server/routes/store/tax/calculate.post.d.ts +9 -0
- package/dist/runtime/server/routes/store/tax/calculate.post.mjs +18 -0
- package/dist/runtime/server/routes/tenants/[tenant]/domains/[domain]/index.delete.mjs +14 -1
- package/dist/runtime/server/routes/tenants/[tenant]/domains/[domain]/verify.post.mjs +14 -1
- package/dist/runtime/server/routes/tenants/[tenant]/domains/index.get.mjs +24 -1
- package/dist/runtime/server/routes/tenants/[tenant]/domains/index.post.mjs +6 -0
- package/dist/runtime/server/utils/ai.d.ts +146 -21
- package/dist/runtime/server/utils/ai.mjs +406 -11
- package/dist/runtime/server/utils/analytics-query.d.ts +13 -58
- package/dist/runtime/server/utils/audit.d.ts +5 -4
- package/dist/runtime/server/utils/audit.mjs +25 -14
- package/dist/runtime/server/utils/auth.d.ts +29 -12
- package/dist/runtime/server/utils/auth.mjs +88 -70
- package/dist/runtime/server/utils/bots.d.ts +20 -4
- package/dist/runtime/server/utils/bots.mjs +7 -3
- package/dist/runtime/server/utils/browser-utils.d.ts +23 -23
- package/dist/runtime/server/utils/cache.d.ts +31 -10
- package/dist/runtime/server/utils/cache.mjs +130 -53
- package/dist/runtime/server/utils/cms/collections.d.ts +23 -0
- package/dist/runtime/server/utils/cms/collections.mjs +143 -0
- package/dist/runtime/server/utils/cms/common.d.ts +9 -0
- package/dist/runtime/server/utils/cms/common.mjs +28 -0
- package/dist/runtime/server/utils/cms/git.mjs +2 -2
- package/dist/runtime/server/utils/cms/navigation.d.ts +46 -0
- package/dist/runtime/server/utils/cms/navigation.mjs +683 -0
- package/dist/runtime/server/utils/cms/preview.d.ts +11 -0
- package/dist/runtime/server/utils/cms/preview.mjs +50 -0
- package/dist/runtime/server/utils/cms/search.d.ts +1 -1
- package/dist/runtime/server/utils/cms/search.mjs +11 -7
- package/dist/runtime/server/utils/cms/sync.d.ts +34 -0
- package/dist/runtime/server/utils/cms/sync.mjs +1082 -0
- package/dist/runtime/server/utils/cms/taxonomy.d.ts +103 -0
- package/dist/runtime/server/utils/cms/taxonomy.mjs +348 -0
- package/dist/runtime/server/utils/cms.d.ts +13 -249
- package/dist/runtime/server/utils/cms.mjs +108 -2350
- package/dist/runtime/server/utils/comments-filter.d.ts +45 -0
- package/dist/runtime/server/utils/comments-filter.mjs +541 -0
- package/dist/runtime/server/utils/comments.d.ts +71 -0
- package/dist/runtime/server/utils/comments.mjs +137 -61
- package/dist/runtime/server/utils/config.d.ts +4 -3
- package/dist/runtime/server/utils/config.mjs +7 -6
- package/dist/runtime/server/utils/context.d.ts +14 -0
- package/dist/runtime/server/utils/context.mjs +2 -2
- package/dist/runtime/server/utils/crypto.d.ts +44 -5
- package/dist/runtime/server/utils/crypto.mjs +18 -0
- package/dist/runtime/server/utils/domains.mjs +9 -5
- package/dist/runtime/server/utils/drizzle.d.ts +35 -3
- package/dist/runtime/server/utils/drizzle.mjs +115 -6
- package/dist/runtime/server/utils/durable-object.d.ts +106 -0
- package/dist/runtime/server/utils/durable-object.mjs +628 -0
- package/dist/runtime/server/utils/email-inbound.d.ts +29 -0
- package/dist/runtime/server/utils/email-inbound.mjs +76 -0
- package/dist/runtime/server/utils/impersonation.d.ts +32 -2
- package/dist/runtime/server/utils/impersonation.mjs +16 -13
- package/dist/runtime/server/utils/lock.d.ts +71 -0
- package/dist/runtime/server/utils/lock.mjs +139 -0
- package/dist/runtime/server/utils/mail-suppression.mjs +7 -7
- package/dist/runtime/server/utils/mail.d.ts +30 -19
- package/dist/runtime/server/utils/mcp.d.ts +44 -0
- package/dist/runtime/server/utils/mcp.mjs +308 -0
- package/dist/runtime/server/utils/notifications.d.ts +72 -7
- package/dist/runtime/server/utils/notifications.mjs +67 -55
- package/dist/runtime/server/utils/oauth.d.ts +27 -4
- package/dist/runtime/server/utils/organization.d.ts +63 -7
- package/dist/runtime/server/utils/organization.mjs +26 -21
- package/dist/runtime/server/utils/passwordless.d.ts +62 -33
- package/dist/runtime/server/utils/payments.d.ts +51 -12
- package/dist/runtime/server/utils/payments.mjs +472 -282
- package/dist/runtime/server/utils/queue.d.ts +106 -13
- package/dist/runtime/server/utils/queue.mjs +313 -28
- package/dist/runtime/server/utils/rag.d.ts +42 -14
- package/dist/runtime/server/utils/rag.mjs +169 -4
- package/dist/runtime/server/utils/ratelimit.d.ts +34 -5
- package/dist/runtime/server/utils/ratelimit.mjs +95 -14
- package/dist/runtime/server/utils/realtime.d.ts +68 -52
- package/dist/runtime/server/utils/scheduled-task.d.ts +42 -1
- package/dist/runtime/server/utils/scheduled-task.mjs +29 -0
- package/dist/runtime/server/utils/schema-fallback.d.ts +1 -1
- package/dist/runtime/server/utils/schema-fallback.mjs +1 -1
- package/dist/runtime/server/utils/session.d.ts +84 -10
- package/dist/runtime/server/utils/session.mjs +12 -10
- package/dist/runtime/server/utils/sse.d.ts +75 -0
- package/dist/runtime/server/utils/sse.mjs +147 -0
- package/dist/runtime/server/utils/sso.d.ts +1 -1
- package/dist/runtime/server/utils/storage.d.ts +53 -4
- package/dist/runtime/server/utils/storage.mjs +15 -11
- package/dist/runtime/server/utils/store-localization.d.ts +51 -0
- package/dist/runtime/server/utils/store-localization.mjs +417 -0
- package/dist/runtime/server/utils/store.d.ts +207 -0
- package/dist/runtime/server/utils/store.mjs +2227 -0
- package/dist/runtime/server/utils/table-names.d.ts +51 -0
- package/dist/runtime/server/utils/table-names.mjs +69 -0
- package/dist/runtime/server/utils/tenant.d.ts +26 -2
- package/dist/runtime/server/utils/tenant.mjs +16 -12
- package/dist/runtime/server/utils/tools-registry-fallback.d.ts +1 -0
- package/dist/runtime/server/utils/tools-registry-fallback.mjs +1 -0
- package/dist/runtime/server/utils/totp.d.ts +45 -2
- package/dist/runtime/server/utils/tracker.d.ts +10 -11
- package/dist/runtime/server/utils/turnstile.d.ts +16 -1
- package/dist/runtime/server/utils/workflows.d.ts +92 -0
- package/dist/runtime/server/utils/workflows.mjs +299 -0
- package/dist/runtime/server.d.ts +8 -1
- package/dist/runtime/server.mjs +8 -5
- package/dist/runtime/service.d.ts +10 -10
- package/dist/runtime/storage.d.ts +1 -0
- package/dist/runtime/storage.mjs +1 -0
- package/dist/runtime/store.d.ts +4 -0
- package/dist/runtime/store.mjs +2 -0
- package/dist/runtime/test-utils.d.ts +11 -10
- package/dist/runtime/test-utils.mjs +64 -4
- package/dist/runtime/types/ai.d.ts +21 -2
- package/dist/runtime/types/d1.d.ts +17 -0
- package/dist/runtime/types/drizzle.d.ts +2 -2
- package/dist/runtime/types/durable-object.d.ts +62 -2
- package/dist/runtime/types/email.d.ts +46 -0
- package/dist/runtime/types/imports.d.ts +6 -1
- package/dist/runtime/types/index.d.ts +58 -0
- package/dist/runtime/types/index.mjs +3 -0
- package/dist/runtime/types/mcp.d.ts +83 -0
- package/dist/runtime/types/mcp.mjs +0 -0
- package/dist/runtime/types/queue.d.ts +36 -0
- package/dist/runtime/types/r2.d.ts +41 -0
- package/dist/runtime/types/store.d.ts +313 -0
- package/dist/runtime/types/store.mjs +0 -0
- package/dist/runtime/types/workflows.d.ts +98 -0
- package/dist/runtime/types/workflows.mjs +0 -0
- package/dist/runtime/workflows.d.ts +5 -0
- package/dist/runtime/workflows.mjs +2 -0
- package/dist/types/ai.d.ts +721 -0
- package/dist/types/cms.d.ts +710 -0
- package/dist/types.d.ts +430 -1292
- package/dist/utils/auth-schema.d.ts +3 -2
- package/dist/utils/bots.d.ts +20 -0
- package/dist/utils/comments-schema.d.ts +5 -4
- package/dist/utils/drizzle.d.ts +83 -3
- package/dist/utils/durable-objects.d.ts +10 -0
- package/dist/utils/email-inbound.d.ts +10 -0
- package/dist/utils/local-db.d.ts +1 -3
- package/dist/utils/mcp.d.ts +24 -0
- package/dist/utils/notifications-schema.d.ts +5 -4
- package/dist/utils/payments-schema.d.ts +4 -3
- package/dist/utils/store-schema.d.ts +14 -0
- package/dist/utils/table-names.d.ts +1 -0
- package/dist/utils/tenants-schema.d.ts +4 -3
- package/dist/utils/tools.d.ts +20 -0
- package/dist/utils/workflows.d.ts +8 -0
- package/package.json +34 -4
- package/dist/chunks/wrangler.mjs +0 -897
- package/dist/runtime/app/components/AuthRegister.vue +0 -306
- package/dist/runtime/app/components/AuthSessionsList.vue +0 -137
- package/dist/runtime/app/components/AuthSignIn.vue +0 -298
- package/dist/runtime/app/components/CMSBookmarkButton.vue +0 -59
- package/dist/runtime/app/components/CMSImage.vue +0 -47
- package/dist/runtime/app/components/CMSLocaleSwitcher.vue +0 -110
- package/dist/runtime/app/components/CMSNewsletter.vue +0 -81
- package/dist/runtime/app/components/CMSPreviewBanner.vue +0 -83
- package/dist/runtime/app/components/CMSTableOfContents.vue +0 -84
- package/dist/runtime/app/components/CMSVersionBanner.vue +0 -53
- package/dist/runtime/app/components/CMSVersionSwitcher.vue +0 -140
- package/dist/runtime/app/components/Comments.vue +0 -411
- package/dist/runtime/app/components/FileUpload.vue +0 -200
- package/dist/runtime/app/components/ImpersonationBanner.vue +0 -61
- package/dist/runtime/app/components/NotificationBell.vue +0 -263
- package/dist/runtime/app/components/NotificationList.vue +0 -205
- package/dist/runtime/app/components/NotificationPrompt.vue +0 -133
- package/dist/runtime/app/components/PricingGated.vue +0 -81
- package/dist/runtime/app/components/PricingTable.vue +0 -274
- package/dist/runtime/app/components/RealtimeChat.vue +0 -164
- package/dist/runtime/app/components/RealtimePresence.vue +0 -129
- package/dist/runtime/app/components/RealtimeStatus.vue +0 -79
- package/dist/runtime/app/components/SubscriptionCard.vue +0 -207
- package/dist/runtime/app/components/UserProfile.vue +0 -558
|
@@ -1,8 +1,26 @@
|
|
|
1
1
|
import { createError, useRuntimeConfig, getRequestHeader } from "nuxt/server";
|
|
2
2
|
import { getD1Binding } from "../../db.mjs";
|
|
3
|
+
import { getKVBinding } from "../../kv.mjs";
|
|
3
4
|
import { getQarpeoConfig } from "./config.mjs";
|
|
4
5
|
import { getAuthContext } from "./context.mjs";
|
|
5
6
|
import { nanoid } from "./crypto.mjs";
|
|
7
|
+
import { updateStoreOrderStatus } from "./store.mjs";
|
|
8
|
+
import { resolveTableName, resolveIndexName } from "./table-names.mjs";
|
|
9
|
+
function getTbl(base, event) {
|
|
10
|
+
return resolveTableName(base, event);
|
|
11
|
+
}
|
|
12
|
+
function customerTbl(event) {
|
|
13
|
+
return getTbl("customer", event);
|
|
14
|
+
}
|
|
15
|
+
function subscriptionTbl(event) {
|
|
16
|
+
return getTbl("subscription", event);
|
|
17
|
+
}
|
|
18
|
+
function webhookEventTbl(event) {
|
|
19
|
+
return getTbl("webhook_event", event);
|
|
20
|
+
}
|
|
21
|
+
function usageRecordTbl(event) {
|
|
22
|
+
return getTbl("usage_record", event);
|
|
23
|
+
}
|
|
6
24
|
const STRIPE_API_VERSION = "2024-06-20";
|
|
7
25
|
function getPaymentsDb(event) {
|
|
8
26
|
const bindingName = globalThis.process?.env?.QARPEO_DEFAULT_DB_BINDING || "DB";
|
|
@@ -46,8 +64,12 @@ export async function ensurePaymentsTables(event) {
|
|
|
46
64
|
if (!ensurePaymentsTablesPromise) {
|
|
47
65
|
ensurePaymentsTablesPromise = (async () => {
|
|
48
66
|
const db = getPaymentsDb(event);
|
|
67
|
+
const ct = customerTbl(event);
|
|
68
|
+
const st = subscriptionTbl(event);
|
|
69
|
+
const wt = webhookEventTbl(event);
|
|
70
|
+
const ut = usageRecordTbl(event);
|
|
49
71
|
const statements = [
|
|
50
|
-
`CREATE TABLE IF NOT EXISTS
|
|
72
|
+
`CREATE TABLE IF NOT EXISTS ${ct} (
|
|
51
73
|
id TEXT PRIMARY KEY,
|
|
52
74
|
user_id TEXT NOT NULL,
|
|
53
75
|
org_id TEXT,
|
|
@@ -59,7 +81,7 @@ export async function ensurePaymentsTables(event) {
|
|
|
59
81
|
created_at INTEGER NOT NULL,
|
|
60
82
|
updated_at INTEGER NOT NULL
|
|
61
83
|
);`,
|
|
62
|
-
`CREATE TABLE IF NOT EXISTS
|
|
84
|
+
`CREATE TABLE IF NOT EXISTS ${st} (
|
|
63
85
|
id TEXT PRIMARY KEY,
|
|
64
86
|
customer_id TEXT NOT NULL,
|
|
65
87
|
user_id TEXT NOT NULL,
|
|
@@ -79,22 +101,22 @@ export async function ensurePaymentsTables(event) {
|
|
|
79
101
|
created_at INTEGER NOT NULL,
|
|
80
102
|
updated_at INTEGER NOT NULL
|
|
81
103
|
);`,
|
|
82
|
-
`CREATE TABLE IF NOT EXISTS
|
|
104
|
+
`CREATE TABLE IF NOT EXISTS ${wt} (
|
|
83
105
|
event_id TEXT PRIMARY KEY,
|
|
84
106
|
provider TEXT NOT NULL,
|
|
85
107
|
event_type TEXT,
|
|
86
108
|
processed_at INTEGER NOT NULL
|
|
87
109
|
);`,
|
|
88
|
-
`CREATE INDEX IF NOT EXISTS
|
|
89
|
-
`CREATE UNIQUE INDEX IF NOT EXISTS
|
|
90
|
-
`CREATE INDEX IF NOT EXISTS
|
|
91
|
-
`CREATE INDEX IF NOT EXISTS
|
|
92
|
-
`CREATE INDEX IF NOT EXISTS
|
|
93
|
-
`CREATE INDEX IF NOT EXISTS
|
|
94
|
-
`CREATE INDEX IF NOT EXISTS
|
|
95
|
-
`CREATE INDEX IF NOT EXISTS
|
|
96
|
-
`CREATE INDEX IF NOT EXISTS
|
|
97
|
-
`CREATE TABLE IF NOT EXISTS
|
|
110
|
+
`CREATE INDEX IF NOT EXISTS ${resolveIndexName(ct, "user_idx")} ON ${ct}(user_id);`,
|
|
111
|
+
`CREATE UNIQUE INDEX IF NOT EXISTS ${resolveIndexName(ct, "customer_idx")} ON ${ct}(customer_id);`,
|
|
112
|
+
`CREATE INDEX IF NOT EXISTS ${resolveIndexName(ct, "org_idx")} ON ${ct}(org_id);`,
|
|
113
|
+
`CREATE INDEX IF NOT EXISTS ${resolveIndexName(st, "user_idx")} ON ${st}(user_id);`,
|
|
114
|
+
`CREATE INDEX IF NOT EXISTS ${resolveIndexName(st, "subscription_idx")} ON ${st}(subscription_id);`,
|
|
115
|
+
`CREATE INDEX IF NOT EXISTS ${resolveIndexName(st, "customer_idx")} ON ${st}(customer_id);`,
|
|
116
|
+
`CREATE INDEX IF NOT EXISTS ${resolveIndexName(st, "status_idx")} ON ${st}(status);`,
|
|
117
|
+
`CREATE INDEX IF NOT EXISTS ${resolveIndexName(st, "org_idx")} ON ${st}(org_id);`,
|
|
118
|
+
`CREATE INDEX IF NOT EXISTS ${resolveIndexName(wt, "event_idx")} ON ${wt}(event_id);`,
|
|
119
|
+
`CREATE TABLE IF NOT EXISTS ${ut} (
|
|
98
120
|
id TEXT PRIMARY KEY,
|
|
99
121
|
user_id TEXT,
|
|
100
122
|
org_id TEXT,
|
|
@@ -107,10 +129,10 @@ export async function ensurePaymentsTables(event) {
|
|
|
107
129
|
timestamp INTEGER NOT NULL,
|
|
108
130
|
created_at INTEGER NOT NULL
|
|
109
131
|
);`,
|
|
110
|
-
`CREATE INDEX IF NOT EXISTS
|
|
111
|
-
`CREATE INDEX IF NOT EXISTS
|
|
112
|
-
`CREATE INDEX IF NOT EXISTS
|
|
113
|
-
`CREATE INDEX IF NOT EXISTS
|
|
132
|
+
`CREATE INDEX IF NOT EXISTS ${resolveIndexName(ut, "user_idx")} ON ${ut}(user_id);`,
|
|
133
|
+
`CREATE INDEX IF NOT EXISTS ${resolveIndexName(ut, "sub_idx")} ON ${ut}(subscription_id);`,
|
|
134
|
+
`CREATE INDEX IF NOT EXISTS ${resolveIndexName(ut, "event_idx")} ON ${ut}(event_name);`,
|
|
135
|
+
`CREATE INDEX IF NOT EXISTS ${resolveIndexName(ut, "timestamp_idx")} ON ${ut}(timestamp);`
|
|
114
136
|
];
|
|
115
137
|
for (const rawSql of statements) {
|
|
116
138
|
const sql = rawSql.replace(/\s+/g, " ").trim();
|
|
@@ -140,6 +162,19 @@ async function computeHmacSha256(keyStr, dataStr) {
|
|
|
140
162
|
const sig = await crypto.subtle.sign("HMAC", key, encoder.encode(dataStr));
|
|
141
163
|
return Array.from(new Uint8Array(sig)).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
142
164
|
}
|
|
165
|
+
async function computeHmacSha256Base64(keyStr, dataStr) {
|
|
166
|
+
const encoder = new TextEncoder();
|
|
167
|
+
const key = await crypto.subtle.importKey(
|
|
168
|
+
"raw",
|
|
169
|
+
encoder.encode(keyStr),
|
|
170
|
+
{ name: "HMAC", hash: "SHA-256" },
|
|
171
|
+
false,
|
|
172
|
+
["sign"]
|
|
173
|
+
);
|
|
174
|
+
const sig = await crypto.subtle.sign("HMAC", key, encoder.encode(dataStr));
|
|
175
|
+
const binary = String.fromCharCode(...new Uint8Array(sig));
|
|
176
|
+
return btoa(binary);
|
|
177
|
+
}
|
|
143
178
|
export async function verifyStripeSignature(rawBody, signatureHeader, secret, toleranceSec = 300) {
|
|
144
179
|
if (!signatureHeader || !secret) return false;
|
|
145
180
|
const parts = signatureHeader.split(",");
|
|
@@ -165,12 +200,32 @@ export async function verifyLemonSqueezySignature(rawBody, signatureHeader, secr
|
|
|
165
200
|
const expected = await computeHmacSha256(secret, rawBody);
|
|
166
201
|
return timingSafeEqualString(signatureHeader, expected);
|
|
167
202
|
}
|
|
168
|
-
export async function verifyPolarSignature(rawBody, signatureHeader, secret) {
|
|
203
|
+
export async function verifyPolarSignature(rawBody, signatureHeader, secret, options) {
|
|
169
204
|
if (!signatureHeader || !secret) return false;
|
|
205
|
+
const toleranceSec = options?.toleranceSec ?? 300;
|
|
206
|
+
const ts = options?.timestamp;
|
|
207
|
+
if (ts) {
|
|
208
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
209
|
+
const tsNum = parseInt(ts, 10);
|
|
210
|
+
if (isNaN(tsNum) || Math.abs(now - tsNum) > toleranceSec) {
|
|
211
|
+
return false;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
if (options?.webhookId && ts) {
|
|
215
|
+
const payload = `${options.webhookId}.${ts}.${rawBody}`;
|
|
216
|
+
const rawSecret = secret.startsWith("whsec_") ? secret.slice(6) : secret;
|
|
217
|
+
const expectedHex = await computeHmacSha256(secret, payload);
|
|
218
|
+
const expectedBase64 = await computeHmacSha256Base64(rawSecret, payload);
|
|
219
|
+
const signatures = signatureHeader.split(" ");
|
|
220
|
+
return signatures.some((sig) => {
|
|
221
|
+
const cleanSig = sig.replace(/^v1,/, "");
|
|
222
|
+
return timingSafeEqualString(cleanSig, expectedHex) || timingSafeEqualString(cleanSig, expectedBase64);
|
|
223
|
+
});
|
|
224
|
+
}
|
|
170
225
|
const expected = await computeHmacSha256(secret, rawBody);
|
|
171
226
|
return timingSafeEqualString(signatureHeader, expected);
|
|
172
227
|
}
|
|
173
|
-
export async function verifyPaddleSignature(rawBody, signatureHeader, secret) {
|
|
228
|
+
export async function verifyPaddleSignature(rawBody, signatureHeader, secret, toleranceSec = 300) {
|
|
174
229
|
if (!signatureHeader || !secret) return false;
|
|
175
230
|
const parts = {};
|
|
176
231
|
for (const item of signatureHeader.split(";")) {
|
|
@@ -182,6 +237,11 @@ export async function verifyPaddleSignature(rawBody, signatureHeader, secret) {
|
|
|
182
237
|
const ts = parts.ts;
|
|
183
238
|
const h1 = parts.h1;
|
|
184
239
|
if (!ts || !h1) return false;
|
|
240
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
241
|
+
const tsNum = parseInt(ts, 10);
|
|
242
|
+
if (isNaN(tsNum) || Math.abs(now - tsNum) > toleranceSec) {
|
|
243
|
+
return false;
|
|
244
|
+
}
|
|
185
245
|
const payload = `${ts}:${rawBody}`;
|
|
186
246
|
const expected = await computeHmacSha256(secret, payload);
|
|
187
247
|
return timingSafeEqualString(h1, expected);
|
|
@@ -191,6 +251,9 @@ export async function processPaymentWebhook(event, rawBody) {
|
|
|
191
251
|
const db = getPaymentsDb(event);
|
|
192
252
|
const config = getPaymentsConfig(event);
|
|
193
253
|
const provider = config.provider;
|
|
254
|
+
const ct = customerTbl(event);
|
|
255
|
+
const st = subscriptionTbl(event);
|
|
256
|
+
const wt = webhookEventTbl(event);
|
|
194
257
|
let isValid = false;
|
|
195
258
|
if (provider === "stripe") {
|
|
196
259
|
const sig = getRequestHeader(event, "stripe-signature");
|
|
@@ -200,7 +263,15 @@ export async function processPaymentWebhook(event, rawBody) {
|
|
|
200
263
|
isValid = await verifyLemonSqueezySignature(rawBody, sig, config.webhookSecret);
|
|
201
264
|
} else if (provider === "polar") {
|
|
202
265
|
const sig = getRequestHeader(event, "webhook-signature");
|
|
203
|
-
|
|
266
|
+
const ts = getRequestHeader(event, "webhook-timestamp");
|
|
267
|
+
const webhookId = getRequestHeader(event, "webhook-id");
|
|
268
|
+
isValid = await verifyPolarSignature(rawBody, sig, config.webhookSecret, {
|
|
269
|
+
timestamp: ts,
|
|
270
|
+
webhookId
|
|
271
|
+
});
|
|
272
|
+
} else if (provider === "paddle") {
|
|
273
|
+
const sig = getRequestHeader(event, "paddle-signature");
|
|
274
|
+
isValid = await verifyPaddleSignature(rawBody, sig, config.webhookSecret);
|
|
204
275
|
}
|
|
205
276
|
if (!isValid) {
|
|
206
277
|
console.warn(`[qarpeo:payments] Invalid webhook signature from provider: ${provider}`);
|
|
@@ -227,100 +298,245 @@ export async function processPaymentWebhook(event, rawBody) {
|
|
|
227
298
|
eventId = String(payload.meta?.event_id || payload.meta?.id || payload.data?.id || "");
|
|
228
299
|
} else if (provider === "polar") {
|
|
229
300
|
eventId = payload.id || "";
|
|
301
|
+
} else if (provider === "paddle") {
|
|
302
|
+
eventId = payload.event_id || payload.id || "";
|
|
230
303
|
}
|
|
231
304
|
if (provider === "lemonsqueezy") {
|
|
232
305
|
eventType = payload.meta?.event_name || "";
|
|
306
|
+
} else if (provider === "paddle") {
|
|
307
|
+
eventType = payload.event_type || payload.type || "";
|
|
233
308
|
} else {
|
|
234
309
|
eventType = payload.type || "";
|
|
235
310
|
}
|
|
311
|
+
let kv = null;
|
|
312
|
+
try {
|
|
313
|
+
kv = getKVBinding("KV", event);
|
|
314
|
+
} catch {
|
|
315
|
+
}
|
|
316
|
+
const kvKey = `payments:idempotency:${provider}:${eventId}`;
|
|
317
|
+
if (kv && eventId) {
|
|
318
|
+
try {
|
|
319
|
+
const cached = await kv.get(kvKey);
|
|
320
|
+
if (cached === "completed") {
|
|
321
|
+
return { received: true, skipped: true, reason: "duplicate" };
|
|
322
|
+
}
|
|
323
|
+
} catch {
|
|
324
|
+
}
|
|
325
|
+
}
|
|
236
326
|
if (eventId) {
|
|
237
327
|
const dedupRes = await db.prepare(
|
|
238
|
-
|
|
328
|
+
`INSERT OR IGNORE INTO ${wt} (event_id, provider, event_type, processed_at) VALUES (?, ?, ?, ?)`
|
|
239
329
|
).bind(eventId, provider, eventType || null, now).run();
|
|
240
330
|
if (!dedupRes?.meta?.changes) {
|
|
331
|
+
if (kv) {
|
|
332
|
+
try {
|
|
333
|
+
await kv.put(kvKey, "completed", { expirationTtl: 604800 });
|
|
334
|
+
} catch {
|
|
335
|
+
}
|
|
336
|
+
}
|
|
241
337
|
return { received: true, skipped: true, reason: "duplicate" };
|
|
242
338
|
}
|
|
243
339
|
}
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
340
|
+
try {
|
|
341
|
+
if (provider === "stripe") {
|
|
342
|
+
eventType = payload.type;
|
|
343
|
+
const obj = payload.data?.object || {};
|
|
344
|
+
eventData = obj;
|
|
345
|
+
switch (eventType) {
|
|
346
|
+
case "checkout.session.completed": {
|
|
347
|
+
const customerId = obj.customer;
|
|
348
|
+
const customerEmail = obj.customer_details?.email || obj.customer_email || "";
|
|
349
|
+
const customerName = obj.customer_details?.name || null;
|
|
350
|
+
const userId = obj.metadata?.userId || obj.client_reference_id;
|
|
351
|
+
const orgId = obj.metadata?.orgId || null;
|
|
352
|
+
if (userId && customerId) {
|
|
353
|
+
await db.prepare(
|
|
354
|
+
`INSERT INTO ${ct} (id, user_id, org_id, provider, customer_id, email, name, metadata_json, created_at, updated_at)
|
|
258
355
|
VALUES (?, ?, ?, 'stripe', ?, ?, ?, ?, ?, ?)
|
|
259
356
|
ON CONFLICT(customer_id) DO UPDATE SET email = excluded.email, name = excluded.name, org_id = excluded.org_id, metadata_json = excluded.metadata_json, updated_at = excluded.updated_at`
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
357
|
+
).bind(
|
|
358
|
+
nanoid(),
|
|
359
|
+
userId,
|
|
360
|
+
orgId,
|
|
361
|
+
customerId,
|
|
362
|
+
customerEmail,
|
|
363
|
+
customerName,
|
|
364
|
+
JSON.stringify(obj.metadata || {}),
|
|
365
|
+
now,
|
|
366
|
+
now
|
|
367
|
+
).run();
|
|
368
|
+
}
|
|
369
|
+
const subId = obj.subscription;
|
|
370
|
+
if (subId) {
|
|
371
|
+
affectedSubId = subId;
|
|
372
|
+
const planId = obj.metadata?.planId || `default`;
|
|
373
|
+
await db.prepare(
|
|
374
|
+
`INSERT INTO ${st}
|
|
278
375
|
(id, customer_id, user_id, org_id, provider, subscription_id, plan_id, price_id, status, quantity, cancel_at_period_end, current_period_start, current_period_end, trial_end, canceled_at, metadata_json, created_at, updated_at)
|
|
279
376
|
VALUES (?, ?, ?, ?, 'stripe', ?, ?, '', 'active', 1, 0, ?, ?, null, null, ?, ?, ?)
|
|
280
377
|
ON CONFLICT(subscription_id) DO NOTHING`
|
|
378
|
+
).bind(
|
|
379
|
+
nanoid(),
|
|
380
|
+
customerId || "",
|
|
381
|
+
userId || "",
|
|
382
|
+
orgId,
|
|
383
|
+
subId,
|
|
384
|
+
planId,
|
|
385
|
+
now,
|
|
386
|
+
now + 30 * 864e5,
|
|
387
|
+
JSON.stringify(obj.metadata || {}),
|
|
388
|
+
now,
|
|
389
|
+
now
|
|
390
|
+
).run();
|
|
391
|
+
}
|
|
392
|
+
if (obj.metadata?.type === "store_order" || obj.metadata?.orderId) {
|
|
393
|
+
const orderId = obj.metadata.orderId;
|
|
394
|
+
if (orderId) {
|
|
395
|
+
const paymentIntentId = obj.payment_intent || obj.id;
|
|
396
|
+
await updateStoreOrderStatus(
|
|
397
|
+
orderId,
|
|
398
|
+
"paid",
|
|
399
|
+
{ paymentProvider: "stripe", paymentIntentId, event }
|
|
400
|
+
).catch((err) => {
|
|
401
|
+
console.warn("[qarpeo:store] Webhook failed to update order to paid:", err?.message || err);
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
break;
|
|
406
|
+
}
|
|
407
|
+
case "payment_intent.succeeded": {
|
|
408
|
+
const orderId = obj.metadata?.orderId;
|
|
409
|
+
if (orderId && (obj.metadata?.type === "store_order" || obj.metadata?.type === "store")) {
|
|
410
|
+
await updateStoreOrderStatus(
|
|
411
|
+
orderId,
|
|
412
|
+
"paid",
|
|
413
|
+
{ paymentProvider: "stripe", paymentIntentId: obj.id, event }
|
|
414
|
+
).catch((err) => {
|
|
415
|
+
console.warn("[qarpeo:store] payment_intent.succeeded failed to update order:", err?.message || err);
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
break;
|
|
419
|
+
}
|
|
420
|
+
case "payment_intent.payment_failed": {
|
|
421
|
+
const orderId = obj.metadata?.orderId;
|
|
422
|
+
if (orderId && (obj.metadata?.type === "store_order" || obj.metadata?.type === "store")) {
|
|
423
|
+
await updateStoreOrderStatus(orderId, "cancelled", { event }).catch(() => {
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
break;
|
|
427
|
+
}
|
|
428
|
+
case "charge.refunded": {
|
|
429
|
+
const orderId = obj.metadata?.orderId;
|
|
430
|
+
if (orderId) {
|
|
431
|
+
await updateStoreOrderStatus(orderId, "refunded", { event }).catch(() => {
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
break;
|
|
435
|
+
}
|
|
436
|
+
case "customer.subscription.created":
|
|
437
|
+
case "customer.subscription.updated": {
|
|
438
|
+
const subId = obj.id;
|
|
439
|
+
affectedSubId = subId;
|
|
440
|
+
const customerId = obj.customer;
|
|
441
|
+
const status = obj.status;
|
|
442
|
+
const priceId = obj.items?.data?.[0]?.price?.id || obj.plan?.id || "";
|
|
443
|
+
const planId = obj.metadata?.planId || obj.items?.data?.[0]?.price?.lookup_key || priceId;
|
|
444
|
+
const quantity = obj.items?.data?.[0]?.quantity || obj.quantity || 1;
|
|
445
|
+
const cancelAtPeriodEnd = obj.cancel_at_period_end ? 1 : 0;
|
|
446
|
+
const periodStart = obj.current_period_start ? obj.current_period_start * 1e3 : now;
|
|
447
|
+
const periodEnd = obj.current_period_end ? obj.current_period_end * 1e3 : now + 30 * 864e5;
|
|
448
|
+
const trialEnd = obj.trial_end ? obj.trial_end * 1e3 : null;
|
|
449
|
+
const canceledAt = obj.canceled_at ? obj.canceled_at * 1e3 : null;
|
|
450
|
+
const cust = await db.prepare(`SELECT user_id, org_id FROM ${ct} WHERE customer_id = ? LIMIT 1`).bind(customerId).first();
|
|
451
|
+
const userId = cust?.user_id || obj.metadata?.userId || "";
|
|
452
|
+
const orgId = cust?.org_id || obj.metadata?.orgId || null;
|
|
453
|
+
await db.prepare(
|
|
454
|
+
`INSERT INTO ${st}
|
|
455
|
+
(id, customer_id, user_id, org_id, provider, subscription_id, plan_id, price_id, status, quantity, cancel_at_period_end, current_period_start, current_period_end, trial_end, canceled_at, metadata_json, created_at, updated_at)
|
|
456
|
+
VALUES (?, ?, ?, ?, 'stripe', ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
457
|
+
ON CONFLICT(subscription_id) DO UPDATE SET
|
|
458
|
+
plan_id = excluded.plan_id, price_id = excluded.price_id, status = excluded.status,
|
|
459
|
+
quantity = excluded.quantity, cancel_at_period_end = excluded.cancel_at_period_end,
|
|
460
|
+
current_period_start = excluded.current_period_start, current_period_end = excluded.current_period_end,
|
|
461
|
+
trial_end = excluded.trial_end, canceled_at = excluded.canceled_at,
|
|
462
|
+
metadata_json = excluded.metadata_json, updated_at = excluded.updated_at`
|
|
281
463
|
).bind(
|
|
282
464
|
nanoid(),
|
|
283
|
-
customerId
|
|
284
|
-
userId
|
|
465
|
+
customerId,
|
|
466
|
+
userId,
|
|
285
467
|
orgId,
|
|
286
468
|
subId,
|
|
287
469
|
planId,
|
|
288
|
-
|
|
289
|
-
|
|
470
|
+
priceId,
|
|
471
|
+
status,
|
|
472
|
+
quantity,
|
|
473
|
+
cancelAtPeriodEnd,
|
|
474
|
+
periodStart,
|
|
475
|
+
periodEnd,
|
|
476
|
+
trialEnd,
|
|
477
|
+
canceledAt,
|
|
290
478
|
JSON.stringify(obj.metadata || {}),
|
|
291
479
|
now,
|
|
292
480
|
now
|
|
293
481
|
).run();
|
|
482
|
+
break;
|
|
483
|
+
}
|
|
484
|
+
case "customer.subscription.deleted": {
|
|
485
|
+
const subId = obj.id;
|
|
486
|
+
affectedSubId = subId;
|
|
487
|
+
await db.prepare(
|
|
488
|
+
`UPDATE ${st} SET status = 'canceled', canceled_at = ?, updated_at = ? WHERE subscription_id = ?`
|
|
489
|
+
).bind(now, now, subId).run();
|
|
490
|
+
break;
|
|
491
|
+
}
|
|
492
|
+
case "invoice.payment_succeeded": {
|
|
493
|
+
const subId = obj.subscription;
|
|
494
|
+
if (subId) {
|
|
495
|
+
affectedSubId = subId;
|
|
496
|
+
await db.prepare(
|
|
497
|
+
`UPDATE ${st} SET status = 'active', updated_at = ? WHERE subscription_id = ?`
|
|
498
|
+
).bind(now, subId).run();
|
|
499
|
+
}
|
|
500
|
+
break;
|
|
501
|
+
}
|
|
502
|
+
case "invoice.payment_failed": {
|
|
503
|
+
const subId = obj.subscription;
|
|
504
|
+
if (subId) {
|
|
505
|
+
affectedSubId = subId;
|
|
506
|
+
await db.prepare(
|
|
507
|
+
`UPDATE ${st} SET status = 'past_due', updated_at = ? WHERE subscription_id = ?`
|
|
508
|
+
).bind(now, subId).run();
|
|
509
|
+
}
|
|
510
|
+
break;
|
|
294
511
|
}
|
|
295
|
-
break;
|
|
296
512
|
}
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
const
|
|
312
|
-
const
|
|
313
|
-
const
|
|
513
|
+
}
|
|
514
|
+
if (provider === "lemonsqueezy") {
|
|
515
|
+
eventType = payload.meta?.event_name;
|
|
516
|
+
const data = payload.data?.attributes || {};
|
|
517
|
+
eventData = data;
|
|
518
|
+
const subId = String(payload.data?.id || "");
|
|
519
|
+
affectedSubId = subId;
|
|
520
|
+
const customerId = String(data.customer_id || "");
|
|
521
|
+
const userEmail = data.user_email || "";
|
|
522
|
+
const customData = payload.meta?.custom_data || {};
|
|
523
|
+
const userId = customData.user_id || customData.userId || "";
|
|
524
|
+
const orgId = customData.org_id || customData.orgId || null;
|
|
525
|
+
const planId = String(data.variant_id || "");
|
|
526
|
+
if (eventType === "subscription_created" || eventType === "subscription_updated" || eventType === "subscription_resumed") {
|
|
527
|
+
const status = data.status === "active" ? "active" : data.status === "on_trial" ? "trialing" : data.status === "past_due" ? "past_due" : "canceled";
|
|
528
|
+
const periodStart = data.renews_at ? new Date(data.created_at).getTime() : now;
|
|
529
|
+
const periodEnd = data.renews_at ? new Date(data.renews_at).getTime() : now + 30 * 864e5;
|
|
530
|
+
const cancelAtPeriodEnd = data.cancelled ? 1 : 0;
|
|
531
|
+
if (customerId && userId) {
|
|
532
|
+
await db.prepare(
|
|
533
|
+
`INSERT INTO ${ct} (id, user_id, org_id, provider, customer_id, email, name, metadata_json, created_at, updated_at) VALUES (?, ?, ?, 'lemonsqueezy', ?, ?, ?, '{}', ?, ?) ON CONFLICT(customer_id) DO UPDATE SET email = excluded.email, name = excluded.name, updated_at = excluded.updated_at`
|
|
534
|
+
).bind(nanoid(), userId, orgId, customerId, userEmail, data.user_name || null, now, now).run();
|
|
535
|
+
}
|
|
314
536
|
await db.prepare(
|
|
315
|
-
`INSERT INTO
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
ON CONFLICT(subscription_id) DO UPDATE SET
|
|
319
|
-
plan_id = excluded.plan_id, price_id = excluded.price_id, status = excluded.status,
|
|
320
|
-
quantity = excluded.quantity, cancel_at_period_end = excluded.cancel_at_period_end,
|
|
321
|
-
current_period_start = excluded.current_period_start, current_period_end = excluded.current_period_end,
|
|
322
|
-
trial_end = excluded.trial_end, canceled_at = excluded.canceled_at,
|
|
323
|
-
metadata_json = excluded.metadata_json, updated_at = excluded.updated_at`
|
|
537
|
+
`INSERT INTO ${st} (id, customer_id, user_id, org_id, provider, subscription_id, plan_id, price_id, status, quantity, cancel_at_period_end, current_period_start, current_period_end, trial_end, canceled_at, metadata_json, created_at, updated_at)
|
|
538
|
+
VALUES (?, ?, ?, ?, 'lemonsqueezy', ?, ?, ?, ?, 1, ?, ?, ?, null, null, '{}', ?, ?)
|
|
539
|
+
ON CONFLICT(subscription_id) DO UPDATE SET status = excluded.status, cancel_at_period_end = excluded.cancel_at_period_end, current_period_start = excluded.current_period_start, current_period_end = excluded.current_period_end, updated_at = excluded.updated_at`
|
|
324
540
|
).bind(
|
|
325
541
|
nanoid(),
|
|
326
542
|
customerId,
|
|
@@ -328,172 +544,116 @@ export async function processPaymentWebhook(event, rawBody) {
|
|
|
328
544
|
orgId,
|
|
329
545
|
subId,
|
|
330
546
|
planId,
|
|
331
|
-
|
|
547
|
+
planId,
|
|
332
548
|
status,
|
|
333
|
-
quantity,
|
|
334
549
|
cancelAtPeriodEnd,
|
|
335
550
|
periodStart,
|
|
336
551
|
periodEnd,
|
|
337
|
-
trialEnd,
|
|
338
|
-
canceledAt,
|
|
339
|
-
JSON.stringify(obj.metadata || {}),
|
|
340
552
|
now,
|
|
341
553
|
now
|
|
342
554
|
).run();
|
|
343
|
-
|
|
344
|
-
}
|
|
345
|
-
case "customer.subscription.deleted": {
|
|
346
|
-
const subId = obj.id;
|
|
347
|
-
affectedSubId = subId;
|
|
555
|
+
} else if (eventType === "subscription_cancelled" || eventType === "subscription_expired") {
|
|
348
556
|
await db.prepare(
|
|
349
|
-
|
|
557
|
+
`UPDATE ${st} SET status = 'canceled', canceled_at = ?, updated_at = ? WHERE subscription_id = ?`
|
|
350
558
|
).bind(now, now, subId).run();
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
)
|
|
360
|
-
}
|
|
361
|
-
break;
|
|
362
|
-
}
|
|
363
|
-
case "invoice.payment_failed": {
|
|
364
|
-
const subId = obj.subscription;
|
|
365
|
-
if (subId) {
|
|
366
|
-
affectedSubId = subId;
|
|
367
|
-
await db.prepare(
|
|
368
|
-
"UPDATE _qarpeo_subscriptions SET status = 'past_due', updated_at = ? WHERE subscription_id = ?"
|
|
369
|
-
).bind(now, subId).run();
|
|
559
|
+
} else if (eventType === "order_created") {
|
|
560
|
+
const orderId = customData.orderId || customData.order_id;
|
|
561
|
+
if (orderId && (customData.type === "store_order" || customData.type === "store")) {
|
|
562
|
+
await updateStoreOrderStatus(
|
|
563
|
+
orderId,
|
|
564
|
+
"paid",
|
|
565
|
+
{ paymentProvider: "lemonsqueezy", paymentIntentId: String(data.id), event }
|
|
566
|
+
).catch(() => {
|
|
567
|
+
});
|
|
370
568
|
}
|
|
371
|
-
break;
|
|
372
569
|
}
|
|
373
570
|
}
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
const status = data.status === "active" ? "active" : data.status === "on_trial" ? "trialing" : data.status === "past_due" ? "past_due" : "canceled";
|
|
389
|
-
const periodStart = data.renews_at ? new Date(data.created_at).getTime() : now;
|
|
390
|
-
const periodEnd = data.renews_at ? new Date(data.renews_at).getTime() : now + 30 * 864e5;
|
|
391
|
-
const cancelAtPeriodEnd = data.cancelled ? 1 : 0;
|
|
392
|
-
if (customerId && userId) {
|
|
571
|
+
if (provider === "polar") {
|
|
572
|
+
eventType = payload.type;
|
|
573
|
+
const data = payload.data || {};
|
|
574
|
+
eventData = data;
|
|
575
|
+
const subId = data.id;
|
|
576
|
+
affectedSubId = subId;
|
|
577
|
+
const customerId = data.customer_id || data.user_id || "";
|
|
578
|
+
const userId = data.metadata?.userId || data.user?.id || "";
|
|
579
|
+
const orgId = data.metadata?.orgId || null;
|
|
580
|
+
const priceId = data.price_id || data.product_id || "";
|
|
581
|
+
if (eventType === "subscription.created" || eventType === "subscription.updated" || eventType === "subscription.active") {
|
|
582
|
+
const status = data.status === "active" ? "active" : data.status === "canceled" ? "canceled" : "past_due";
|
|
583
|
+
const periodStart = data.current_period_start ? new Date(data.current_period_start).getTime() : now;
|
|
584
|
+
const periodEnd = data.current_period_end ? new Date(data.current_period_end).getTime() : now + 30 * 864e5;
|
|
393
585
|
await db.prepare(
|
|
394
|
-
|
|
395
|
-
).bind(nanoid(), userId, orgId, customerId, userEmail, data.user_name || null, now, now).run();
|
|
396
|
-
}
|
|
397
|
-
await db.prepare(
|
|
398
|
-
`INSERT INTO _qarpeo_subscriptions (id, customer_id, user_id, org_id, provider, subscription_id, plan_id, price_id, status, quantity, cancel_at_period_end, current_period_start, current_period_end, trial_end, canceled_at, metadata_json, created_at, updated_at)
|
|
399
|
-
VALUES (?, ?, ?, ?, 'lemonsqueezy', ?, ?, ?, ?, 1, ?, ?, ?, null, null, '{}', ?, ?)
|
|
400
|
-
ON CONFLICT(subscription_id) DO UPDATE SET status = excluded.status, cancel_at_period_end = excluded.cancel_at_period_end, current_period_start = excluded.current_period_start, current_period_end = excluded.current_period_end, updated_at = excluded.updated_at`
|
|
401
|
-
).bind(
|
|
402
|
-
nanoid(),
|
|
403
|
-
customerId,
|
|
404
|
-
userId,
|
|
405
|
-
orgId,
|
|
406
|
-
subId,
|
|
407
|
-
planId,
|
|
408
|
-
planId,
|
|
409
|
-
status,
|
|
410
|
-
cancelAtPeriodEnd,
|
|
411
|
-
periodStart,
|
|
412
|
-
periodEnd,
|
|
413
|
-
now,
|
|
414
|
-
now
|
|
415
|
-
).run();
|
|
416
|
-
} else if (eventType === "subscription_cancelled" || eventType === "subscription_expired") {
|
|
417
|
-
await db.prepare(
|
|
418
|
-
"UPDATE _qarpeo_subscriptions SET status = 'canceled', canceled_at = ?, updated_at = ? WHERE subscription_id = ?"
|
|
419
|
-
).bind(now, now, subId).run();
|
|
420
|
-
}
|
|
421
|
-
}
|
|
422
|
-
if (provider === "polar") {
|
|
423
|
-
eventType = payload.type;
|
|
424
|
-
const data = payload.data || {};
|
|
425
|
-
eventData = data;
|
|
426
|
-
const subId = data.id;
|
|
427
|
-
affectedSubId = subId;
|
|
428
|
-
const customerId = data.customer_id || data.user_id || "";
|
|
429
|
-
const userId = data.metadata?.userId || data.user?.id || "";
|
|
430
|
-
const orgId = data.metadata?.orgId || null;
|
|
431
|
-
const priceId = data.price_id || data.product_id || "";
|
|
432
|
-
if (eventType === "subscription.created" || eventType === "subscription.updated" || eventType === "subscription.active") {
|
|
433
|
-
const status = data.status === "active" ? "active" : data.status === "canceled" ? "canceled" : "past_due";
|
|
434
|
-
const periodStart = data.current_period_start ? new Date(data.current_period_start).getTime() : now;
|
|
435
|
-
const periodEnd = data.current_period_end ? new Date(data.current_period_end).getTime() : now + 30 * 864e5;
|
|
436
|
-
await db.prepare(
|
|
437
|
-
`INSERT INTO _qarpeo_subscriptions (id, customer_id, user_id, org_id, provider, subscription_id, plan_id, price_id, status, quantity, cancel_at_period_end, current_period_start, current_period_end, trial_end, canceled_at, metadata_json, created_at, updated_at)
|
|
586
|
+
`INSERT INTO ${st} (id, customer_id, user_id, org_id, provider, subscription_id, plan_id, price_id, status, quantity, cancel_at_period_end, current_period_start, current_period_end, trial_end, canceled_at, metadata_json, created_at, updated_at)
|
|
438
587
|
VALUES (?, ?, ?, ?, 'polar', ?, ?, ?, ?, 1, 0, ?, ?, null, null, '{}', ?, ?)
|
|
439
588
|
ON CONFLICT(subscription_id) DO UPDATE SET status = excluded.status, current_period_start = excluded.current_period_start, current_period_end = excluded.current_period_end, updated_at = excluded.updated_at`
|
|
440
|
-
).bind(
|
|
441
|
-
nanoid(),
|
|
442
|
-
customerId,
|
|
443
|
-
userId,
|
|
444
|
-
orgId,
|
|
445
|
-
subId,
|
|
446
|
-
priceId,
|
|
447
|
-
priceId,
|
|
448
|
-
status,
|
|
449
|
-
periodStart,
|
|
450
|
-
periodEnd,
|
|
451
|
-
now,
|
|
452
|
-
now
|
|
453
|
-
).run();
|
|
454
|
-
} else if (eventType === "subscription.canceled" || eventType === "subscription.revoked") {
|
|
455
|
-
await db.prepare(
|
|
456
|
-
"UPDATE _qarpeo_subscriptions SET status = 'canceled', canceled_at = ?, updated_at = ? WHERE subscription_id = ?"
|
|
457
|
-
).bind(now, now, subId).run();
|
|
458
|
-
}
|
|
459
|
-
}
|
|
460
|
-
if (provider === "paddle") {
|
|
461
|
-
eventType = payload.event_type || payload.type || "";
|
|
462
|
-
const data = payload.data || {};
|
|
463
|
-
eventData = data;
|
|
464
|
-
const subId = String(data.id || "");
|
|
465
|
-
affectedSubId = subId;
|
|
466
|
-
const customerId = String(data.customer_id || "");
|
|
467
|
-
const customData = data.custom_data || {};
|
|
468
|
-
const userId = customData.userId || customData.user_id || "";
|
|
469
|
-
const orgId = customData.orgId || customData.org_id || null;
|
|
470
|
-
const priceId = data.items?.[0]?.price?.id || "";
|
|
471
|
-
const planId = customData.planId || priceId;
|
|
472
|
-
if (eventType === "subscription.created" || eventType === "subscription.updated" || eventType === "subscription.activated" || eventType === "subscription.resumed") {
|
|
473
|
-
const status = data.status === "active" ? "active" : data.status === "trialing" ? "trialing" : data.status === "past_due" ? "past_due" : data.status === "paused" ? "paused" : "canceled";
|
|
474
|
-
const periodStart = data.current_billing_period?.starts_at ? new Date(data.current_billing_period.starts_at).getTime() : now;
|
|
475
|
-
const periodEnd = data.current_billing_period?.ends_at ? new Date(data.current_billing_period.ends_at).getTime() : now + 30 * 864e5;
|
|
476
|
-
const cancelAtPeriodEnd = data.scheduled_change?.action === "cancel" ? 1 : 0;
|
|
477
|
-
const quantity = data.items?.[0]?.quantity || 1;
|
|
478
|
-
if (customerId && userId) {
|
|
479
|
-
await db.prepare(
|
|
480
|
-
`INSERT INTO _qarpeo_customers (id, user_id, org_id, provider, customer_id, email, name, metadata_json, created_at, updated_at)
|
|
481
|
-
VALUES (?, ?, ?, 'paddle', ?, ?, ?, ?, ?, ?)
|
|
482
|
-
ON CONFLICT(customer_id) DO UPDATE SET org_id = excluded.org_id, metadata_json = excluded.metadata_json, updated_at = excluded.updated_at`
|
|
483
589
|
).bind(
|
|
484
590
|
nanoid(),
|
|
591
|
+
customerId,
|
|
485
592
|
userId,
|
|
486
593
|
orgId,
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
594
|
+
subId,
|
|
595
|
+
priceId,
|
|
596
|
+
priceId,
|
|
597
|
+
status,
|
|
598
|
+
periodStart,
|
|
599
|
+
periodEnd,
|
|
491
600
|
now,
|
|
492
601
|
now
|
|
493
602
|
).run();
|
|
603
|
+
} else if (eventType === "subscription.canceled" || eventType === "subscription.revoked") {
|
|
604
|
+
await db.prepare(
|
|
605
|
+
`UPDATE ${st} SET status = 'canceled', canceled_at = ?, updated_at = ? WHERE subscription_id = ?`
|
|
606
|
+
).bind(now, now, subId).run();
|
|
607
|
+
} else if (eventType === "order.created" || eventType === "payment.succeeded") {
|
|
608
|
+
const meta = data.metadata || {};
|
|
609
|
+
const orderId = meta.orderId || meta.order_id;
|
|
610
|
+
if (orderId && (meta.type === "store_order" || meta.type === "store")) {
|
|
611
|
+
await updateStoreOrderStatus(
|
|
612
|
+
orderId,
|
|
613
|
+
"paid",
|
|
614
|
+
{ paymentProvider: "polar", paymentIntentId: String(data.id), event }
|
|
615
|
+
).catch(() => {
|
|
616
|
+
});
|
|
617
|
+
}
|
|
494
618
|
}
|
|
495
|
-
|
|
496
|
-
|
|
619
|
+
}
|
|
620
|
+
if (provider === "paddle") {
|
|
621
|
+
eventType = payload.event_type || payload.type || "";
|
|
622
|
+
const data = payload.data || {};
|
|
623
|
+
eventData = data;
|
|
624
|
+
const subId = String(data.id || "");
|
|
625
|
+
affectedSubId = subId;
|
|
626
|
+
const customerId = String(data.customer_id || "");
|
|
627
|
+
const customData = data.custom_data || {};
|
|
628
|
+
const userId = customData.userId || customData.user_id || "";
|
|
629
|
+
const orgId = customData.orgId || customData.org_id || null;
|
|
630
|
+
const priceId = data.items?.[0]?.price?.id || "";
|
|
631
|
+
const planId = customData.planId || priceId;
|
|
632
|
+
if (eventType === "subscription.created" || eventType === "subscription.updated" || eventType === "subscription.activated" || eventType === "subscription.resumed") {
|
|
633
|
+
const status = data.status === "active" ? "active" : data.status === "trialing" ? "trialing" : data.status === "past_due" ? "past_due" : data.status === "paused" ? "paused" : "canceled";
|
|
634
|
+
const periodStart = data.current_billing_period?.starts_at ? new Date(data.current_billing_period.starts_at).getTime() : now;
|
|
635
|
+
const periodEnd = data.current_billing_period?.ends_at ? new Date(data.current_billing_period.ends_at).getTime() : now + 30 * 864e5;
|
|
636
|
+
const cancelAtPeriodEnd = data.scheduled_change?.action === "cancel" ? 1 : 0;
|
|
637
|
+
const quantity = data.items?.[0]?.quantity || 1;
|
|
638
|
+
if (customerId && userId) {
|
|
639
|
+
await db.prepare(
|
|
640
|
+
`INSERT INTO ${ct} (id, user_id, org_id, provider, customer_id, email, name, metadata_json, created_at, updated_at)
|
|
641
|
+
VALUES (?, ?, ?, 'paddle', ?, ?, ?, ?, ?, ?)
|
|
642
|
+
ON CONFLICT(customer_id) DO UPDATE SET org_id = excluded.org_id, metadata_json = excluded.metadata_json, updated_at = excluded.updated_at`
|
|
643
|
+
).bind(
|
|
644
|
+
nanoid(),
|
|
645
|
+
userId,
|
|
646
|
+
orgId,
|
|
647
|
+
customerId,
|
|
648
|
+
customData.email || ``,
|
|
649
|
+
customData.name || null,
|
|
650
|
+
JSON.stringify(customData),
|
|
651
|
+
now,
|
|
652
|
+
now
|
|
653
|
+
).run();
|
|
654
|
+
}
|
|
655
|
+
await db.prepare(
|
|
656
|
+
`INSERT INTO ${st}
|
|
497
657
|
(id, customer_id, user_id, org_id, provider, subscription_id, plan_id, price_id, status, quantity, cancel_at_period_end, current_period_start, current_period_end, trial_end, canceled_at, metadata_json, created_at, updated_at)
|
|
498
658
|
VALUES (?, ?, ?, ?, 'paddle', ?, ?, ?, ?, ?, ?, ?, ?, null, null, ?, ?, ?)
|
|
499
659
|
ON CONFLICT(subscription_id) DO UPDATE SET
|
|
@@ -501,59 +661,81 @@ export async function processPaymentWebhook(event, rawBody) {
|
|
|
501
661
|
quantity = excluded.quantity, cancel_at_period_end = excluded.cancel_at_period_end,
|
|
502
662
|
current_period_start = excluded.current_period_start, current_period_end = excluded.current_period_end,
|
|
503
663
|
metadata_json = excluded.metadata_json, updated_at = excluded.updated_at`
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
664
|
+
).bind(
|
|
665
|
+
nanoid(),
|
|
666
|
+
customerId,
|
|
667
|
+
userId,
|
|
668
|
+
orgId,
|
|
669
|
+
subId,
|
|
670
|
+
planId,
|
|
671
|
+
priceId,
|
|
672
|
+
status,
|
|
673
|
+
quantity,
|
|
674
|
+
cancelAtPeriodEnd,
|
|
675
|
+
periodStart,
|
|
676
|
+
periodEnd,
|
|
677
|
+
JSON.stringify(customData),
|
|
678
|
+
now,
|
|
679
|
+
now
|
|
680
|
+
).run();
|
|
681
|
+
} else if (eventType === "subscription.canceled" || eventType === "subscription.past_due") {
|
|
682
|
+
const newStatus = eventType === "subscription.past_due" ? "past_due" : "canceled";
|
|
683
|
+
await db.prepare(
|
|
684
|
+
`UPDATE ${st} SET status = ?, canceled_at = ?, updated_at = ? WHERE subscription_id = ?`
|
|
685
|
+
).bind(newStatus, newStatus === "canceled" ? now : null, now, subId).run();
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
let syncedSubscription = null;
|
|
689
|
+
if (affectedSubId) {
|
|
690
|
+
try {
|
|
691
|
+
const subRow = await db.prepare(`SELECT * FROM ${st} WHERE subscription_id = ? LIMIT 1`).bind(affectedSubId).first();
|
|
692
|
+
if (subRow) {
|
|
693
|
+
syncedSubscription = rowToSubscription(subRow);
|
|
694
|
+
}
|
|
695
|
+
} catch {
|
|
696
|
+
}
|
|
526
697
|
}
|
|
527
|
-
}
|
|
528
|
-
let syncedSubscription = null;
|
|
529
|
-
if (affectedSubId) {
|
|
530
698
|
try {
|
|
531
|
-
const
|
|
532
|
-
|
|
533
|
-
|
|
699
|
+
const nitroApp = globalThis.useNitroApp?.();
|
|
700
|
+
await nitroApp?.hooks?.callHook("qarpeo:payments:webhook", {
|
|
701
|
+
event,
|
|
702
|
+
type: eventType,
|
|
703
|
+
data: eventData,
|
|
704
|
+
subscription: syncedSubscription,
|
|
705
|
+
provider
|
|
706
|
+
});
|
|
707
|
+
} catch (hookErr) {
|
|
708
|
+
console.warn(
|
|
709
|
+
"[qarpeo:payments] Error calling qarpeo:payments:webhook hook:",
|
|
710
|
+
hookErr?.message || hookErr
|
|
711
|
+
);
|
|
712
|
+
}
|
|
713
|
+
if (kv && eventId) {
|
|
714
|
+
try {
|
|
715
|
+
await kv.put(kvKey, "completed", { expirationTtl: 604800 });
|
|
716
|
+
} catch {
|
|
534
717
|
}
|
|
535
|
-
} catch {
|
|
536
718
|
}
|
|
719
|
+
return { success: true, eventType };
|
|
720
|
+
} catch (processingErr) {
|
|
721
|
+
if (eventId) {
|
|
722
|
+
try {
|
|
723
|
+
await db.prepare(`DELETE FROM ${wt} WHERE event_id = ?`).bind(eventId).run();
|
|
724
|
+
} catch {
|
|
725
|
+
}
|
|
726
|
+
if (kv) {
|
|
727
|
+
try {
|
|
728
|
+
await kv.delete(kvKey);
|
|
729
|
+
} catch {
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
throw processingErr;
|
|
537
734
|
}
|
|
538
|
-
try {
|
|
539
|
-
const nitroApp = globalThis.useNitroApp?.();
|
|
540
|
-
await nitroApp?.hooks?.callHook("qarpeo:payments:webhook", {
|
|
541
|
-
event,
|
|
542
|
-
type: eventType,
|
|
543
|
-
data: eventData,
|
|
544
|
-
subscription: syncedSubscription,
|
|
545
|
-
provider
|
|
546
|
-
});
|
|
547
|
-
} catch (hookErr) {
|
|
548
|
-
console.warn(
|
|
549
|
-
"[qarpeo:payments] Error calling qarpeo:payments:webhook hook:",
|
|
550
|
-
hookErr?.message || hookErr
|
|
551
|
-
);
|
|
552
|
-
}
|
|
553
|
-
return { success: true, eventType };
|
|
554
735
|
}
|
|
555
736
|
export async function createCheckoutSession(event, options) {
|
|
556
737
|
await ensurePaymentsTables(event);
|
|
738
|
+
const ct = customerTbl(event);
|
|
557
739
|
const db = getPaymentsDb(event);
|
|
558
740
|
const config = getPaymentsConfig(event);
|
|
559
741
|
const auth = await getAuthContext(event);
|
|
@@ -597,7 +779,7 @@ export async function createCheckoutSession(event, options) {
|
|
|
597
779
|
let customerId = null;
|
|
598
780
|
if (user?.id) {
|
|
599
781
|
const cust = await db.prepare(
|
|
600
|
-
|
|
782
|
+
`SELECT customer_id FROM ${ct} WHERE user_id = ? AND provider = 'stripe' LIMIT 1`
|
|
601
783
|
).bind(user.id).first();
|
|
602
784
|
if (cust) customerId = cust.customer_id;
|
|
603
785
|
}
|
|
@@ -675,6 +857,7 @@ function safeReturnUrl(url, siteUrl, fallbackPath = "/") {
|
|
|
675
857
|
}
|
|
676
858
|
export async function createCustomerPortalSession(event, options) {
|
|
677
859
|
await ensurePaymentsTables(event);
|
|
860
|
+
const ct = customerTbl(event);
|
|
678
861
|
const db = getPaymentsDb(event);
|
|
679
862
|
const config = getPaymentsConfig(event);
|
|
680
863
|
const auth = await getAuthContext(event);
|
|
@@ -685,7 +868,7 @@ export async function createCustomerPortalSession(event, options) {
|
|
|
685
868
|
message: "Authentication required to access billing portal"
|
|
686
869
|
});
|
|
687
870
|
}
|
|
688
|
-
const cust = await db.prepare(
|
|
871
|
+
const cust = await db.prepare(`SELECT customer_id FROM ${ct} WHERE user_id = ? AND provider = ? LIMIT 1`).bind(user.id, config.provider).first();
|
|
689
872
|
if (!cust?.customer_id) {
|
|
690
873
|
throw createError({ statusCode: 404, message: "No customer account found for this user" });
|
|
691
874
|
}
|
|
@@ -719,6 +902,7 @@ export async function createCustomerPortalSession(event, options) {
|
|
|
719
902
|
}
|
|
720
903
|
export async function cancelSubscription(event, subscriptionId) {
|
|
721
904
|
await ensurePaymentsTables(event);
|
|
905
|
+
const st = subscriptionTbl(event);
|
|
722
906
|
const db = getPaymentsDb(event);
|
|
723
907
|
const config = getPaymentsConfig(event);
|
|
724
908
|
if (!subscriptionId) {
|
|
@@ -755,12 +939,13 @@ export async function cancelSubscription(event, subscriptionId) {
|
|
|
755
939
|
}
|
|
756
940
|
const now = Date.now();
|
|
757
941
|
await db.prepare(
|
|
758
|
-
|
|
942
|
+
`UPDATE ${st} SET status = 'canceled', cancel_at_period_end = 1, updated_at = ? WHERE subscription_id = ?`
|
|
759
943
|
).bind(now, subscriptionId).run();
|
|
760
944
|
return { success: true, subscriptionId, status: "canceled" };
|
|
761
945
|
}
|
|
762
946
|
export async function pauseSubscription(event, subscriptionId) {
|
|
763
947
|
await ensurePaymentsTables(event);
|
|
948
|
+
const st = subscriptionTbl(event);
|
|
764
949
|
const db = getPaymentsDb(event);
|
|
765
950
|
const config = getPaymentsConfig(event);
|
|
766
951
|
if (!subscriptionId) {
|
|
@@ -798,12 +983,13 @@ export async function pauseSubscription(event, subscriptionId) {
|
|
|
798
983
|
}
|
|
799
984
|
const now = Date.now();
|
|
800
985
|
await db.prepare(
|
|
801
|
-
|
|
986
|
+
`UPDATE ${st} SET status = 'paused', updated_at = ? WHERE subscription_id = ?`
|
|
802
987
|
).bind(now, subscriptionId).run();
|
|
803
988
|
return { success: true, subscriptionId, status: "paused" };
|
|
804
989
|
}
|
|
805
990
|
export async function changePlan(event, subscriptionId, newPriceId) {
|
|
806
991
|
await ensurePaymentsTables(event);
|
|
992
|
+
const st = subscriptionTbl(event);
|
|
807
993
|
const db = getPaymentsDb(event);
|
|
808
994
|
const config = getPaymentsConfig(event);
|
|
809
995
|
if (!subscriptionId) {
|
|
@@ -867,7 +1053,7 @@ export async function changePlan(event, subscriptionId, newPriceId) {
|
|
|
867
1053
|
const newPlanId = updated?.items?.data?.[0]?.price?.id || newPriceId;
|
|
868
1054
|
const now = Date.now();
|
|
869
1055
|
await db.prepare(
|
|
870
|
-
|
|
1056
|
+
`UPDATE ${st} SET plan_id = ?, price_id = ?, updated_at = ? WHERE subscription_id = ?`
|
|
871
1057
|
).bind(newPlanId, newPriceId, now, subscriptionId).run();
|
|
872
1058
|
return { success: true, subscriptionId, planId: newPlanId };
|
|
873
1059
|
}
|
|
@@ -901,6 +1087,7 @@ function rowToSubscription(row) {
|
|
|
901
1087
|
}
|
|
902
1088
|
export async function getSubscription(event, options) {
|
|
903
1089
|
await ensurePaymentsTables(event);
|
|
1090
|
+
const st = subscriptionTbl(event);
|
|
904
1091
|
const db = getPaymentsDb(event);
|
|
905
1092
|
const auth = await getAuthContext(event);
|
|
906
1093
|
const userId = options?.userId || auth?.user?.id;
|
|
@@ -909,12 +1096,12 @@ export async function getSubscription(event, options) {
|
|
|
909
1096
|
let row = null;
|
|
910
1097
|
if (orgId) {
|
|
911
1098
|
row = await db.prepare(
|
|
912
|
-
|
|
1099
|
+
`SELECT * FROM ${st} WHERE org_id = ? AND status IN ('active', 'trialing') ORDER BY created_at DESC LIMIT 1`
|
|
913
1100
|
).bind(orgId).first();
|
|
914
1101
|
}
|
|
915
1102
|
if (!row && userId) {
|
|
916
1103
|
row = await db.prepare(
|
|
917
|
-
|
|
1104
|
+
`SELECT * FROM ${st} WHERE user_id = ? AND status IN ('active', 'trialing') ORDER BY created_at DESC LIMIT 1`
|
|
918
1105
|
).bind(userId).first();
|
|
919
1106
|
}
|
|
920
1107
|
return row ? rowToSubscription(row) : null;
|
|
@@ -969,6 +1156,8 @@ export async function checkLimit(event, limitKey, currentUsage) {
|
|
|
969
1156
|
}
|
|
970
1157
|
export async function reportUsage(event, options) {
|
|
971
1158
|
await ensurePaymentsTables(event);
|
|
1159
|
+
const ct = customerTbl(event);
|
|
1160
|
+
const ut = usageRecordTbl(event);
|
|
972
1161
|
const db = getPaymentsDb(event);
|
|
973
1162
|
const config = getPaymentsConfig(event);
|
|
974
1163
|
const auth = await getAuthContext(event).catch(() => null);
|
|
@@ -981,7 +1170,7 @@ export async function reportUsage(event, options) {
|
|
|
981
1170
|
const action = options.action || "increment";
|
|
982
1171
|
const metadataJson = options.metadata ? JSON.stringify(options.metadata) : "{}";
|
|
983
1172
|
await db.prepare(
|
|
984
|
-
`INSERT INTO
|
|
1173
|
+
`INSERT INTO ${ut}
|
|
985
1174
|
(id, user_id, org_id, customer_id, subscription_id, event_name, quantity, action, metadata_json, timestamp, created_at)
|
|
986
1175
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
|
|
987
1176
|
).bind(
|
|
@@ -1001,7 +1190,7 @@ export async function reportUsage(event, options) {
|
|
|
1001
1190
|
let custId = options.customerId;
|
|
1002
1191
|
if (!custId && userId) {
|
|
1003
1192
|
const cust = await db.prepare(
|
|
1004
|
-
|
|
1193
|
+
`SELECT customer_id FROM ${ct} WHERE user_id = ? AND provider = 'stripe' LIMIT 1`
|
|
1005
1194
|
).bind(userId).first();
|
|
1006
1195
|
if (cust) custId = cust.customer_id;
|
|
1007
1196
|
}
|
|
@@ -1041,11 +1230,12 @@ export async function reportUsage(event, options) {
|
|
|
1041
1230
|
}
|
|
1042
1231
|
export async function getUsageSummary(event, options) {
|
|
1043
1232
|
await ensurePaymentsTables(event);
|
|
1233
|
+
const ut = usageRecordTbl(event);
|
|
1044
1234
|
const db = getPaymentsDb(event);
|
|
1045
1235
|
const auth = await getAuthContext(event).catch(() => null);
|
|
1046
1236
|
const userId = options.userId || auth?.user?.id;
|
|
1047
1237
|
const orgId = options.orgId || auth?.organization?.id;
|
|
1048
|
-
let sql =
|
|
1238
|
+
let sql = `SELECT * FROM ${ut} WHERE 1=1`;
|
|
1049
1239
|
const params = [];
|
|
1050
1240
|
if (options.eventName) {
|
|
1051
1241
|
sql += " AND event_name = ?";
|