@polar-sh/better-auth 0.0.7 → 0.0.8

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 CHANGED
@@ -127,7 +127,7 @@ The plugin adds the following API routes:
127
127
 
128
128
  - `GET /checkout/:slug` - Redirect to Polar checkout
129
129
  - `GET /state` - Customer state (Customer Data, Active Subscriptions, Entitlements, etc.) for the authenticated user
130
- - `GET /customer-portal` - Redirects to Polar Customer Portal for authenticated user
130
+ - `GET /portal` - Redirects to Polar Customer Portal for authenticated user
131
131
  - `POST /polar/webhooks` - Incoming webhooks are automatically parsed & validated
132
132
 
133
133
  ## Customers
package/dist/index.cjs CHANGED
@@ -29,11 +29,11 @@ var import_api = require("better-auth/api");
29
29
  var import_plugins = require("better-auth/plugins");
30
30
  var import_zod = require("zod");
31
31
  var checkout = (options) => (0, import_plugins.createAuthEndpoint)(
32
- "/checkout/:slug",
32
+ "/checkout",
33
33
  {
34
34
  method: "GET",
35
35
  query: import_zod.z.object({
36
- productId: import_zod.z.string().optional()
36
+ productId: import_zod.z.string()
37
37
  })
38
38
  },
39
39
  async (ctx) => {
@@ -42,13 +42,50 @@ var checkout = (options) => (0, import_plugins.createAuthEndpoint)(
42
42
  message: "Checkout is not enabled"
43
43
  });
44
44
  }
