@polar-sh/better-auth 1.1.5 → 1.1.6

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
@@ -391,7 +391,7 @@ var checkout = (checkoutOptions = {}) => (polar2) => {
391
391
  products: productIds,
392
392
  successUrl: checkoutOptions.successUrl ? new URL(
393
393
  checkoutOptions.successUrl,
394
- ctx.request?.url
394
+ ctx.request?.url ?? ctx.context.baseURL
395
395
  ).toString() : void 0,
396
396
  metadata: ctx.body.referenceId ? {
397
397
  referenceId: ctx.body.referenceId,
@@ -665,7 +665,7 @@ var handleWebhookPayload = async (payload, { webhookSecret, entitlements, onPayl
665
665
  };
666
666
 
667
667
  // src/plugins/webhooks.ts
668
- var import_webhooks = require("@polar-sh/sdk/webhooks.js");
668
+ var import_webhooks = require("@polar-sh/sdk/webhooks");
669
669
  var import_api6 = require("better-auth/api");
670
670
  var webhooks = (options) => (_polar) => {
671
671
  return {
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/hooks/customer.ts","../src/client.ts","../src/plugins/portal.ts","../src/plugins/checkout.ts","../src/plugins/usage.ts","../../adapter-utils/dist/index.js","../src/plugins/webhooks.ts"],"sourcesContent":["import type { BetterAuthPlugin } from \"better-auth\";\nimport {\n\tonAfterUserCreate,\n\tonBeforeUserCreate,\n\tonUserUpdate,\n} from \"./hooks/customer\";\nimport type { PolarEndpoints, PolarOptions } from \"./types\";\n\nexport { polarClient } from \"./client\";\n\nexport * from \"./plugins/portal\";\nexport * from \"./plugins/checkout\";\nexport * from \"./plugins/usage\";\nexport * from \"./plugins/webhooks\";\n\nexport const polar = <O extends PolarOptions>(options: O) => {\n\tconst plugins = options.use\n\t\t.map((use) => use(options.client))\n\t\t.reduce((acc, plugin) => {\n\t\t\tObject.assign(acc, plugin);\n\t\t\treturn acc;\n\t\t}, {} as PolarEndpoints);\n\n\treturn {\n\t\tid: \"polar\",\n\t\tendpoints: {\n\t\t\t...plugins,\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\tbefore: onBeforeUserCreate(options),\n\t\t\t\t\t\t\t\tafter: onAfterUserCreate(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 type { GenericEndpointContext, User } from \"better-auth\";\nimport { APIError } from \"better-auth/api\";\nimport type { PolarOptions } from \"../types\";\n\nexport const onBeforeUserCreate =\n\t(options: PolarOptions) =>\n\tasync (user: Partial<User>, context?: GenericEndpointContext | undefined) => {\n\t\tif (context && options.createCustomerOnSignUp) {\n\t\t\ttry {\n\t\t\t\tconst params = options.getCustomerCreateParams\n\t\t\t\t\t? await options.getCustomerCreateParams({\n\t\t\t\t\t\t\tuser,\n\t\t\t\t\t\t})\n\t\t\t\t\t: {};\n\n\t\t\t\tif (!user.email) {\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"An associated email is required\",\n\t\t\t\t\t});\n\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});\n\t\t\t} catch (e: unknown) {\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 onAfterUserCreate =\n\t(options: PolarOptions) =>\n\tasync (user: User, context?: GenericEndpointContext | undefined) => {\n\t\tif (context && options.createCustomerOnSignUp) {\n\t\t\ttry {\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}\n\t\t\t} catch (e: unknown) {\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, context?: GenericEndpointContext | undefined) => {\n\t\tif (context && 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\tcustomerUpdateExternalID: {\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\tcontext.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\tcontext.context.logger.error(\n\t\t\t\t\t\t`Polar customer update failed. Error: ${e}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t};\n","import type { BetterAuthClientPlugin } from \"better-auth\";\nimport type { polar } from \"./index\";\n\nexport const polarClient = () => {\n\treturn {\n\t\tid: \"polar-client\",\n\t\t$InferServerPlugin: {} as ReturnType<typeof polar>,\n\t} satisfies BetterAuthClientPlugin;\n};\n","import type { Polar } from \"@polar-sh/sdk\";\nimport { APIError } from \"better-auth/api\";\nimport { sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport { z } from \"zod\";\n\nexport const portal = () => (polar: Polar) => {\n\treturn {\n\t\tportal: createAuthEndpoint(\n\t\t\t\"/customer/portal\",\n\t\t\t{\n\t\t\t\tmethod: \"GET\",\n\t\t\t\tuse: [sessionMiddleware],\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tif (!ctx.context.session?.user.id) {\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tconst customerSession = await polar.customerSessions.create({\n\t\t\t\t\t\texternalCustomerId: ctx.context.session?.user.id,\n\t\t\t\t\t});\n\n\t\t\t\t\treturn ctx.json({\n\t\t\t\t\t\turl: customerSession.customerPortalUrl,\n\t\t\t\t\t\tredirect: true,\n\t\t\t\t\t});\n\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t`Polar customer portal creation failed. Error: ${e.message}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: \"Customer portal creation failed\",\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t},\n\t\t),\n\t\tstate: createAuthEndpoint(\n\t\t\t\"/customer/state\",\n\t\t\t{\n\t\t\t\tmethod: \"GET\",\n\t\t\t\tuse: [sessionMiddleware],\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tconst state = await polar.customers.getStateExternal({\n\t\t\t\t\t\texternalId: ctx.context.session?.user.id,\n\t\t\t\t\t});\n\n\t\t\t\t\treturn ctx.json(state);\n\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t`Polar subscriptions list failed. Error: ${e.message}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: \"Subscriptions list failed\",\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t},\n\t\t),\n\t\tbenefits: createAuthEndpoint(\n\t\t\t\"/customer/benefits/list\",\n\t\t\t{\n\t\t\t\tmethod: \"GET\",\n\t\t\t\tquery: z\n\t\t\t\t\t.object({\n\t\t\t\t\t\tpage: z.coerce.number().optional(),\n\t\t\t\t\t\tlimit: z.coerce.number().optional(),\n\t\t\t\t\t})\n\t\t\t\t\t.optional(),\n\t\t\t\tuse: [sessionMiddleware],\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tconst customerSession = await polar.customerSessions.create({\n\t\t\t\t\t\texternalCustomerId: ctx.context.session?.user.id,\n\t\t\t\t\t});\n\n\t\t\t\t\tconst benefits = await polar.customerPortal.benefitGrants.list(\n\t\t\t\t\t\t{ customerSession: customerSession.token },\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tpage: ctx.query?.page,\n\t\t\t\t\t\t\tlimit: ctx.query?.limit,\n\t\t\t\t\t\t},\n\t\t\t\t\t);\n\n\t\t\t\t\treturn ctx.json(benefits);\n\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t`Polar benefits list failed. Error: ${e.message}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: \"Benefits list failed\",\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t},\n\t\t),\n\t\tsubscriptions: createAuthEndpoint(\n\t\t\t\"/customer/subscriptions/list\",\n\t\t\t{\n\t\t\t\tmethod: \"GET\",\n\t\t\t\tquery: z\n\t\t\t\t\t.object({\n\t\t\t\t\t\treferenceId: z.string().optional(),\n\t\t\t\t\t\tpage: z.coerce.number().optional(),\n\t\t\t\t\t\tlimit: z.coerce.number().optional(),\n\t\t\t\t\t\tactive: z.coerce.boolean().optional(),\n\t\t\t\t\t})\n\t\t\t\t\t.optional(),\n\t\t\t\tuse: [sessionMiddleware],\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\tif (ctx.query?.referenceId) {\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst subscriptions = await polar.subscriptions.list({\n\t\t\t\t\t\t\tpage: ctx.query?.page,\n\t\t\t\t\t\t\tlimit: ctx.query?.limit,\n\t\t\t\t\t\t\tactive: ctx.query?.active,\n\t\t\t\t\t\t\tmetadata: {\n\t\t\t\t\t\t\t\treferenceId: ctx.query?.referenceId,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\treturn ctx.json(subscriptions);\n\t\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\t\tconsole.log(e);\n\t\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t\t`Polar subscriptions list with referenceId failed. Error: ${e.message}`,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\t\tmessage: \"Subscriptions list with referenceId failed\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tconst customerSession = await polar.customerSessions.create({\n\t\t\t\t\t\texternalCustomerId: ctx.context.session?.user.id,\n\t\t\t\t\t});\n\n\t\t\t\t\tconst subscriptions = await polar.customerPortal.subscriptions.list(\n\t\t\t\t\t\t{ customerSession: customerSession.token },\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tpage: ctx.query?.page,\n\t\t\t\t\t\t\tlimit: ctx.query?.limit,\n\t\t\t\t\t\t\tactive: ctx.query?.active,\n\t\t\t\t\t\t},\n\t\t\t\t\t);\n\n\t\t\t\t\treturn ctx.json(subscriptions);\n\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t`Polar subscriptions list failed. Error: ${e.message}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: \"Polar subscriptions list failed\",\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t},\n\t\t),\n\t\torders: createAuthEndpoint(\n\t\t\t\"/customer/orders/list\",\n\t\t\t{\n\t\t\t\tmethod: \"GET\",\n\t\t\t\tquery: z\n\t\t\t\t\t.object({\n\t\t\t\t\t\tpage: z.coerce.number().optional(),\n\t\t\t\t\t\tlimit: z.coerce.number().optional(),\n\t\t\t\t\t\tproductBillingType: z.enum([\"recurring\", \"one_time\"]).optional(),\n\t\t\t\t\t})\n\t\t\t\t\t.optional(),\n\t\t\t\tuse: [sessionMiddleware],\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tconst customerSession = await polar.customerSessions.create({\n\t\t\t\t\t\texternalCustomerId: ctx.context.session?.user.id,\n\t\t\t\t\t});\n\n\t\t\t\t\tconst orders = await polar.customerPortal.orders.list(\n\t\t\t\t\t\t{ customerSession: customerSession.token },\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tpage: ctx.query?.page,\n\t\t\t\t\t\t\tlimit: ctx.query?.limit,\n\t\t\t\t\t\t\tproductBillingType: ctx.query?.productBillingType,\n\t\t\t\t\t\t},\n\t\t\t\t\t);\n\n\t\t\t\t\treturn ctx.json(orders);\n\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t`Polar orders list failed. Error: ${e.message}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: \"Orders list failed\",\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t},\n\t\t),\n\t};\n};\n","import type { Polar } from \"@polar-sh/sdk\";\nimport { APIError, getSessionFromCtx } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport { z } from \"zod\";\nimport type { Product } from \"../types\";\n\nexport interface CheckoutOptions {\n\t/**\n\t * Optional list of slug -> productId mappings for easy slug checkouts\n\t */\n\tproducts?: Product[] | (() => Promise<Product[]>);\n\t/**\n\t * Checkout Success URL\n\t */\n\tsuccessUrl?: string;\n\t/**\n\t * Only allow authenticated customers to checkout\n\t */\n\tauthenticatedUsersOnly?: boolean;\n\t/**\n\t * Checkout theme\n\t */\n\ttheme?: \"light\" | \"dark\";\n\t/**\n\t * Allow discount codes\n\t */\n\tallowDiscountCodes?: boolean;\n\t/**\n\t * Discount ID\n\t */\n\tdiscountId?: string;\n}\n\nexport const checkout =\n\t(checkoutOptions: CheckoutOptions = {}) =>\n\t(polar: Polar) => {\n\t\treturn {\n\t\t\tcheckout: createAuthEndpoint(\n\t\t\t\t\"/checkout\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\tbody: z.object({\n\t\t\t\t\t\tproducts: z.union([z.array(z.string()), z.string()]).optional(),\n\t\t\t\t\t\tslug: z.string().optional(),\n\t\t\t\t\t\treferenceId: z.string().optional(),\n\t\t\t\t\t\tcustomFieldData: z\n\t\t\t\t\t\t\t.record(\n\t\t\t\t\t\t\t\tz.string(),\n\t\t\t\t\t\t\t\tz.union([z.string(), z.number(), z.boolean()]),\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t.optional(),\n\t\t\t\t\t\tmetadata: z\n\t\t\t\t\t\t\t.record(\n\t\t\t\t\t\t\t\tz.string(),\n\t\t\t\t\t\t\t\tz.union([z.string(), z.number(), z.boolean()]),\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t.optional(),\n\t\t\t\t\t\tallowDiscountCodes: z.coerce.boolean().optional().default(true),\n\t\t\t\t\t\tdiscountId: z.string().optional(),\n\t\t\t\t\t}),\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tconst session = await getSessionFromCtx(ctx);\n\n\t\t\t\t\tlet productIds: string[] = [];\n\n\t\t\t\t\tif (ctx.body.slug) {\n\t\t\t\t\t\tconst resolvedProducts = await (typeof checkoutOptions.products ===\n\t\t\t\t\t\t\"function\"\n\t\t\t\t\t\t\t? checkoutOptions.products()\n\t\t\t\t\t\t\t: checkoutOptions.products);\n\n\t\t\t\t\t\tconst productId = resolvedProducts?.find(\n\t\t\t\t\t\t\t(product) => product.slug === ctx.body.slug,\n\t\t\t\t\t\t)?.productId;\n\n\t\t\t\t\t\tif (!productId) {\n\t\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\t\tmessage: \"Product not found\",\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tproductIds = [productId];\n\t\t\t\t\t} else {\n\t\t\t\t\t\tproductIds = Array.isArray(ctx.body.products)\n\t\t\t\t\t\t\t? ctx.body.products.filter((id) => id !== undefined)\n\t\t\t\t\t\t\t: [ctx.body.products].filter((id) => id !== undefined);\n\t\t\t\t\t}\n\n\t\t\t\t\tif (checkoutOptions.authenticatedUsersOnly && !session?.user.id) {\n\t\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\t\tmessage: \"You must be logged in to checkout\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst checkout = await polar.checkouts.create({\n\t\t\t\t\t\t\texternalCustomerId: session?.user.id,\n\t\t\t\t\t\t\tproducts: productIds,\n\t\t\t\t\t\t\tsuccessUrl: checkoutOptions.successUrl\n\t\t\t\t\t\t\t\t? new URL(\n\t\t\t\t\t\t\t\t\t\tcheckoutOptions.successUrl,\n\t\t\t\t\t\t\t\t\t\tctx.request?.url,\n\t\t\t\t\t\t\t\t\t).toString()\n\t\t\t\t\t\t\t\t: undefined,\n\t\t\t\t\t\t\tmetadata: ctx.body.referenceId\n\t\t\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\t\t\treferenceId: ctx.body.referenceId,\n\t\t\t\t\t\t\t\t\t\t...ctx.body.metadata,\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t: ctx.body.metadata,\n\t\t\t\t\t\t\tcustomFieldData: ctx.body.customFieldData,\n\t\t\t\t\t\t\tallowDiscountCodes: ctx.body.allowDiscountCodes ?? true,\n\t\t\t\t\t\t\tdiscountId: ctx.body.discountId,\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tconst redirectUrl = new URL(checkout.url);\n\n\t\t\t\t\t\tif (checkoutOptions.theme) {\n\t\t\t\t\t\t\tredirectUrl.searchParams.set(\"theme\", checkoutOptions.theme);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\treturn ctx.json({\n\t\t\t\t\t\t\turl: redirectUrl.toString(),\n\t\t\t\t\t\t\tredirect: true,\n\t\t\t\t\t\t});\n\t\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t\t`Polar checkout creation failed. Error: ${e.message}`,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\t\tmessage: \"Checkout creation failed\",\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};\n","import type { Polar } from \"@polar-sh/sdk\";\nimport {\n\tAPIError,\n\tcreateAuthEndpoint,\n\tsessionMiddleware,\n} from \"better-auth/api\";\nimport { z } from \"zod\";\nimport type { Product } from \"../types\";\n\nexport interface UsageOptions {\n\t/**\n\t * Products to use for topping up credits\n\t */\n\tcreditProducts?: Product[] | (() => Promise<Product[]>);\n}\n\nexport const usage = (_usageOptions?: UsageOptions) => (polar: Polar) => {\n\treturn {\n\t\tmeters: createAuthEndpoint(\n\t\t\t\"/usage/meters/list\",\n\t\t\t{\n\t\t\t\tmethod: \"GET\",\n\t\t\t\tuse: [sessionMiddleware],\n\t\t\t\tquery: z.object({\n\t\t\t\t\tpage: z.coerce.number().optional(),\n\t\t\t\t\tlimit: z.coerce.number().optional(),\n\t\t\t\t}),\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tconst customerSession = await polar.customerSessions.create({\n\t\t\t\t\t\texternalCustomerId: ctx.context.session.user.id,\n\t\t\t\t\t});\n\n\t\t\t\t\tconst customerMeters = await polar.customerPortal.customerMeters.list(\n\t\t\t\t\t\t{ customerSession: customerSession.token },\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tpage: ctx.query?.page,\n\t\t\t\t\t\t\tlimit: ctx.query?.limit,\n\t\t\t\t\t\t},\n\t\t\t\t\t);\n\n\t\t\t\t\treturn ctx.json(customerMeters);\n\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t`Polar meters list failed. Error: ${e.message}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: \"Meters list failed\",\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t},\n\t\t),\n\t\tingestion: createAuthEndpoint(\n\t\t\t\"/usage/ingest\",\n\t\t\t{\n\t\t\t\tmethod: \"POST\",\n\t\t\t\tbody: z.object({\n\t\t\t\t\tevent: z.string(),\n\t\t\t\t\tmetadata: z.record(\n\t\t\t\t\t\tz.string(),\n\t\t\t\t\t\tz.union([z.string(), z.number(), z.boolean()]),\n\t\t\t\t\t),\n\t\t\t\t}),\n\t\t\t\tuse: [sessionMiddleware],\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tconst ingestion = await polar.events.ingest({\n\t\t\t\t\t\tevents: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tname: ctx.body.event,\n\t\t\t\t\t\t\t\tmetadata: ctx.body.metadata,\n\t\t\t\t\t\t\t\texternalCustomerId: ctx.context.session.user.id,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t],\n\t\t\t\t\t});\n\n\t\t\t\t\treturn ctx.json(ingestion);\n\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t`Polar ingestion failed. Error: ${e.message}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: \"Ingestion failed\",\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t},\n\t\t),\n\t};\n};\n","// src/webhooks/webhooks.ts\nvar handleWebhookPayload = async (payload, { webhookSecret, entitlements, onPayload, ...eventHandlers }) => {\n const promises = [];\n if (onPayload) {\n promises.push(onPayload(payload));\n }\n switch (payload.type) {\n case \"checkout.created\":\n if (eventHandlers.onCheckoutCreated) {\n promises.push(eventHandlers.onCheckoutCreated(payload));\n }\n break;\n case \"checkout.updated\":\n if (eventHandlers.onCheckoutUpdated) {\n promises.push(eventHandlers.onCheckoutUpdated(payload));\n }\n break;\n case \"order.created\":\n if (eventHandlers.onOrderCreated) {\n promises.push(eventHandlers.onOrderCreated(payload));\n }\n break;\n case \"order.updated\":\n if (eventHandlers.onOrderUpdated) {\n promises.push(eventHandlers.onOrderUpdated(payload));\n }\n break;\n case \"order.paid\":\n if (eventHandlers.onOrderPaid) {\n promises.push(eventHandlers.onOrderPaid(payload));\n }\n break;\n case \"subscription.created\":\n if (eventHandlers.onSubscriptionCreated) {\n promises.push(eventHandlers.onSubscriptionCreated(payload));\n }\n break;\n case \"subscription.updated\":\n if (eventHandlers.onSubscriptionUpdated) {\n promises.push(eventHandlers.onSubscriptionUpdated(payload));\n }\n break;\n case \"subscription.active\":\n if (eventHandlers.onSubscriptionActive) {\n promises.push(eventHandlers.onSubscriptionActive(payload));\n }\n break;\n case \"subscription.canceled\":\n if (eventHandlers.onSubscriptionCanceled) {\n promises.push(eventHandlers.onSubscriptionCanceled(payload));\n }\n break;\n case \"subscription.uncanceled\":\n if (eventHandlers.onSubscriptionUncanceled) {\n promises.push(eventHandlers.onSubscriptionUncanceled(payload));\n }\n break;\n case \"subscription.revoked\":\n if (eventHandlers.onSubscriptionRevoked) {\n promises.push(eventHandlers.onSubscriptionRevoked(payload));\n }\n break;\n case \"product.created\":\n if (eventHandlers.onProductCreated) {\n promises.push(eventHandlers.onProductCreated(payload));\n }\n break;\n case \"product.updated\":\n if (eventHandlers.onProductUpdated) {\n promises.push(eventHandlers.onProductUpdated(payload));\n }\n break;\n case \"organization.updated\":\n if (eventHandlers.onOrganizationUpdated) {\n promises.push(eventHandlers.onOrganizationUpdated(payload));\n }\n break;\n case \"benefit.created\":\n if (eventHandlers.onBenefitCreated) {\n promises.push(eventHandlers.onBenefitCreated(payload));\n }\n break;\n case \"benefit.updated\":\n if (eventHandlers.onBenefitUpdated) {\n promises.push(eventHandlers.onBenefitUpdated(payload));\n }\n break;\n case \"benefit_grant.created\":\n if (eventHandlers.onBenefitGrantCreated) {\n promises.push(eventHandlers.onBenefitGrantCreated(payload));\n }\n break;\n case \"benefit_grant.updated\":\n if (eventHandlers.onBenefitGrantUpdated) {\n promises.push(eventHandlers.onBenefitGrantUpdated(payload));\n }\n break;\n case \"benefit_grant.revoked\":\n if (eventHandlers.onBenefitGrantRevoked) {\n promises.push(eventHandlers.onBenefitGrantRevoked(payload));\n }\n break;\n case \"customer.created\":\n if (eventHandlers.onCustomerCreated) {\n promises.push(eventHandlers.onCustomerCreated(payload));\n }\n break;\n case \"customer.updated\":\n if (eventHandlers.onCustomerUpdated) {\n promises.push(eventHandlers.onCustomerUpdated(payload));\n }\n break;\n case \"customer.deleted\":\n if (eventHandlers.onCustomerDeleted) {\n promises.push(eventHandlers.onCustomerDeleted(payload));\n }\n break;\n case \"customer.state_changed\":\n if (eventHandlers.onCustomerStateChanged) {\n promises.push(eventHandlers.onCustomerStateChanged(payload));\n }\n break;\n case \"order.refunded\":\n if (eventHandlers.onOrderRefunded) {\n promises.push(eventHandlers.onOrderRefunded(payload));\n }\n break;\n case \"refund.created\":\n if (eventHandlers.onRefundCreated) {\n promises.push(eventHandlers.onRefundCreated(payload));\n }\n break;\n case \"refund.updated\":\n if (eventHandlers.onRefundUpdated) {\n promises.push(eventHandlers.onRefundUpdated(payload));\n }\n break;\n }\n switch (payload.type) {\n case \"benefit_grant.created\":\n case \"benefit_grant.revoked\":\n if (entitlements) {\n for (const handler of entitlements.handlers) {\n promises.push(handler(payload));\n }\n }\n }\n return Promise.all(promises);\n};\n\n// src/entitlement/entitlement.ts\nvar EntitlementStrategy = class {\n grantCallbacks = [];\n revokeCallbacks = [];\n grant(callback) {\n this.grantCallbacks.push(callback);\n return this;\n }\n revoke(callback) {\n this.revokeCallbacks.push(callback);\n return this;\n }\n handler(slug) {\n return async (payload) => {\n if (payload.data.benefit.description === slug) {\n switch (payload.type) {\n case \"benefit_grant.created\":\n await Promise.all(\n this.grantCallbacks.map(\n (callback) => callback({\n customer: payload.data.customer,\n properties: payload.data.properties,\n payload\n })\n )\n );\n break;\n case \"benefit_grant.revoked\":\n await Promise.all(\n this.revokeCallbacks.map(\n (callback) => callback({\n customer: payload.data.customer,\n properties: payload.data.properties,\n payload\n })\n )\n );\n break;\n }\n }\n };\n }\n};\nvar Entitlements = class {\n static handlers = [];\n static use(slug, strategy) {\n this.handlers.push(strategy.handler(slug));\n return this;\n }\n};\nexport {\n EntitlementStrategy,\n Entitlements,\n handleWebhookPayload\n};\n","import { handleWebhookPayload } from \"@polar-sh/adapter-utils\";\nimport type { Polar } from \"@polar-sh/sdk\";\nimport type { WebhookBenefitCreatedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitcreatedpayload.js\";\nimport type { WebhookBenefitGrantCreatedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitgrantcreatedpayload.js\";\nimport type { WebhookBenefitGrantRevokedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitgrantrevokedpayload.js\";\nimport type { WebhookBenefitGrantUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitgrantupdatedpayload.js\";\nimport type { WebhookBenefitUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitupdatedpayload.js\";\nimport type { WebhookCheckoutCreatedPayload } from \"@polar-sh/sdk/models/components/webhookcheckoutcreatedpayload.js\";\nimport type { WebhookCheckoutUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookcheckoutupdatedpayload.js\";\nimport type { WebhookCustomerCreatedPayload } from \"@polar-sh/sdk/models/components/webhookcustomercreatedpayload.js\";\nimport type { WebhookCustomerDeletedPayload } from \"@polar-sh/sdk/models/components/webhookcustomerdeletedpayload.js\";\nimport type { WebhookCustomerStateChangedPayload } from \"@polar-sh/sdk/models/components/webhookcustomerstatechangedpayload.js\";\nimport type { WebhookCustomerUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookcustomerupdatedpayload.js\";\nimport type { WebhookOrderCreatedPayload } from \"@polar-sh/sdk/models/components/webhookordercreatedpayload.js\";\nimport type { WebhookOrderPaidPayload } from \"@polar-sh/sdk/models/components/webhookorderpaidpayload.js\";\nimport type { WebhookOrderRefundedPayload } from \"@polar-sh/sdk/models/components/webhookorderrefundedpayload.js\";\nimport type { WebhookOrganizationUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookorganizationupdatedpayload.js\";\nimport type { WebhookProductCreatedPayload } from \"@polar-sh/sdk/models/components/webhookproductcreatedpayload.js\";\nimport type { WebhookProductUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookproductupdatedpayload.js\";\nimport type { WebhookRefundCreatedPayload } from \"@polar-sh/sdk/models/components/webhookrefundcreatedpayload.js\";\nimport type { WebhookRefundUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookrefundupdatedpayload.js\";\nimport type { WebhookSubscriptionActivePayload } from \"@polar-sh/sdk/models/components/webhooksubscriptionactivepayload.js\";\nimport type { WebhookSubscriptionCanceledPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptioncanceledpayload.js\";\nimport type { WebhookSubscriptionCreatedPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptioncreatedpayload.js\";\nimport type { WebhookSubscriptionRevokedPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptionrevokedpayload.js\";\nimport type { WebhookSubscriptionUncanceledPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptionuncanceledpayload.js\";\nimport type { WebhookSubscriptionUpdatedPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptionupdatedpayload.js\";\nimport { validateEvent } from \"@polar-sh/sdk/webhooks.js\";\nimport { APIError, createAuthEndpoint } from \"better-auth/api\";\n\nexport interface WebhooksOptions {\n\t/**\n\t * Webhook Secret\n\t */\n\tsecret: string;\n\t/**\n\t * Generic handler for all webhooks\n\t */\n\tonPayload?: (payload: ReturnType<typeof validateEvent>) => Promise<void>;\n\t/**\n\t * Webhook for checkout created\n\t */\n\tonCheckoutCreated?: (payload: WebhookCheckoutCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for checkout updated\n\t */\n\tonCheckoutUpdated?: (payload: WebhookCheckoutUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for order created\n\t */\n\tonOrderCreated?: (payload: WebhookOrderCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for order refunded\n\t */\n\tonOrderRefunded?: (payload: WebhookOrderRefundedPayload) => Promise<void>;\n\t/**\n\t * Webhook for order paid\n\t */\n\tonOrderPaid?: (payload: WebhookOrderPaidPayload) => Promise<void>;\n\t/**\n\t * Webhook for refund created\n\t */\n\tonRefundCreated?: (payload: WebhookRefundCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for refund updated\n\t */\n\tonRefundUpdated?: (payload: WebhookRefundUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for subscription created\n\t */\n\tonSubscriptionCreated?: (\n\t\tpayload: WebhookSubscriptionCreatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription updated\n\t */\n\tonSubscriptionUpdated?: (\n\t\tpayload: WebhookSubscriptionUpdatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription active\n\t */\n\tonSubscriptionActive?: (\n\t\tpayload: WebhookSubscriptionActivePayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription canceled\n\t */\n\tonSubscriptionCanceled?: (\n\t\tpayload: WebhookSubscriptionCanceledPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription revoked\n\t */\n\tonSubscriptionRevoked?: (\n\t\tpayload: WebhookSubscriptionRevokedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription uncanceled\n\t */\n\tonSubscriptionUncanceled?: (\n\t\tpayload: WebhookSubscriptionUncanceledPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for product created\n\t */\n\tonProductCreated?: (payload: WebhookProductCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for product updated\n\t */\n\tonProductUpdated?: (payload: WebhookProductUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for organization updated\n\t */\n\tonOrganizationUpdated?: (\n\t\tpayload: WebhookOrganizationUpdatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for benefit created\n\t */\n\tonBenefitCreated?: (payload: WebhookBenefitCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for benefit updated\n\t */\n\tonBenefitUpdated?: (payload: WebhookBenefitUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for benefit grant created\n\t */\n\tonBenefitGrantCreated?: (\n\t\tpayload: WebhookBenefitGrantCreatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for benefit grant updated\n\t */\n\tonBenefitGrantUpdated?: (\n\t\tpayload: WebhookBenefitGrantUpdatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for benefit grant revoked\n\t */\n\tonBenefitGrantRevoked?: (\n\t\tpayload: WebhookBenefitGrantRevokedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for customer created\n\t */\n\tonCustomerCreated?: (payload: WebhookCustomerCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for customer updated\n\t */\n\tonCustomerUpdated?: (payload: WebhookCustomerUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for customer deleted\n\t */\n\tonCustomerDeleted?: (payload: WebhookCustomerDeletedPayload) => Promise<void>;\n\t/**\n\t * Webhook for customer state changed\n\t */\n\tonCustomerStateChanged?: (\n\t\tpayload: WebhookCustomerStateChangedPayload,\n\t) => Promise<void>;\n}\n\nexport const webhooks = (options: WebhooksOptions) => (_polar: Polar) => {\n\treturn {\n\t\tpolarWebhooks: createAuthEndpoint(\n\t\t\t\"/polar/webhooks\",\n\t\t\t{\n\t\t\t\tmethod: \"POST\",\n\t\t\t\tmetadata: {\n\t\t\t\t\tisAction: false,\n\t\t\t\t},\n\t\t\t\tcloneRequest: true,\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tconst { secret, ...eventHandlers } = options;\n\n\t\t\t\tif (!ctx.request?.body) {\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\");\n\t\t\t\t}\n\t\t\t\tconst buf = await ctx.request.text();\n\t\t\t\tlet event: ReturnType<typeof validateEvent>;\n\t\t\t\ttry {\n\t\t\t\t\tif (!secret) {\n\t\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\t\tmessage: \"Polar webhook secret not found\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\tconst headers = {\n\t\t\t\t\t\t\"webhook-id\": ctx.request.headers.get(\"webhook-id\") as string,\n\t\t\t\t\t\t\"webhook-timestamp\": ctx.request.headers.get(\n\t\t\t\t\t\t\t\"webhook-timestamp\",\n\t\t\t\t\t\t) as string,\n\t\t\t\t\t\t\"webhook-signature\": ctx.request.headers.get(\n\t\t\t\t\t\t\t\"webhook-signature\",\n\t\t\t\t\t\t) as string,\n\t\t\t\t\t};\n\n\t\t\t\t\tevent = validateEvent(buf, headers, secret);\n\t\t\t\t} catch (err: unknown) {\n\t\t\t\t\tif (err instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(`${err.message}`);\n\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\tmessage: `Webhook Error: ${err.message}`,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: `Webhook Error: ${err}`,\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tawait handleWebhookPayload(event, {\n\t\t\t\t\t\twebhookSecret: secret,\n\t\t\t\t\t\t...eventHandlers,\n\t\t\t\t\t});\n\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t`Polar webhook failed. Error: ${e.message}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t} else {\n\t\t\t\t\t\tctx.context.logger.error(`Polar webhook failed. Error: ${e}`);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"Webhook error: See server logs for more information.\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\treturn ctx.json({ received: true });\n\t\t\t},\n\t\t),\n\t};\n};\n"],"mappings":";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,YACvC,CAAC;AAAA,UACF;AACA,gBAAM,IAAI,qBAAS,eAAe;AAAA,YACjC,SAAS,kBAAkB,GAAG;AAAA,UAC/B,CAAC;AAAA,QACF;AAEA,YAAI;AACH,gBAAM,qBAAqB,OAAO;AAAA,YACjC,eAAe;AAAA,YACf,GAAG;AAAA,UACJ,CAAC;AAAA,QACF,SAAS,GAAY;AACpB,cAAI,aAAa,OAAO;AACvB,gBAAI,QAAQ,OAAO;AAAA,cAClB,gCAAgC,EAAE,OAAO;AAAA,YAC1C;AAAA,UACD,OAAO;AACN,gBAAI,QAAQ,OAAO,MAAM,gCAAgC,CAAC,EAAE;AAAA,UAC7D;AAEA,gBAAM,IAAI,qBAAS,eAAe;AAAA,YACjC,SAAS;AAAA,UACV,CAAC;AAAA,QACF;AAEA,eAAO,IAAI,KAAK,EAAE,UAAU,KAAK,CAAC;AAAA,MACnC;AAAA,IACD;AAAA,EACD;AACD;;;AP5NO,IAAM,QAAQ,CAAyB,YAAe;AAC5D,QAAM,UAAU,QAAQ,IACtB,IAAI,CAAC,QAAQ,IAAI,QAAQ,MAAM,CAAC,EAChC,OAAO,CAAC,KAAK,WAAW;AACxB,WAAO,OAAO,KAAK,MAAM;AACzB,WAAO;AAAA,EACR,GAAG,CAAC,CAAmB;AAExB,SAAO;AAAA,IACN,IAAI;AAAA,IACJ,WAAW;AAAA,MACV,GAAG;AAAA,IACJ;AAAA,IACA,OAAO;AACN,aAAO;AAAA,QACN,SAAS;AAAA,UACR,eAAe;AAAA,YACd,MAAM;AAAA,cACL,QAAQ;AAAA,gBACP,QAAQ,mBAAmB,OAAO;AAAA,gBAClC,OAAO,kBAAkB,OAAO;AAAA,cACjC;AAAA,cACA,QAAQ;AAAA,gBACP,OAAO,aAAa,OAAO;AAAA,cAC5B;AAAA,YACD;AAAA,UACD;AAAA,QACD;AAAA,MACD;AAAA,IACD;AAAA,EACD;AACD;","names":["import_api","polar","import_api","import_plugins","import_zod","polar","checkout","import_api","import_zod","polar","import_api"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/hooks/customer.ts","../src/client.ts","../src/plugins/portal.ts","../src/plugins/checkout.ts","../src/plugins/usage.ts","../../adapter-utils/dist/index.js","../src/plugins/webhooks.ts"],"sourcesContent":["import type { BetterAuthPlugin } from \"better-auth\";\nimport {\n\tonAfterUserCreate,\n\tonBeforeUserCreate,\n\tonUserUpdate,\n} from \"./hooks/customer\";\nimport type { PolarEndpoints, PolarOptions } from \"./types\";\n\nexport { polarClient } from \"./client\";\n\nexport * from \"./plugins/portal\";\nexport * from \"./plugins/checkout\";\nexport * from \"./plugins/usage\";\nexport * from \"./plugins/webhooks\";\n\nexport const polar = <O extends PolarOptions>(options: O) => {\n\tconst plugins = options.use\n\t\t.map((use) => use(options.client))\n\t\t.reduce((acc, plugin) => {\n\t\t\tObject.assign(acc, plugin);\n\t\t\treturn acc;\n\t\t}, {} as PolarEndpoints);\n\n\treturn {\n\t\tid: \"polar\",\n\t\tendpoints: {\n\t\t\t...plugins,\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\tbefore: onBeforeUserCreate(options),\n\t\t\t\t\t\t\t\tafter: onAfterUserCreate(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 type { GenericEndpointContext, User } from \"better-auth\";\nimport { APIError } from \"better-auth/api\";\nimport type { PolarOptions } from \"../types\";\n\nexport const onBeforeUserCreate =\n\t(options: PolarOptions) =>\n\tasync (user: Partial<User>, context?: GenericEndpointContext | undefined) => {\n\t\tif (context && options.createCustomerOnSignUp) {\n\t\t\ttry {\n\t\t\t\tconst params = options.getCustomerCreateParams\n\t\t\t\t\t? await options.getCustomerCreateParams({\n\t\t\t\t\t\t\tuser,\n\t\t\t\t\t\t})\n\t\t\t\t\t: {};\n\n\t\t\t\tif (!user.email) {\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"An associated email is required\",\n\t\t\t\t\t});\n\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});\n\t\t\t} catch (e: unknown) {\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 onAfterUserCreate =\n\t(options: PolarOptions) =>\n\tasync (user: User, context?: GenericEndpointContext | undefined) => {\n\t\tif (context && options.createCustomerOnSignUp) {\n\t\t\ttry {\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}\n\t\t\t} catch (e: unknown) {\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, context?: GenericEndpointContext | undefined) => {\n\t\tif (context && 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\tcustomerUpdateExternalID: {\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\tcontext.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\tcontext.context.logger.error(\n\t\t\t\t\t\t`Polar customer update failed. Error: ${e}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t};\n","import type { BetterAuthClientPlugin } from \"better-auth\";\nimport type { polar } from \"./index\";\n\nexport const polarClient = () => {\n\treturn {\n\t\tid: \"polar-client\",\n\t\t$InferServerPlugin: {} as ReturnType<typeof polar>,\n\t} satisfies BetterAuthClientPlugin;\n};\n","import type { Polar } from \"@polar-sh/sdk\";\nimport { APIError } from \"better-auth/api\";\nimport { sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport { z } from \"zod\";\n\nexport const portal = () => (polar: Polar) => {\n\treturn {\n\t\tportal: createAuthEndpoint(\n\t\t\t\"/customer/portal\",\n\t\t\t{\n\t\t\t\tmethod: \"GET\",\n\t\t\t\tuse: [sessionMiddleware],\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tif (!ctx.context.session?.user.id) {\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tconst customerSession = await polar.customerSessions.create({\n\t\t\t\t\t\texternalCustomerId: ctx.context.session?.user.id,\n\t\t\t\t\t});\n\n\t\t\t\t\treturn ctx.json({\n\t\t\t\t\t\turl: customerSession.customerPortalUrl,\n\t\t\t\t\t\tredirect: true,\n\t\t\t\t\t});\n\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t`Polar customer portal creation failed. Error: ${e.message}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: \"Customer portal creation failed\",\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t},\n\t\t),\n\t\tstate: createAuthEndpoint(\n\t\t\t\"/customer/state\",\n\t\t\t{\n\t\t\t\tmethod: \"GET\",\n\t\t\t\tuse: [sessionMiddleware],\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tconst state = await polar.customers.getStateExternal({\n\t\t\t\t\t\texternalId: ctx.context.session?.user.id,\n\t\t\t\t\t});\n\n\t\t\t\t\treturn ctx.json(state);\n\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t`Polar subscriptions list failed. Error: ${e.message}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: \"Subscriptions list failed\",\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t},\n\t\t),\n\t\tbenefits: createAuthEndpoint(\n\t\t\t\"/customer/benefits/list\",\n\t\t\t{\n\t\t\t\tmethod: \"GET\",\n\t\t\t\tquery: z\n\t\t\t\t\t.object({\n\t\t\t\t\t\tpage: z.coerce.number().optional(),\n\t\t\t\t\t\tlimit: z.coerce.number().optional(),\n\t\t\t\t\t})\n\t\t\t\t\t.optional(),\n\t\t\t\tuse: [sessionMiddleware],\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tconst customerSession = await polar.customerSessions.create({\n\t\t\t\t\t\texternalCustomerId: ctx.context.session?.user.id,\n\t\t\t\t\t});\n\n\t\t\t\t\tconst benefits = await polar.customerPortal.benefitGrants.list(\n\t\t\t\t\t\t{ customerSession: customerSession.token },\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tpage: ctx.query?.page,\n\t\t\t\t\t\t\tlimit: ctx.query?.limit,\n\t\t\t\t\t\t},\n\t\t\t\t\t);\n\n\t\t\t\t\treturn ctx.json(benefits);\n\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t`Polar benefits list failed. Error: ${e.message}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: \"Benefits list failed\",\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t},\n\t\t),\n\t\tsubscriptions: createAuthEndpoint(\n\t\t\t\"/customer/subscriptions/list\",\n\t\t\t{\n\t\t\t\tmethod: \"GET\",\n\t\t\t\tquery: z\n\t\t\t\t\t.object({\n\t\t\t\t\t\treferenceId: z.string().optional(),\n\t\t\t\t\t\tpage: z.coerce.number().optional(),\n\t\t\t\t\t\tlimit: z.coerce.number().optional(),\n\t\t\t\t\t\tactive: z.coerce.boolean().optional(),\n\t\t\t\t\t})\n\t\t\t\t\t.optional(),\n\t\t\t\tuse: [sessionMiddleware],\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\tif (ctx.query?.referenceId) {\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst subscriptions = await polar.subscriptions.list({\n\t\t\t\t\t\t\tpage: ctx.query?.page,\n\t\t\t\t\t\t\tlimit: ctx.query?.limit,\n\t\t\t\t\t\t\tactive: ctx.query?.active,\n\t\t\t\t\t\t\tmetadata: {\n\t\t\t\t\t\t\t\treferenceId: ctx.query?.referenceId,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\treturn ctx.json(subscriptions);\n\t\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\t\tconsole.log(e);\n\t\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t\t`Polar subscriptions list with referenceId failed. Error: ${e.message}`,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\t\tmessage: \"Subscriptions list with referenceId failed\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tconst customerSession = await polar.customerSessions.create({\n\t\t\t\t\t\texternalCustomerId: ctx.context.session?.user.id,\n\t\t\t\t\t});\n\n\t\t\t\t\tconst subscriptions = await polar.customerPortal.subscriptions.list(\n\t\t\t\t\t\t{ customerSession: customerSession.token },\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tpage: ctx.query?.page,\n\t\t\t\t\t\t\tlimit: ctx.query?.limit,\n\t\t\t\t\t\t\tactive: ctx.query?.active,\n\t\t\t\t\t\t},\n\t\t\t\t\t);\n\n\t\t\t\t\treturn ctx.json(subscriptions);\n\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t`Polar subscriptions list failed. Error: ${e.message}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: \"Polar subscriptions list failed\",\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t},\n\t\t),\n\t\torders: createAuthEndpoint(\n\t\t\t\"/customer/orders/list\",\n\t\t\t{\n\t\t\t\tmethod: \"GET\",\n\t\t\t\tquery: z\n\t\t\t\t\t.object({\n\t\t\t\t\t\tpage: z.coerce.number().optional(),\n\t\t\t\t\t\tlimit: z.coerce.number().optional(),\n\t\t\t\t\t\tproductBillingType: z.enum([\"recurring\", \"one_time\"]).optional(),\n\t\t\t\t\t})\n\t\t\t\t\t.optional(),\n\t\t\t\tuse: [sessionMiddleware],\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tconst customerSession = await polar.customerSessions.create({\n\t\t\t\t\t\texternalCustomerId: ctx.context.session?.user.id,\n\t\t\t\t\t});\n\n\t\t\t\t\tconst orders = await polar.customerPortal.orders.list(\n\t\t\t\t\t\t{ customerSession: customerSession.token },\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tpage: ctx.query?.page,\n\t\t\t\t\t\t\tlimit: ctx.query?.limit,\n\t\t\t\t\t\t\tproductBillingType: ctx.query?.productBillingType,\n\t\t\t\t\t\t},\n\t\t\t\t\t);\n\n\t\t\t\t\treturn ctx.json(orders);\n\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t`Polar orders list failed. Error: ${e.message}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: \"Orders list failed\",\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t},\n\t\t),\n\t};\n};\n","import type { Polar } from \"@polar-sh/sdk\";\nimport { APIError, getSessionFromCtx } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport { z } from \"zod\";\nimport type { Product } from \"../types\";\n\nexport interface CheckoutOptions {\n\t/**\n\t * Optional list of slug -> productId mappings for easy slug checkouts\n\t */\n\tproducts?: Product[] | (() => Promise<Product[]>);\n\t/**\n\t * Checkout Success URL\n\t */\n\tsuccessUrl?: string;\n\t/**\n\t * Only allow authenticated customers to checkout\n\t */\n\tauthenticatedUsersOnly?: boolean;\n\t/**\n\t * Checkout theme\n\t */\n\ttheme?: \"light\" | \"dark\";\n\t/**\n\t * Allow discount codes\n\t */\n\tallowDiscountCodes?: boolean;\n\t/**\n\t * Discount ID\n\t */\n\tdiscountId?: string;\n}\n\nexport const checkout =\n\t(checkoutOptions: CheckoutOptions = {}) =>\n\t(polar: Polar) => {\n\t\treturn {\n\t\t\tcheckout: createAuthEndpoint(\n\t\t\t\t\"/checkout\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\tbody: z.object({\n\t\t\t\t\t\tproducts: z.union([z.array(z.string()), z.string()]).optional(),\n\t\t\t\t\t\tslug: z.string().optional(),\n\t\t\t\t\t\treferenceId: z.string().optional(),\n\t\t\t\t\t\tcustomFieldData: z\n\t\t\t\t\t\t\t.record(\n\t\t\t\t\t\t\t\tz.string(),\n\t\t\t\t\t\t\t\tz.union([z.string(), z.number(), z.boolean()]),\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t.optional(),\n\t\t\t\t\t\tmetadata: z\n\t\t\t\t\t\t\t.record(\n\t\t\t\t\t\t\t\tz.string(),\n\t\t\t\t\t\t\t\tz.union([z.string(), z.number(), z.boolean()]),\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t.optional(),\n\t\t\t\t\t\tallowDiscountCodes: z.coerce.boolean().optional().default(true),\n\t\t\t\t\t\tdiscountId: z.string().optional(),\n\t\t\t\t\t}),\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tconst session = await getSessionFromCtx(ctx);\n\n\t\t\t\t\tlet productIds: string[] = [];\n\n\t\t\t\t\tif (ctx.body.slug) {\n\t\t\t\t\t\tconst resolvedProducts = await (typeof checkoutOptions.products ===\n\t\t\t\t\t\t\"function\"\n\t\t\t\t\t\t\t? checkoutOptions.products()\n\t\t\t\t\t\t\t: checkoutOptions.products);\n\n\t\t\t\t\t\tconst productId = resolvedProducts?.find(\n\t\t\t\t\t\t\t(product) => product.slug === ctx.body.slug,\n\t\t\t\t\t\t)?.productId;\n\n\t\t\t\t\t\tif (!productId) {\n\t\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\t\tmessage: \"Product not found\",\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tproductIds = [productId];\n\t\t\t\t\t} else {\n\t\t\t\t\t\tproductIds = Array.isArray(ctx.body.products)\n\t\t\t\t\t\t\t? ctx.body.products.filter((id) => id !== undefined)\n\t\t\t\t\t\t\t: [ctx.body.products].filter((id) => id !== undefined);\n\t\t\t\t\t}\n\n\t\t\t\t\tif (checkoutOptions.authenticatedUsersOnly && !session?.user.id) {\n\t\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\t\tmessage: \"You must be logged in to checkout\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst checkout = await polar.checkouts.create({\n\t\t\t\t\t\t\texternalCustomerId: session?.user.id,\n\t\t\t\t\t\t\tproducts: productIds,\n\t\t\t\t\t\t\tsuccessUrl: checkoutOptions.successUrl\n\t\t\t\t\t\t\t\t? new URL(\n\t\t\t\t\t\t\t\t\t\tcheckoutOptions.successUrl,\n\t\t\t\t\t\t\t\t\t\tctx.request?.url ?? ctx.context.baseURL,\n\t\t\t\t\t\t\t\t\t).toString()\n\t\t\t\t\t\t\t\t: undefined,\n\t\t\t\t\t\t\tmetadata: ctx.body.referenceId\n\t\t\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\t\t\treferenceId: ctx.body.referenceId,\n\t\t\t\t\t\t\t\t\t\t...ctx.body.metadata,\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t: ctx.body.metadata,\n\t\t\t\t\t\t\tcustomFieldData: ctx.body.customFieldData,\n\t\t\t\t\t\t\tallowDiscountCodes: ctx.body.allowDiscountCodes ?? true,\n\t\t\t\t\t\t\tdiscountId: ctx.body.discountId,\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tconst redirectUrl = new URL(checkout.url);\n\n\t\t\t\t\t\tif (checkoutOptions.theme) {\n\t\t\t\t\t\t\tredirectUrl.searchParams.set(\"theme\", checkoutOptions.theme);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\treturn ctx.json({\n\t\t\t\t\t\t\turl: redirectUrl.toString(),\n\t\t\t\t\t\t\tredirect: true,\n\t\t\t\t\t\t});\n\t\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t\t`Polar checkout creation failed. Error: ${e.message}`,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\t\tmessage: \"Checkout creation failed\",\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};\n","import type { Polar } from \"@polar-sh/sdk\";\nimport {\n\tAPIError,\n\tcreateAuthEndpoint,\n\tsessionMiddleware,\n} from \"better-auth/api\";\nimport { z } from \"zod\";\nimport type { Product } from \"../types\";\n\nexport interface UsageOptions {\n\t/**\n\t * Products to use for topping up credits\n\t */\n\tcreditProducts?: Product[] | (() => Promise<Product[]>);\n}\n\nexport const usage = (_usageOptions?: UsageOptions) => (polar: Polar) => {\n\treturn {\n\t\tmeters: createAuthEndpoint(\n\t\t\t\"/usage/meters/list\",\n\t\t\t{\n\t\t\t\tmethod: \"GET\",\n\t\t\t\tuse: [sessionMiddleware],\n\t\t\t\tquery: z.object({\n\t\t\t\t\tpage: z.coerce.number().optional(),\n\t\t\t\t\tlimit: z.coerce.number().optional(),\n\t\t\t\t}),\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tconst customerSession = await polar.customerSessions.create({\n\t\t\t\t\t\texternalCustomerId: ctx.context.session.user.id,\n\t\t\t\t\t});\n\n\t\t\t\t\tconst customerMeters = await polar.customerPortal.customerMeters.list(\n\t\t\t\t\t\t{ customerSession: customerSession.token },\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tpage: ctx.query?.page,\n\t\t\t\t\t\t\tlimit: ctx.query?.limit,\n\t\t\t\t\t\t},\n\t\t\t\t\t);\n\n\t\t\t\t\treturn ctx.json(customerMeters);\n\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t`Polar meters list failed. Error: ${e.message}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: \"Meters list failed\",\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t},\n\t\t),\n\t\tingestion: createAuthEndpoint(\n\t\t\t\"/usage/ingest\",\n\t\t\t{\n\t\t\t\tmethod: \"POST\",\n\t\t\t\tbody: z.object({\n\t\t\t\t\tevent: z.string(),\n\t\t\t\t\tmetadata: z.record(\n\t\t\t\t\t\tz.string(),\n\t\t\t\t\t\tz.union([z.string(), z.number(), z.boolean()]),\n\t\t\t\t\t),\n\t\t\t\t}),\n\t\t\t\tuse: [sessionMiddleware],\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tconst ingestion = await polar.events.ingest({\n\t\t\t\t\t\tevents: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tname: ctx.body.event,\n\t\t\t\t\t\t\t\tmetadata: ctx.body.metadata,\n\t\t\t\t\t\t\t\texternalCustomerId: ctx.context.session.user.id,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t],\n\t\t\t\t\t});\n\n\t\t\t\t\treturn ctx.json(ingestion);\n\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t`Polar ingestion failed. Error: ${e.message}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: \"Ingestion failed\",\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t},\n\t\t),\n\t};\n};\n","// src/webhooks/webhooks.ts\nvar handleWebhookPayload = async (payload, { webhookSecret, entitlements, onPayload, ...eventHandlers }) => {\n const promises = [];\n if (onPayload) {\n promises.push(onPayload(payload));\n }\n switch (payload.type) {\n case \"checkout.created\":\n if (eventHandlers.onCheckoutCreated) {\n promises.push(eventHandlers.onCheckoutCreated(payload));\n }\n break;\n case \"checkout.updated\":\n if (eventHandlers.onCheckoutUpdated) {\n promises.push(eventHandlers.onCheckoutUpdated(payload));\n }\n break;\n case \"order.created\":\n if (eventHandlers.onOrderCreated) {\n promises.push(eventHandlers.onOrderCreated(payload));\n }\n break;\n case \"order.updated\":\n if (eventHandlers.onOrderUpdated) {\n promises.push(eventHandlers.onOrderUpdated(payload));\n }\n break;\n case \"order.paid\":\n if (eventHandlers.onOrderPaid) {\n promises.push(eventHandlers.onOrderPaid(payload));\n }\n break;\n case \"subscription.created\":\n if (eventHandlers.onSubscriptionCreated) {\n promises.push(eventHandlers.onSubscriptionCreated(payload));\n }\n break;\n case \"subscription.updated\":\n if (eventHandlers.onSubscriptionUpdated) {\n promises.push(eventHandlers.onSubscriptionUpdated(payload));\n }\n break;\n case \"subscription.active\":\n if (eventHandlers.onSubscriptionActive) {\n promises.push(eventHandlers.onSubscriptionActive(payload));\n }\n break;\n case \"subscription.canceled\":\n if (eventHandlers.onSubscriptionCanceled) {\n promises.push(eventHandlers.onSubscriptionCanceled(payload));\n }\n break;\n case \"subscription.uncanceled\":\n if (eventHandlers.onSubscriptionUncanceled) {\n promises.push(eventHandlers.onSubscriptionUncanceled(payload));\n }\n break;\n case \"subscription.revoked\":\n if (eventHandlers.onSubscriptionRevoked) {\n promises.push(eventHandlers.onSubscriptionRevoked(payload));\n }\n break;\n case \"product.created\":\n if (eventHandlers.onProductCreated) {\n promises.push(eventHandlers.onProductCreated(payload));\n }\n break;\n case \"product.updated\":\n if (eventHandlers.onProductUpdated) {\n promises.push(eventHandlers.onProductUpdated(payload));\n }\n break;\n case \"organization.updated\":\n if (eventHandlers.onOrganizationUpdated) {\n promises.push(eventHandlers.onOrganizationUpdated(payload));\n }\n break;\n case \"benefit.created\":\n if (eventHandlers.onBenefitCreated) {\n promises.push(eventHandlers.onBenefitCreated(payload));\n }\n break;\n case \"benefit.updated\":\n if (eventHandlers.onBenefitUpdated) {\n promises.push(eventHandlers.onBenefitUpdated(payload));\n }\n break;\n case \"benefit_grant.created\":\n if (eventHandlers.onBenefitGrantCreated) {\n promises.push(eventHandlers.onBenefitGrantCreated(payload));\n }\n break;\n case \"benefit_grant.updated\":\n if (eventHandlers.onBenefitGrantUpdated) {\n promises.push(eventHandlers.onBenefitGrantUpdated(payload));\n }\n break;\n case \"benefit_grant.revoked\":\n if (eventHandlers.onBenefitGrantRevoked) {\n promises.push(eventHandlers.onBenefitGrantRevoked(payload));\n }\n break;\n case \"customer.created\":\n if (eventHandlers.onCustomerCreated) {\n promises.push(eventHandlers.onCustomerCreated(payload));\n }\n break;\n case \"customer.updated\":\n if (eventHandlers.onCustomerUpdated) {\n promises.push(eventHandlers.onCustomerUpdated(payload));\n }\n break;\n case \"customer.deleted\":\n if (eventHandlers.onCustomerDeleted) {\n promises.push(eventHandlers.onCustomerDeleted(payload));\n }\n break;\n case \"customer.state_changed\":\n if (eventHandlers.onCustomerStateChanged) {\n promises.push(eventHandlers.onCustomerStateChanged(payload));\n }\n break;\n case \"order.refunded\":\n if (eventHandlers.onOrderRefunded) {\n promises.push(eventHandlers.onOrderRefunded(payload));\n }\n break;\n case \"refund.created\":\n if (eventHandlers.onRefundCreated) {\n promises.push(eventHandlers.onRefundCreated(payload));\n }\n break;\n case \"refund.updated\":\n if (eventHandlers.onRefundUpdated) {\n promises.push(eventHandlers.onRefundUpdated(payload));\n }\n break;\n }\n switch (payload.type) {\n case \"benefit_grant.created\":\n case \"benefit_grant.revoked\":\n if (entitlements) {\n for (const handler of entitlements.handlers) {\n promises.push(handler(payload));\n }\n }\n }\n return Promise.all(promises);\n};\n\n// src/entitlement/entitlement.ts\nvar EntitlementStrategy = class {\n grantCallbacks = [];\n revokeCallbacks = [];\n grant(callback) {\n this.grantCallbacks.push(callback);\n return this;\n }\n revoke(callback) {\n this.revokeCallbacks.push(callback);\n return this;\n }\n handler(slug) {\n return async (payload) => {\n if (payload.data.benefit.description === slug) {\n switch (payload.type) {\n case \"benefit_grant.created\":\n await Promise.all(\n this.grantCallbacks.map(\n (callback) => callback({\n customer: payload.data.customer,\n properties: payload.data.properties,\n payload\n })\n )\n );\n break;\n case \"benefit_grant.revoked\":\n await Promise.all(\n this.revokeCallbacks.map(\n (callback) => callback({\n customer: payload.data.customer,\n properties: payload.data.properties,\n payload\n })\n )\n );\n break;\n }\n }\n };\n }\n};\nvar Entitlements = class {\n static handlers = [];\n static use(slug, strategy) {\n this.handlers.push(strategy.handler(slug));\n return this;\n }\n};\nexport {\n EntitlementStrategy,\n Entitlements,\n handleWebhookPayload\n};\n","import { handleWebhookPayload } from \"@polar-sh/adapter-utils\";\nimport type { Polar } from \"@polar-sh/sdk\";\nimport type { WebhookBenefitCreatedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitcreatedpayload\";\nimport type { WebhookBenefitGrantCreatedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitgrantcreatedpayload\";\nimport type { WebhookBenefitGrantRevokedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitgrantrevokedpayload\";\nimport type { WebhookBenefitGrantUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitgrantupdatedpayload\";\nimport type { WebhookBenefitUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitupdatedpayload\";\nimport type { WebhookCheckoutCreatedPayload } from \"@polar-sh/sdk/models/components/webhookcheckoutcreatedpayload\";\nimport type { WebhookCheckoutUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookcheckoutupdatedpayload\";\nimport type { WebhookCustomerCreatedPayload } from \"@polar-sh/sdk/models/components/webhookcustomercreatedpayload\";\nimport type { WebhookCustomerDeletedPayload } from \"@polar-sh/sdk/models/components/webhookcustomerdeletedpayload\";\nimport type { WebhookCustomerStateChangedPayload } from \"@polar-sh/sdk/models/components/webhookcustomerstatechangedpayload\";\nimport type { WebhookCustomerUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookcustomerupdatedpayload\";\nimport type { WebhookOrderCreatedPayload } from \"@polar-sh/sdk/models/components/webhookordercreatedpayload\";\nimport type { WebhookOrderPaidPayload } from \"@polar-sh/sdk/models/components/webhookorderpaidpayload\";\nimport type { WebhookOrderRefundedPayload } from \"@polar-sh/sdk/models/components/webhookorderrefundedpayload\";\nimport type { WebhookOrganizationUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookorganizationupdatedpayload\";\nimport type { WebhookProductCreatedPayload } from \"@polar-sh/sdk/models/components/webhookproductcreatedpayload\";\nimport type { WebhookProductUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookproductupdatedpayload\";\nimport type { WebhookRefundCreatedPayload } from \"@polar-sh/sdk/models/components/webhookrefundcreatedpayload\";\nimport type { WebhookRefundUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookrefundupdatedpayload\";\nimport type { WebhookSubscriptionActivePayload } from \"@polar-sh/sdk/models/components/webhooksubscriptionactivepayload\";\nimport type { WebhookSubscriptionCanceledPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptioncanceledpayload\";\nimport type { WebhookSubscriptionCreatedPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptioncreatedpayload\";\nimport type { WebhookSubscriptionRevokedPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptionrevokedpayload\";\nimport type { WebhookSubscriptionUncanceledPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptionuncanceledpayload\";\nimport type { WebhookSubscriptionUpdatedPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptionupdatedpayload\";\nimport { validateEvent } from \"@polar-sh/sdk/webhooks\";\nimport { APIError, createAuthEndpoint } from \"better-auth/api\";\n\nexport interface WebhooksOptions {\n\t/**\n\t * Webhook Secret\n\t */\n\tsecret: string;\n\t/**\n\t * Generic handler for all webhooks\n\t */\n\tonPayload?: (payload: ReturnType<typeof validateEvent>) => Promise<void>;\n\t/**\n\t * Webhook for checkout created\n\t */\n\tonCheckoutCreated?: (payload: WebhookCheckoutCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for checkout updated\n\t */\n\tonCheckoutUpdated?: (payload: WebhookCheckoutUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for order created\n\t */\n\tonOrderCreated?: (payload: WebhookOrderCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for order refunded\n\t */\n\tonOrderRefunded?: (payload: WebhookOrderRefundedPayload) => Promise<void>;\n\t/**\n\t * Webhook for order paid\n\t */\n\tonOrderPaid?: (payload: WebhookOrderPaidPayload) => Promise<void>;\n\t/**\n\t * Webhook for refund created\n\t */\n\tonRefundCreated?: (payload: WebhookRefundCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for refund updated\n\t */\n\tonRefundUpdated?: (payload: WebhookRefundUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for subscription created\n\t */\n\tonSubscriptionCreated?: (\n\t\tpayload: WebhookSubscriptionCreatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription updated\n\t */\n\tonSubscriptionUpdated?: (\n\t\tpayload: WebhookSubscriptionUpdatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription active\n\t */\n\tonSubscriptionActive?: (\n\t\tpayload: WebhookSubscriptionActivePayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription canceled\n\t */\n\tonSubscriptionCanceled?: (\n\t\tpayload: WebhookSubscriptionCanceledPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription revoked\n\t */\n\tonSubscriptionRevoked?: (\n\t\tpayload: WebhookSubscriptionRevokedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription uncanceled\n\t */\n\tonSubscriptionUncanceled?: (\n\t\tpayload: WebhookSubscriptionUncanceledPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for product created\n\t */\n\tonProductCreated?: (payload: WebhookProductCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for product updated\n\t */\n\tonProductUpdated?: (payload: WebhookProductUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for organization updated\n\t */\n\tonOrganizationUpdated?: (\n\t\tpayload: WebhookOrganizationUpdatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for benefit created\n\t */\n\tonBenefitCreated?: (payload: WebhookBenefitCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for benefit updated\n\t */\n\tonBenefitUpdated?: (payload: WebhookBenefitUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for benefit grant created\n\t */\n\tonBenefitGrantCreated?: (\n\t\tpayload: WebhookBenefitGrantCreatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for benefit grant updated\n\t */\n\tonBenefitGrantUpdated?: (\n\t\tpayload: WebhookBenefitGrantUpdatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for benefit grant revoked\n\t */\n\tonBenefitGrantRevoked?: (\n\t\tpayload: WebhookBenefitGrantRevokedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for customer created\n\t */\n\tonCustomerCreated?: (payload: WebhookCustomerCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for customer updated\n\t */\n\tonCustomerUpdated?: (payload: WebhookCustomerUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for customer deleted\n\t */\n\tonCustomerDeleted?: (payload: WebhookCustomerDeletedPayload) => Promise<void>;\n\t/**\n\t * Webhook for customer state changed\n\t */\n\tonCustomerStateChanged?: (\n\t\tpayload: WebhookCustomerStateChangedPayload,\n\t) => Promise<void>;\n}\n\nexport const webhooks = (options: WebhooksOptions) => (_polar: Polar) => {\n\treturn {\n\t\tpolarWebhooks: createAuthEndpoint(\n\t\t\t\"/polar/webhooks\",\n\t\t\t{\n\t\t\t\tmethod: \"POST\",\n\t\t\t\tmetadata: {\n\t\t\t\t\tisAction: false,\n\t\t\t\t},\n\t\t\t\tcloneRequest: true,\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tconst { secret, ...eventHandlers } = options;\n\n\t\t\t\tif (!ctx.request?.body) {\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\");\n\t\t\t\t}\n\t\t\t\tconst buf = await ctx.request.text();\n\t\t\t\tlet event: ReturnType<typeof validateEvent>;\n\t\t\t\ttry {\n\t\t\t\t\tif (!secret) {\n\t\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\t\tmessage: \"Polar webhook secret not found\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\tconst headers = {\n\t\t\t\t\t\t\"webhook-id\": ctx.request.headers.get(\"webhook-id\") as string,\n\t\t\t\t\t\t\"webhook-timestamp\": ctx.request.headers.get(\n\t\t\t\t\t\t\t\"webhook-timestamp\",\n\t\t\t\t\t\t) as string,\n\t\t\t\t\t\t\"webhook-signature\": ctx.request.headers.get(\n\t\t\t\t\t\t\t\"webhook-signature\",\n\t\t\t\t\t\t) as string,\n\t\t\t\t\t};\n\n\t\t\t\t\tevent = validateEvent(buf, headers, secret);\n\t\t\t\t} catch (err: unknown) {\n\t\t\t\t\tif (err instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(`${err.message}`);\n\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\tmessage: `Webhook Error: ${err.message}`,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: `Webhook Error: ${err}`,\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tawait handleWebhookPayload(event, {\n\t\t\t\t\t\twebhookSecret: secret,\n\t\t\t\t\t\t...eventHandlers,\n\t\t\t\t\t});\n\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t`Polar webhook failed. Error: ${e.message}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t} else {\n\t\t\t\t\t\tctx.context.logger.error(`Polar webhook failed. Error: ${e}`);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"Webhook error: See server logs for more information.\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\treturn ctx.json({ received: true });\n\t\t\t},\n\t\t),\n\t};\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCA,iBAAyB;AAGlB,IAAM,qBACZ,CAAC,YACD,OAAO,MAAqB,YAAiD;AAC5E,MAAI,WAAW,QAAQ,wBAAwB;AAC9C,QAAI;AACH,YAAM,SAAS,QAAQ,0BACpB,MAAM,QAAQ,wBAAwB;AAAA,QACtC;AAAA,MACD,CAAC,IACA,CAAC;AAEJ,UAAI,CAAC,KAAK,OAAO;AAChB,cAAM,IAAI,oBAAS,eAAe;AAAA,UACjC,SAAS;AAAA,QACV,CAAC;AAAA,MACF;AAEA,YAAM,QAAQ,OAAO,UAAU,OAAO;AAAA,QACrC,GAAG;AAAA,QACH,OAAO,KAAK;AAAA,QACZ,MAAM,KAAK;AAAA,MACZ,CAAC;AAAA,IACF,SAAS,GAAY;AACpB,UAAI,aAAa,OAAO;AACvB,cAAM,IAAI,oBAAS,yBAAyB;AAAA,UAC3C,SAAS,0CAA0C,EAAE,OAAO;AAAA,QAC7D,CAAC;AAAA,MACF;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AAC,QAAQ,IAAI,QAAQ,MAAM,CAAC,EAChC,OAAO,CAAC,KAAK,WAAW;AACxB,WAAO,OAAO,KAAK,MAAM;AACzB,WAAO;AAAA,EACR,GAAG,CAAC,CAAmB;AAExB,SAAO;AAAA,IACN,IAAI;AAAA,IACJ,WAAW;AAAA,MACV,GAAG;AAAA,IACJ;AAAA,IACA,OAAO;AACN,aAAO;AAAA,QACN,SAAS;AAAA,UACR,eAAe;AAAA,YACd,MAAM;AAAA,cACL,QAAQ;AAAA,gBACP,QAAQ,mBAAmB,OAAO;AAAA,gBAClC,OAAO,kBAAkB,OAAO;AAAA,cACjC;AAAA,cACA,QAAQ;AAAA,gBACP,OAAO,aAAa,OAAO;AAAA,cAC5B;AAAA,YACD;AAAA,UACD;AAAA,QACD;AAAA,MACD;AAAA,IACD;AAAA,EACD;AACD;","names":["import_api","polar","import_api","import_plugins","import_zod","polar","checkout","import_api","import_zod","polar","import_api"]}
package/dist/index.d.cts CHANGED
@@ -11,32 +11,32 @@ import { z } from 'zod';
11
11
  import * as better_auth from 'better-auth';
