@polar-sh/better-auth 0.0.2 → 0.0.3

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
@@ -116,13 +116,17 @@ The plugin supports handlers for all Polar webhook events:
116
116
  - `onBenefitGrantCreated` - Triggered when a benefit grant is created
117
117
  - `onBenefitGrantUpdated` - Triggered when a benefit grant is updated
118
118
  - `onBenefitGrantRevoked` - Triggered when a benefit grant is revoked
119
+ - `onCustomerCreated` - Triggered when a customer is created
120
+ - `onCustomerUpdated` - Triggered when a customer is updated
121
+ - `onCustomerDeleted` - Triggered when a customer is deleted
122
+ - `onCustomerStateChanged` - Triggered when a customer is created
119
123
 
120
124
  ## API Routes
121
125
 
122
126
  The plugin adds the following API routes:
123
127
 
124
128
  - `GET /checkout/:slug` - Redirect to Polar checkout
125
- - `GET /subscriptions/list` - List active subscriptions for the authenticated user
129
+ - `GET /state` - Customer state (Customer Data, Active Subscriptions, Entitlements, etc.) for the authenticated user
126
130
  - `GET /customer-portal` - Redirects to Polar Customer Portal for authenticated user
127
131
  - `POST /polar/webhooks` - Incoming webhooks are automatically parsed & validated
128
132
 
package/dist/index.cjs CHANGED
@@ -113,11 +113,11 @@ var customerPortal = (options) => (0, import_plugins2.createAuthEndpoint)(
113
113
  }
114
114
  );
115
115
 
116
- // src/endpoints/subscriptions.ts
116
+ // src/endpoints/customerState.ts
117
117
  var import_api3 = require("better-auth/api");
118
118
  var import_plugins3 = require("better-auth/plugins");