45
- let productId = ctx.query?.productId;
46
- if (ctx.params.slug) {
47
- const products = await (typeof options.checkout.products === "function" ? options.checkout.products() : options.checkout.products);
48
- productId = products.find(
49
- (product) => product.slug === ctx.params.slug
50
- )?.productId;
45
+ const productId = ctx.query?.productId;
46
+ if (!productId) {
47
+ throw new import_api.APIError("BAD_REQUEST", {
48
+ message: "Product Id not found"
49
+ });
50
+ }
51
+ const session = await (0, import_api.getSessionFromCtx)(ctx);
52
+ try {
53
+ const checkout2 = await options.client.checkouts.create({
54
+ customerExternalId: session?.user.id,
55
+ productId,
56
+ successUrl: options.checkout.successUrl ? new URL(options.checkout.successUrl, ctx.request?.url).toString() : void 0
57
+ });
58
+ return ctx.redirect(checkout2.url);
59
+ } catch (e) {
60
+ if (e instanceof Error) {
61
+ ctx.context.logger.error(
62
+ `Polar checkout creation failed. Error: ${e.message}`
63
+ );
64
+ }
65
+ throw new import_api.APIError("INTERNAL_SERVER_ERROR", {
66
+ message: "Checkout creation failed"
67
+ });
68
+ }
69
+ }
70
+ );
71
+ var checkoutWithSlug = (options) => (0, import_plugins.createAuthEndpoint)(
72
+ "/checkout/:slug",
73
+ {
74
+ method: "GET",
75
+ params: import_zod.z.object({
76
+ slug: import_zod.z.string()
77
+ })
78
+ },
79
+ async (ctx) => {
80
+ if (!options.checkout?.enabled) {
81
+ throw new import_api.APIError("BAD_REQUEST", {
82
+ message: "Checkout is not enabled"
83
+ });
51
84
  }
85
+ const products = await (typeof options.checkout.products === "function" ? options.checkout.products() : options.checkout.products);
86
+ const productId = products.find(
87
+ (product) => product.slug === ctx.params?.["slug"]
88
+ )?.productId;
52
89
  if (!productId) {
53
90
  throw new import_api.APIError("BAD_REQUEST", {
54
91
  message: "Product Id not found"
@@ -434,6 +471,7 @@ var polar = (options) => {
434
471
  id: "polar",
435
472
  endpoints: {
436
473
  polarCheckout: checkout(options),
474
+ polarCheckoutWithSlug: checkoutWithSlug(options),
437
475
  polarWebhooks: webhooks(options),
438
476
  polarCustomerPortal: customerPortal(options),
439
477
  polarCustomerState: customerState(options)
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/endpoints/checkout.ts","../src/endpoints/customerPortal.ts","../src/endpoints/customerState.ts","../src/endpoints/webhooks.ts","../src/hooks/customer.ts"],"sourcesContent":["import type { BetterAuthPlugin } from \"better-auth\";\nimport { checkout } from \"./endpoints/checkout\";\nimport { customerPortal } from \"./endpoints/customerPortal\";\nimport { customerState } from \"./endpoints/customerState\";\nimport { webhooks } from \"./endpoints/webhooks\";\nimport { onUserCreate, onUserUpdate } from \"./hooks/customer\";\nimport type { PolarOptions } from \"./types\";\n\nexport const polar = <O extends PolarOptions>(options: O) => {\n\treturn {\n\t\tid: \"polar\",\n\t\tendpoints: {\n\t\t\tpolarCheckout: checkout(options),\n\t\t\tpolarWebhooks: webhooks(options),\n\t\t\tpolarCustomerPortal: customerPortal(options),\n\t\t\tpolarCustomerState: customerState(options),\n\t\t},\n\t\tinit() {\n\t\t\treturn {\n\t\t\t\toptions: {\n\t\t\t\t\tdatabaseHooks: {\n\t\t\t\t\t\tuser: {\n\t\t\t\t\t\t\tcreate: {\n\t\t\t\t\t\t\t\tafter: onUserCreate(options),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\tupdate: {\n\t\t\t\t\t\t\t\tafter: onUserUpdate(options),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t};\n\t\t},\n\t} satisfies BetterAuthPlugin;\n};\n","import { APIError, getSessionFromCtx } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport { z } from \"zod\";\nimport type { PolarOptions } from \"../types\";\n\nexport const checkout = (options: PolarOptions) =>\n\tcreateAuthEndpoint(\n\t\t\"/checkout/:slug\",\n\t\t{\n\t\t\tmethod: \"GET\",\n\t\t\tquery: z.object({\n\t\t\t\tproductId: z.string().optional(),\n\t\t\t}),\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tif (!options.checkout?.enabled) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"Checkout is not enabled\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tlet productId = ctx.query?.productId;\n\n\t\t\tif (ctx.params.slug) {\n\t\t\t\tconst products = await (typeof options.checkout.products === \"function\"\n\t\t\t\t\t? options.checkout.products()\n\t\t\t\t\t: options.checkout.products);\n\n\t\t\t\tproductId = products.find(\n\t\t\t\t\t(product) => product.slug === ctx.params.slug,\n\t\t\t\t)?.productId;\n\t\t\t}\n\n\t\t\tif (!productId) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"Product Id not found\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tconst session = await getSessionFromCtx(ctx);\n\n\t\t\ttry {\n\t\t\t\tconst checkout = await options.client.checkouts.create({\n\t\t\t\t\tcustomerExternalId: session?.user.id,\n\t\t\t\t\tproductId,\n\t\t\t\t\tsuccessUrl: options.checkout.successUrl\n\t\t\t\t\t\t? new URL(options.checkout.successUrl, ctx.request?.url).toString()\n\t\t\t\t\t\t: undefined,\n\t\t\t\t});\n\n\t\t\t\treturn ctx.redirect(checkout.url);\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar checkout creation failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\tmessage: \"Checkout creation failed\",\n\t\t\t\t});\n\t\t\t}\n\t\t},\n\t);\n","import { APIError, sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport type { PolarOptions } from \"../types\";\n\nexport const customerPortal = (options: PolarOptions) =>\n\tcreateAuthEndpoint(\n\t\t\"/portal\",\n\t\t{\n\t\t\tmethod: \"GET\",\n\t\t\tuse: [sessionMiddleware],\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tif (!options.enableCustomerPortal) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"Customer portal is not enabled\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tif (!ctx.context.session?.user.id) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\ttry {\n\t\t\t\tconst customerSession = await options.client.customerSessions.create({\n\t\t\t\t\tcustomerExternalId: ctx.context.session?.user.id,\n\t\t\t\t});\n\n\t\t\t\treturn ctx.redirect(customerSession.customerPortalUrl);\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar customer portal creation failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\tmessage: \"Customer portal creation failed\",\n\t\t\t\t});\n\t\t\t}\n\t\t},\n\t);\n","import { APIError, sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport type { PolarOptions } from \"../types\";\n\nexport const customerState = (options: PolarOptions) =>\n\tcreateAuthEndpoint(\n\t\t\"/state\",\n\t\t{\n\t\t\tmethod: \"GET\",\n\t\t\tuse: [sessionMiddleware],\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\ttry {\n\t\t\t\tconst state = await options.client.customers.getStateExternal({\n\t\t\t\t\texternalId: ctx.context.session?.user.id,\n\t\t\t\t});\n\n\t\t\t\treturn ctx.json(state);\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar subscriptions list failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\tmessage: \"Subscriptions list failed\",\n\t\t\t\t});\n\t\t\t}\n\t\t},\n\t);\n","import { validateEvent } from \"@polar-sh/sdk/webhooks.js\";\nimport { APIError } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport type { PolarOptions } from \"../types\";\n\nexport const webhooks = (options: PolarOptions) =>\n\tcreateAuthEndpoint(\n\t\t\"/polar/webhooks\",\n\t\t{\n\t\t\tmethod: \"POST\",\n\t\t\tmetadata: {\n\t\t\t\tisAction: false,\n\t\t\t},\n\t\t\tcloneRequest: true,\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tconst { webhooks } = options;\n\n\t\t\tif (!webhooks) {\n\t\t\t\tthrow new APIError(\"NOT_FOUND\", {\n\t\t\t\t\tmessage: \"Webhooks not enabled\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tconst {\n\t\t\t\tsecret,\n\t\t\t\tonPayload,\n\t\t\t\tonCheckoutCreated,\n\t\t\t\tonCheckoutUpdated,\n\t\t\t\tonOrderCreated,\n\t\t\t\tonOrderRefunded,\n\t\t\t\tonRefundCreated,\n\t\t\t\tonRefundUpdated,\n\t\t\t\tonSubscriptionCreated,\n\t\t\t\tonSubscriptionUpdated,\n\t\t\t\tonSubscriptionActive,\n\t\t\t\tonSubscriptionCanceled,\n\t\t\t\tonSubscriptionRevoked,\n\t\t\t\tonSubscriptionUncanceled,\n\t\t\t\tonProductCreated,\n\t\t\t\tonProductUpdated,\n\t\t\t\tonOrganizationUpdated,\n\t\t\t\tonBenefitCreated,\n\t\t\t\tonBenefitUpdated,\n\t\t\t\tonBenefitGrantCreated,\n\t\t\t\tonBenefitGrantUpdated,\n\t\t\t\tonBenefitGrantRevoked,\n\t\t\t\tonCustomerCreated,\n\t\t\t\tonCustomerUpdated,\n\t\t\t\tonCustomerDeleted,\n\t\t\t\tonCustomerStateChanged,\n\t\t\t} = webhooks;\n\n\t\t\tif (!ctx.request?.body) {\n\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\");\n\t\t\t}\n\t\t\tconst buf = await ctx.request.text();\n\t\t\tlet event: ReturnType<typeof validateEvent>;\n\t\t\ttry {\n\t\t\t\tif (!secret) {\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: \"Polar webhook secret not found\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\tconst headers = {\n\t\t\t\t\t\"webhook-id\": ctx.request.headers.get(\"webhook-id\") as string,\n\t\t\t\t\t\"webhook-timestamp\": ctx.request.headers.get(\n\t\t\t\t\t\t\"webhook-timestamp\",\n\t\t\t\t\t) as string,\n\t\t\t\t\t\"webhook-signature\": ctx.request.headers.get(\n\t\t\t\t\t\t\"webhook-signature\",\n\t\t\t\t\t) as string,\n\t\t\t\t};\n\n\t\t\t\tevent = validateEvent(buf, headers, secret);\n\t\t\t} catch (err: unknown) {\n\t\t\t\tif (err instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(`${err.message}`);\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: `Webhook Error: ${err.message}`,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: `Webhook Error: ${err}`,\n\t\t\t\t});\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tif (onPayload) {\n\t\t\t\t\tonPayload(event);\n\t\t\t\t}\n\n\t\t\t\tswitch (event.type) {\n\t\t\t\t\tcase \"checkout.created\":\n\t\t\t\t\t\tif (onCheckoutCreated) {\n\t\t\t\t\t\t\tonCheckoutCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"checkout.updated\":\n\t\t\t\t\t\tif (onCheckoutUpdated) {\n\t\t\t\t\t\t\tonCheckoutUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"order.created\":\n\t\t\t\t\t\tif (onOrderCreated) {\n\t\t\t\t\t\t\tonOrderCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.created\":\n\t\t\t\t\t\tif (onSubscriptionCreated) {\n\t\t\t\t\t\t\tonSubscriptionCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.updated\":\n\t\t\t\t\t\tif (onSubscriptionUpdated) {\n\t\t\t\t\t\t\tonSubscriptionUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.active\":\n\t\t\t\t\t\tif (onSubscriptionActive) {\n\t\t\t\t\t\t\tonSubscriptionActive(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.canceled\":\n\t\t\t\t\t\tif (onSubscriptionCanceled) {\n\t\t\t\t\t\t\tonSubscriptionCanceled(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.uncanceled\":\n\t\t\t\t\t\tif (onSubscriptionUncanceled) {\n\t\t\t\t\t\t\tonSubscriptionUncanceled(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.revoked\":\n\t\t\t\t\t\tif (onSubscriptionRevoked) {\n\t\t\t\t\t\t\tonSubscriptionRevoked(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"product.created\":\n\t\t\t\t\t\tif (onProductCreated) {\n\t\t\t\t\t\t\tonProductCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"product.updated\":\n\t\t\t\t\t\tif (onProductUpdated) {\n\t\t\t\t\t\t\tonProductUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"organization.updated\":\n\t\t\t\t\t\tif (onOrganizationUpdated) {\n\t\t\t\t\t\t\tonOrganizationUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"benefit.created\":\n\t\t\t\t\t\tif (onBenefitCreated) {\n\t\t\t\t\t\t\tonBenefitCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"benefit.updated\":\n\t\t\t\t\t\tif (onBenefitUpdated) {\n\t\t\t\t\t\t\tonBenefitUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"benefit_grant.created\":\n\t\t\t\t\t\tif (onBenefitGrantCreated) {\n\t\t\t\t\t\t\tonBenefitGrantCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"benefit_grant.updated\":\n\t\t\t\t\t\tif (onBenefitGrantUpdated) {\n\t\t\t\t\t\t\tonBenefitGrantUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"benefit_grant.revoked\":\n\t\t\t\t\t\tif (onBenefitGrantRevoked) {\n\t\t\t\t\t\t\tonBenefitGrantRevoked(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"order.refunded\":\n\t\t\t\t\t\tif (onOrderRefunded) {\n\t\t\t\t\t\t\tonOrderRefunded(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"refund.created\":\n\t\t\t\t\t\tif (onRefundCreated) {\n\t\t\t\t\t\t\tonRefundCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"refund.updated\":\n\t\t\t\t\t\tif (onRefundUpdated) {\n\t\t\t\t\t\t\tonRefundUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"customer.created\":\n\t\t\t\t\t\tif (onCustomerCreated) {\n\t\t\t\t\t\t\tonCustomerCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"customer.updated\":\n\t\t\t\t\t\tif (onCustomerUpdated) {\n\t\t\t\t\t\t\tonCustomerUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"customer.deleted\":\n\t\t\t\t\t\tif (onCustomerDeleted) {\n\t\t\t\t\t\t\tonCustomerDeleted(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"customer.state_changed\":\n\t\t\t\t\t\tif (onCustomerStateChanged) {\n\t\t\t\t\t\t\tonCustomerStateChanged(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(`Polar webhook failed. Error: ${e.message}`);\n\t\t\t\t} else {\n\t\t\t\t\tctx.context.logger.error(`Polar webhook failed. Error: ${e}`);\n\t\t\t\t}\n\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"Webhook error: See server logs for more information.\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\treturn ctx.json({ received: true });\n\t\t},\n\t);\n","import type { GenericEndpointContext, User } from \"better-auth\";\nimport type { PolarOptions } from \"../types\";\n\nexport const onUserCreate =\n\t(options: PolarOptions) =>\n\tasync (user: User, ctx?: GenericEndpointContext) => {\n\t\tif (ctx && options.createCustomerOnSignUp) {\n\t\t\ttry {\n\t\t\t\tconst params =\n\t\t\t\t\toptions.getCustomerCreateParams && ctx.context.session\n\t\t\t\t\t\t? await options.getCustomerCreateParams({\n\t\t\t\t\t\t\t\tuser,\n\t\t\t\t\t\t\t\tsession: ctx.context.session.session,\n\t\t\t\t\t\t\t})\n\t\t\t\t\t\t: {};\n\n\t\t\t\tconst { result: existingCustomers } =\n\t\t\t\t\tawait options.client.customers.list({ email: user.email });\n\t\t\t\tconst existingCustomer = existingCustomers.items[0];\n\n\t\t\t\tif (existingCustomer) {\n\t\t\t\t\tif (existingCustomer.externalId !== user.id) {\n\t\t\t\t\t\tawait options.client.customers.update({\n\t\t\t\t\t\t\tid: existingCustomer.id,\n\t\t\t\t\t\t\tcustomerUpdate: {\n\t\t\t\t\t\t\t\texternalId: user.id,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tawait options.client.customers.create({\n\t\t\t\t\t\t...params,\n\t\t\t\t\t\temail: user.email,\n\t\t\t\t\t\tname: user.name,\n\t\t\t\t\t\texternalId: user.id,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar customer creation failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar customer creation failed. Error: ${e}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t};\n\nexport const onUserUpdate =\n\t(options: PolarOptions) =>\n\tasync (user: User, ctx?: GenericEndpointContext) => {\n\t\tif (ctx && options.createCustomerOnSignUp) {\n\t\t\ttry {\n\t\t\t\tawait options.client.customers.updateExternal({\n\t\t\t\t\texternalId: user.id,\n\t\t\t\t\tcustomerUpdate: {\n\t\t\t\t\t\temail: user.email,\n\t\t\t\t\t\tname: user.name,\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar customer update failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\tctx.context.logger.error(`Polar customer update failed. Error: ${e}`);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,iBAA4C;AAC5C,qBAAmC;AACnC,iBAAkB;AAGX,IAAM,WAAW,CAAC,gBACxB;AAAA,EACC;AAAA,EACA;AAAA,IACC,QAAQ;AAAA,IACR,OAAO,aAAE,OAAO;AAAA,MACf,WAAW,aAAE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AAC1C,QAAI;AACH,YAAM,QAAQ,OAAO,UAAU,eAAe;AAAA,QAC7C,YAAY,KAAK;AAAA,QACjB,gBAAgB;AAAA,UACf,OAAO,KAAK;AAAA,UACZ,MAAM,KAAK;AAAA,QACZ;AAAA,MACD,CAAC;AAAA,IACF,SAAS,GAAY;AACpB,UAAI,aAAa,OAAO;AACvB,YAAI,QAAQ,OAAO;AAAA,UAClB,wCAAwC,EAAE,OAAO;AAAA,QAClD;AAAA,MACD,OAAO;AACN,YAAI,QAAQ,OAAO,MAAM,wCAAwC,CAAC,EAAE;AAAA,MACrE;AAAA,IACD;AAAA,EACD;AACD;;;ALjEM,IAAM,QAAQ,CAAyB,YAAe;AAC5D,SAAO;AAAA,IACN,IAAI;AAAA,IACJ,WAAW;AAAA,MACV,eAAe,SAAS,OAAO;AAAA,MAC/B,eAAe,SAAS,OAAO;AAAA,MAC/B,qBAAqB,eAAe,OAAO;AAAA,MAC3C,oBAAoB,cAAc,OAAO;AAAA,IAC1C;AAAA,IACA,OAAO;AACN,aAAO;AAAA,QACN,SAAS;AAAA,UACR,eAAe;AAAA,YACd,MAAM;AAAA,cACL,QAAQ;AAAA,gBACP,OAAO,aAAa,OAAO;AAAA,cAC5B;AAAA,cACA,QAAQ;AAAA,gBACP,OAAO,aAAa,OAAO;AAAA,cAC5B;AAAA,YACD;AAAA,UACD;AAAA,QACD;AAAA,MACD;AAAA,IACD;AAAA,EACD;AACD;","names":["checkout","import_api","import_plugins","import_api","import_plugins","import_api","import_plugins","webhooks"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/endpoints/checkout.ts","../src/endpoints/customerPortal.ts","../src/endpoints/customerState.ts","../src/endpoints/webhooks.ts","../src/hooks/customer.ts"],"sourcesContent":["import type { BetterAuthPlugin } from \"better-auth\";\nimport { checkout, checkoutWithSlug } from \"./endpoints/checkout\";\nimport { customerPortal } from \"./endpoints/customerPortal\";\nimport { customerState } from \"./endpoints/customerState\";\nimport { webhooks } from \"./endpoints/webhooks\";\nimport { onUserCreate, onUserUpdate } from \"./hooks/customer\";\nimport type { PolarOptions } from \"./types\";\n\nexport const polar = <O extends PolarOptions>(options: O) => {\n\treturn {\n\t\tid: \"polar\",\n\t\tendpoints: {\n\t\t\tpolarCheckout: checkout(options),\n\t\t\tpolarCheckoutWithSlug: checkoutWithSlug(options),\n\t\t\tpolarWebhooks: webhooks(options),\n\t\t\tpolarCustomerPortal: customerPortal(options),\n\t\t\tpolarCustomerState: customerState(options),\n\t\t},\n\t\tinit() {\n\t\t\treturn {\n\t\t\t\toptions: {\n\t\t\t\t\tdatabaseHooks: {\n\t\t\t\t\t\tuser: {\n\t\t\t\t\t\t\tcreate: {\n\t\t\t\t\t\t\t\tafter: onUserCreate(options),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\tupdate: {\n\t\t\t\t\t\t\t\tafter: onUserUpdate(options),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t};\n\t\t},\n\t} satisfies BetterAuthPlugin;\n};\n","import { APIError, getSessionFromCtx } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport { z } from \"zod\";\nimport type { PolarOptions } from \"../types\";\n\nexport const checkout = (options: PolarOptions) =>\n\tcreateAuthEndpoint(\n\t\t\"/checkout\",\n\t\t{\n\t\t\tmethod: \"GET\",\n\t\t\tquery: z.object({\n\t\t\t\tproductId: z.string(),\n\t\t\t}),\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tif (!options.checkout?.enabled) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"Checkout is not enabled\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tconst productId = ctx.query?.productId;\n\n\t\t\tif (!productId) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"Product Id not found\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tconst session = await getSessionFromCtx(ctx);\n\n\t\t\ttry {\n\t\t\t\tconst checkout = await options.client.checkouts.create({\n\t\t\t\t\tcustomerExternalId: session?.user.id,\n\t\t\t\t\tproductId,\n\t\t\t\t\tsuccessUrl: options.checkout.successUrl\n\t\t\t\t\t\t? new URL(options.checkout.successUrl, ctx.request?.url).toString()\n\t\t\t\t\t\t: undefined,\n\t\t\t\t});\n\n\t\t\t\treturn ctx.redirect(checkout.url);\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar checkout creation failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\tmessage: \"Checkout creation failed\",\n\t\t\t\t});\n\t\t\t}\n\t\t},\n\t);\n\nexport const checkoutWithSlug = (options: PolarOptions) =>\n\tcreateAuthEndpoint(\n\t\t\"/checkout/:slug\",\n\t\t{\n\t\t\tmethod: \"GET\",\n\t\t\tparams: z.object({\n\t\t\t\tslug: z.string(),\n\t\t\t}),\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tif (!options.checkout?.enabled) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"Checkout is not enabled\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tconst products = await (typeof options.checkout.products === \"function\"\n\t\t\t\t? options.checkout.products()\n\t\t\t\t: options.checkout.products);\n\n\t\t\tconst productId = products.find(\n\t\t\t\t(product) => product.slug === ctx.params?.[\"slug\"],\n\t\t\t)?.productId;\n\n\t\t\tif (!productId) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"Product Id not found\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tconst session = await getSessionFromCtx(ctx);\n\n\t\t\ttry {\n\t\t\t\tconst checkout = await options.client.checkouts.create({\n\t\t\t\t\tcustomerExternalId: session?.user.id,\n\t\t\t\t\tproductId,\n\t\t\t\t\tsuccessUrl: options.checkout.successUrl\n\t\t\t\t\t\t? new URL(options.checkout.successUrl, ctx.request?.url).toString()\n\t\t\t\t\t\t: undefined,\n\t\t\t\t});\n\n\t\t\t\treturn ctx.redirect(checkout.url);\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar checkout creation failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\tmessage: \"Checkout creation failed\",\n\t\t\t\t});\n\t\t\t}\n\t\t},\n\t);\n","import { APIError, sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport type { PolarOptions } from \"../types\";\n\nexport const customerPortal = (options: PolarOptions) =>\n\tcreateAuthEndpoint(\n\t\t\"/portal\",\n\t\t{\n\t\t\tmethod: \"GET\",\n\t\t\tuse: [sessionMiddleware],\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tif (!options.enableCustomerPortal) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"Customer portal is not enabled\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tif (!ctx.context.session?.user.id) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\ttry {\n\t\t\t\tconst customerSession = await options.client.customerSessions.create({\n\t\t\t\t\tcustomerExternalId: ctx.context.session?.user.id,\n\t\t\t\t});\n\n\t\t\t\treturn ctx.redirect(customerSession.customerPortalUrl);\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar customer portal creation failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\tmessage: \"Customer portal creation failed\",\n\t\t\t\t});\n\t\t\t}\n\t\t},\n\t);\n","import { APIError, sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport type { PolarOptions } from \"../types\";\n\nexport const customerState = (options: PolarOptions) =>\n\tcreateAuthEndpoint(\n\t\t\"/state\",\n\t\t{\n\t\t\tmethod: \"GET\",\n\t\t\tuse: [sessionMiddleware],\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\ttry {\n\t\t\t\tconst state = await options.client.customers.getStateExternal({\n\t\t\t\t\texternalId: ctx.context.session?.user.id,\n\t\t\t\t});\n\n\t\t\t\treturn ctx.json(state);\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar subscriptions list failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\tmessage: \"Subscriptions list failed\",\n\t\t\t\t});\n\t\t\t}\n\t\t},\n\t);\n","import { validateEvent } from \"@polar-sh/sdk/webhooks.js\";\nimport { APIError } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport type { PolarOptions } from \"../types\";\n\nexport const webhooks = (options: PolarOptions) =>\n\tcreateAuthEndpoint(\n\t\t\"/polar/webhooks\",\n\t\t{\n\t\t\tmethod: \"POST\",\n\t\t\tmetadata: {\n\t\t\t\tisAction: false,\n\t\t\t},\n\t\t\tcloneRequest: true,\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tconst { webhooks } = options;\n\n\t\t\tif (!webhooks) {\n\t\t\t\tthrow new APIError(\"NOT_FOUND\", {\n\t\t\t\t\tmessage: \"Webhooks not enabled\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tconst {\n\t\t\t\tsecret,\n\t\t\t\tonPayload,\n\t\t\t\tonCheckoutCreated,\n\t\t\t\tonCheckoutUpdated,\n\t\t\t\tonOrderCreated,\n\t\t\t\tonOrderRefunded,\n\t\t\t\tonRefundCreated,\n\t\t\t\tonRefundUpdated,\n\t\t\t\tonSubscriptionCreated,\n\t\t\t\tonSubscriptionUpdated,\n\t\t\t\tonSubscriptionActive,\n\t\t\t\tonSubscriptionCanceled,\n\t\t\t\tonSubscriptionRevoked,\n\t\t\t\tonSubscriptionUncanceled,\n\t\t\t\tonProductCreated,\n\t\t\t\tonProductUpdated,\n\t\t\t\tonOrganizationUpdated,\n\t\t\t\tonBenefitCreated,\n\t\t\t\tonBenefitUpdated,\n\t\t\t\tonBenefitGrantCreated,\n\t\t\t\tonBenefitGrantUpdated,\n\t\t\t\tonBenefitGrantRevoked,\n\t\t\t\tonCustomerCreated,\n\t\t\t\tonCustomerUpdated,\n\t\t\t\tonCustomerDeleted,\n\t\t\t\tonCustomerStateChanged,\n\t\t\t} = webhooks;\n\n\t\t\tif (!ctx.request?.body) {\n\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\");\n\t\t\t}\n\t\t\tconst buf = await ctx.request.text();\n\t\t\tlet event: ReturnType<typeof validateEvent>;\n\t\t\ttry {\n\t\t\t\tif (!secret) {\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: \"Polar webhook secret not found\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\tconst headers = {\n\t\t\t\t\t\"webhook-id\": ctx.request.headers.get(\"webhook-id\") as string,\n\t\t\t\t\t\"webhook-timestamp\": ctx.request.headers.get(\n\t\t\t\t\t\t\"webhook-timestamp\",\n\t\t\t\t\t) as string,\n\t\t\t\t\t\"webhook-signature\": ctx.request.headers.get(\n\t\t\t\t\t\t\"webhook-signature\",\n\t\t\t\t\t) as string,\n\t\t\t\t};\n\n\t\t\t\tevent = validateEvent(buf, headers, secret);\n\t\t\t} catch (err: unknown) {\n\t\t\t\tif (err instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(`${err.message}`);\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: `Webhook Error: ${err.message}`,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: `Webhook Error: ${err}`,\n\t\t\t\t});\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tif (onPayload) {\n\t\t\t\t\tonPayload(event);\n\t\t\t\t}\n\n\t\t\t\tswitch (event.type) {\n\t\t\t\t\tcase \"checkout.created\":\n\t\t\t\t\t\tif (onCheckoutCreated) {\n\t\t\t\t\t\t\tonCheckoutCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"checkout.updated\":\n\t\t\t\t\t\tif (onCheckoutUpdated) {\n\t\t\t\t\t\t\tonCheckoutUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"order.created\":\n\t\t\t\t\t\tif (onOrderCreated) {\n\t\t\t\t\t\t\tonOrderCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.created\":\n\t\t\t\t\t\tif (onSubscriptionCreated) {\n\t\t\t\t\t\t\tonSubscriptionCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.updated\":\n\t\t\t\t\t\tif (onSubscriptionUpdated) {\n\t\t\t\t\t\t\tonSubscriptionUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.active\":\n\t\t\t\t\t\tif (onSubscriptionActive) {\n\t\t\t\t\t\t\tonSubscriptionActive(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.canceled\":\n\t\t\t\t\t\tif (onSubscriptionCanceled) {\n\t\t\t\t\t\t\tonSubscriptionCanceled(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.uncanceled\":\n\t\t\t\t\t\tif (onSubscriptionUncanceled) {\n\t\t\t\t\t\t\tonSubscriptionUncanceled(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.revoked\":\n\t\t\t\t\t\tif (onSubscriptionRevoked) {\n\t\t\t\t\t\t\tonSubscriptionRevoked(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"product.created\":\n\t\t\t\t\t\tif (onProductCreated) {\n\t\t\t\t\t\t\tonProductCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"product.updated\":\n\t\t\t\t\t\tif (onProductUpdated) {\n\t\t\t\t\t\t\tonProductUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"organization.updated\":\n\t\t\t\t\t\tif (onOrganizationUpdated) {\n\t\t\t\t\t\t\tonOrganizationUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"benefit.created\":\n\t\t\t\t\t\tif (onBenefitCreated) {\n\t\t\t\t\t\t\tonBenefitCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"benefit.updated\":\n\t\t\t\t\t\tif (onBenefitUpdated) {\n\t\t\t\t\t\t\tonBenefitUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"benefit_grant.created\":\n\t\t\t\t\t\tif (onBenefitGrantCreated) {\n\t\t\t\t\t\t\tonBenefitGrantCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"benefit_grant.updated\":\n\t\t\t\t\t\tif (onBenefitGrantUpdated) {\n\t\t\t\t\t\t\tonBenefitGrantUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"benefit_grant.revoked\":\n\t\t\t\t\t\tif (onBenefitGrantRevoked) {\n\t\t\t\t\t\t\tonBenefitGrantRevoked(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"order.refunded\":\n\t\t\t\t\t\tif (onOrderRefunded) {\n\t\t\t\t\t\t\tonOrderRefunded(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"refund.created\":\n\t\t\t\t\t\tif (onRefundCreated) {\n\t\t\t\t\t\t\tonRefundCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"refund.updated\":\n\t\t\t\t\t\tif (onRefundUpdated) {\n\t\t\t\t\t\t\tonRefundUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"customer.created\":\n\t\t\t\t\t\tif (onCustomerCreated) {\n\t\t\t\t\t\t\tonCustomerCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"customer.updated\":\n\t\t\t\t\t\tif (onCustomerUpdated) {\n\t\t\t\t\t\t\tonCustomerUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"customer.deleted\":\n\t\t\t\t\t\tif (onCustomerDeleted) {\n\t\t\t\t\t\t\tonCustomerDeleted(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"customer.state_changed\":\n\t\t\t\t\t\tif (onCustomerStateChanged) {\n\t\t\t\t\t\t\tonCustomerStateChanged(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(`Polar webhook failed. Error: ${e.message}`);\n\t\t\t\t} else {\n\t\t\t\t\tctx.context.logger.error(`Polar webhook failed. Error: ${e}`);\n\t\t\t\t}\n\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"Webhook error: See server logs for more information.\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\treturn ctx.json({ received: true });\n\t\t},\n\t);\n","import type { GenericEndpointContext, User } from \"better-auth\";\nimport type { PolarOptions } from \"../types\";\n\nexport const onUserCreate =\n\t(options: PolarOptions) =>\n\tasync (user: User, ctx?: GenericEndpointContext) => {\n\t\tif (ctx && options.createCustomerOnSignUp) {\n\t\t\ttry {\n\t\t\t\tconst params =\n\t\t\t\t\toptions.getCustomerCreateParams && ctx.context.session\n\t\t\t\t\t\t? await options.getCustomerCreateParams({\n\t\t\t\t\t\t\t\tuser,\n\t\t\t\t\t\t\t\tsession: ctx.context.session.session,\n\t\t\t\t\t\t\t})\n\t\t\t\t\t\t: {};\n\n\t\t\t\tconst { result: existingCustomers } =\n\t\t\t\t\tawait options.client.customers.list({ email: user.email });\n\t\t\t\tconst existingCustomer = existingCustomers.items[0];\n\n\t\t\t\tif (existingCustomer) {\n\t\t\t\t\tif (existingCustomer.externalId !== user.id) {\n\t\t\t\t\t\tawait options.client.customers.update({\n\t\t\t\t\t\t\tid: existingCustomer.id,\n\t\t\t\t\t\t\tcustomerUpdate: {\n\t\t\t\t\t\t\t\texternalId: user.id,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tawait options.client.customers.create({\n\t\t\t\t\t\t...params,\n\t\t\t\t\t\temail: user.email,\n\t\t\t\t\t\tname: user.name,\n\t\t\t\t\t\texternalId: user.id,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar customer creation failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar customer creation failed. Error: ${e}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t};\n\nexport const onUserUpdate =\n\t(options: PolarOptions) =>\n\tasync (user: User, ctx?: GenericEndpointContext) => {\n\t\tif (ctx && options.createCustomerOnSignUp) {\n\t\t\ttry {\n\t\t\t\tawait options.client.customers.updateExternal({\n\t\t\t\t\texternalId: user.id,\n\t\t\t\t\tcustomerUpdate: {\n\t\t\t\t\t\temail: user.email,\n\t\t\t\t\t\tname: user.name,\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar customer update failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\tctx.context.logger.error(`Polar customer update failed. Error: ${e}`);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,iBAA4C;AAC5C,qBAAmC;AACnC,iBAAkB;AAGX,IAAM,WAAW,CAAC,gBACxB;AAAA,EACC;AAAA,EACA;AAAA,IACC,QAAQ;AAAA,IACR,OAAO,aAAE,OAAO;AAAA,MACf,WAAW,aAAE,OAAO;AAAA,IACrB,CAAC;AAAA,EACF;AAAA,EACA,OAAO,QAAQ;AACd,QAAI,CAAC,QAAQ,UAAU,SAAS;AAC/B,YAAM,IAAI,oBAAS,eAAe;AAAA,QACjC,SAAS;AAAA,MACV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AO;AAAA,MAC/B,uBAAuB,iBAAiB,OAAO;AAAA,MAC/C,eAAe,SAAS,OAAO;AAAA,MAC/B,qBAAqB,eAAe,OAAO;AAAA,MAC3C,oBAAoB,cAAc,OAAO;AAAA,IAC1C;AAAA,IACA,OAAO;AACN,aAAO;AAAA,QACN,SAAS;AAAA,UACR,eAAe;AAAA,YACd,MAAM;AAAA,cACL,QAAQ;AAAA,gBACP,OAAO,aAAa,OAAO;AAAA,cAC5B;AAAA,cACA,QAAQ;AAAA,gBACP,OAAO,aAAa,OAAO;AAAA,cAC5B;AAAA,YACD;AAAA,UACD;AAAA,QACD;AAAA,MACD;AAAA,IACD;AAAA,EACD;AACD;","names":["checkout","import_api","import_plugins","import_api","import_plugins","import_api","import_plugins","webhooks"]}
package/dist/index.d.cts CHANGED
@@ -198,13 +198,47 @@ declare const polar: <O extends PolarOptions>(options: O) => {
198
198
  polarCheckout: {
199
199
  <C extends [{
200
200
  query: {
201
- productId?: string | undefined;
201
+ productId: string;
202
202
  };
203
+ body?: undefined;
204
+ method?: "GET" | undefined;
205
+ params?: Record<string, any> | undefined;
206
+ request?: Request | undefined;
207
+ headers?: HeadersInit | undefined;
208
+ asResponse?: boolean | undefined;
209
+ returnHeaders?: boolean | undefined;
210
+ use?: better_auth.Middleware[] | undefined;
211
+ path?: string | undefined;
212
+ }]>(...inputCtx: C): Promise<C extends [{
213
+ asResponse: true;
214
+ }] ? Response : C extends [{
215
+ returnHeaders: true;
216
+ }] ? {
217
+ headers: Headers;
218
+ response: better_auth.APIError;
219
+ } : better_auth.APIError>;
220
+ options: {
221
+ method: "GET";
222
+ query: zod.ZodObject<{
223
+ productId: zod.ZodString;
224
+ }, "strip", zod.ZodTypeAny, {
225
+ productId: string;
226
+ }, {
227
+ productId: string;
228
+ }>;
229
+ } & {
230
+ use: any[];
231
+ };
232
+ path: "/checkout";
233
+ };
234
+ polarCheckoutWithSlug: {
235
+ <C extends [{
203
236
  params: {
204
237
  slug: string;
205
238
  };
206
239
  body?: undefined;
207
240
  method?: "GET" | undefined;
241
+ query?: Record<string, any> | undefined;
208
242
  request?: Request | undefined;
209
243
  headers?: HeadersInit | undefined;
210
244
  asResponse?: boolean | undefined;
@@ -221,12 +255,12 @@ declare const polar: <O extends PolarOptions>(options: O) => {
221
255
  } : better_auth.APIError>;
222
256
  options: {
223
257
  method: "GET";
224
- query: zod.ZodObject<{
225
- productId: zod.ZodOptional<zod.ZodString>;
258
+ params: zod.ZodObject<{
259
+ slug: zod.ZodString;
226
260
  }, "strip", zod.ZodTypeAny, {
227
- productId?: string | undefined;
261
+ slug: string;
228
262
  }, {
229
- productId?: string | undefined;
263
+ slug: string;
230
264
  }>;
231
265
  } & {
232
266
  use: any[];
package/dist/index.d.ts CHANGED
@@ -198,13 +198,47 @@ declare const polar: <O extends PolarOptions>(options: O) => {
198
198
  polarCheckout: {
199
199
  <C extends [{
200
200
  query: {
201
- productId?: string | undefined;
201
+ productId: string;
202
202
  };
203
+ body?: undefined;
204
+ method?: "GET" | undefined;
205
+ params?: Record<string, any> | undefined;
206
+ request?: Request | undefined;
207
+ headers?: HeadersInit | undefined;
208
+ asResponse?: boolean | undefined;
209
+ returnHeaders?: boolean | undefined;
210
+ use?: better_auth.Middleware[] | undefined;
211
+ path?: string | undefined;
212
+ }]>(...inputCtx: C): Promise<C extends [{
213
+ asResponse: true;
214
+ }] ? Response : C extends [{
215
+ returnHeaders: true;
216
+ }] ? {
217
+ headers: Headers;
218
+ response: better_auth.APIError;
219
+ } : better_auth.APIError>;
220
+ options: {
221
+ method: "GET";
222
+ query: zod.ZodObject<{
223
+ productId: zod.ZodString;
224
+ }, "strip", zod.ZodTypeAny, {
225
+ productId: string;
226
+ }, {
227
+ productId: string;
228
+ }>;
229
+ } & {
230
+ use: any[];
231
+ };
232
+ path: "/checkout";
233
+ };
234
+ polarCheckoutWithSlug: {
235
+ <C extends [{
203
236
  params: {
204
237
  slug: string;
205
238
  };
206
239
  body?: undefined;
207
240
  method?: "GET" | undefined;
241
+ query?: Record<string, any> | undefined;
208
242
  request?: Request | undefined;
209
243
  headers?: HeadersInit | undefined;
210
244
  asResponse?: boolean | undefined;
@@ -221,12 +255,12 @@ declare const polar: <O extends PolarOptions>(options: O) => {
221
255
  } : better_auth.APIError>;
222
256
  options: {
223
257
  method: "GET";
224
- query: zod.ZodObject<{
225
- productId: zod.ZodOptional<zod.ZodString>;
258
+ params: zod.ZodObject<{
259
+ slug: zod.ZodString;
226
260
  }, "strip", zod.ZodTypeAny, {
227
- productId?: string | undefined;
261
+ slug: string;
228
262
  }, {
229
- productId?: string | undefined;
263
+ slug: string;
230
264
  }>;
231
265
  } & {
232
266
  use: any[];
package/dist/index.js CHANGED
@@ -3,11 +3,11 @@ import { APIError, getSessionFromCtx } from "better-auth/api";
3
3
  import { createAuthEndpoint } from "better-auth/plugins";
4
4
  import { z } from "zod";
5
5
  var checkout = (options) => createAuthEndpoint(
6
- "/checkout/:slug",
6
+ "/checkout",
7
7
  {
8
8
  method: "GET",
9
9
  query: z.object({
10
- productId: z.string().optional()
10
+ productId: z.string()
11
11
  })
12
12
  },
13
13
  async (ctx) => {
@@ -16,13 +16,50 @@ var checkout = (options) => createAuthEndpoint(
16
16
  message: "Checkout is not enabled"
17
17
  });
18
18
  }
19
- let productId = ctx.query?.productId;
20
- if (ctx.params.slug) {
21
- const products = await (typeof options.checkout.products === "function" ? options.checkout.products() : options.checkout.products);
22
- productId = products.find(
23
- (product) => product.slug === ctx.params.slug
24
- )?.productId;
19
+ const productId = ctx.query?.productId;
20
+ if (!productId) {
21
+ throw new APIError("BAD_REQUEST", {
22
+ message: "Product Id not found"
23
+ });
24
+ }
25
+ const session = await getSessionFromCtx(ctx);
26
+ try {
27
+ const checkout2 = await options.client.checkouts.create({
28
+ customerExternalId: session?.user.id,
29
+ productId,
30
+ successUrl: options.checkout.successUrl ? new URL(options.checkout.successUrl, ctx.request?.url).toString() : void 0
31
+ });
32
+ return ctx.redirect(checkout2.url);
33
+ } catch (e) {
34
+ if (e instanceof Error) {
35
+ ctx.context.logger.error(
36
+ `Polar checkout creation failed. Error: ${e.message}`
37
+ );
38
+ }
39
+ throw new APIError("INTERNAL_SERVER_ERROR", {
40
+ message: "Checkout creation failed"
41
+ });
42
+ }
43
+ }
44
+ );
45
+ var checkoutWithSlug = (options) => createAuthEndpoint(
46
+ "/checkout/:slug",
47
+ {
48
+ method: "GET",
49
+ params: z.object({
50
+ slug: z.string()
51
+ })
52
+ },
53
+ async (ctx) => {
54
+ if (!options.checkout?.enabled) {
55
+ throw new APIError("BAD_REQUEST", {
56
+ message: "Checkout is not enabled"
57
+ });
25
58
  }
59
+ const products = await (typeof options.checkout.products === "function" ? options.checkout.products() : options.checkout.products);
60
+ const productId = products.find(
61
+ (product) => product.slug === ctx.params?.["slug"]
62
+ )?.productId;
26
63
  if (!productId) {
27
64
  throw new APIError("BAD_REQUEST", {
28
65
  message: "Product Id not found"
@@ -408,6 +445,7 @@ var polar = (options) => {
408
445
  id: "polar",
409
446
  endpoints: {
410
447
  polarCheckout: checkout(options),
448
+ polarCheckoutWithSlug: checkoutWithSlug(options),
411
449
  polarWebhooks: webhooks(options),
412
450
  polarCustomerPortal: customerPortal(options),
413
451
  polarCustomerState: customerState(options)
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/endpoints/checkout.ts","../src/endpoints/customerPortal.ts","../src/endpoints/customerState.ts","../src/endpoints/webhooks.ts","../src/hooks/customer.ts","../src/index.ts"],"sourcesContent":["import { APIError, getSessionFromCtx } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport { z } from \"zod\";\nimport type { PolarOptions } from \"../types\";\n\nexport const checkout = (options: PolarOptions) =>\n\tcreateAuthEndpoint(\n\t\t\"/checkout/:slug\",\n\t\t{\n\t\t\tmethod: \"GET\",\n\t\t\tquery: z.object({\n\t\t\t\tproductId: z.string().optional(),\n\t\t\t}),\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tif (!options.checkout?.enabled) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"Checkout is not enabled\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tlet productId = ctx.query?.productId;\n\n\t\t\tif (ctx.params.slug) {\n\t\t\t\tconst products = await (typeof options.checkout.products === \"function\"\n\t\t\t\t\t? options.checkout.products()\n\t\t\t\t\t: options.checkout.products);\n\n\t\t\t\tproductId = products.find(\n\t\t\t\t\t(product) => product.slug === ctx.params.slug,\n\t\t\t\t)?.productId;\n\t\t\t}\n\n\t\t\tif (!productId) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"Product Id not found\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tconst session = await getSessionFromCtx(ctx);\n\n\t\t\ttry {\n\t\t\t\tconst checkout = await options.client.checkouts.create({\n\t\t\t\t\tcustomerExternalId: session?.user.id,\n\t\t\t\t\tproductId,\n\t\t\t\t\tsuccessUrl: options.checkout.successUrl\n\t\t\t\t\t\t? new URL(options.checkout.successUrl, ctx.request?.url).toString()\n\t\t\t\t\t\t: undefined,\n\t\t\t\t});\n\n\t\t\t\treturn ctx.redirect(checkout.url);\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar checkout creation failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\tmessage: \"Checkout creation failed\",\n\t\t\t\t});\n\t\t\t}\n\t\t},\n\t);\n","import { APIError, sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport type { PolarOptions } from \"../types\";\n\nexport const customerPortal = (options: PolarOptions) =>\n\tcreateAuthEndpoint(\n\t\t\"/portal\",\n\t\t{\n\t\t\tmethod: \"GET\",\n\t\t\tuse: [sessionMiddleware],\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tif (!options.enableCustomerPortal) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"Customer portal is not enabled\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tif (!ctx.context.session?.user.id) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\ttry {\n\t\t\t\tconst customerSession = await options.client.customerSessions.create({\n\t\t\t\t\tcustomerExternalId: ctx.context.session?.user.id,\n\t\t\t\t});\n\n\t\t\t\treturn ctx.redirect(customerSession.customerPortalUrl);\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar customer portal creation failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\tmessage: \"Customer portal creation failed\",\n\t\t\t\t});\n\t\t\t}\n\t\t},\n\t);\n","import { APIError, sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport type { PolarOptions } from \"../types\";\n\nexport const customerState = (options: PolarOptions) =>\n\tcreateAuthEndpoint(\n\t\t\"/state\",\n\t\t{\n\t\t\tmethod: \"GET\",\n\t\t\tuse: [sessionMiddleware],\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\ttry {\n\t\t\t\tconst state = await options.client.customers.getStateExternal({\n\t\t\t\t\texternalId: ctx.context.session?.user.id,\n\t\t\t\t});\n\n\t\t\t\treturn ctx.json(state);\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar subscriptions list failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\tmessage: \"Subscriptions list failed\",\n\t\t\t\t});\n\t\t\t}\n\t\t},\n\t);\n","import { validateEvent } from \"@polar-sh/sdk/webhooks.js\";\nimport { APIError } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport type { PolarOptions } from \"../types\";\n\nexport const webhooks = (options: PolarOptions) =>\n\tcreateAuthEndpoint(\n\t\t\"/polar/webhooks\",\n\t\t{\n\t\t\tmethod: \"POST\",\n\t\t\tmetadata: {\n\t\t\t\tisAction: false,\n\t\t\t},\n\t\t\tcloneRequest: true,\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tconst { webhooks } = options;\n\n\t\t\tif (!webhooks) {\n\t\t\t\tthrow new APIError(\"NOT_FOUND\", {\n\t\t\t\t\tmessage: \"Webhooks not enabled\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tconst {\n\t\t\t\tsecret,\n\t\t\t\tonPayload,\n\t\t\t\tonCheckoutCreated,\n\t\t\t\tonCheckoutUpdated,\n\t\t\t\tonOrderCreated,\n\t\t\t\tonOrderRefunded,\n\t\t\t\tonRefundCreated,\n\t\t\t\tonRefundUpdated,\n\t\t\t\tonSubscriptionCreated,\n\t\t\t\tonSubscriptionUpdated,\n\t\t\t\tonSubscriptionActive,\n\t\t\t\tonSubscriptionCanceled,\n\t\t\t\tonSubscriptionRevoked,\n\t\t\t\tonSubscriptionUncanceled,\n\t\t\t\tonProductCreated,\n\t\t\t\tonProductUpdated,\n\t\t\t\tonOrganizationUpdated,\n\t\t\t\tonBenefitCreated,\n\t\t\t\tonBenefitUpdated,\n\t\t\t\tonBenefitGrantCreated,\n\t\t\t\tonBenefitGrantUpdated,\n\t\t\t\tonBenefitGrantRevoked,\n\t\t\t\tonCustomerCreated,\n\t\t\t\tonCustomerUpdated,\n\t\t\t\tonCustomerDeleted,\n\t\t\t\tonCustomerStateChanged,\n\t\t\t} = webhooks;\n\n\t\t\tif (!ctx.request?.body) {\n\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\");\n\t\t\t}\n\t\t\tconst buf = await ctx.request.text();\n\t\t\tlet event: ReturnType<typeof validateEvent>;\n\t\t\ttry {\n\t\t\t\tif (!secret) {\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: \"Polar webhook secret not found\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\tconst headers = {\n\t\t\t\t\t\"webhook-id\": ctx.request.headers.get(\"webhook-id\") as string,\n\t\t\t\t\t\"webhook-timestamp\": ctx.request.headers.get(\n\t\t\t\t\t\t\"webhook-timestamp\",\n\t\t\t\t\t) as string,\n\t\t\t\t\t\"webhook-signature\": ctx.request.headers.get(\n\t\t\t\t\t\t\"webhook-signature\",\n\t\t\t\t\t) as string,\n\t\t\t\t};\n\n\t\t\t\tevent = validateEvent(buf, headers, secret);\n\t\t\t} catch (err: unknown) {\n\t\t\t\tif (err instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(`${err.message}`);\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: `Webhook Error: ${err.message}`,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: `Webhook Error: ${err}`,\n\t\t\t\t});\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tif (onPayload) {\n\t\t\t\t\tonPayload(event);\n\t\t\t\t}\n\n\t\t\t\tswitch (event.type) {\n\t\t\t\t\tcase \"checkout.created\":\n\t\t\t\t\t\tif (onCheckoutCreated) {\n\t\t\t\t\t\t\tonCheckoutCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"checkout.updated\":\n\t\t\t\t\t\tif (onCheckoutUpdated) {\n\t\t\t\t\t\t\tonCheckoutUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"order.created\":\n\t\t\t\t\t\tif (onOrderCreated) {\n\t\t\t\t\t\t\tonOrderCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.created\":\n\t\t\t\t\t\tif (onSubscriptionCreated) {\n\t\t\t\t\t\t\tonSubscriptionCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.updated\":\n\t\t\t\t\t\tif (onSubscriptionUpdated) {\n\t\t\t\t\t\t\tonSubscriptionUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.active\":\n\t\t\t\t\t\tif (onSubscriptionActive) {\n\t\t\t\t\t\t\tonSubscriptionActive(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.canceled\":\n\t\t\t\t\t\tif (onSubscriptionCanceled) {\n\t\t\t\t\t\t\tonSubscriptionCanceled(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.uncanceled\":\n\t\t\t\t\t\tif (onSubscriptionUncanceled) {\n\t\t\t\t\t\t\tonSubscriptionUncanceled(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.revoked\":\n\t\t\t\t\t\tif (onSubscriptionRevoked) {\n\t\t\t\t\t\t\tonSubscriptionRevoked(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"product.created\":\n\t\t\t\t\t\tif (onProductCreated) {\n\t\t\t\t\t\t\tonProductCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"product.updated\":\n\t\t\t\t\t\tif (onProductUpdated) {\n\t\t\t\t\t\t\tonProductUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"organization.updated\":\n\t\t\t\t\t\tif (onOrganizationUpdated) {\n\t\t\t\t\t\t\tonOrganizationUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"benefit.created\":\n\t\t\t\t\t\tif (onBenefitCreated) {\n\t\t\t\t\t\t\tonBenefitCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"benefit.updated\":\n\t\t\t\t\t\tif (onBenefitUpdated) {\n\t\t\t\t\t\t\tonBenefitUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"benefit_grant.created\":\n\t\t\t\t\t\tif (onBenefitGrantCreated) {\n\t\t\t\t\t\t\tonBenefitGrantCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"benefit_grant.updated\":\n\t\t\t\t\t\tif (onBenefitGrantUpdated) {\n\t\t\t\t\t\t\tonBenefitGrantUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"benefit_grant.revoked\":\n\t\t\t\t\t\tif (onBenefitGrantRevoked) {\n\t\t\t\t\t\t\tonBenefitGrantRevoked(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"order.refunded\":\n\t\t\t\t\t\tif (onOrderRefunded) {\n\t\t\t\t\t\t\tonOrderRefunded(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"refund.created\":\n\t\t\t\t\t\tif (onRefundCreated) {\n\t\t\t\t\t\t\tonRefundCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"refund.updated\":\n\t\t\t\t\t\tif (onRefundUpdated) {\n\t\t\t\t\t\t\tonRefundUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"customer.created\":\n\t\t\t\t\t\tif (onCustomerCreated) {\n\t\t\t\t\t\t\tonCustomerCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"customer.updated\":\n\t\t\t\t\t\tif (onCustomerUpdated) {\n\t\t\t\t\t\t\tonCustomerUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"customer.deleted\":\n\t\t\t\t\t\tif (onCustomerDeleted) {\n\t\t\t\t\t\t\tonCustomerDeleted(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"customer.state_changed\":\n\t\t\t\t\t\tif (onCustomerStateChanged) {\n\t\t\t\t\t\t\tonCustomerStateChanged(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(`Polar webhook failed. Error: ${e.message}`);\n\t\t\t\t} else {\n\t\t\t\t\tctx.context.logger.error(`Polar webhook failed. Error: ${e}`);\n\t\t\t\t}\n\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"Webhook error: See server logs for more information.\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\treturn ctx.json({ received: true });\n\t\t},\n\t);\n","import type { GenericEndpointContext, User } from \"better-auth\";\nimport type { PolarOptions } from \"../types\";\n\nexport const onUserCreate =\n\t(options: PolarOptions) =>\n\tasync (user: User, ctx?: GenericEndpointContext) => {\n\t\tif (ctx && options.createCustomerOnSignUp) {\n\t\t\ttry {\n\t\t\t\tconst params =\n\t\t\t\t\toptions.getCustomerCreateParams && ctx.context.session\n\t\t\t\t\t\t? await options.getCustomerCreateParams({\n\t\t\t\t\t\t\t\tuser,\n\t\t\t\t\t\t\t\tsession: ctx.context.session.session,\n\t\t\t\t\t\t\t})\n\t\t\t\t\t\t: {};\n\n\t\t\t\tconst { result: existingCustomers } =\n\t\t\t\t\tawait options.client.customers.list({ email: user.email });\n\t\t\t\tconst existingCustomer = existingCustomers.items[0];\n\n\t\t\t\tif (existingCustomer) {\n\t\t\t\t\tif (existingCustomer.externalId !== user.id) {\n\t\t\t\t\t\tawait options.client.customers.update({\n\t\t\t\t\t\t\tid: existingCustomer.id,\n\t\t\t\t\t\t\tcustomerUpdate: {\n\t\t\t\t\t\t\t\texternalId: user.id,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tawait options.client.customers.create({\n\t\t\t\t\t\t...params,\n\t\t\t\t\t\temail: user.email,\n\t\t\t\t\t\tname: user.name,\n\t\t\t\t\t\texternalId: user.id,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar customer creation failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar customer creation failed. Error: ${e}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t};\n\nexport const onUserUpdate =\n\t(options: PolarOptions) =>\n\tasync (user: User, ctx?: GenericEndpointContext) => {\n\t\tif (ctx && options.createCustomerOnSignUp) {\n\t\t\ttry {\n\t\t\t\tawait options.client.customers.updateExternal({\n\t\t\t\t\texternalId: user.id,\n\t\t\t\t\tcustomerUpdate: {\n\t\t\t\t\t\temail: user.email,\n\t\t\t\t\t\tname: user.name,\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar customer update failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\tctx.context.logger.error(`Polar customer update failed. Error: ${e}`);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t};\n","import type { BetterAuthPlugin } from \"better-auth\";\nimport { checkout } from \"./endpoints/checkout\";\nimport { customerPortal } from \"./endpoints/customerPortal\";\nimport { customerState } from \"./endpoints/customerState\";\nimport { webhooks } from \"./endpoints/webhooks\";\nimport { onUserCreate, onUserUpdate } from \"./hooks/customer\";\nimport type { PolarOptions } from \"./types\";\n\nexport const polar = <O extends PolarOptions>(options: O) => {\n\treturn {\n\t\tid: \"polar\",\n\t\tendpoints: {\n\t\t\tpolarCheckout: checkout(options),\n\t\t\tpolarWebhooks: webhooks(options),\n\t\t\tpolarCustomerPortal: customerPortal(options),\n\t\t\tpolarCustomerState: customerState(options),\n\t\t},\n\t\tinit() {\n\t\t\treturn {\n\t\t\t\toptions: {\n\t\t\t\t\tdatabaseHooks: {\n\t\t\t\t\t\tuser: {\n\t\t\t\t\t\t\tcreate: {\n\t\t\t\t\t\t\t\tafter: onUserCreate(options),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\tupdate: {\n\t\t\t\t\t\t\t\tafter: onUserUpdate(options),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t};\n\t\t},\n\t} satisfies BetterAuthPlugin;\n};\n"],"mappings":";AAAA,SAAS,UAAU,yBAAyB;AAC5C,SAAS,0BAA0B;AACnC,SAAS,SAAS;AAGX,IAAM,WAAW,CAAC,YACxB;AAAA,EACC;AAAA,EACA;AAAA,IACC,QAAQ;AAAA,IACR,OAAO,EAAE,OAAO;AAAA,MACf,WAAW,EAAE,OAAO,EAAE,SAAS;AAAA,IAChC,CAAC;AAAA,EACF;AAAA,EACA,OAAO,QAAQ;AACd,QAAI,CAAC,QAAQ,UAAU,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U,eAAe;AAAA,QAC7C,YAAY,KAAK;AAAA,QACjB,gBAAgB;AAAA,UACf,OAAO,KAAK;AAAA,UACZ,MAAM,KAAK;AAAA,QACZ;AAAA,MACD,CAAC;AAAA,IACF,SAAS,GAAY;AACpB,UAAI,aAAa,OAAO;AACvB,YAAI,QAAQ,OAAO;AAAA,UAClB,wCAAwC,EAAE,OAAO;AAAA,QAClD;AAAA,MACD,OAAO;AACN,YAAI,QAAQ,OAAO,MAAM,wCAAwC,CAAC,EAAE;AAAA,MACrE;AAAA,IACD;AAAA,EACD;AACD;;;ACjEM,IAAM,QAAQ,CAAyB,YAAe;AAC5D,SAAO;AAAA,IACN,IAAI;AAAA,IACJ,WAAW;AAAA,MACV,eAAe,SAAS,OAAO;AAAA,MAC/B,eAAe,SAAS,OAAO;AAAA,MAC/B,qBAAqB,eAAe,OAAO;AAAA,MAC3C,oBAAoB,cAAc,OAAO;AAAA,IAC1C;AAAA,IACA,OAAO;AACN,aAAO;AAAA,QACN,SAAS;AAAA,UACR,eAAe;AAAA,YACd,MAAM;AAAA,cACL,QAAQ;AAAA,gBACP,OAAO,aAAa,OAAO;AAAA,cAC5B;AAAA,cACA,QAAQ;AAAA,gBACP,OAAO,aAAa,OAAO;AAAA,cAC5B;AAAA,YACD;AAAA,UACD;AAAA,QACD;AAAA,MACD;AAAA,IACD;AAAA,EACD;AACD;","names":["checkout","APIError","createAuthEndpoint","APIError","sessionMiddleware","createAuthEndpoint","APIError","createAuthEndpoint","webhooks"]}
1
+ {"version":3,"sources":["../src/endpoints/checkout.ts","../src/endpoints/customerPortal.ts","../src/endpoints/customerState.ts","../src/endpoints/webhooks.ts","../src/hooks/customer.ts","../src/index.ts"],"sourcesContent":["import { APIError, getSessionFromCtx } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport { z } from \"zod\";\nimport type { PolarOptions } from \"../types\";\n\nexport const checkout = (options: PolarOptions) =>\n\tcreateAuthEndpoint(\n\t\t\"/checkout\",\n\t\t{\n\t\t\tmethod: \"GET\",\n\t\t\tquery: z.object({\n\t\t\t\tproductId: z.string(),\n\t\t\t}),\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tif (!options.checkout?.enabled) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"Checkout is not enabled\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tconst productId = ctx.query?.productId;\n\n\t\t\tif (!productId) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"Product Id not found\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tconst session = await getSessionFromCtx(ctx);\n\n\t\t\ttry {\n\t\t\t\tconst checkout = await options.client.checkouts.create({\n\t\t\t\t\tcustomerExternalId: session?.user.id,\n\t\t\t\t\tproductId,\n\t\t\t\t\tsuccessUrl: options.checkout.successUrl\n\t\t\t\t\t\t? new URL(options.checkout.successUrl, ctx.request?.url).toString()\n\t\t\t\t\t\t: undefined,\n\t\t\t\t});\n\n\t\t\t\treturn ctx.redirect(checkout.url);\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar checkout creation failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\tmessage: \"Checkout creation failed\",\n\t\t\t\t});\n\t\t\t}\n\t\t},\n\t);\n\nexport const checkoutWithSlug = (options: PolarOptions) =>\n\tcreateAuthEndpoint(\n\t\t\"/checkout/:slug\",\n\t\t{\n\t\t\tmethod: \"GET\",\n\t\t\tparams: z.object({\n\t\t\t\tslug: z.string(),\n\t\t\t}),\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tif (!options.checkout?.enabled) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"Checkout is not enabled\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tconst products = await (typeof options.checkout.products === \"function\"\n\t\t\t\t? options.checkout.products()\n\t\t\t\t: options.checkout.products);\n\n\t\t\tconst productId = products.find(\n\t\t\t\t(product) => product.slug === ctx.params?.[\"slug\"],\n\t\t\t)?.productId;\n\n\t\t\tif (!productId) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"Product Id not found\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tconst session = await getSessionFromCtx(ctx);\n\n\t\t\ttry {\n\t\t\t\tconst checkout = await options.client.checkouts.create({\n\t\t\t\t\tcustomerExternalId: session?.user.id,\n\t\t\t\t\tproductId,\n\t\t\t\t\tsuccessUrl: options.checkout.successUrl\n\t\t\t\t\t\t? new URL(options.checkout.successUrl, ctx.request?.url).toString()\n\t\t\t\t\t\t: undefined,\n\t\t\t\t});\n\n\t\t\t\treturn ctx.redirect(checkout.url);\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar checkout creation failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\tmessage: \"Checkout creation failed\",\n\t\t\t\t});\n\t\t\t}\n\t\t},\n\t);\n","import { APIError, sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport type { PolarOptions } from \"../types\";\n\nexport const customerPortal = (options: PolarOptions) =>\n\tcreateAuthEndpoint(\n\t\t\"/portal\",\n\t\t{\n\t\t\tmethod: \"GET\",\n\t\t\tuse: [sessionMiddleware],\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tif (!options.enableCustomerPortal) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"Customer portal is not enabled\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tif (!ctx.context.session?.user.id) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\ttry {\n\t\t\t\tconst customerSession = await options.client.customerSessions.create({\n\t\t\t\t\tcustomerExternalId: ctx.context.session?.user.id,\n\t\t\t\t});\n\n\t\t\t\treturn ctx.redirect(customerSession.customerPortalUrl);\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar customer portal creation failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\tmessage: \"Customer portal creation failed\",\n\t\t\t\t});\n\t\t\t}\n\t\t},\n\t);\n","import { APIError, sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport type { PolarOptions } from \"../types\";\n\nexport const customerState = (options: PolarOptions) =>\n\tcreateAuthEndpoint(\n\t\t\"/state\",\n\t\t{\n\t\t\tmethod: \"GET\",\n\t\t\tuse: [sessionMiddleware],\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\ttry {\n\t\t\t\tconst state = await options.client.customers.getStateExternal({\n\t\t\t\t\texternalId: ctx.context.session?.user.id,\n\t\t\t\t});\n\n\t\t\t\treturn ctx.json(state);\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar subscriptions list failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\tmessage: \"Subscriptions list failed\",\n\t\t\t\t});\n\t\t\t}\n\t\t},\n\t);\n","import { validateEvent } from \"@polar-sh/sdk/webhooks.js\";\nimport { APIError } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport type { PolarOptions } from \"../types\";\n\nexport const webhooks = (options: PolarOptions) =>\n\tcreateAuthEndpoint(\n\t\t\"/polar/webhooks\",\n\t\t{\n\t\t\tmethod: \"POST\",\n\t\t\tmetadata: {\n\t\t\t\tisAction: false,\n\t\t\t},\n\t\t\tcloneRequest: true,\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tconst { webhooks } = options;\n\n\t\t\tif (!webhooks) {\n\t\t\t\tthrow new APIError(\"NOT_FOUND\", {\n\t\t\t\t\tmessage: \"Webhooks not enabled\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tconst {\n\t\t\t\tsecret,\n\t\t\t\tonPayload,\n\t\t\t\tonCheckoutCreated,\n\t\t\t\tonCheckoutUpdated,\n\t\t\t\tonOrderCreated,\n\t\t\t\tonOrderRefunded,\n\t\t\t\tonRefundCreated,\n\t\t\t\tonRefundUpdated,\n\t\t\t\tonSubscriptionCreated,\n\t\t\t\tonSubscriptionUpdated,\n\t\t\t\tonSubscriptionActive,\n\t\t\t\tonSubscriptionCanceled,\n\t\t\t\tonSubscriptionRevoked,\n\t\t\t\tonSubscriptionUncanceled,\n\t\t\t\tonProductCreated,\n\t\t\t\tonProductUpdated,\n\t\t\t\tonOrganizationUpdated,\n\t\t\t\tonBenefitCreated,\n\t\t\t\tonBenefitUpdated,\n\t\t\t\tonBenefitGrantCreated,\n\t\t\t\tonBenefitGrantUpdated,\n\t\t\t\tonBenefitGrantRevoked,\n\t\t\t\tonCustomerCreated,\n\t\t\t\tonCustomerUpdated,\n\t\t\t\tonCustomerDeleted,\n\t\t\t\tonCustomerStateChanged,\n\t\t\t} = webhooks;\n\n\t\t\tif (!ctx.request?.body) {\n\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\");\n\t\t\t}\n\t\t\tconst buf = await ctx.request.text();\n\t\t\tlet event: ReturnType<typeof validateEvent>;\n\t\t\ttry {\n\t\t\t\tif (!secret) {\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: \"Polar webhook secret not found\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\tconst headers = {\n\t\t\t\t\t\"webhook-id\": ctx.request.headers.get(\"webhook-id\") as string,\n\t\t\t\t\t\"webhook-timestamp\": ctx.request.headers.get(\n\t\t\t\t\t\t\"webhook-timestamp\",\n\t\t\t\t\t) as string,\n\t\t\t\t\t\"webhook-signature\": ctx.request.headers.get(\n\t\t\t\t\t\t\"webhook-signature\",\n\t\t\t\t\t) as string,\n\t\t\t\t};\n\n\t\t\t\tevent = validateEvent(buf, headers, secret);\n\t\t\t} catch (err: unknown) {\n\t\t\t\tif (err instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(`${err.message}`);\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: `Webhook Error: ${err.message}`,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: `Webhook Error: ${err}`,\n\t\t\t\t});\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tif (onPayload) {\n\t\t\t\t\tonPayload(event);\n\t\t\t\t}\n\n\t\t\t\tswitch (event.type) {\n\t\t\t\t\tcase \"checkout.created\":\n\t\t\t\t\t\tif (onCheckoutCreated) {\n\t\t\t\t\t\t\tonCheckoutCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"checkout.updated\":\n\t\t\t\t\t\tif (onCheckoutUpdated) {\n\t\t\t\t\t\t\tonCheckoutUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"order.created\":\n\t\t\t\t\t\tif (onOrderCreated) {\n\t\t\t\t\t\t\tonOrderCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.created\":\n\t\t\t\t\t\tif (onSubscriptionCreated) {\n\t\t\t\t\t\t\tonSubscriptionCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.updated\":\n\t\t\t\t\t\tif (onSubscriptionUpdated) {\n\t\t\t\t\t\t\tonSubscriptionUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.active\":\n\t\t\t\t\t\tif (onSubscriptionActive) {\n\t\t\t\t\t\t\tonSubscriptionActive(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.canceled\":\n\t\t\t\t\t\tif (onSubscriptionCanceled) {\n\t\t\t\t\t\t\tonSubscriptionCanceled(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.uncanceled\":\n\t\t\t\t\t\tif (onSubscriptionUncanceled) {\n\t\t\t\t\t\t\tonSubscriptionUncanceled(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.revoked\":\n\t\t\t\t\t\tif (onSubscriptionRevoked) {\n\t\t\t\t\t\t\tonSubscriptionRevoked(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"product.created\":\n\t\t\t\t\t\tif (onProductCreated) {\n\t\t\t\t\t\t\tonProductCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"product.updated\":\n\t\t\t\t\t\tif (onProductUpdated) {\n\t\t\t\t\t\t\tonProductUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"organization.updated\":\n\t\t\t\t\t\tif (onOrganizationUpdated) {\n\t\t\t\t\t\t\tonOrganizationUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"benefit.created\":\n\t\t\t\t\t\tif (onBenefitCreated) {\n\t\t\t\t\t\t\tonBenefitCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"benefit.updated\":\n\t\t\t\t\t\tif (onBenefitUpdated) {\n\t\t\t\t\t\t\tonBenefitUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"benefit_grant.created\":\n\t\t\t\t\t\tif (onBenefitGrantCreated) {\n\t\t\t\t\t\t\tonBenefitGrantCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"benefit_grant.updated\":\n\t\t\t\t\t\tif (onBenefitGrantUpdated) {\n\t\t\t\t\t\t\tonBenefitGrantUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"benefit_grant.revoked\":\n\t\t\t\t\t\tif (onBenefitGrantRevoked) {\n\t\t\t\t\t\t\tonBenefitGrantRevoked(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"order.refunded\":\n\t\t\t\t\t\tif (onOrderRefunded) {\n\t\t\t\t\t\t\tonOrderRefunded(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"refund.created\":\n\t\t\t\t\t\tif (onRefundCreated) {\n\t\t\t\t\t\t\tonRefundCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"refund.updated\":\n\t\t\t\t\t\tif (onRefundUpdated) {\n\t\t\t\t\t\t\tonRefundUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"customer.created\":\n\t\t\t\t\t\tif (onCustomerCreated) {\n\t\t\t\t\t\t\tonCustomerCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"customer.updated\":\n\t\t\t\t\t\tif (onCustomerUpdated) {\n\t\t\t\t\t\t\tonCustomerUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"customer.deleted\":\n\t\t\t\t\t\tif (onCustomerDeleted) {\n\t\t\t\t\t\t\tonCustomerDeleted(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"customer.state_changed\":\n\t\t\t\t\t\tif (onCustomerStateChanged) {\n\t\t\t\t\t\t\tonCustomerStateChanged(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(`Polar webhook failed. Error: ${e.message}`);\n\t\t\t\t} else {\n\t\t\t\t\tctx.context.logger.error(`Polar webhook failed. Error: ${e}`);\n\t\t\t\t}\n\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"Webhook error: See server logs for more information.\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\treturn ctx.json({ received: true });\n\t\t},\n\t);\n","import type { GenericEndpointContext, User } from \"better-auth\";\nimport type { PolarOptions } from \"../types\";\n\nexport const onUserCreate =\n\t(options: PolarOptions) =>\n\tasync (user: User, ctx?: GenericEndpointContext) => {\n\t\tif (ctx && options.createCustomerOnSignUp) {\n\t\t\ttry {\n\t\t\t\tconst params =\n\t\t\t\t\toptions.getCustomerCreateParams && ctx.context.session\n\t\t\t\t\t\t? await options.getCustomerCreateParams({\n\t\t\t\t\t\t\t\tuser,\n\t\t\t\t\t\t\t\tsession: ctx.context.session.session,\n\t\t\t\t\t\t\t})\n\t\t\t\t\t\t: {};\n\n\t\t\t\tconst { result: existingCustomers } =\n\t\t\t\t\tawait options.client.customers.list({ email: user.email });\n\t\t\t\tconst existingCustomer = existingCustomers.items[0];\n\n\t\t\t\tif (existingCustomer) {\n\t\t\t\t\tif (existingCustomer.externalId !== user.id) {\n\t\t\t\t\t\tawait options.client.customers.update({\n\t\t\t\t\t\t\tid: existingCustomer.id,\n\t\t\t\t\t\t\tcustomerUpdate: {\n\t\t\t\t\t\t\t\texternalId: user.id,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tawait options.client.customers.create({\n\t\t\t\t\t\t...params,\n\t\t\t\t\t\temail: user.email,\n\t\t\t\t\t\tname: user.name,\n\t\t\t\t\t\texternalId: user.id,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar customer creation failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar customer creation failed. Error: ${e}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t};\n\nexport const onUserUpdate =\n\t(options: PolarOptions) =>\n\tasync (user: User, ctx?: GenericEndpointContext) => {\n\t\tif (ctx && options.createCustomerOnSignUp) {\n\t\t\ttry {\n\t\t\t\tawait options.client.customers.updateExternal({\n\t\t\t\t\texternalId: user.id,\n\t\t\t\t\tcustomerUpdate: {\n\t\t\t\t\t\temail: user.email,\n\t\t\t\t\t\tname: user.name,\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar customer update failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\tctx.context.logger.error(`Polar customer update failed. Error: ${e}`);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t};\n","import type { BetterAuthPlugin } from \"better-auth\";\nimport { checkout, checkoutWithSlug } from \"./endpoints/checkout\";\nimport { customerPortal } from \"./endpoints/customerPortal\";\nimport { customerState } from \"./endpoints/customerState\";\nimport { webhooks } from \"./endpoints/webhooks\";\nimport { onUserCreate, onUserUpdate } from \"./hooks/customer\";\nimport type { PolarOptions } from \"./types\";\n\nexport const polar = <O extends PolarOptions>(options: O) => {\n\treturn {\n\t\tid: \"polar\",\n\t\tendpoints: {\n\t\t\tpolarCheckout: checkout(options),\n\t\t\tpolarCheckoutWithSlug: checkoutWithSlug(options),\n\t\t\tpolarWebhooks: webhooks(options),\n\t\t\tpolarCustomerPortal: customerPortal(options),\n\t\t\tpolarCustomerState: customerState(options),\n\t\t},\n\t\tinit() {\n\t\t\treturn {\n\t\t\t\toptions: {\n\t\t\t\t\tdatabaseHooks: {\n\t\t\t\t\t\tuser: {\n\t\t\t\t\t\t\tcreate: {\n\t\t\t\t\t\t\t\tafter: onUserCreate(options),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\tupdate: {\n\t\t\t\t\t\t\t\tafter: onUserUpdate(options),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t};\n\t\t},\n\t} satisfies BetterAuthPlugin;\n};\n"],"mappings":";AAAA,SAAS,UAAU,yBAAyB;AAC5C,SAAS,0BAA0B;AACnC,SAAS,SAAS;AAGX,IAAM,WAAW,CAAC,YACxB;AAAA,EACC;AAAA,EACA;AAAA,IACC,QAAQ;AAAA,IACR,OAAO,EAAE,OAAO;AAAA,MACf,WAAW,EAAE,OAAO;AAAA,IACrB,CAAC;AAAA,EACF;AAAA,EACA,OAAO,QAAQ;AACd,QAAI,CAAC,QAAQ,UAAU,SAAS;AAC/B,YAAM,IAAI,SAAS,eAAe;AAAA,QACjC,SAAS;AAAA,MACV,CAAC;AAAA,IACF;AAEA,UAAM,YAAY,IAAI,OAAO;AAE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AS,OAAO;AAAA,MAC/B,uBAAuB,iBAAiB,OAAO;AAAA,MAC/C,eAAe,SAAS,OAAO;AAAA,MAC/B,qBAAqB,eAAe,OAAO;AAAA,MAC3C,oBAAoB,cAAc,OAAO;AAAA,IAC1C;AAAA,IACA,OAAO;AACN,aAAO;AAAA,QACN,SAAS;AAAA,UACR,eAAe;AAAA,YACd,MAAM;AAAA,cACL,QAAQ;AAAA,gBACP,OAAO,aAAa,OAAO;AAAA,cAC5B;AAAA,cACA,QAAQ;AAAA,gBACP,OAAO,aAAa,OAAO;AAAA,cAC5B;AAAA,YACD;AAAA,UACD;AAAA,QACD;AAAA,MACD;AAAA,IACD;AAAA,EACD;AACD;","names":["checkout","APIError","createAuthEndpoint","APIError","sessionMiddleware","createAuthEndpoint","APIError","createAuthEndpoint","webhooks"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polar-sh/better-auth",
3
- "version": "0.0.7",
3
+ "version": "0.0.8",
4
4
  "description": "Polar integration for better-auth",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",