12
12
  import { User } from 'better-auth';
13
13
  import { Polar } from '@polar-sh/sdk';
14
- import { WebhookBenefitCreatedPayload } from '@polar-sh/sdk/models/components/webhookbenefitcreatedpayload.js';
15
- import { WebhookBenefitGrantCreatedPayload } from '@polar-sh/sdk/models/components/webhookbenefitgrantcreatedpayload.js';
16
- import { WebhookBenefitGrantRevokedPayload } from '@polar-sh/sdk/models/components/webhookbenefitgrantrevokedpayload.js';
17
- import { WebhookBenefitGrantUpdatedPayload } from '@polar-sh/sdk/models/components/webhookbenefitgrantupdatedpayload.js';
18
- import { WebhookBenefitUpdatedPayload } from '@polar-sh/sdk/models/components/webhookbenefitupdatedpayload.js';
19
- import { WebhookCheckoutCreatedPayload } from '@polar-sh/sdk/models/components/webhookcheckoutcreatedpayload.js';
20
- import { WebhookCheckoutUpdatedPayload } from '@polar-sh/sdk/models/components/webhookcheckoutupdatedpayload.js';
21
- import { WebhookCustomerCreatedPayload } from '@polar-sh/sdk/models/components/webhookcustomercreatedpayload.js';
22
- import { WebhookCustomerDeletedPayload } from '@polar-sh/sdk/models/components/webhookcustomerdeletedpayload.js';
23
- import { WebhookCustomerStateChangedPayload } from '@polar-sh/sdk/models/components/webhookcustomerstatechangedpayload.js';
24
- import { WebhookCustomerUpdatedPayload } from '@polar-sh/sdk/models/components/webhookcustomerupdatedpayload.js';
25
- import { WebhookOrderCreatedPayload } from '@polar-sh/sdk/models/components/webhookordercreatedpayload.js';
26
- import { WebhookOrderPaidPayload } from '@polar-sh/sdk/models/components/webhookorderpaidpayload.js';
27
- import { WebhookOrderRefundedPayload } from '@polar-sh/sdk/models/components/webhookorderrefundedpayload.js';
28
- import { WebhookOrganizationUpdatedPayload } from '@polar-sh/sdk/models/components/webhookorganizationupdatedpayload.js';
29
- import { WebhookProductCreatedPayload } from '@polar-sh/sdk/models/components/webhookproductcreatedpayload.js';
30
- import { WebhookProductUpdatedPayload } from '@polar-sh/sdk/models/components/webhookproductupdatedpayload.js';
31
- import { WebhookRefundCreatedPayload } from '@polar-sh/sdk/models/components/webhookrefundcreatedpayload.js';
32
- import { WebhookRefundUpdatedPayload } from '@polar-sh/sdk/models/components/webhookrefundupdatedpayload.js';
33
- import { WebhookSubscriptionActivePayload } from '@polar-sh/sdk/models/components/webhooksubscriptionactivepayload.js';
34
- import { WebhookSubscriptionCanceledPayload } from '@polar-sh/sdk/models/components/webhooksubscriptioncanceledpayload.js';
35
- import { WebhookSubscriptionCreatedPayload } from '@polar-sh/sdk/models/components/webhooksubscriptioncreatedpayload.js';
36
- import { WebhookSubscriptionRevokedPayload } from '@polar-sh/sdk/models/components/webhooksubscriptionrevokedpayload.js';
37
- import { WebhookSubscriptionUncanceledPayload } from '@polar-sh/sdk/models/components/webhooksubscriptionuncanceledpayload.js';
38
- import { WebhookSubscriptionUpdatedPayload } from '@polar-sh/sdk/models/components/webhooksubscriptionupdatedpayload.js';
39
- import { validateEvent } from '@polar-sh/sdk/webhooks.js';
14
+ import { WebhookBenefitCreatedPayload } from '@polar-sh/sdk/models/components/webhookbenefitcreatedpayload';
15
+ import { WebhookBenefitGrantCreatedPayload } from '@polar-sh/sdk/models/components/webhookbenefitgrantcreatedpayload';
16
+ import { WebhookBenefitGrantRevokedPayload } from '@polar-sh/sdk/models/components/webhookbenefitgrantrevokedpayload';
17
+ import { WebhookBenefitGrantUpdatedPayload } from '@polar-sh/sdk/models/components/webhookbenefitgrantupdatedpayload';
18
+ import { WebhookBenefitUpdatedPayload } from '@polar-sh/sdk/models/components/webhookbenefitupdatedpayload';
19
+ import { WebhookCheckoutCreatedPayload } from '@polar-sh/sdk/models/components/webhookcheckoutcreatedpayload';
20
+ import { WebhookCheckoutUpdatedPayload } from '@polar-sh/sdk/models/components/webhookcheckoutupdatedpayload';
21
+ import { WebhookCustomerCreatedPayload } from '@polar-sh/sdk/models/components/webhookcustomercreatedpayload';
22
+ import { WebhookCustomerDeletedPayload } from '@polar-sh/sdk/models/components/webhookcustomerdeletedpayload';
23
+ import { WebhookCustomerStateChangedPayload } from '@polar-sh/sdk/models/components/webhookcustomerstatechangedpayload';
24
+ import { WebhookCustomerUpdatedPayload } from '@polar-sh/sdk/models/components/webhookcustomerupdatedpayload';
25
+ import { WebhookOrderCreatedPayload } from '@polar-sh/sdk/models/components/webhookordercreatedpayload';
26
+ import { WebhookOrderPaidPayload } from '@polar-sh/sdk/models/components/webhookorderpaidpayload';
27
+ import { WebhookOrderRefundedPayload } from '@polar-sh/sdk/models/components/webhookorderrefundedpayload';
28
+ import { WebhookOrganizationUpdatedPayload } from '@polar-sh/sdk/models/components/webhookorganizationupdatedpayload';
29
+ import { WebhookProductCreatedPayload } from '@polar-sh/sdk/models/components/webhookproductcreatedpayload';
30
+ import { WebhookProductUpdatedPayload } from '@polar-sh/sdk/models/components/webhookproductupdatedpayload';
31
+ import { WebhookRefundCreatedPayload } from '@polar-sh/sdk/models/components/webhookrefundcreatedpayload';
32
+ import { WebhookRefundUpdatedPayload } from '@polar-sh/sdk/models/components/webhookrefundupdatedpayload';
33
+ import { WebhookSubscriptionActivePayload } from '@polar-sh/sdk/models/components/webhooksubscriptionactivepayload';
34
+ import { WebhookSubscriptionCanceledPayload } from '@polar-sh/sdk/models/components/webhooksubscriptioncanceledpayload';
35
+ import { WebhookSubscriptionCreatedPayload } from '@polar-sh/sdk/models/components/webhooksubscriptioncreatedpayload';
36
+ import { WebhookSubscriptionRevokedPayload } from '@polar-sh/sdk/models/components/webhooksubscriptionrevokedpayload';
37
+ import { WebhookSubscriptionUncanceledPayload } from '@polar-sh/sdk/models/components/webhooksubscriptionuncanceledpayload';
38
+ import { WebhookSubscriptionUpdatedPayload } from '@polar-sh/sdk/models/components/webhooksubscriptionupdatedpayload';
39
+ import { validateEvent } from '@polar-sh/sdk/webhooks';
40
40
 