119
- var subscriptions = (options) => (0, import_plugins3.createAuthEndpoint)(
120
- "/subscriptions/list",
119
+ var customerState = (options) => (0, import_plugins3.createAuthEndpoint)(
120
+ "/state",
121
121
  {
122
122
  method: "GET",
123
123
  use: [import_api3.sessionMiddleware]
@@ -129,18 +129,10 @@ var subscriptions = (options) => (0, import_plugins3.createAuthEndpoint)(
129
129
  });
130
130
  }
131
131
  try {
132
- const { token } = await options.client.customerSessions.create({
133
- customerExternalId: ctx.context.session?.user.id
132
+ const state = await options.client.customers.getStateExternal({
133
+ externalId: ctx.context.session?.user.id
134
134
  });
135
- const subscriptions2 = await options.client.customerPortal.subscriptions.list(
136
- {
137
- customerSession: token
138
- },
139
- {
140
- active: true
141
- }
142
- );
143
- return ctx.json(subscriptions2);
135
+ return ctx.json(state);
144
136
  } catch (e) {
145
137
  if (e instanceof Error) {
146
138
  ctx.context.logger.error(
@@ -196,7 +188,11 @@ var webhooks = (options) => (0, import_plugins4.createAuthEndpoint)(
196
188
  onBenefitUpdated,
197
189
  onBenefitGrantCreated,
198
190
  onBenefitGrantUpdated,
199
- onBenefitGrantRevoked
191
+ onBenefitGrantRevoked,
192
+ onCustomerCreated,
193
+ onCustomerUpdated,
194
+ onCustomerDeleted,
195
+ onCustomerStateChanged
200
196
  } = webhooks2;
201
197
  if (!ctx.request?.body) {
202
198
  throw new import_api4.APIError("INTERNAL_SERVER_ERROR");
@@ -335,6 +331,26 @@ var webhooks = (options) => (0, import_plugins4.createAuthEndpoint)(
335
331
  onRefundUpdated(event);
336
332
  }
337
333
  break;
334
+ case "customer.created":
335
+ if (onCustomerCreated) {
336
+ onCustomerCreated(event);
337
+ }
338
+ break;
339
+ case "customer.updated":
340
+ if (onCustomerUpdated) {
341
+ onCustomerUpdated(event);
342
+ }
343
+ break;
344
+ case "customer.deleted":
345
+ if (onCustomerDeleted) {
346
+ onCustomerDeleted(event);
347
+ }
348
+ break;
349
+ case "customer.state_changed":
350
+ if (onCustomerStateChanged) {
351
+ onCustomerStateChanged(event);
352
+ }
353
+ break;
338
354
  }
339
355
  } catch (e) {
340
356
  if (e instanceof Error) {
@@ -411,7 +427,7 @@ var polar = (options) => {
411
427
  polarCheckout: checkout(options),
412
428
  polarWebhooks: webhooks(options),
413
429
  polarCustomerPortal: customerPortal(options),
414
- polarSubscriptions: subscriptions(options)
430
+ polarCustomerState: customerState(options)
415
431
  },
416
432
  init() {
417
433
  return {
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/endpoints/checkout.ts","../src/endpoints/customerPortal.ts","../src/endpoints/subscriptions.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 { subscriptions } from \"./endpoints/subscriptions\";\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\tpolarSubscriptions: subscriptions(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 subscriptions = (options: PolarOptions) =>\n\tcreateAuthEndpoint(\n\t\t\"/subscriptions/list\",\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 { token } = 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\tconst subscriptions =\n\t\t\t\t\tawait options.client.customerPortal.subscriptions.list(\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tcustomerSession: token,\n\t\t\t\t\t\t},\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tactive: true,\n\t\t\t\t\t\t},\n\t\t\t\t\t);\n\n\t\t\t\treturn ctx.json(subscriptions);\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} = 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}\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\tawait options.client.customers.create({\n\t\t\t\t\t...params,\n\t\t\t\t\temail: user.email,\n\t\t\t\t\tname: user.name,\n\t\t\t\t\texternalId: user.id,\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\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage:\n\t\t\t\t\t\t\"Polar customer creation failed. See server logs for more information.\",\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,EAAE,SAAS;AAAA,IAChC,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,QAAI,YAAY,IAAI,OAAO;AAE3B,QAAI,IAAI,OAAO,MAAM;AACpB,YAAM,WAAW,OAAO,OAAO,QAAQ,SAAS,aAAa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AQ;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","subscriptions","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 } 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 { 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\tawait options.client.customers.create({\n\t\t\t\t\t...params,\n\t\t\t\t\temail: user.email,\n\t\t\t\t\tname: user.name,\n\t\t\t\t\texternalId: user.id,\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\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage:\n\t\t\t\t\t\t\"Polar customer creation failed. See server logs for more information.\",\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\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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;;;ALxDM,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","import_api"]}
package/dist/index.d.cts CHANGED
@@ -1,5 +1,4 @@
1
- import * as _polar_sh_sdk_types_operations_js from '@polar-sh/sdk/types/operations.js';
2
- import * as _polar_sh_sdk_models_operations_customerportalsubscriptionslist_js from '@polar-sh/sdk/models/operations/customerportalsubscriptionslist.js';
1
+ import * as _polar_sh_sdk_models_components_customerstate_js from '@polar-sh/sdk/models/components/customerstate.js';
3
2
  import * as zod from 'zod';
4
3
  import * as better_auth from 'better-auth';
5
4
  import { User, Session } from 'better-auth';
@@ -11,6 +10,10 @@ import { WebhookBenefitGrantUpdatedPayload } from '@polar-sh/sdk/models/componen
11
10
  import { WebhookBenefitUpdatedPayload } from '@polar-sh/sdk/models/components/webhookbenefitupdatedpayload.js';
12
11
  import { WebhookCheckoutCreatedPayload } from '@polar-sh/sdk/models/components/webhookcheckoutcreatedpayload.js';
13
12
  import { WebhookCheckoutUpdatedPayload } from '@polar-sh/sdk/models/components/webhookcheckoutupdatedpayload.js';
13
+ import { WebhookCustomerCreatedPayload } from '@polar-sh/sdk/models/components/webhookcustomercreatedpayload.js';
14
+ import { WebhookCustomerDeletedPayload } from '@polar-sh/sdk/models/components/webhookcustomerdeletedpayload.js';
15
+ import { WebhookCustomerStateChangedPayload } from '@polar-sh/sdk/models/components/webhookcustomerstatechangedpayload.js';
16
+ import { WebhookCustomerUpdatedPayload } from '@polar-sh/sdk/models/components/webhookcustomerupdatedpayload.js';
14
17
  import { WebhookOrderCreatedPayload } from '@polar-sh/sdk/models/components/webhookordercreatedpayload.js';
15
18
  import { WebhookOrderRefundedPayload } from '@polar-sh/sdk/models/components/webhookorderrefundedpayload.js';
16
19
  import { WebhookOrganizationUpdatedPayload } from '@polar-sh/sdk/models/components/webhookorganizationupdatedpayload.js';
@@ -170,6 +173,22 @@ interface PolarOptions {
170
173
  * Webhook for benefit grant revoked
171
174
  */
172
175
  onBenefitGrantRevoked?: (payload: WebhookBenefitGrantRevokedPayload) => Promise<void>;
176
+ /**
177
+ * Webhook for customer created
178
+ */
179
+ onCustomerCreated?: (payload: WebhookCustomerCreatedPayload) => Promise<void>;
180
+ /**
181
+ * Webhook for customer updated
182
+ */
183
+ onCustomerUpdated?: (payload: WebhookCustomerUpdatedPayload) => Promise<void>;
184
+ /**
185
+ * Webhook for customer deleted
186
+ */
187
+ onCustomerDeleted?: (payload: WebhookCustomerDeletedPayload) => Promise<void>;
188
+ /**
189
+ * Webhook for customer state changed
190
+ */
191
+ onCustomerStateChanged?: (payload: WebhookCustomerStateChangedPayload) => Promise<void>;
173
192
  };
174
193
  }
175
194
 
@@ -307,7 +326,7 @@ declare const polar: <O extends PolarOptions>(options: O) => {
307
326
  };
308
327
  path: "/portal";
309
328
  };
310
- polarSubscriptions: {
329
+ polarCustomerState: {
311
330
  <C extends [({
312
331
  body?: undefined;
313
332
  method?: "GET" | undefined;
@@ -325,12 +344,8 @@ declare const polar: <O extends PolarOptions>(options: O) => {
325
344
  returnHeaders: true;
326
345
  }] ? {
327
346
  headers: Headers;
328
- response: _polar_sh_sdk_types_operations_js.PageIterator<_polar_sh_sdk_models_operations_customerportalsubscriptionslist_js.CustomerPortalSubscriptionsListResponse, {
329
- page: number;
330
- }>;
331
- } : _polar_sh_sdk_types_operations_js.PageIterator<_polar_sh_sdk_models_operations_customerportalsubscriptionslist_js.CustomerPortalSubscriptionsListResponse, {
332
- page: number;
333
- }>>;
347
+ response: _polar_sh_sdk_models_components_customerstate_js.CustomerState;
348
+ } : _polar_sh_sdk_models_components_customerstate_js.CustomerState>;
334
349
  options: {
335
350
  method: "GET";
336
351
  use: ((inputContext: {
@@ -367,7 +382,7 @@ declare const polar: <O extends PolarOptions>(options: O) => {
367
382
  } & {
368
383
  use: any[];
369
384
  };
370
- path: "/subscriptions/list";
385
+ path: "/state";
371
386
  };
372
387
  };
373
388
  init(): {
package/dist/index.d.ts CHANGED
@@ -1,5 +1,4 @@
1
- import * as _polar_sh_sdk_types_operations_js from '@polar-sh/sdk/types/operations.js';
2
- import * as _polar_sh_sdk_models_operations_customerportalsubscriptionslist_js from '@polar-sh/sdk/models/operations/customerportalsubscriptionslist.js';
1
+ import * as _polar_sh_sdk_models_components_customerstate_js from '@polar-sh/sdk/models/components/customerstate.js';
3
2
  import * as zod from 'zod';
4
3
  import * as better_auth from 'better-auth';
5
4
  import { User, Session } from 'better-auth';
@@ -11,6 +10,10 @@ import { WebhookBenefitGrantUpdatedPayload } from '@polar-sh/sdk/models/componen
11
10
  import { WebhookBenefitUpdatedPayload } from '@polar-sh/sdk/models/components/webhookbenefitupdatedpayload.js';
12
11
  import { WebhookCheckoutCreatedPayload } from '@polar-sh/sdk/models/components/webhookcheckoutcreatedpayload.js';
13
12
  import { WebhookCheckoutUpdatedPayload } from '@polar-sh/sdk/models/components/webhookcheckoutupdatedpayload.js';
13
+ import { WebhookCustomerCreatedPayload } from '@polar-sh/sdk/models/components/webhookcustomercreatedpayload.js';
14
+ import { WebhookCustomerDeletedPayload } from '@polar-sh/sdk/models/components/webhookcustomerdeletedpayload.js';
15
+ import { WebhookCustomerStateChangedPayload } from '@polar-sh/sdk/models/components/webhookcustomerstatechangedpayload.js';
16
+ import { WebhookCustomerUpdatedPayload } from '@polar-sh/sdk/models/components/webhookcustomerupdatedpayload.js';
14
17
  import { WebhookOrderCreatedPayload } from '@polar-sh/sdk/models/components/webhookordercreatedpayload.js';
15
18
  import { WebhookOrderRefundedPayload } from '@polar-sh/sdk/models/components/webhookorderrefundedpayload.js';
16
19
  import { WebhookOrganizationUpdatedPayload } from '@polar-sh/sdk/models/components/webhookorganizationupdatedpayload.js';
@@ -170,6 +173,22 @@ interface PolarOptions {
170
173
  * Webhook for benefit grant revoked
171
174
  */
172
175
  onBenefitGrantRevoked?: (payload: WebhookBenefitGrantRevokedPayload) => Promise<void>;
176
+ /**
177
+ * Webhook for customer created
178
+ */
179
+ onCustomerCreated?: (payload: WebhookCustomerCreatedPayload) => Promise<void>;
180
+ /**
181
+ * Webhook for customer updated
182
+ */
183
+ onCustomerUpdated?: (payload: WebhookCustomerUpdatedPayload) => Promise<void>;
184
+ /**
185
+ * Webhook for customer deleted
186
+ */
187
+ onCustomerDeleted?: (payload: WebhookCustomerDeletedPayload) => Promise<void>;
188
+ /**
189
+ * Webhook for customer state changed
190
+ */
191
+ onCustomerStateChanged?: (payload: WebhookCustomerStateChangedPayload) => Promise<void>;
173
192
  };
174
193
  }
175
194
 
@@ -307,7 +326,7 @@ declare const polar: <O extends PolarOptions>(options: O) => {
307
326
  };
308
327
  path: "/portal";
309
328
  };
310
- polarSubscriptions: {
329
+ polarCustomerState: {
311
330
  <C extends [({
312
331
  body?: undefined;
313
332
  method?: "GET" | undefined;
@@ -325,12 +344,8 @@ declare const polar: <O extends PolarOptions>(options: O) => {
325
344
  returnHeaders: true;
326
345
  }] ? {
327
346
  headers: Headers;
328
- response: _polar_sh_sdk_types_operations_js.PageIterator<_polar_sh_sdk_models_operations_customerportalsubscriptionslist_js.CustomerPortalSubscriptionsListResponse, {
329
- page: number;
330
- }>;
331
- } : _polar_sh_sdk_types_operations_js.PageIterator<_polar_sh_sdk_models_operations_customerportalsubscriptionslist_js.CustomerPortalSubscriptionsListResponse, {
332
- page: number;
333
- }>>;
347
+ response: _polar_sh_sdk_models_components_customerstate_js.CustomerState;
348
+ } : _polar_sh_sdk_models_components_customerstate_js.CustomerState>;
334
349
  options: {
335
350
  method: "GET";
336
351
  use: ((inputContext: {
@@ -367,7 +382,7 @@ declare const polar: <O extends PolarOptions>(options: O) => {
367
382
  } & {
368
383
  use: any[];
369
384
  };
370
- path: "/subscriptions/list";
385
+ path: "/state";
371
386
  };
372
387
  };
373
388
  init(): {
package/dist/index.js CHANGED
@@ -87,11 +87,11 @@ var customerPortal = (options) => createAuthEndpoint2(
87
87
  }
88
88
  );
89
89
 
90
- // src/endpoints/subscriptions.ts
90
+ // src/endpoints/customerState.ts
91
91
  import { APIError as APIError3, sessionMiddleware as sessionMiddleware2 } from "better-auth/api";
92
92
  import { createAuthEndpoint as createAuthEndpoint3 } from "better-auth/plugins";
93
- var subscriptions = (options) => createAuthEndpoint3(
94
- "/subscriptions/list",
93
+ var customerState = (options) => createAuthEndpoint3(
94
+ "/state",
95
95
  {
96
96
  method: "GET",
97
97
  use: [sessionMiddleware2]
@@ -103,18 +103,10 @@ var subscriptions = (options) => createAuthEndpoint3(
103
103
  });
104
104
  }
105
105
  try {
106
- const { token } = await options.client.customerSessions.create({
107
- customerExternalId: ctx.context.session?.user.id
106
+ const state = await options.client.customers.getStateExternal({
107
+ externalId: ctx.context.session?.user.id
108
108
  });
109
- const subscriptions2 = await options.client.customerPortal.subscriptions.list(
110
- {
111
- customerSession: token
112
- },
113
- {
114
- active: true
115
- }
116
- );
117
- return ctx.json(subscriptions2);
109
+ return ctx.json(state);
118
110
  } catch (e) {
119
111
  if (e instanceof Error) {
120
112
  ctx.context.logger.error(
@@ -170,7 +162,11 @@ var webhooks = (options) => createAuthEndpoint4(
170
162
  onBenefitUpdated,
171
163
  onBenefitGrantCreated,
172
164
  onBenefitGrantUpdated,
173
- onBenefitGrantRevoked
165
+ onBenefitGrantRevoked,
166
+ onCustomerCreated,
167
+ onCustomerUpdated,
168
+ onCustomerDeleted,
169
+ onCustomerStateChanged
174
170
  } = webhooks2;
175
171
  if (!ctx.request?.body) {
176
172
  throw new APIError4("INTERNAL_SERVER_ERROR");
@@ -309,6 +305,26 @@ var webhooks = (options) => createAuthEndpoint4(
309
305
  onRefundUpdated(event);
310
306
  }
311
307
  break;
308
+ case "customer.created":
309
+ if (onCustomerCreated) {
310
+ onCustomerCreated(event);
311
+ }
312
+ break;
313
+ case "customer.updated":
314
+ if (onCustomerUpdated) {
315
+ onCustomerUpdated(event);
316
+ }
317
+ break;
318
+ case "customer.deleted":
319
+ if (onCustomerDeleted) {
320
+ onCustomerDeleted(event);
321
+ }
322
+ break;
323
+ case "customer.state_changed":
324
+ if (onCustomerStateChanged) {
325
+ onCustomerStateChanged(event);
326
+ }
327
+ break;
312
328
  }
313
329
  } catch (e) {
314
330
  if (e instanceof Error) {
@@ -385,7 +401,7 @@ var polar = (options) => {
385
401
  polarCheckout: checkout(options),
386
402
  polarWebhooks: webhooks(options),
387
403
  polarCustomerPortal: customerPortal(options),
388
- polarSubscriptions: subscriptions(options)
404
+ polarCustomerState: customerState(options)
389
405
  },
390
406
  init() {
391
407
  return {
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/endpoints/checkout.ts","../src/endpoints/customerPortal.ts","../src/endpoints/subscriptions.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 subscriptions = (options: PolarOptions) =>\n\tcreateAuthEndpoint(\n\t\t\"/subscriptions/list\",\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 { token } = 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\tconst subscriptions =\n\t\t\t\t\tawait options.client.customerPortal.subscriptions.list(\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tcustomerSession: token,\n\t\t\t\t\t\t},\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tactive: true,\n\t\t\t\t\t\t},\n\t\t\t\t\t);\n\n\t\t\t\treturn ctx.json(subscriptions);\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} = 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}\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\tawait options.client.customers.create({\n\t\t\t\t\t...params,\n\t\t\t\t\temail: user.email,\n\t\t\t\t\tname: user.name,\n\t\t\t\t\texternalId: user.id,\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\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage:\n\t\t\t\t\t\t\"Polar customer creation failed. See server logs for more information.\",\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 { subscriptions } from \"./endpoints/subscriptions\";\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\tpolarSubscriptions: subscriptions(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,SAAS;AAC/B,YAAM,IAAI,SAAS,eAAe;AAAA,QACjC,SAAS;AAAA,MACV,CAAC;AAAA,IACF;AAEA,QAAI,YAAY,IAAI,OAAO;AAE3B,QAAI,IAAI,OAAO,MAAM;AACpB,YAAM,WAAW,OAAO,OAAO,QAAQ,SAAS,aAAa,aAC1D,QAAQ,SAAS,SAAS,IAC1B,QAAQ,SAAS;AAEpB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B;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","subscriptions","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/: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 { 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\tawait options.client.customers.create({\n\t\t\t\t\t...params,\n\t\t\t\t\temail: user.email,\n\t\t\t\t\tname: user.name,\n\t\t\t\t\texternalId: user.id,\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\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage:\n\t\t\t\t\t\t\"Polar customer creation failed. See server logs for more information.\",\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: onUserUp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AAA,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;;;ACxDM,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","APIError"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polar-sh/better-auth",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "Polar integration for better-auth",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
@@ -37,14 +37,14 @@
37
37
  "vitest": "^2.1.8"
38
38
  },
39
39
  "dependencies": {
40
- "@polar-sh/sdk": "^0.28.0",
40
+ "@polar-sh/sdk": "^0.29.2",
41
41
  "zod": "^3.24.2"
42
42
  },
43
43
  "peerDependencies": {
44
44
  "better-auth": "^1.2.0"
45
45
  },
46
46
  "scripts": {
47
- "test": "vitest",
47
+ "test": "exit 0",
48
48
  "build": "tsup ./src/index.ts --format esm,cjs --dts --clean --sourcemap",
49
49
  "dev": "tsc --watch",
50
50
  "check": "biome check --write ./src"