@polar-sh/better-auth 0.0.11 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -33,7 +33,7 @@ var checkout = (options) => (0, import_plugins.createAuthEndpoint)(
33
33
  {
34
34
  method: "GET",
35
35
  query: import_zod.z.object({
36
- productId: import_zod.z.string()
36
+ products: import_zod.z.array(import_zod.z.string())
37
37
  })
38
38
  },
39
39
  async (ctx) => {
@@ -42,17 +42,12 @@ var checkout = (options) => (0, import_plugins.createAuthEndpoint)(
42
42
  message: "Checkout is not enabled"
43
43
  });
44
44
  }
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
- }
45
+ const products = ctx.query.products;
51
46
  const session = await (0, import_api.getSessionFromCtx)(ctx);
52
47
  try {
53
48
  const checkout2 = await options.client.checkouts.create({
54
49
  customerExternalId: session?.user.id,
55
- productId,
50
+ products,
56
51
  successUrl: options.checkout.successUrl ? new URL(options.checkout.successUrl, ctx.request?.url).toString() : void 0
57
52
  });
58
53
  return ctx.redirect(checkout2.url);
@@ -95,7 +90,7 @@ var checkoutWithSlug = (options) => (0, import_plugins.createAuthEndpoint)(
95
90
  try {
96
91
  const checkout2 = await options.client.checkouts.create({
97
92
  customerExternalId: session?.user.id,
98
- productId,
93
+ products: [productId],
99
94
  successUrl: options.checkout.successUrl ? new URL(options.checkout.successUrl, ctx.request?.url).toString() : void 0
100
95
  });
101
96
  return ctx.redirect(checkout2.url);
@@ -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, 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\";\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 { APIError } from \"better-auth/api\";\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\t// Throw a better auth ApiError to ensure the user isn't created\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: `Polar customer 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: `Polar customer creation failed. Error: ${e}`,\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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,OAAO;AACN,YAAI,QAAQ,OAAO,MAAM,wCAAwC,CAAC,EAAE;AAAA,MACrE;AAAA,IACD;AAAA,EACD;AACD;;;ALnEM,IAAM,QAAQ,CAAyB,YAAe;AAC5D,SAAO;AAAA,IACN,IAAI;AAAA,IACJ,WAAW;AAAA,MACV,eAAe,SAAS,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","import_api","import_plugins","import_api","import_plugins","import_api","import_plugins","webhooks","import_api"]}
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\tproducts: z.array(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 = ctx.query.products;\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\tproducts,\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\tproducts: [productId],\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\";\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 { APIError } from \"better-auth/api\";\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\t// Throw a better auth ApiError to ensure the user isn't created\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: `Polar customer 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: `Polar customer creation failed. Error: ${e}`,\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,UAAU,aAAE,MAAM,aAAE,OAAO,CAAC;AAAA,IAC7B,CAAC;AAAA,EACF;AAAA,EACA,OAAO,QAAQ;AACd,QAAI,CAAC,QAAQ,UAAU,SAAS;AAC/B,YAAM,IAAI,oBAAS,eAAe;AAAA,QACjC,SAAS;AAAA,MACV,CAAC;AAAA,IACF;AAEA,UAAM,WAAW,IAAI,MAAM;AAC3B,UAAM,UAAU,UAAM,8BAAkB,GAAG;AAE3C,QAAI;AACH,YAAMA,YAAW,MAAM,QAAQ,OAAO,UAAU,OAAO;AAAA,QACtD,oBAAoB,SAAS,KAAK;AAAA,QAClC;AAAA,QACA,YAAY,QAAQ,SAAS,aAC1B,IAAI,IAAI,QAAQ,S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,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","import_api"]}
package/dist/index.d.cts CHANGED
@@ -198,7 +198,7 @@ declare const polar: <O extends PolarOptions>(options: O) => {
198
198
  polarCheckout: {
199
199
  <C extends [{
200
200
  query: {
201
- productId: string;
201
+ products: string[];
202
202
  };
203
203
  body?: undefined;
204
204
  method?: "GET" | undefined;
@@ -220,11 +220,11 @@ declare const polar: <O extends PolarOptions>(options: O) => {
220
220
  options: {
221
221
  method: "GET";
222
222
  query: zod.ZodObject<{
223
- productId: zod.ZodString;
223
+ products: zod.ZodArray<zod.ZodString, "many">;
224
224
  }, "strip", zod.ZodTypeAny, {
225
- productId: string;
225
+ products: string[];
226
226
  }, {
227
- productId: string;
227
+ products: string[];
228
228
  }>;
229
229
  } & {
230
230
  use: any[];
package/dist/index.d.ts CHANGED
@@ -198,7 +198,7 @@ declare const polar: <O extends PolarOptions>(options: O) => {
198
198
  polarCheckout: {
199
199
  <C extends [{
200
200
  query: {
201
- productId: string;
201
+ products: string[];
202
202
  };
203
203
  body?: undefined;
204
204
  method?: "GET" | undefined;
@@ -220,11 +220,11 @@ declare const polar: <O extends PolarOptions>(options: O) => {
220
220
  options: {
221
221
  method: "GET";
222
222
  query: zod.ZodObject<{
223
- productId: zod.ZodString;
223
+ products: zod.ZodArray<zod.ZodString, "many">;
224
224
  }, "strip", zod.ZodTypeAny, {
225
- productId: string;
225
+ products: string[];
226
226
  }, {
227
- productId: string;
227
+ products: string[];
228
228
  }>;
229
229
  } & {
230
230
  use: any[];
package/dist/index.js CHANGED
@@ -7,7 +7,7 @@ var checkout = (options) => createAuthEndpoint(
7
7
  {
8
8
  method: "GET",
9
9
  query: z.object({
10
- productId: z.string()
10
+ products: z.array(z.string())
11
11
  })
12
12
  },
13
13
  async (ctx) => {
@@ -16,17 +16,12 @@ var checkout = (options) => createAuthEndpoint(
16
16
  message: "Checkout is not enabled"
17
17
  });
18
18
  }
19
- const productId = ctx.query?.productId;
20
- if (!productId) {
21
- throw new APIError("BAD_REQUEST", {
22
- message: "Product Id not found"
23
- });
24
- }
19
+ const products = ctx.query.products;
25
20
  const session = await getSessionFromCtx(ctx);
26
21
  try {
27
22
  const checkout2 = await options.client.checkouts.create({
28
23
  customerExternalId: session?.user.id,
29
- productId,
24
+ products,
30
25
  successUrl: options.checkout.successUrl ? new URL(options.checkout.successUrl, ctx.request?.url).toString() : void 0
31
26
  });
32
27
  return ctx.redirect(checkout2.url);
@@ -69,7 +64,7 @@ var checkoutWithSlug = (options) => createAuthEndpoint(
69
64
  try {
70
65
  const checkout2 = await options.client.checkouts.create({
71
66
  customerExternalId: session?.user.id,
72
- productId,
67
+ products: [productId],
73
68
  successUrl: options.checkout.successUrl ? new URL(options.checkout.successUrl, ctx.request?.url).toString() : void 0
74
69
  });
75
70
  return ctx.redirect(checkout2.url);
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\",\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\";\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 { APIError } from \"better-auth/api\";\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\t// Throw a better auth ApiError to ensure the user isn't created\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: `Polar customer 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: `Polar customer creation failed. Error: ${e}`,\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;AAE7B,QAAI,CAAC,WAAW;AACf,YAAM,IAAI,SAAS,eAAe;AAAA,QACjC,SAAS;AAAA,MACV,CAAC;AAAA,IACF;AAEA,UAAM,UAAU,MAAM,kBAAkB,GAAG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;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","APIError"]}
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\tproducts: z.array(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 = ctx.query.products;\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\tproducts,\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\tproducts: [productId],\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\";\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 { APIError } from \"better-auth/api\";\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\t// Throw a better auth ApiError to ensure the user isn't created\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: `Polar customer 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: `Polar customer creation failed. Error: ${e}`,\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,UAAU,EAAE,MAAM,EAAE,OAAO,CAAC;AAAA,IAC7B,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,WAAW,IAAI,MAAM;AAC3B,UAAM,UAAU,MAAM,kBAAkB,GAAG;AAE3C,QAAI;AACH,YAAMA,YAAW,MAAM,QAAQ,OAAO,UAAU,OAAO;AAAA,QACtD,oBAAoB,SAAS,KAAK;AAAA,QAClC;AAAA,QACA,YAAY,QAAQ,SAAS,aAC1B,IAAI,IAAI,QAAQ,SAAS,YAAY,IAAI,SAAS,GAAG,EAAE,SAAS,IAChE;AAAA,MACJ,CAAC;AAED,aAAO,IAAI,SAASA,UAAS,GAAG;AAAA,IACjC,SAAS,GAAY;AACpB,UAAI,aAAa,OAAO;AACvB,YAAI,QAAQ,OAAO;AAAA,UAClB,0CAA0C,EAAE,OAAO;AAAA,QACpD;AAAA,MACD;AAEA,YAAM,IAAI,SAAS,yBAAyB;AAAA,QAC3C,SAAS;AAAA,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O;AAAA,QAC7D,CAAC;AAAA,MACF;AAEA,YAAM,IAAIA,UAAS,yBAAyB;AAAA,QAC3C,SAAS,0CAA0C,CAAC;AAAA,MACrD,CAAC;AAAA,IACF;AAAA,EACD;AACD;AAEM,IAAM,eACZ,CAAC,YACD,OAAO,MAAY,QAAiC;AACnD,MAAI,OAAO,QAAQ,wBAAwB;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;;;ACnEM,IAAM,QAAQ,CAAyB,YAAe;AAC5D,SAAO;AAAA,IACN,IAAI;AAAA,IACJ,WAAW;AAAA,MACV,eAAe,SAAS,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","APIError"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polar-sh/better-auth",
3
- "version": "0.0.11",
3
+ "version": "0.1.0",
4
4
  "description": "Polar integration for better-auth",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
@@ -30,7 +30,7 @@
30
30
  ],
31
31
  "devDependencies": {
32
32
  "@biomejs/biome": "1.9.4",
33
- "@polar-sh/sdk": "^0.32.0",
33
+ "@polar-sh/sdk": "^0.32.11",
34
34
  "@sindresorhus/tsconfig": "^7.0.0",
35
35
  "@types/node": "^20.0.0",
36
36
  "better-auth": "^1.2.0",
@@ -41,7 +41,7 @@
41
41
  "zod": "^3.24.2"
42
42
  },
43
43
  "peerDependencies": {
44
- "@polar-sh/sdk": "^0.32.0",
44
+ "@polar-sh/sdk": "^0.32.11",
45
45
  "better-auth": "^1.2.0"
46
46
  },
47
47
  "scripts": {