41
41
  interface CheckoutOptions {
42
42
  /**
package/dist/index.d.ts CHANGED
@@ -11,32 +11,32 @@ import { z } from 'zod';
11
11
  import * as better_auth from 'better-auth';
12
12
  import { User } from 'better-auth';
13
13
  import { Polar } from '@polar-sh/sdk';
14
- import { WebhookBenefitCreatedPayload } from '@polar-sh/sdk/models/components/webhookbenefitcreatedpayload.js';
15
- import { WebhookBenefitGrantCreatedPayload } from '@polar-sh/sdk/models/components/webhookbenefitgrantcreatedpayload.js';
16
- import { WebhookBenefitGrantRevokedPayload } from '@polar-sh/sdk/models/components/webhookbenefitgrantrevokedpayload.js';
17
- import { WebhookBenefitGrantUpdatedPayload } from '@polar-sh/sdk/models/components/webhookbenefitgrantupdatedpayload.js';
18
- import { WebhookBenefitUpdatedPayload } from '@polar-sh/sdk/models/components/webhookbenefitupdatedpayload.js';
19
- import { WebhookCheckoutCreatedPayload } from '@polar-sh/sdk/models/components/webhookcheckoutcreatedpayload.js';
20
- import { WebhookCheckoutUpdatedPayload } from '@polar-sh/sdk/models/components/webhookcheckoutupdatedpayload.js';
21
- import { WebhookCustomerCreatedPayload } from '@polar-sh/sdk/models/components/webhookcustomercreatedpayload.js';
22
- import { WebhookCustomerDeletedPayload } from '@polar-sh/sdk/models/components/webhookcustomerdeletedpayload.js';
23
- import { WebhookCustomerStateChangedPayload } from '@polar-sh/sdk/models/components/webhookcustomerstatechangedpayload.js';
24
- import { WebhookCustomerUpdatedPayload } from '@polar-sh/sdk/models/components/webhookcustomerupdatedpayload.js';
25
- import { WebhookOrderCreatedPayload } from '@polar-sh/sdk/models/components/webhookordercreatedpayload.js';
26
- import { WebhookOrderPaidPayload } from '@polar-sh/sdk/models/components/webhookorderpaidpayload.js';
27
- import { WebhookOrderRefundedPayload } from '@polar-sh/sdk/models/components/webhookorderrefundedpayload.js';
28
- import { WebhookOrganizationUpdatedPayload } from '@polar-sh/sdk/models/components/webhookorganizationupdatedpayload.js';
29
- import { WebhookProductCreatedPayload } from '@polar-sh/sdk/models/components/webhookproductcreatedpayload.js';
30
- import { WebhookProductUpdatedPayload } from '@polar-sh/sdk/models/components/webhookproductupdatedpayload.js';
31
- import { WebhookRefundCreatedPayload } from '@polar-sh/sdk/models/components/webhookrefundcreatedpayload.js';
32
- import { WebhookRefundUpdatedPayload } from '@polar-sh/sdk/models/components/webhookrefundupdatedpayload.js';
33
- import { WebhookSubscriptionActivePayload } from '@polar-sh/sdk/models/components/webhooksubscriptionactivepayload.js';
34
- import { WebhookSubscriptionCanceledPayload } from '@polar-sh/sdk/models/components/webhooksubscriptioncanceledpayload.js';
35
- import { WebhookSubscriptionCreatedPayload } from '@polar-sh/sdk/models/components/webhooksubscriptioncreatedpayload.js';
36
- import { WebhookSubscriptionRevokedPayload } from '@polar-sh/sdk/models/components/webhooksubscriptionrevokedpayload.js';
37
- import { WebhookSubscriptionUncanceledPayload } from '@polar-sh/sdk/models/components/webhooksubscriptionuncanceledpayload.js';
38
- import { WebhookSubscriptionUpdatedPayload } from '@polar-sh/sdk/models/components/webhooksubscriptionupdatedpayload.js';
39
- import { validateEvent } from '@polar-sh/sdk/webhooks.js';
14
+ import { WebhookBenefitCreatedPayload } from '@polar-sh/sdk/models/components/webhookbenefitcreatedpayload';
15
+ import { WebhookBenefitGrantCreatedPayload } from '@polar-sh/sdk/models/components/webhookbenefitgrantcreatedpayload';
16
+ import { WebhookBenefitGrantRevokedPayload } from '@polar-sh/sdk/models/components/webhookbenefitgrantrevokedpayload';
17
+ import { WebhookBenefitGrantUpdatedPayload } from '@polar-sh/sdk/models/components/webhookbenefitgrantupdatedpayload';
18
+ import { WebhookBenefitUpdatedPayload } from '@polar-sh/sdk/models/components/webhookbenefitupdatedpayload';
19
+ import { WebhookCheckoutCreatedPayload } from '@polar-sh/sdk/models/components/webhookcheckoutcreatedpayload';
20
+ import { WebhookCheckoutUpdatedPayload } from '@polar-sh/sdk/models/components/webhookcheckoutupdatedpayload';
21
+ import { WebhookCustomerCreatedPayload } from '@polar-sh/sdk/models/components/webhookcustomercreatedpayload';
22
+ import { WebhookCustomerDeletedPayload } from '@polar-sh/sdk/models/components/webhookcustomerdeletedpayload';
23
+ import { WebhookCustomerStateChangedPayload } from '@polar-sh/sdk/models/components/webhookcustomerstatechangedpayload';
24
+ import { WebhookCustomerUpdatedPayload } from '@polar-sh/sdk/models/components/webhookcustomerupdatedpayload';
25
+ import { WebhookOrderCreatedPayload } from '@polar-sh/sdk/models/components/webhookordercreatedpayload';
26
+ import { WebhookOrderPaidPayload } from '@polar-sh/sdk/models/components/webhookorderpaidpayload';
27
+ import { WebhookOrderRefundedPayload } from '@polar-sh/sdk/models/components/webhookorderrefundedpayload';
28
+ import { WebhookOrganizationUpdatedPayload } from '@polar-sh/sdk/models/components/webhookorganizationupdatedpayload';
29
+ import { WebhookProductCreatedPayload } from '@polar-sh/sdk/models/components/webhookproductcreatedpayload';
30
+ import { WebhookProductUpdatedPayload } from '@polar-sh/sdk/models/components/webhookproductupdatedpayload';
31
+ import { WebhookRefundCreatedPayload } from '@polar-sh/sdk/models/components/webhookrefundcreatedpayload';
32
+ import { WebhookRefundUpdatedPayload } from '@polar-sh/sdk/models/components/webhookrefundupdatedpayload';
33
+ import { WebhookSubscriptionActivePayload } from '@polar-sh/sdk/models/components/webhooksubscriptionactivepayload';
34
+ import { WebhookSubscriptionCanceledPayload } from '@polar-sh/sdk/models/components/webhooksubscriptioncanceledpayload';
35
+ import { WebhookSubscriptionCreatedPayload } from '@polar-sh/sdk/models/components/webhooksubscriptioncreatedpayload';
36
+ import { WebhookSubscriptionRevokedPayload } from '@polar-sh/sdk/models/components/webhooksubscriptionrevokedpayload';
37
+ import { WebhookSubscriptionUncanceledPayload } from '@polar-sh/sdk/models/components/webhooksubscriptionuncanceledpayload';
38
+ import { WebhookSubscriptionUpdatedPayload } from '@polar-sh/sdk/models/components/webhooksubscriptionupdatedpayload';
39
+ import { validateEvent } from '@polar-sh/sdk/webhooks';
40
40
 
41
41
  interface CheckoutOptions {
42
42
  /**
package/dist/index.js CHANGED
@@ -360,7 +360,7 @@ var checkout = (checkoutOptions = {}) => (polar2) => {
360
360
  products: productIds,
361
361
  successUrl: checkoutOptions.successUrl ? new URL(
362
362
  checkoutOptions.successUrl,
363
- ctx.request?.url
363
+ ctx.request?.url ?? ctx.context.baseURL
364
364
  ).toString() : void 0,
365
365
  metadata: ctx.body.referenceId ? {
366
366
  referenceId: ctx.body.referenceId,
@@ -638,7 +638,7 @@ var handleWebhookPayload = async (payload, { webhookSecret, entitlements, onPayl
638
638
  };
639
639
 
640
640
  // src/plugins/webhooks.ts
641
- import { validateEvent } from "@polar-sh/sdk/webhooks.js";
641
+ import { validateEvent } from "@polar-sh/sdk/webhooks";
642
642
  import { APIError as APIError5, createAuthEndpoint as createAuthEndpoint4 } from "better-auth/api";
643
643
  var webhooks = (options) => (_polar) => {
644
644
  return {
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/hooks/customer.ts","../src/client.ts","../src/plugins/portal.ts","../src/plugins/checkout.ts","../src/plugins/usage.ts","../../adapter-utils/dist/index.js","../src/plugins/webhooks.ts","../src/index.ts"],"sourcesContent":["import type { GenericEndpointContext, User } from \"better-auth\";\nimport { APIError } from \"better-auth/api\";\nimport type { PolarOptions } from \"../types\";\n\nexport const onBeforeUserCreate =\n\t(options: PolarOptions) =>\n\tasync (user: Partial<User>, context?: GenericEndpointContext | undefined) => {\n\t\tif (context && options.createCustomerOnSignUp) {\n\t\t\ttry {\n\t\t\t\tconst params = options.getCustomerCreateParams\n\t\t\t\t\t? await options.getCustomerCreateParams({\n\t\t\t\t\t\t\tuser,\n\t\t\t\t\t\t})\n\t\t\t\t\t: {};\n\n\t\t\t\tif (!user.email) {\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"An associated email is required\",\n\t\t\t\t\t});\n\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});\n\t\t\t} catch (e: unknown) {\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 onAfterUserCreate =\n\t(options: PolarOptions) =>\n\tasync (user: User, context?: GenericEndpointContext | undefined) => {\n\t\tif (context && options.createCustomerOnSignUp) {\n\t\t\ttry {\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}\n\t\t\t} catch (e: unknown) {\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, context?: GenericEndpointContext | undefined) => {\n\t\tif (context && 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\tcustomerUpdateExternalID: {\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\tcontext.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\tcontext.context.logger.error(\n\t\t\t\t\t\t`Polar customer update failed. Error: ${e}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t};\n","import type { BetterAuthClientPlugin } from \"better-auth\";\nimport type { polar } from \"./index\";\n\nexport const polarClient = () => {\n\treturn {\n\t\tid: \"polar-client\",\n\t\t$InferServerPlugin: {} as ReturnType<typeof polar>,\n\t} satisfies BetterAuthClientPlugin;\n};\n","import type { Polar } from \"@polar-sh/sdk\";\nimport { APIError } from \"better-auth/api\";\nimport { sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport { z } from \"zod\";\n\nexport const portal = () => (polar: Polar) => {\n\treturn {\n\t\tportal: createAuthEndpoint(\n\t\t\t\"/customer/portal\",\n\t\t\t{\n\t\t\t\tmethod: \"GET\",\n\t\t\t\tuse: [sessionMiddleware],\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tif (!ctx.context.session?.user.id) {\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tconst customerSession = await polar.customerSessions.create({\n\t\t\t\t\t\texternalCustomerId: ctx.context.session?.user.id,\n\t\t\t\t\t});\n\n\t\t\t\t\treturn ctx.json({\n\t\t\t\t\t\turl: customerSession.customerPortalUrl,\n\t\t\t\t\t\tredirect: true,\n\t\t\t\t\t});\n\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t`Polar customer portal creation failed. Error: ${e.message}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: \"Customer portal creation failed\",\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t},\n\t\t),\n\t\tstate: createAuthEndpoint(\n\t\t\t\"/customer/state\",\n\t\t\t{\n\t\t\t\tmethod: \"GET\",\n\t\t\t\tuse: [sessionMiddleware],\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tconst state = await polar.customers.getStateExternal({\n\t\t\t\t\t\texternalId: ctx.context.session?.user.id,\n\t\t\t\t\t});\n\n\t\t\t\t\treturn ctx.json(state);\n\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t`Polar subscriptions list failed. Error: ${e.message}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: \"Subscriptions list failed\",\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t},\n\t\t),\n\t\tbenefits: createAuthEndpoint(\n\t\t\t\"/customer/benefits/list\",\n\t\t\t{\n\t\t\t\tmethod: \"GET\",\n\t\t\t\tquery: z\n\t\t\t\t\t.object({\n\t\t\t\t\t\tpage: z.coerce.number().optional(),\n\t\t\t\t\t\tlimit: z.coerce.number().optional(),\n\t\t\t\t\t})\n\t\t\t\t\t.optional(),\n\t\t\t\tuse: [sessionMiddleware],\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tconst customerSession = await polar.customerSessions.create({\n\t\t\t\t\t\texternalCustomerId: ctx.context.session?.user.id,\n\t\t\t\t\t});\n\n\t\t\t\t\tconst benefits = await polar.customerPortal.benefitGrants.list(\n\t\t\t\t\t\t{ customerSession: customerSession.token },\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tpage: ctx.query?.page,\n\t\t\t\t\t\t\tlimit: ctx.query?.limit,\n\t\t\t\t\t\t},\n\t\t\t\t\t);\n\n\t\t\t\t\treturn ctx.json(benefits);\n\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t`Polar benefits list failed. Error: ${e.message}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: \"Benefits list failed\",\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t},\n\t\t),\n\t\tsubscriptions: createAuthEndpoint(\n\t\t\t\"/customer/subscriptions/list\",\n\t\t\t{\n\t\t\t\tmethod: \"GET\",\n\t\t\t\tquery: z\n\t\t\t\t\t.object({\n\t\t\t\t\t\treferenceId: z.string().optional(),\n\t\t\t\t\t\tpage: z.coerce.number().optional(),\n\t\t\t\t\t\tlimit: z.coerce.number().optional(),\n\t\t\t\t\t\tactive: z.coerce.boolean().optional(),\n\t\t\t\t\t})\n\t\t\t\t\t.optional(),\n\t\t\t\tuse: [sessionMiddleware],\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\tif (ctx.query?.referenceId) {\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst subscriptions = await polar.subscriptions.list({\n\t\t\t\t\t\t\tpage: ctx.query?.page,\n\t\t\t\t\t\t\tlimit: ctx.query?.limit,\n\t\t\t\t\t\t\tactive: ctx.query?.active,\n\t\t\t\t\t\t\tmetadata: {\n\t\t\t\t\t\t\t\treferenceId: ctx.query?.referenceId,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\treturn ctx.json(subscriptions);\n\t\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\t\tconsole.log(e);\n\t\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t\t`Polar subscriptions list with referenceId failed. Error: ${e.message}`,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\t\tmessage: \"Subscriptions list with referenceId failed\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tconst customerSession = await polar.customerSessions.create({\n\t\t\t\t\t\texternalCustomerId: ctx.context.session?.user.id,\n\t\t\t\t\t});\n\n\t\t\t\t\tconst subscriptions = await polar.customerPortal.subscriptions.list(\n\t\t\t\t\t\t{ customerSession: customerSession.token },\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tpage: ctx.query?.page,\n\t\t\t\t\t\t\tlimit: ctx.query?.limit,\n\t\t\t\t\t\t\tactive: ctx.query?.active,\n\t\t\t\t\t\t},\n\t\t\t\t\t);\n\n\t\t\t\t\treturn ctx.json(subscriptions);\n\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t`Polar subscriptions list failed. Error: ${e.message}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: \"Polar subscriptions list failed\",\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t},\n\t\t),\n\t\torders: createAuthEndpoint(\n\t\t\t\"/customer/orders/list\",\n\t\t\t{\n\t\t\t\tmethod: \"GET\",\n\t\t\t\tquery: z\n\t\t\t\t\t.object({\n\t\t\t\t\t\tpage: z.coerce.number().optional(),\n\t\t\t\t\t\tlimit: z.coerce.number().optional(),\n\t\t\t\t\t\tproductBillingType: z.enum([\"recurring\", \"one_time\"]).optional(),\n\t\t\t\t\t})\n\t\t\t\t\t.optional(),\n\t\t\t\tuse: [sessionMiddleware],\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tconst customerSession = await polar.customerSessions.create({\n\t\t\t\t\t\texternalCustomerId: ctx.context.session?.user.id,\n\t\t\t\t\t});\n\n\t\t\t\t\tconst orders = await polar.customerPortal.orders.list(\n\t\t\t\t\t\t{ customerSession: customerSession.token },\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tpage: ctx.query?.page,\n\t\t\t\t\t\t\tlimit: ctx.query?.limit,\n\t\t\t\t\t\t\tproductBillingType: ctx.query?.productBillingType,\n\t\t\t\t\t\t},\n\t\t\t\t\t);\n\n\t\t\t\t\treturn ctx.json(orders);\n\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t`Polar orders list failed. Error: ${e.message}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: \"Orders list failed\",\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t},\n\t\t),\n\t};\n};\n","import type { Polar } from \"@polar-sh/sdk\";\nimport { APIError, getSessionFromCtx } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport { z } from \"zod\";\nimport type { Product } from \"../types\";\n\nexport interface CheckoutOptions {\n\t/**\n\t * Optional list of slug -> productId mappings for easy slug checkouts\n\t */\n\tproducts?: Product[] | (() => Promise<Product[]>);\n\t/**\n\t * Checkout Success URL\n\t */\n\tsuccessUrl?: string;\n\t/**\n\t * Only allow authenticated customers to checkout\n\t */\n\tauthenticatedUsersOnly?: boolean;\n\t/**\n\t * Checkout theme\n\t */\n\ttheme?: \"light\" | \"dark\";\n\t/**\n\t * Allow discount codes\n\t */\n\tallowDiscountCodes?: boolean;\n\t/**\n\t * Discount ID\n\t */\n\tdiscountId?: string;\n}\n\nexport const checkout =\n\t(checkoutOptions: CheckoutOptions = {}) =>\n\t(polar: Polar) => {\n\t\treturn {\n\t\t\tcheckout: createAuthEndpoint(\n\t\t\t\t\"/checkout\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\tbody: z.object({\n\t\t\t\t\t\tproducts: z.union([z.array(z.string()), z.string()]).optional(),\n\t\t\t\t\t\tslug: z.string().optional(),\n\t\t\t\t\t\treferenceId: z.string().optional(),\n\t\t\t\t\t\tcustomFieldData: z\n\t\t\t\t\t\t\t.record(\n\t\t\t\t\t\t\t\tz.string(),\n\t\t\t\t\t\t\t\tz.union([z.string(), z.number(), z.boolean()]),\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t.optional(),\n\t\t\t\t\t\tmetadata: z\n\t\t\t\t\t\t\t.record(\n\t\t\t\t\t\t\t\tz.string(),\n\t\t\t\t\t\t\t\tz.union([z.string(), z.number(), z.boolean()]),\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t.optional(),\n\t\t\t\t\t\tallowDiscountCodes: z.coerce.boolean().optional().default(true),\n\t\t\t\t\t\tdiscountId: z.string().optional(),\n\t\t\t\t\t}),\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tconst session = await getSessionFromCtx(ctx);\n\n\t\t\t\t\tlet productIds: string[] = [];\n\n\t\t\t\t\tif (ctx.body.slug) {\n\t\t\t\t\t\tconst resolvedProducts = await (typeof checkoutOptions.products ===\n\t\t\t\t\t\t\"function\"\n\t\t\t\t\t\t\t? checkoutOptions.products()\n\t\t\t\t\t\t\t: checkoutOptions.products);\n\n\t\t\t\t\t\tconst productId = resolvedProducts?.find(\n\t\t\t\t\t\t\t(product) => product.slug === ctx.body.slug,\n\t\t\t\t\t\t)?.productId;\n\n\t\t\t\t\t\tif (!productId) {\n\t\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\t\tmessage: \"Product not found\",\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tproductIds = [productId];\n\t\t\t\t\t} else {\n\t\t\t\t\t\tproductIds = Array.isArray(ctx.body.products)\n\t\t\t\t\t\t\t? ctx.body.products.filter((id) => id !== undefined)\n\t\t\t\t\t\t\t: [ctx.body.products].filter((id) => id !== undefined);\n\t\t\t\t\t}\n\n\t\t\t\t\tif (checkoutOptions.authenticatedUsersOnly && !session?.user.id) {\n\t\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\t\tmessage: \"You must be logged in to checkout\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst checkout = await polar.checkouts.create({\n\t\t\t\t\t\t\texternalCustomerId: session?.user.id,\n\t\t\t\t\t\t\tproducts: productIds,\n\t\t\t\t\t\t\tsuccessUrl: checkoutOptions.successUrl\n\t\t\t\t\t\t\t\t? new URL(\n\t\t\t\t\t\t\t\t\t\tcheckoutOptions.successUrl,\n\t\t\t\t\t\t\t\t\t\tctx.request?.url,\n\t\t\t\t\t\t\t\t\t).toString()\n\t\t\t\t\t\t\t\t: undefined,\n\t\t\t\t\t\t\tmetadata: ctx.body.referenceId\n\t\t\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\t\t\treferenceId: ctx.body.referenceId,\n\t\t\t\t\t\t\t\t\t\t...ctx.body.metadata,\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t: ctx.body.metadata,\n\t\t\t\t\t\t\tcustomFieldData: ctx.body.customFieldData,\n\t\t\t\t\t\t\tallowDiscountCodes: ctx.body.allowDiscountCodes ?? true,\n\t\t\t\t\t\t\tdiscountId: ctx.body.discountId,\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tconst redirectUrl = new URL(checkout.url);\n\n\t\t\t\t\t\tif (checkoutOptions.theme) {\n\t\t\t\t\t\t\tredirectUrl.searchParams.set(\"theme\", checkoutOptions.theme);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\treturn ctx.json({\n\t\t\t\t\t\t\turl: redirectUrl.toString(),\n\t\t\t\t\t\t\tredirect: true,\n\t\t\t\t\t\t});\n\t\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t\t`Polar checkout creation failed. Error: ${e.message}`,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\t\tmessage: \"Checkout creation failed\",\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};\n","import type { Polar } from \"@polar-sh/sdk\";\nimport {\n\tAPIError,\n\tcreateAuthEndpoint,\n\tsessionMiddleware,\n} from \"better-auth/api\";\nimport { z } from \"zod\";\nimport type { Product } from \"../types\";\n\nexport interface UsageOptions {\n\t/**\n\t * Products to use for topping up credits\n\t */\n\tcreditProducts?: Product[] | (() => Promise<Product[]>);\n}\n\nexport const usage = (_usageOptions?: UsageOptions) => (polar: Polar) => {\n\treturn {\n\t\tmeters: createAuthEndpoint(\n\t\t\t\"/usage/meters/list\",\n\t\t\t{\n\t\t\t\tmethod: \"GET\",\n\t\t\t\tuse: [sessionMiddleware],\n\t\t\t\tquery: z.object({\n\t\t\t\t\tpage: z.coerce.number().optional(),\n\t\t\t\t\tlimit: z.coerce.number().optional(),\n\t\t\t\t}),\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tconst customerSession = await polar.customerSessions.create({\n\t\t\t\t\t\texternalCustomerId: ctx.context.session.user.id,\n\t\t\t\t\t});\n\n\t\t\t\t\tconst customerMeters = await polar.customerPortal.customerMeters.list(\n\t\t\t\t\t\t{ customerSession: customerSession.token },\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tpage: ctx.query?.page,\n\t\t\t\t\t\t\tlimit: ctx.query?.limit,\n\t\t\t\t\t\t},\n\t\t\t\t\t);\n\n\t\t\t\t\treturn ctx.json(customerMeters);\n\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t`Polar meters list failed. Error: ${e.message}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: \"Meters list failed\",\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t},\n\t\t),\n\t\tingestion: createAuthEndpoint(\n\t\t\t\"/usage/ingest\",\n\t\t\t{\n\t\t\t\tmethod: \"POST\",\n\t\t\t\tbody: z.object({\n\t\t\t\t\tevent: z.string(),\n\t\t\t\t\tmetadata: z.record(\n\t\t\t\t\t\tz.string(),\n\t\t\t\t\t\tz.union([z.string(), z.number(), z.boolean()]),\n\t\t\t\t\t),\n\t\t\t\t}),\n\t\t\t\tuse: [sessionMiddleware],\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tconst ingestion = await polar.events.ingest({\n\t\t\t\t\t\tevents: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tname: ctx.body.event,\n\t\t\t\t\t\t\t\tmetadata: ctx.body.metadata,\n\t\t\t\t\t\t\t\texternalCustomerId: ctx.context.session.user.id,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t],\n\t\t\t\t\t});\n\n\t\t\t\t\treturn ctx.json(ingestion);\n\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t`Polar ingestion failed. Error: ${e.message}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: \"Ingestion failed\",\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t},\n\t\t),\n\t};\n};\n","// src/webhooks/webhooks.ts\nvar handleWebhookPayload = async (payload, { webhookSecret, entitlements, onPayload, ...eventHandlers }) => {\n const promises = [];\n if (onPayload) {\n promises.push(onPayload(payload));\n }\n switch (payload.type) {\n case \"checkout.created\":\n if (eventHandlers.onCheckoutCreated) {\n promises.push(eventHandlers.onCheckoutCreated(payload));\n }\n break;\n case \"checkout.updated\":\n if (eventHandlers.onCheckoutUpdated) {\n promises.push(eventHandlers.onCheckoutUpdated(payload));\n }\n break;\n case \"order.created\":\n if (eventHandlers.onOrderCreated) {\n promises.push(eventHandlers.onOrderCreated(payload));\n }\n break;\n case \"order.updated\":\n if (eventHandlers.onOrderUpdated) {\n promises.push(eventHandlers.onOrderUpdated(payload));\n }\n break;\n case \"order.paid\":\n if (eventHandlers.onOrderPaid) {\n promises.push(eventHandlers.onOrderPaid(payload));\n }\n break;\n case \"subscription.created\":\n if (eventHandlers.onSubscriptionCreated) {\n promises.push(eventHandlers.onSubscriptionCreated(payload));\n }\n break;\n case \"subscription.updated\":\n if (eventHandlers.onSubscriptionUpdated) {\n promises.push(eventHandlers.onSubscriptionUpdated(payload));\n }\n break;\n case \"subscription.active\":\n if (eventHandlers.onSubscriptionActive) {\n promises.push(eventHandlers.onSubscriptionActive(payload));\n }\n break;\n case \"subscription.canceled\":\n if (eventHandlers.onSubscriptionCanceled) {\n promises.push(eventHandlers.onSubscriptionCanceled(payload));\n }\n break;\n case \"subscription.uncanceled\":\n if (eventHandlers.onSubscriptionUncanceled) {\n promises.push(eventHandlers.onSubscriptionUncanceled(payload));\n }\n break;\n case \"subscription.revoked\":\n if (eventHandlers.onSubscriptionRevoked) {\n promises.push(eventHandlers.onSubscriptionRevoked(payload));\n }\n break;\n case \"product.created\":\n if (eventHandlers.onProductCreated) {\n promises.push(eventHandlers.onProductCreated(payload));\n }\n break;\n case \"product.updated\":\n if (eventHandlers.onProductUpdated) {\n promises.push(eventHandlers.onProductUpdated(payload));\n }\n break;\n case \"organization.updated\":\n if (eventHandlers.onOrganizationUpdated) {\n promises.push(eventHandlers.onOrganizationUpdated(payload));\n }\n break;\n case \"benefit.created\":\n if (eventHandlers.onBenefitCreated) {\n promises.push(eventHandlers.onBenefitCreated(payload));\n }\n break;\n case \"benefit.updated\":\n if (eventHandlers.onBenefitUpdated) {\n promises.push(eventHandlers.onBenefitUpdated(payload));\n }\n break;\n case \"benefit_grant.created\":\n if (eventHandlers.onBenefitGrantCreated) {\n promises.push(eventHandlers.onBenefitGrantCreated(payload));\n }\n break;\n case \"benefit_grant.updated\":\n if (eventHandlers.onBenefitGrantUpdated) {\n promises.push(eventHandlers.onBenefitGrantUpdated(payload));\n }\n break;\n case \"benefit_grant.revoked\":\n if (eventHandlers.onBenefitGrantRevoked) {\n promises.push(eventHandlers.onBenefitGrantRevoked(payload));\n }\n break;\n case \"customer.created\":\n if (eventHandlers.onCustomerCreated) {\n promises.push(eventHandlers.onCustomerCreated(payload));\n }\n break;\n case \"customer.updated\":\n if (eventHandlers.onCustomerUpdated) {\n promises.push(eventHandlers.onCustomerUpdated(payload));\n }\n break;\n case \"customer.deleted\":\n if (eventHandlers.onCustomerDeleted) {\n promises.push(eventHandlers.onCustomerDeleted(payload));\n }\n break;\n case \"customer.state_changed\":\n if (eventHandlers.onCustomerStateChanged) {\n promises.push(eventHandlers.onCustomerStateChanged(payload));\n }\n break;\n case \"order.refunded\":\n if (eventHandlers.onOrderRefunded) {\n promises.push(eventHandlers.onOrderRefunded(payload));\n }\n break;\n case \"refund.created\":\n if (eventHandlers.onRefundCreated) {\n promises.push(eventHandlers.onRefundCreated(payload));\n }\n break;\n case \"refund.updated\":\n if (eventHandlers.onRefundUpdated) {\n promises.push(eventHandlers.onRefundUpdated(payload));\n }\n break;\n }\n switch (payload.type) {\n case \"benefit_grant.created\":\n case \"benefit_grant.revoked\":\n if (entitlements) {\n for (const handler of entitlements.handlers) {\n promises.push(handler(payload));\n }\n }\n }\n return Promise.all(promises);\n};\n\n// src/entitlement/entitlement.ts\nvar EntitlementStrategy = class {\n grantCallbacks = [];\n revokeCallbacks = [];\n grant(callback) {\n this.grantCallbacks.push(callback);\n return this;\n }\n revoke(callback) {\n this.revokeCallbacks.push(callback);\n return this;\n }\n handler(slug) {\n return async (payload) => {\n if (payload.data.benefit.description === slug) {\n switch (payload.type) {\n case \"benefit_grant.created\":\n await Promise.all(\n this.grantCallbacks.map(\n (callback) => callback({\n customer: payload.data.customer,\n properties: payload.data.properties,\n payload\n })\n )\n );\n break;\n case \"benefit_grant.revoked\":\n await Promise.all(\n this.revokeCallbacks.map(\n (callback) => callback({\n customer: payload.data.customer,\n properties: payload.data.properties,\n payload\n })\n )\n );\n break;\n }\n }\n };\n }\n};\nvar Entitlements = class {\n static handlers = [];\n static use(slug, strategy) {\n this.handlers.push(strategy.handler(slug));\n return this;\n }\n};\nexport {\n EntitlementStrategy,\n Entitlements,\n handleWebhookPayload\n};\n","import { handleWebhookPayload } from \"@polar-sh/adapter-utils\";\nimport type { Polar } from \"@polar-sh/sdk\";\nimport type { WebhookBenefitCreatedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitcreatedpayload.js\";\nimport type { WebhookBenefitGrantCreatedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitgrantcreatedpayload.js\";\nimport type { WebhookBenefitGrantRevokedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitgrantrevokedpayload.js\";\nimport type { WebhookBenefitGrantUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitgrantupdatedpayload.js\";\nimport type { WebhookBenefitUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitupdatedpayload.js\";\nimport type { WebhookCheckoutCreatedPayload } from \"@polar-sh/sdk/models/components/webhookcheckoutcreatedpayload.js\";\nimport type { WebhookCheckoutUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookcheckoutupdatedpayload.js\";\nimport type { WebhookCustomerCreatedPayload } from \"@polar-sh/sdk/models/components/webhookcustomercreatedpayload.js\";\nimport type { WebhookCustomerDeletedPayload } from \"@polar-sh/sdk/models/components/webhookcustomerdeletedpayload.js\";\nimport type { WebhookCustomerStateChangedPayload } from \"@polar-sh/sdk/models/components/webhookcustomerstatechangedpayload.js\";\nimport type { WebhookCustomerUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookcustomerupdatedpayload.js\";\nimport type { WebhookOrderCreatedPayload } from \"@polar-sh/sdk/models/components/webhookordercreatedpayload.js\";\nimport type { WebhookOrderPaidPayload } from \"@polar-sh/sdk/models/components/webhookorderpaidpayload.js\";\nimport type { WebhookOrderRefundedPayload } from \"@polar-sh/sdk/models/components/webhookorderrefundedpayload.js\";\nimport type { WebhookOrganizationUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookorganizationupdatedpayload.js\";\nimport type { WebhookProductCreatedPayload } from \"@polar-sh/sdk/models/components/webhookproductcreatedpayload.js\";\nimport type { WebhookProductUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookproductupdatedpayload.js\";\nimport type { WebhookRefundCreatedPayload } from \"@polar-sh/sdk/models/components/webhookrefundcreatedpayload.js\";\nimport type { WebhookRefundUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookrefundupdatedpayload.js\";\nimport type { WebhookSubscriptionActivePayload } from \"@polar-sh/sdk/models/components/webhooksubscriptionactivepayload.js\";\nimport type { WebhookSubscriptionCanceledPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptioncanceledpayload.js\";\nimport type { WebhookSubscriptionCreatedPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptioncreatedpayload.js\";\nimport type { WebhookSubscriptionRevokedPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptionrevokedpayload.js\";\nimport type { WebhookSubscriptionUncanceledPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptionuncanceledpayload.js\";\nimport type { WebhookSubscriptionUpdatedPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptionupdatedpayload.js\";\nimport { validateEvent } from \"@polar-sh/sdk/webhooks.js\";\nimport { APIError, createAuthEndpoint } from \"better-auth/api\";\n\nexport interface WebhooksOptions {\n\t/**\n\t * Webhook Secret\n\t */\n\tsecret: string;\n\t/**\n\t * Generic handler for all webhooks\n\t */\n\tonPayload?: (payload: ReturnType<typeof validateEvent>) => Promise<void>;\n\t/**\n\t * Webhook for checkout created\n\t */\n\tonCheckoutCreated?: (payload: WebhookCheckoutCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for checkout updated\n\t */\n\tonCheckoutUpdated?: (payload: WebhookCheckoutUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for order created\n\t */\n\tonOrderCreated?: (payload: WebhookOrderCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for order refunded\n\t */\n\tonOrderRefunded?: (payload: WebhookOrderRefundedPayload) => Promise<void>;\n\t/**\n\t * Webhook for order paid\n\t */\n\tonOrderPaid?: (payload: WebhookOrderPaidPayload) => Promise<void>;\n\t/**\n\t * Webhook for refund created\n\t */\n\tonRefundCreated?: (payload: WebhookRefundCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for refund updated\n\t */\n\tonRefundUpdated?: (payload: WebhookRefundUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for subscription created\n\t */\n\tonSubscriptionCreated?: (\n\t\tpayload: WebhookSubscriptionCreatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription updated\n\t */\n\tonSubscriptionUpdated?: (\n\t\tpayload: WebhookSubscriptionUpdatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription active\n\t */\n\tonSubscriptionActive?: (\n\t\tpayload: WebhookSubscriptionActivePayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription canceled\n\t */\n\tonSubscriptionCanceled?: (\n\t\tpayload: WebhookSubscriptionCanceledPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription revoked\n\t */\n\tonSubscriptionRevoked?: (\n\t\tpayload: WebhookSubscriptionRevokedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription uncanceled\n\t */\n\tonSubscriptionUncanceled?: (\n\t\tpayload: WebhookSubscriptionUncanceledPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for product created\n\t */\n\tonProductCreated?: (payload: WebhookProductCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for product updated\n\t */\n\tonProductUpdated?: (payload: WebhookProductUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for organization updated\n\t */\n\tonOrganizationUpdated?: (\n\t\tpayload: WebhookOrganizationUpdatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for benefit created\n\t */\n\tonBenefitCreated?: (payload: WebhookBenefitCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for benefit updated\n\t */\n\tonBenefitUpdated?: (payload: WebhookBenefitUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for benefit grant created\n\t */\n\tonBenefitGrantCreated?: (\n\t\tpayload: WebhookBenefitGrantCreatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for benefit grant updated\n\t */\n\tonBenefitGrantUpdated?: (\n\t\tpayload: WebhookBenefitGrantUpdatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for benefit grant revoked\n\t */\n\tonBenefitGrantRevoked?: (\n\t\tpayload: WebhookBenefitGrantRevokedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for customer created\n\t */\n\tonCustomerCreated?: (payload: WebhookCustomerCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for customer updated\n\t */\n\tonCustomerUpdated?: (payload: WebhookCustomerUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for customer deleted\n\t */\n\tonCustomerDeleted?: (payload: WebhookCustomerDeletedPayload) => Promise<void>;\n\t/**\n\t * Webhook for customer state changed\n\t */\n\tonCustomerStateChanged?: (\n\t\tpayload: WebhookCustomerStateChangedPayload,\n\t) => Promise<void>;\n}\n\nexport const webhooks = (options: WebhooksOptions) => (_polar: Polar) => {\n\treturn {\n\t\tpolarWebhooks: createAuthEndpoint(\n\t\t\t\"/polar/webhooks\",\n\t\t\t{\n\t\t\t\tmethod: \"POST\",\n\t\t\t\tmetadata: {\n\t\t\t\t\tisAction: false,\n\t\t\t\t},\n\t\t\t\tcloneRequest: true,\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tconst { secret, ...eventHandlers } = options;\n\n\t\t\t\tif (!ctx.request?.body) {\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\");\n\t\t\t\t}\n\t\t\t\tconst buf = await ctx.request.text();\n\t\t\t\tlet event: ReturnType<typeof validateEvent>;\n\t\t\t\ttry {\n\t\t\t\t\tif (!secret) {\n\t\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\t\tmessage: \"Polar webhook secret not found\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\tconst headers = {\n\t\t\t\t\t\t\"webhook-id\": ctx.request.headers.get(\"webhook-id\") as string,\n\t\t\t\t\t\t\"webhook-timestamp\": ctx.request.headers.get(\n\t\t\t\t\t\t\t\"webhook-timestamp\",\n\t\t\t\t\t\t) as string,\n\t\t\t\t\t\t\"webhook-signature\": ctx.request.headers.get(\n\t\t\t\t\t\t\t\"webhook-signature\",\n\t\t\t\t\t\t) as string,\n\t\t\t\t\t};\n\n\t\t\t\t\tevent = validateEvent(buf, headers, secret);\n\t\t\t\t} catch (err: unknown) {\n\t\t\t\t\tif (err instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(`${err.message}`);\n\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\tmessage: `Webhook Error: ${err.message}`,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: `Webhook Error: ${err}`,\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tawait handleWebhookPayload(event, {\n\t\t\t\t\t\twebhookSecret: secret,\n\t\t\t\t\t\t...eventHandlers,\n\t\t\t\t\t});\n\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t`Polar webhook failed. Error: ${e.message}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t} else {\n\t\t\t\t\t\tctx.context.logger.error(`Polar webhook failed. Error: ${e}`);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"Webhook error: See server logs for more information.\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\treturn ctx.json({ received: true });\n\t\t\t},\n\t\t),\n\t};\n};\n","import type { BetterAuthPlugin } from \"better-auth\";\nimport {\n\tonAfterUserCreate,\n\tonBeforeUserCreate,\n\tonUserUpdate,\n} from \"./hooks/customer\";\nimport type { PolarEndpoints, PolarOptions } from \"./types\";\n\nexport { polarClient } from \"./client\";\n\nexport * from \"./plugins/portal\";\nexport * from \"./plugins/checkout\";\nexport * from \"./plugins/usage\";\nexport * from \"./plugins/webhooks\";\n\nexport const polar = <O extends PolarOptions>(options: O) => {\n\tconst plugins = options.use\n\t\t.map((use) => use(options.client))\n\t\t.reduce((acc, plugin) => {\n\t\t\tObject.assign(acc, plugin);\n\t\t\treturn acc;\n\t\t}, {} as PolarEndpoints);\n\n\treturn {\n\t\tid: \"polar\",\n\t\tendpoints: {\n\t\t\t...plugins,\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\tbefore: onBeforeUserCreate(options),\n\t\t\t\t\t\t\t\tafter: onAfterUserCreate(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":";AACA,SAAS,gBAAgB;AAGlB,IAAM,qBACZ,CAAC,YACD,OAAO,MAAqB,YAAiD;AAC5E,MAAI,WAAW,QAAQ,wBAAwB;AAC9C,QAAI;AACH,YAAM,SAAS,QAAQ,0BACpB,MAAM,QAAQ,wBAAwB;AAAA,QACtC;AAAA,MACD,CAAC,IACA,CAAC;AAEJ,UAAI,CAAC,KAAK,OAAO;AAChB,cAAM,IAAI,SAAS,eAAe;AAAA,UACjC,SAAS;AAAA,QACV,CAAC;AAAA,MACF;AAEA,YAAM,QAAQ,OAAO,UAAU,OAAO;AAAA,QACrC,GAAG;AAAA,QACH,OAAO,KAAK;AAAA,QACZ,MAAM,KAAK;AAAA,MACZ,CAAC;AAAA,IACF,SAAS,GAAY;AACpB,UAAI,aAAa,OAAO;AACvB,cAAM,IAAI,SAAS,yBAAyB;AAAA,UAC3C,SAAS,0CAA0C,EAAE,OAAO;AAAA,QAC7D,CAAC;AAAA,MACF;AAEA,YAAM,IAAI,SAAS,yBAAyB;AAAA,QAC3C,SAAS,0CAA0C,CAAC;AAAA,MACrD,CAAC;AAAA,IACF;AAAA,EACD;AACD;AAEM,IAAM,oBACZ,CAAC,YACD,OAAO,MAAY,YAAiD;AACnE,MAAI,WAAW,QAAQ,wBAAwB;AAC9C,QAAI;AAC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,OAAO,KAAK,MAAM;AACzB,WAAO;AAAA,EACR,GAAG,CAAC,CAAmB;AAExB,SAAO;AAAA,IACN,IAAI;AAAA,IACJ,WAAW;AAAA,MACV,GAAG;AAAA,IACJ;AAAA,IACA,OAAO;AACN,aAAO;AAAA,QACN,SAAS;AAAA,UACR,eAAe;AAAA,YACd,MAAM;AAAA,cACL,QAAQ;AAAA,gBACP,QAAQ,mBAAmB,OAAO;AAAA,gBAClC,OAAO,kBAAkB,OAAO;AAAA,cACjC;AAAA,cACA,QAAQ;AAAA,gBACP,OAAO,aAAa,OAAO;AAAA,cAC5B;AAAA,YACD;AAAA,UACD;AAAA,QACD;AAAA,MACD;AAAA,IACD;AAAA,EACD;AACD;","names":["APIError","polar","APIError","createAuthEndpoint","z","polar","checkout","APIError","createAuthEndpoint","sessionMiddleware","z","polar","APIError","createAuthEndpoint"]}
1
+ {"version":3,"sources":["../src/hooks/customer.ts","../src/client.ts","../src/plugins/portal.ts","../src/plugins/checkout.ts","../src/plugins/usage.ts","../../adapter-utils/dist/index.js","../src/plugins/webhooks.ts","../src/index.ts"],"sourcesContent":["import type { GenericEndpointContext, User } from \"better-auth\";\nimport { APIError } from \"better-auth/api\";\nimport type { PolarOptions } from \"../types\";\n\nexport const onBeforeUserCreate =\n\t(options: PolarOptions) =>\n\tasync (user: Partial<User>, context?: GenericEndpointContext | undefined) => {\n\t\tif (context && options.createCustomerOnSignUp) {\n\t\t\ttry {\n\t\t\t\tconst params = options.getCustomerCreateParams\n\t\t\t\t\t? await options.getCustomerCreateParams({\n\t\t\t\t\t\t\tuser,\n\t\t\t\t\t\t})\n\t\t\t\t\t: {};\n\n\t\t\t\tif (!user.email) {\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"An associated email is required\",\n\t\t\t\t\t});\n\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});\n\t\t\t} catch (e: unknown) {\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 onAfterUserCreate =\n\t(options: PolarOptions) =>\n\tasync (user: User, context?: GenericEndpointContext | undefined) => {\n\t\tif (context && options.createCustomerOnSignUp) {\n\t\t\ttry {\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}\n\t\t\t} catch (e: unknown) {\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, context?: GenericEndpointContext | undefined) => {\n\t\tif (context && 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\tcustomerUpdateExternalID: {\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\tcontext.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\tcontext.context.logger.error(\n\t\t\t\t\t\t`Polar customer update failed. Error: ${e}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t};\n","import type { BetterAuthClientPlugin } from \"better-auth\";\nimport type { polar } from \"./index\";\n\nexport const polarClient = () => {\n\treturn {\n\t\tid: \"polar-client\",\n\t\t$InferServerPlugin: {} as ReturnType<typeof polar>,\n\t} satisfies BetterAuthClientPlugin;\n};\n","import type { Polar } from \"@polar-sh/sdk\";\nimport { APIError } from \"better-auth/api\";\nimport { sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport { z } from \"zod\";\n\nexport const portal = () => (polar: Polar) => {\n\treturn {\n\t\tportal: createAuthEndpoint(\n\t\t\t\"/customer/portal\",\n\t\t\t{\n\t\t\t\tmethod: \"GET\",\n\t\t\t\tuse: [sessionMiddleware],\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tif (!ctx.context.session?.user.id) {\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tconst customerSession = await polar.customerSessions.create({\n\t\t\t\t\t\texternalCustomerId: ctx.context.session?.user.id,\n\t\t\t\t\t});\n\n\t\t\t\t\treturn ctx.json({\n\t\t\t\t\t\turl: customerSession.customerPortalUrl,\n\t\t\t\t\t\tredirect: true,\n\t\t\t\t\t});\n\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t`Polar customer portal creation failed. Error: ${e.message}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: \"Customer portal creation failed\",\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t},\n\t\t),\n\t\tstate: createAuthEndpoint(\n\t\t\t\"/customer/state\",\n\t\t\t{\n\t\t\t\tmethod: \"GET\",\n\t\t\t\tuse: [sessionMiddleware],\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tconst state = await polar.customers.getStateExternal({\n\t\t\t\t\t\texternalId: ctx.context.session?.user.id,\n\t\t\t\t\t});\n\n\t\t\t\t\treturn ctx.json(state);\n\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t`Polar subscriptions list failed. Error: ${e.message}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: \"Subscriptions list failed\",\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t},\n\t\t),\n\t\tbenefits: createAuthEndpoint(\n\t\t\t\"/customer/benefits/list\",\n\t\t\t{\n\t\t\t\tmethod: \"GET\",\n\t\t\t\tquery: z\n\t\t\t\t\t.object({\n\t\t\t\t\t\tpage: z.coerce.number().optional(),\n\t\t\t\t\t\tlimit: z.coerce.number().optional(),\n\t\t\t\t\t})\n\t\t\t\t\t.optional(),\n\t\t\t\tuse: [sessionMiddleware],\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tconst customerSession = await polar.customerSessions.create({\n\t\t\t\t\t\texternalCustomerId: ctx.context.session?.user.id,\n\t\t\t\t\t});\n\n\t\t\t\t\tconst benefits = await polar.customerPortal.benefitGrants.list(\n\t\t\t\t\t\t{ customerSession: customerSession.token },\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tpage: ctx.query?.page,\n\t\t\t\t\t\t\tlimit: ctx.query?.limit,\n\t\t\t\t\t\t},\n\t\t\t\t\t);\n\n\t\t\t\t\treturn ctx.json(benefits);\n\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t`Polar benefits list failed. Error: ${e.message}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: \"Benefits list failed\",\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t},\n\t\t),\n\t\tsubscriptions: createAuthEndpoint(\n\t\t\t\"/customer/subscriptions/list\",\n\t\t\t{\n\t\t\t\tmethod: \"GET\",\n\t\t\t\tquery: z\n\t\t\t\t\t.object({\n\t\t\t\t\t\treferenceId: z.string().optional(),\n\t\t\t\t\t\tpage: z.coerce.number().optional(),\n\t\t\t\t\t\tlimit: z.coerce.number().optional(),\n\t\t\t\t\t\tactive: z.coerce.boolean().optional(),\n\t\t\t\t\t})\n\t\t\t\t\t.optional(),\n\t\t\t\tuse: [sessionMiddleware],\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\tif (ctx.query?.referenceId) {\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst subscriptions = await polar.subscriptions.list({\n\t\t\t\t\t\t\tpage: ctx.query?.page,\n\t\t\t\t\t\t\tlimit: ctx.query?.limit,\n\t\t\t\t\t\t\tactive: ctx.query?.active,\n\t\t\t\t\t\t\tmetadata: {\n\t\t\t\t\t\t\t\treferenceId: ctx.query?.referenceId,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\treturn ctx.json(subscriptions);\n\t\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\t\tconsole.log(e);\n\t\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t\t`Polar subscriptions list with referenceId failed. Error: ${e.message}`,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\t\tmessage: \"Subscriptions list with referenceId failed\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tconst customerSession = await polar.customerSessions.create({\n\t\t\t\t\t\texternalCustomerId: ctx.context.session?.user.id,\n\t\t\t\t\t});\n\n\t\t\t\t\tconst subscriptions = await polar.customerPortal.subscriptions.list(\n\t\t\t\t\t\t{ customerSession: customerSession.token },\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tpage: ctx.query?.page,\n\t\t\t\t\t\t\tlimit: ctx.query?.limit,\n\t\t\t\t\t\t\tactive: ctx.query?.active,\n\t\t\t\t\t\t},\n\t\t\t\t\t);\n\n\t\t\t\t\treturn ctx.json(subscriptions);\n\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t`Polar subscriptions list failed. Error: ${e.message}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: \"Polar subscriptions list failed\",\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t},\n\t\t),\n\t\torders: createAuthEndpoint(\n\t\t\t\"/customer/orders/list\",\n\t\t\t{\n\t\t\t\tmethod: \"GET\",\n\t\t\t\tquery: z\n\t\t\t\t\t.object({\n\t\t\t\t\t\tpage: z.coerce.number().optional(),\n\t\t\t\t\t\tlimit: z.coerce.number().optional(),\n\t\t\t\t\t\tproductBillingType: z.enum([\"recurring\", \"one_time\"]).optional(),\n\t\t\t\t\t})\n\t\t\t\t\t.optional(),\n\t\t\t\tuse: [sessionMiddleware],\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tconst customerSession = await polar.customerSessions.create({\n\t\t\t\t\t\texternalCustomerId: ctx.context.session?.user.id,\n\t\t\t\t\t});\n\n\t\t\t\t\tconst orders = await polar.customerPortal.orders.list(\n\t\t\t\t\t\t{ customerSession: customerSession.token },\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tpage: ctx.query?.page,\n\t\t\t\t\t\t\tlimit: ctx.query?.limit,\n\t\t\t\t\t\t\tproductBillingType: ctx.query?.productBillingType,\n\t\t\t\t\t\t},\n\t\t\t\t\t);\n\n\t\t\t\t\treturn ctx.json(orders);\n\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t`Polar orders list failed. Error: ${e.message}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: \"Orders list failed\",\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t},\n\t\t),\n\t};\n};\n","import type { Polar } from \"@polar-sh/sdk\";\nimport { APIError, getSessionFromCtx } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport { z } from \"zod\";\nimport type { Product } from \"../types\";\n\nexport interface CheckoutOptions {\n\t/**\n\t * Optional list of slug -> productId mappings for easy slug checkouts\n\t */\n\tproducts?: Product[] | (() => Promise<Product[]>);\n\t/**\n\t * Checkout Success URL\n\t */\n\tsuccessUrl?: string;\n\t/**\n\t * Only allow authenticated customers to checkout\n\t */\n\tauthenticatedUsersOnly?: boolean;\n\t/**\n\t * Checkout theme\n\t */\n\ttheme?: \"light\" | \"dark\";\n\t/**\n\t * Allow discount codes\n\t */\n\tallowDiscountCodes?: boolean;\n\t/**\n\t * Discount ID\n\t */\n\tdiscountId?: string;\n}\n\nexport const checkout =\n\t(checkoutOptions: CheckoutOptions = {}) =>\n\t(polar: Polar) => {\n\t\treturn {\n\t\t\tcheckout: createAuthEndpoint(\n\t\t\t\t\"/checkout\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\tbody: z.object({\n\t\t\t\t\t\tproducts: z.union([z.array(z.string()), z.string()]).optional(),\n\t\t\t\t\t\tslug: z.string().optional(),\n\t\t\t\t\t\treferenceId: z.string().optional(),\n\t\t\t\t\t\tcustomFieldData: z\n\t\t\t\t\t\t\t.record(\n\t\t\t\t\t\t\t\tz.string(),\n\t\t\t\t\t\t\t\tz.union([z.string(), z.number(), z.boolean()]),\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t.optional(),\n\t\t\t\t\t\tmetadata: z\n\t\t\t\t\t\t\t.record(\n\t\t\t\t\t\t\t\tz.string(),\n\t\t\t\t\t\t\t\tz.union([z.string(), z.number(), z.boolean()]),\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t.optional(),\n\t\t\t\t\t\tallowDiscountCodes: z.coerce.boolean().optional().default(true),\n\t\t\t\t\t\tdiscountId: z.string().optional(),\n\t\t\t\t\t}),\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tconst session = await getSessionFromCtx(ctx);\n\n\t\t\t\t\tlet productIds: string[] = [];\n\n\t\t\t\t\tif (ctx.body.slug) {\n\t\t\t\t\t\tconst resolvedProducts = await (typeof checkoutOptions.products ===\n\t\t\t\t\t\t\"function\"\n\t\t\t\t\t\t\t? checkoutOptions.products()\n\t\t\t\t\t\t\t: checkoutOptions.products);\n\n\t\t\t\t\t\tconst productId = resolvedProducts?.find(\n\t\t\t\t\t\t\t(product) => product.slug === ctx.body.slug,\n\t\t\t\t\t\t)?.productId;\n\n\t\t\t\t\t\tif (!productId) {\n\t\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\t\tmessage: \"Product not found\",\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tproductIds = [productId];\n\t\t\t\t\t} else {\n\t\t\t\t\t\tproductIds = Array.isArray(ctx.body.products)\n\t\t\t\t\t\t\t? ctx.body.products.filter((id) => id !== undefined)\n\t\t\t\t\t\t\t: [ctx.body.products].filter((id) => id !== undefined);\n\t\t\t\t\t}\n\n\t\t\t\t\tif (checkoutOptions.authenticatedUsersOnly && !session?.user.id) {\n\t\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\t\tmessage: \"You must be logged in to checkout\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst checkout = await polar.checkouts.create({\n\t\t\t\t\t\t\texternalCustomerId: session?.user.id,\n\t\t\t\t\t\t\tproducts: productIds,\n\t\t\t\t\t\t\tsuccessUrl: checkoutOptions.successUrl\n\t\t\t\t\t\t\t\t? new URL(\n\t\t\t\t\t\t\t\t\t\tcheckoutOptions.successUrl,\n\t\t\t\t\t\t\t\t\t\tctx.request?.url ?? ctx.context.baseURL,\n\t\t\t\t\t\t\t\t\t).toString()\n\t\t\t\t\t\t\t\t: undefined,\n\t\t\t\t\t\t\tmetadata: ctx.body.referenceId\n\t\t\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\t\t\treferenceId: ctx.body.referenceId,\n\t\t\t\t\t\t\t\t\t\t...ctx.body.metadata,\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t: ctx.body.metadata,\n\t\t\t\t\t\t\tcustomFieldData: ctx.body.customFieldData,\n\t\t\t\t\t\t\tallowDiscountCodes: ctx.body.allowDiscountCodes ?? true,\n\t\t\t\t\t\t\tdiscountId: ctx.body.discountId,\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tconst redirectUrl = new URL(checkout.url);\n\n\t\t\t\t\t\tif (checkoutOptions.theme) {\n\t\t\t\t\t\t\tredirectUrl.searchParams.set(\"theme\", checkoutOptions.theme);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\treturn ctx.json({\n\t\t\t\t\t\t\turl: redirectUrl.toString(),\n\t\t\t\t\t\t\tredirect: true,\n\t\t\t\t\t\t});\n\t\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t\t`Polar checkout creation failed. Error: ${e.message}`,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\t\tmessage: \"Checkout creation failed\",\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};\n","import type { Polar } from \"@polar-sh/sdk\";\nimport {\n\tAPIError,\n\tcreateAuthEndpoint,\n\tsessionMiddleware,\n} from \"better-auth/api\";\nimport { z } from \"zod\";\nimport type { Product } from \"../types\";\n\nexport interface UsageOptions {\n\t/**\n\t * Products to use for topping up credits\n\t */\n\tcreditProducts?: Product[] | (() => Promise<Product[]>);\n}\n\nexport const usage = (_usageOptions?: UsageOptions) => (polar: Polar) => {\n\treturn {\n\t\tmeters: createAuthEndpoint(\n\t\t\t\"/usage/meters/list\",\n\t\t\t{\n\t\t\t\tmethod: \"GET\",\n\t\t\t\tuse: [sessionMiddleware],\n\t\t\t\tquery: z.object({\n\t\t\t\t\tpage: z.coerce.number().optional(),\n\t\t\t\t\tlimit: z.coerce.number().optional(),\n\t\t\t\t}),\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tconst customerSession = await polar.customerSessions.create({\n\t\t\t\t\t\texternalCustomerId: ctx.context.session.user.id,\n\t\t\t\t\t});\n\n\t\t\t\t\tconst customerMeters = await polar.customerPortal.customerMeters.list(\n\t\t\t\t\t\t{ customerSession: customerSession.token },\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tpage: ctx.query?.page,\n\t\t\t\t\t\t\tlimit: ctx.query?.limit,\n\t\t\t\t\t\t},\n\t\t\t\t\t);\n\n\t\t\t\t\treturn ctx.json(customerMeters);\n\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t`Polar meters list failed. Error: ${e.message}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: \"Meters list failed\",\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t},\n\t\t),\n\t\tingestion: createAuthEndpoint(\n\t\t\t\"/usage/ingest\",\n\t\t\t{\n\t\t\t\tmethod: \"POST\",\n\t\t\t\tbody: z.object({\n\t\t\t\t\tevent: z.string(),\n\t\t\t\t\tmetadata: z.record(\n\t\t\t\t\t\tz.string(),\n\t\t\t\t\t\tz.union([z.string(), z.number(), z.boolean()]),\n\t\t\t\t\t),\n\t\t\t\t}),\n\t\t\t\tuse: [sessionMiddleware],\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tconst ingestion = await polar.events.ingest({\n\t\t\t\t\t\tevents: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tname: ctx.body.event,\n\t\t\t\t\t\t\t\tmetadata: ctx.body.metadata,\n\t\t\t\t\t\t\t\texternalCustomerId: ctx.context.session.user.id,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t],\n\t\t\t\t\t});\n\n\t\t\t\t\treturn ctx.json(ingestion);\n\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t`Polar ingestion failed. Error: ${e.message}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: \"Ingestion failed\",\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t},\n\t\t),\n\t};\n};\n","// src/webhooks/webhooks.ts\nvar handleWebhookPayload = async (payload, { webhookSecret, entitlements, onPayload, ...eventHandlers }) => {\n const promises = [];\n if (onPayload) {\n promises.push(onPayload(payload));\n }\n switch (payload.type) {\n case \"checkout.created\":\n if (eventHandlers.onCheckoutCreated) {\n promises.push(eventHandlers.onCheckoutCreated(payload));\n }\n break;\n case \"checkout.updated\":\n if (eventHandlers.onCheckoutUpdated) {\n promises.push(eventHandlers.onCheckoutUpdated(payload));\n }\n break;\n case \"order.created\":\n if (eventHandlers.onOrderCreated) {\n promises.push(eventHandlers.onOrderCreated(payload));\n }\n break;\n case \"order.updated\":\n if (eventHandlers.onOrderUpdated) {\n promises.push(eventHandlers.onOrderUpdated(payload));\n }\n break;\n case \"order.paid\":\n if (eventHandlers.onOrderPaid) {\n promises.push(eventHandlers.onOrderPaid(payload));\n }\n break;\n case \"subscription.created\":\n if (eventHandlers.onSubscriptionCreated) {\n promises.push(eventHandlers.onSubscriptionCreated(payload));\n }\n break;\n case \"subscription.updated\":\n if (eventHandlers.onSubscriptionUpdated) {\n promises.push(eventHandlers.onSubscriptionUpdated(payload));\n }\n break;\n case \"subscription.active\":\n if (eventHandlers.onSubscriptionActive) {\n promises.push(eventHandlers.onSubscriptionActive(payload));\n }\n break;\n case \"subscription.canceled\":\n if (eventHandlers.onSubscriptionCanceled) {\n promises.push(eventHandlers.onSubscriptionCanceled(payload));\n }\n break;\n case \"subscription.uncanceled\":\n if (eventHandlers.onSubscriptionUncanceled) {\n promises.push(eventHandlers.onSubscriptionUncanceled(payload));\n }\n break;\n case \"subscription.revoked\":\n if (eventHandlers.onSubscriptionRevoked) {\n promises.push(eventHandlers.onSubscriptionRevoked(payload));\n }\n break;\n case \"product.created\":\n if (eventHandlers.onProductCreated) {\n promises.push(eventHandlers.onProductCreated(payload));\n }\n break;\n case \"product.updated\":\n if (eventHandlers.onProductUpdated) {\n promises.push(eventHandlers.onProductUpdated(payload));\n }\n break;\n case \"organization.updated\":\n if (eventHandlers.onOrganizationUpdated) {\n promises.push(eventHandlers.onOrganizationUpdated(payload));\n }\n break;\n case \"benefit.created\":\n if (eventHandlers.onBenefitCreated) {\n promises.push(eventHandlers.onBenefitCreated(payload));\n }\n break;\n case \"benefit.updated\":\n if (eventHandlers.onBenefitUpdated) {\n promises.push(eventHandlers.onBenefitUpdated(payload));\n }\n break;\n case \"benefit_grant.created\":\n if (eventHandlers.onBenefitGrantCreated) {\n promises.push(eventHandlers.onBenefitGrantCreated(payload));\n }\n break;\n case \"benefit_grant.updated\":\n if (eventHandlers.onBenefitGrantUpdated) {\n promises.push(eventHandlers.onBenefitGrantUpdated(payload));\n }\n break;\n case \"benefit_grant.revoked\":\n if (eventHandlers.onBenefitGrantRevoked) {\n promises.push(eventHandlers.onBenefitGrantRevoked(payload));\n }\n break;\n case \"customer.created\":\n if (eventHandlers.onCustomerCreated) {\n promises.push(eventHandlers.onCustomerCreated(payload));\n }\n break;\n case \"customer.updated\":\n if (eventHandlers.onCustomerUpdated) {\n promises.push(eventHandlers.onCustomerUpdated(payload));\n }\n break;\n case \"customer.deleted\":\n if (eventHandlers.onCustomerDeleted) {\n promises.push(eventHandlers.onCustomerDeleted(payload));\n }\n break;\n case \"customer.state_changed\":\n if (eventHandlers.onCustomerStateChanged) {\n promises.push(eventHandlers.onCustomerStateChanged(payload));\n }\n break;\n case \"order.refunded\":\n if (eventHandlers.onOrderRefunded) {\n promises.push(eventHandlers.onOrderRefunded(payload));\n }\n break;\n case \"refund.created\":\n if (eventHandlers.onRefundCreated) {\n promises.push(eventHandlers.onRefundCreated(payload));\n }\n break;\n case \"refund.updated\":\n if (eventHandlers.onRefundUpdated) {\n promises.push(eventHandlers.onRefundUpdated(payload));\n }\n break;\n }\n switch (payload.type) {\n case \"benefit_grant.created\":\n case \"benefit_grant.revoked\":\n if (entitlements) {\n for (const handler of entitlements.handlers) {\n promises.push(handler(payload));\n }\n }\n }\n return Promise.all(promises);\n};\n\n// src/entitlement/entitlement.ts\nvar EntitlementStrategy = class {\n grantCallbacks = [];\n revokeCallbacks = [];\n grant(callback) {\n this.grantCallbacks.push(callback);\n return this;\n }\n revoke(callback) {\n this.revokeCallbacks.push(callback);\n return this;\n }\n handler(slug) {\n return async (payload) => {\n if (payload.data.benefit.description === slug) {\n switch (payload.type) {\n case \"benefit_grant.created\":\n await Promise.all(\n this.grantCallbacks.map(\n (callback) => callback({\n customer: payload.data.customer,\n properties: payload.data.properties,\n payload\n })\n )\n );\n break;\n case \"benefit_grant.revoked\":\n await Promise.all(\n this.revokeCallbacks.map(\n (callback) => callback({\n customer: payload.data.customer,\n properties: payload.data.properties,\n payload\n })\n )\n );\n break;\n }\n }\n };\n }\n};\nvar Entitlements = class {\n static handlers = [];\n static use(slug, strategy) {\n this.handlers.push(strategy.handler(slug));\n return this;\n }\n};\nexport {\n EntitlementStrategy,\n Entitlements,\n handleWebhookPayload\n};\n","import { handleWebhookPayload } from \"@polar-sh/adapter-utils\";\nimport type { Polar } from \"@polar-sh/sdk\";\nimport type { WebhookBenefitCreatedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitcreatedpayload\";\nimport type { WebhookBenefitGrantCreatedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitgrantcreatedpayload\";\nimport type { WebhookBenefitGrantRevokedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitgrantrevokedpayload\";\nimport type { WebhookBenefitGrantUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitgrantupdatedpayload\";\nimport type { WebhookBenefitUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitupdatedpayload\";\nimport type { WebhookCheckoutCreatedPayload } from \"@polar-sh/sdk/models/components/webhookcheckoutcreatedpayload\";\nimport type { WebhookCheckoutUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookcheckoutupdatedpayload\";\nimport type { WebhookCustomerCreatedPayload } from \"@polar-sh/sdk/models/components/webhookcustomercreatedpayload\";\nimport type { WebhookCustomerDeletedPayload } from \"@polar-sh/sdk/models/components/webhookcustomerdeletedpayload\";\nimport type { WebhookCustomerStateChangedPayload } from \"@polar-sh/sdk/models/components/webhookcustomerstatechangedpayload\";\nimport type { WebhookCustomerUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookcustomerupdatedpayload\";\nimport type { WebhookOrderCreatedPayload } from \"@polar-sh/sdk/models/components/webhookordercreatedpayload\";\nimport type { WebhookOrderPaidPayload } from \"@polar-sh/sdk/models/components/webhookorderpaidpayload\";\nimport type { WebhookOrderRefundedPayload } from \"@polar-sh/sdk/models/components/webhookorderrefundedpayload\";\nimport type { WebhookOrganizationUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookorganizationupdatedpayload\";\nimport type { WebhookProductCreatedPayload } from \"@polar-sh/sdk/models/components/webhookproductcreatedpayload\";\nimport type { WebhookProductUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookproductupdatedpayload\";\nimport type { WebhookRefundCreatedPayload } from \"@polar-sh/sdk/models/components/webhookrefundcreatedpayload\";\nimport type { WebhookRefundUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookrefundupdatedpayload\";\nimport type { WebhookSubscriptionActivePayload } from \"@polar-sh/sdk/models/components/webhooksubscriptionactivepayload\";\nimport type { WebhookSubscriptionCanceledPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptioncanceledpayload\";\nimport type { WebhookSubscriptionCreatedPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptioncreatedpayload\";\nimport type { WebhookSubscriptionRevokedPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptionrevokedpayload\";\nimport type { WebhookSubscriptionUncanceledPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptionuncanceledpayload\";\nimport type { WebhookSubscriptionUpdatedPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptionupdatedpayload\";\nimport { validateEvent } from \"@polar-sh/sdk/webhooks\";\nimport { APIError, createAuthEndpoint } from \"better-auth/api\";\n\nexport interface WebhooksOptions {\n\t/**\n\t * Webhook Secret\n\t */\n\tsecret: string;\n\t/**\n\t * Generic handler for all webhooks\n\t */\n\tonPayload?: (payload: ReturnType<typeof validateEvent>) => Promise<void>;\n\t/**\n\t * Webhook for checkout created\n\t */\n\tonCheckoutCreated?: (payload: WebhookCheckoutCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for checkout updated\n\t */\n\tonCheckoutUpdated?: (payload: WebhookCheckoutUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for order created\n\t */\n\tonOrderCreated?: (payload: WebhookOrderCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for order refunded\n\t */\n\tonOrderRefunded?: (payload: WebhookOrderRefundedPayload) => Promise<void>;\n\t/**\n\t * Webhook for order paid\n\t */\n\tonOrderPaid?: (payload: WebhookOrderPaidPayload) => Promise<void>;\n\t/**\n\t * Webhook for refund created\n\t */\n\tonRefundCreated?: (payload: WebhookRefundCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for refund updated\n\t */\n\tonRefundUpdated?: (payload: WebhookRefundUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for subscription created\n\t */\n\tonSubscriptionCreated?: (\n\t\tpayload: WebhookSubscriptionCreatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription updated\n\t */\n\tonSubscriptionUpdated?: (\n\t\tpayload: WebhookSubscriptionUpdatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription active\n\t */\n\tonSubscriptionActive?: (\n\t\tpayload: WebhookSubscriptionActivePayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription canceled\n\t */\n\tonSubscriptionCanceled?: (\n\t\tpayload: WebhookSubscriptionCanceledPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription revoked\n\t */\n\tonSubscriptionRevoked?: (\n\t\tpayload: WebhookSubscriptionRevokedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription uncanceled\n\t */\n\tonSubscriptionUncanceled?: (\n\t\tpayload: WebhookSubscriptionUncanceledPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for product created\n\t */\n\tonProductCreated?: (payload: WebhookProductCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for product updated\n\t */\n\tonProductUpdated?: (payload: WebhookProductUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for organization updated\n\t */\n\tonOrganizationUpdated?: (\n\t\tpayload: WebhookOrganizationUpdatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for benefit created\n\t */\n\tonBenefitCreated?: (payload: WebhookBenefitCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for benefit updated\n\t */\n\tonBenefitUpdated?: (payload: WebhookBenefitUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for benefit grant created\n\t */\n\tonBenefitGrantCreated?: (\n\t\tpayload: WebhookBenefitGrantCreatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for benefit grant updated\n\t */\n\tonBenefitGrantUpdated?: (\n\t\tpayload: WebhookBenefitGrantUpdatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for benefit grant revoked\n\t */\n\tonBenefitGrantRevoked?: (\n\t\tpayload: WebhookBenefitGrantRevokedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for customer created\n\t */\n\tonCustomerCreated?: (payload: WebhookCustomerCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for customer updated\n\t */\n\tonCustomerUpdated?: (payload: WebhookCustomerUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for customer deleted\n\t */\n\tonCustomerDeleted?: (payload: WebhookCustomerDeletedPayload) => Promise<void>;\n\t/**\n\t * Webhook for customer state changed\n\t */\n\tonCustomerStateChanged?: (\n\t\tpayload: WebhookCustomerStateChangedPayload,\n\t) => Promise<void>;\n}\n\nexport const webhooks = (options: WebhooksOptions) => (_polar: Polar) => {\n\treturn {\n\t\tpolarWebhooks: createAuthEndpoint(\n\t\t\t\"/polar/webhooks\",\n\t\t\t{\n\t\t\t\tmethod: \"POST\",\n\t\t\t\tmetadata: {\n\t\t\t\t\tisAction: false,\n\t\t\t\t},\n\t\t\t\tcloneRequest: true,\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tconst { secret, ...eventHandlers } = options;\n\n\t\t\t\tif (!ctx.request?.body) {\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\");\n\t\t\t\t}\n\t\t\t\tconst buf = await ctx.request.text();\n\t\t\t\tlet event: ReturnType<typeof validateEvent>;\n\t\t\t\ttry {\n\t\t\t\t\tif (!secret) {\n\t\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\t\tmessage: \"Polar webhook secret not found\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\tconst headers = {\n\t\t\t\t\t\t\"webhook-id\": ctx.request.headers.get(\"webhook-id\") as string,\n\t\t\t\t\t\t\"webhook-timestamp\": ctx.request.headers.get(\n\t\t\t\t\t\t\t\"webhook-timestamp\",\n\t\t\t\t\t\t) as string,\n\t\t\t\t\t\t\"webhook-signature\": ctx.request.headers.get(\n\t\t\t\t\t\t\t\"webhook-signature\",\n\t\t\t\t\t\t) as string,\n\t\t\t\t\t};\n\n\t\t\t\t\tevent = validateEvent(buf, headers, secret);\n\t\t\t\t} catch (err: unknown) {\n\t\t\t\t\tif (err instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(`${err.message}`);\n\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\tmessage: `Webhook Error: ${err.message}`,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: `Webhook Error: ${err}`,\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tawait handleWebhookPayload(event, {\n\t\t\t\t\t\twebhookSecret: secret,\n\t\t\t\t\t\t...eventHandlers,\n\t\t\t\t\t});\n\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t`Polar webhook failed. Error: ${e.message}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t} else {\n\t\t\t\t\t\tctx.context.logger.error(`Polar webhook failed. Error: ${e}`);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"Webhook error: See server logs for more information.\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\treturn ctx.json({ received: true });\n\t\t\t},\n\t\t),\n\t};\n};\n","import type { BetterAuthPlugin } from \"better-auth\";\nimport {\n\tonAfterUserCreate,\n\tonBeforeUserCreate,\n\tonUserUpdate,\n} from \"./hooks/customer\";\nimport type { PolarEndpoints, PolarOptions } from \"./types\";\n\nexport { polarClient } from \"./client\";\n\nexport * from \"./plugins/portal\";\nexport * from \"./plugins/checkout\";\nexport * from \"./plugins/usage\";\nexport * from \"./plugins/webhooks\";\n\nexport const polar = <O extends PolarOptions>(options: O) => {\n\tconst plugins = options.use\n\t\t.map((use) => use(options.client))\n\t\t.reduce((acc, plugin) => {\n\t\t\tObject.assign(acc, plugin);\n\t\t\treturn acc;\n\t\t}, {} as PolarEndpoints);\n\n\treturn {\n\t\tid: \"polar\",\n\t\tendpoints: {\n\t\t\t...plugins,\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\tbefore: onBeforeUserCreate(options),\n\t\t\t\t\t\t\t\tafter: onAfterUserCreate(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":";AACA,SAAS,gBAAgB;AAGlB,IAAM,qBACZ,CAAC,YACD,OAAO,MAAqB,YAAiD;AAC5E,MAAI,WAAW,QAAQ,wBAAwB;AAC9C,QAAI;AACH,YAAM,SAAS,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S,eAAe;AAAA,YACjC,SAAS,kBAAkB,GAAG;AAAA,UAC/B,CAAC;AAAA,QACF;AAEA,YAAI;AACH,gBAAM,qBAAqB,OAAO;AAAA,YACjC,eAAe;AAAA,YACf,GAAG;AAAA,UACJ,CAAC;AAAA,QACF,SAAS,GAAY;AACpB,cAAI,aAAa,OAAO;AACvB,gBAAI,QAAQ,OAAO;AAAA,cAClB,gCAAgC,EAAE,OAAO;AAAA,YAC1C;AAAA,UACD,OAAO;AACN,gBAAI,QAAQ,OAAO,MAAM,gCAAgC,CAAC,EAAE;AAAA,UAC7D;AAEA,gBAAM,IAAIA,UAAS,eAAe;AAAA,YACjC,SAAS;AAAA,UACV,CAAC;AAAA,QACF;AAEA,eAAO,IAAI,KAAK,EAAE,UAAU,KAAK,CAAC;AAAA,MACnC;AAAA,IACD;AAAA,EACD;AACD;;;AC5NO,IAAM,QAAQ,CAAyB,YAAe;AAC5D,QAAM,UAAU,QAAQ,IACtB,IAAI,CAAC,QAAQ,IAAI,QAAQ,MAAM,CAAC,EAChC,OAAO,CAAC,KAAK,WAAW;AACxB,WAAO,OAAO,KAAK,MAAM;AACzB,WAAO;AAAA,EACR,GAAG,CAAC,CAAmB;AAExB,SAAO;AAAA,IACN,IAAI;AAAA,IACJ,WAAW;AAAA,MACV,GAAG;AAAA,IACJ;AAAA,IACA,OAAO;AACN,aAAO;AAAA,QACN,SAAS;AAAA,UACR,eAAe;AAAA,YACd,MAAM;AAAA,cACL,QAAQ;AAAA,gBACP,QAAQ,mBAAmB,OAAO;AAAA,gBAClC,OAAO,kBAAkB,OAAO;AAAA,cACjC;AAAA,cACA,QAAQ;AAAA,gBACP,OAAO,aAAa,OAAO;AAAA,cAC5B;AAAA,YACD;AAAA,UACD;AAAA,QACD;AAAA,MACD;AAAA,IACD;AAAA,EACD;AACD;","names":["APIError","polar","APIError","createAuthEndpoint","z","polar","checkout","APIError","createAuthEndpoint","sessionMiddleware","z","polar","APIError","createAuthEndpoint"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polar-sh/better-auth",
3
- "version": "1.1.5",
3
+ "version": "1.1.6",
4
4
  "description": "Polar integration for better-auth",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
@@ -36,14 +36,12 @@
36
36
  "better-auth": "^1.3.9",
37
37
  "tsup": "^8.3.5",
38
38
  "vitest": "^2.1.8",
39
- "@polar-sh/adapter-utils": "0.2.7"
40
- },
41
- "dependencies": {
42
- "zod": "^3.24.2"
39
+ "@polar-sh/adapter-utils": "0.2.8"
43
40
  },
44
41
  "peerDependencies": {
45
42
  "@polar-sh/sdk": "^0.35.3",
46
- "better-auth": "^1.3.9"
43
+ "better-auth": "^1.3.9",
44
+ "zod": "^3.24.2 || ^4"
47
45
  },
48
46
  "scripts": {
49
47
  "test": "vitest",