@polar-sh/better-auth 1.1.6 → 1.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +26 -0
- package/dist/index.cjs +40 -18
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +61 -11
- package/dist/index.d.ts +61 -11
- package/dist/index.js +39 -18
- package/dist/index.js.map +1 -1
- package/package.json +4 -1
package/README.md
CHANGED
|
@@ -195,6 +195,32 @@ Checkouts will automatically carry the authenticated User as the customer to the
|
|
|
195
195
|
|
|
196
196
|
If `authenticatedUsersOnly` is `false` - then it will be possible to trigger checkout sessions without any associated customer.
|
|
197
197
|
|
|
198
|
+
|
|
199
|
+
### Checkout Embed
|
|
200
|
+
|
|
201
|
+
You can use the `checkoutEmbed` method to instead open the Checkout as an Embed on your site.
|
|
202
|
+
|
|
203
|
+
```typescript
|
|
204
|
+
const embed = await authClient.checkoutEmbed({
|
|
205
|
+
products: ["e651f46d-ac20-4f26-b769-ad088b123df2"],
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
// Listen for successful completion
|
|
209
|
+
checkout.addEventListener("success", (event) => {
|
|
210
|
+
console.log("Purchase successful!", event.detail);
|
|
211
|
+
|
|
212
|
+
// Call event.preventDefault() if you want to prevent the standard behavior
|
|
213
|
+
// event.preventDefault()
|
|
214
|
+
// Note: For success event, this prevents automatic redirection if redirect is true
|
|
215
|
+
|
|
216
|
+
// If redirect is false, you can show your own success message
|
|
217
|
+
if (!event.detail.redirect) {
|
|
218
|
+
showSuccessMessage();
|
|
219
|
+
}
|
|
220
|
+
// Otherwise, the user will be redirected to the success URL (unless prevented)
|
|
221
|
+
});
|
|
222
|
+
```
|
|
223
|
+
|
|
198
224
|
### Organization Support
|
|
199
225
|
|
|
200
226
|
This plugin supports the Organization plugin. If you pass the organization ID to the Checkout referenceId, you will be able to keep track of purchases made from organization members.
|
package/dist/index.cjs
CHANGED
|
@@ -20,6 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
|
+
CheckoutParams: () => CheckoutParams,
|
|
23
24
|
checkout: () => checkout,
|
|
24
25
|
polar: () => polar,
|
|
25
26
|
polarClient: () => polarClient,
|
|
@@ -111,10 +112,32 @@ var onUserUpdate = (options) => async (user, context) => {
|
|
|
111
112
|
};
|
|
112
113
|
|
|
113
114
|
// src/client.ts
|
|
115
|
+
var import_embed = require("@polar-sh/checkout/embed");
|
|
114
116
|
var polarClient = () => {
|
|
115
117
|
return {
|
|
116
118
|
id: "polar-client",
|
|
117
|
-
$InferServerPlugin: {}
|
|
119
|
+
$InferServerPlugin: {},
|
|
120
|
+
getActions: ($fetch) => {
|
|
121
|
+
return {
|
|
122
|
+
checkoutEmbed: async (data, fetchOptions) => {
|
|
123
|
+
const res = await $fetch("/checkout", {
|
|
124
|
+
method: "POST",
|
|
125
|
+
body: {
|
|
126
|
+
...data,
|
|
127
|
+
redirect: false,
|
|
128
|
+
embedOrigin: window.location.origin
|
|
129
|
+
},
|
|
130
|
+
...fetchOptions
|
|
131
|
+
});
|
|
132
|
+
if (res.error) {
|
|
133
|
+
throw new Error(res.error.message);
|
|
134
|
+
}
|
|
135
|
+
const checkout2 = res.data;
|
|
136
|
+
const theme = new URL(checkout2.url).searchParams.get("theme") ?? "light";
|
|
137
|
+
return await import_embed.PolarEmbedCheckout.create(checkout2.url, theme);
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
}
|
|
118
141
|
};
|
|
119
142
|
};
|
|
120
143
|
|
|
@@ -341,27 +364,24 @@ var portal = () => (polar2) => {
|
|
|
341
364
|
var import_api4 = require("better-auth/api");
|
|
342
365
|
var import_plugins2 = require("better-auth/plugins");
|
|
343
366
|
var import_zod2 = require("zod");
|
|
367
|
+
var CheckoutParams = import_zod2.z.object({
|
|
368
|
+
products: import_zod2.z.union([import_zod2.z.array(import_zod2.z.string()), import_zod2.z.string()]).optional(),
|
|
369
|
+
slug: import_zod2.z.string().optional(),
|
|
370
|
+
referenceId: import_zod2.z.string().optional(),
|
|
371
|
+
customFieldData: import_zod2.z.record(import_zod2.z.string(), import_zod2.z.union([import_zod2.z.string(), import_zod2.z.number(), import_zod2.z.boolean()])).optional(),
|
|
372
|
+
metadata: import_zod2.z.record(import_zod2.z.string(), import_zod2.z.union([import_zod2.z.string(), import_zod2.z.number(), import_zod2.z.boolean()])).optional(),
|
|
373
|
+
allowDiscountCodes: import_zod2.z.coerce.boolean().optional(),
|
|
374
|
+
discountId: import_zod2.z.string().optional(),
|
|
375
|
+
redirect: import_zod2.z.coerce.boolean().optional(),
|
|
376
|
+
embedOrigin: import_zod2.z.string().url().optional()
|
|
377
|
+
});
|
|
344
378
|
var checkout = (checkoutOptions = {}) => (polar2) => {
|
|
345
379
|
return {
|
|
346
380
|
checkout: (0, import_plugins2.createAuthEndpoint)(
|
|
347
381
|
"/checkout",
|
|
348
382
|
{
|
|
349
383
|
method: "POST",
|
|
350
|
-
body:
|
|
351
|
-
products: import_zod2.z.union([import_zod2.z.array(import_zod2.z.string()), import_zod2.z.string()]).optional(),
|
|
352
|
-
slug: import_zod2.z.string().optional(),
|
|
353
|
-
referenceId: import_zod2.z.string().optional(),
|
|
354
|
-
customFieldData: import_zod2.z.record(
|
|
355
|
-
import_zod2.z.string(),
|
|
356
|
-
import_zod2.z.union([import_zod2.z.string(), import_zod2.z.number(), import_zod2.z.boolean()])
|
|
357
|
-
).optional(),
|
|
358
|
-
metadata: import_zod2.z.record(
|
|
359
|
-
import_zod2.z.string(),
|
|
360
|
-
import_zod2.z.union([import_zod2.z.string(), import_zod2.z.number(), import_zod2.z.boolean()])
|
|
361
|
-
).optional(),
|
|
362
|
-
allowDiscountCodes: import_zod2.z.coerce.boolean().optional().default(true),
|
|
363
|
-
discountId: import_zod2.z.string().optional()
|
|
364
|
-
})
|
|
384
|
+
body: CheckoutParams
|
|
365
385
|
},
|
|
366
386
|
async (ctx) => {
|
|
367
387
|
const session = await (0, import_api4.getSessionFromCtx)(ctx);
|
|
@@ -399,7 +419,8 @@ var checkout = (checkoutOptions = {}) => (polar2) => {
|
|
|
399
419
|
} : ctx.body.metadata,
|
|
400
420
|
customFieldData: ctx.body.customFieldData,
|
|
401
421
|
allowDiscountCodes: ctx.body.allowDiscountCodes ?? true,
|
|
402
|
-
discountId: ctx.body.discountId
|
|
422
|
+
discountId: ctx.body.discountId,
|
|
423
|
+
embedOrigin: ctx.body.embedOrigin
|
|
403
424
|
});
|
|
404
425
|
const redirectUrl = new URL(checkout2.url);
|
|
405
426
|
if (checkoutOptions.theme) {
|
|
@@ -407,7 +428,7 @@ var checkout = (checkoutOptions = {}) => (polar2) => {
|
|
|
407
428
|
}
|
|
408
429
|
return ctx.json({
|
|
409
430
|
url: redirectUrl.toString(),
|
|
410
|
-
redirect: true
|
|
431
|
+
redirect: ctx.body.redirect ?? true
|
|
411
432
|
});
|
|
412
433
|
} catch (e) {
|
|
413
434
|
if (e instanceof Error) {
|
|
@@ -767,6 +788,7 @@ var polar = (options) => {
|
|
|
767
788
|
};
|
|
768
789
|
// Annotate the CommonJS export names for ESM import in node:
|
|
769
790
|
0 && (module.exports = {
|
|
791
|
+
CheckoutParams,
|
|
770
792
|
checkout,
|
|
771
793
|
polar,
|
|
772
794
|
polarClient,
|
package/dist/index.cjs.map
CHANGED
|
@@ -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 ?? 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;AAEA,YAAM,IAAI,oBAAS,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;AACH,YAAM,EAAE,QAAQ,kBAAkB,IACjC,MAAM,QAAQ,OAAO,UAAU,KAAK,EAAE,OAAO,KAAK,MAAM,CAAC;AAC1D,YAAM,mBA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AAQ,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 { PolarEmbedCheckout } from \"@polar-sh/checkout/embed\";\nimport type { BetterAuthClientPlugin } from \"better-auth\";\nimport type { BetterFetchOption } from \"better-auth/client\";\nimport type { CheckoutParams, polar } from \"./index\";\n\nexport const polarClient = () => {\n\treturn {\n\t\tid: \"polar-client\",\n\t\t$InferServerPlugin: {} as ReturnType<typeof polar>,\n\t\tgetActions: ($fetch) => {\n\t\t\treturn {\n\t\t\t\tcheckoutEmbed: async (\n\t\t\t\t\tdata: Omit<CheckoutParams, \"redirect\" | \"embedOrigin\">,\n\t\t\t\t\tfetchOptions?: BetterFetchOption,\n\t\t\t\t) => {\n\t\t\t\t\tconst res = await $fetch(\"/checkout\", {\n\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\t...data,\n\t\t\t\t\t\t\tredirect: false,\n\t\t\t\t\t\t\tembedOrigin: window.location.origin,\n\t\t\t\t\t\t},\n\t\t\t\t\t\t...fetchOptions,\n\t\t\t\t\t});\n\n\t\t\t\t\tif (res.error) {\n\t\t\t\t\t\tthrow new Error(res.error.message);\n\t\t\t\t\t}\n\n\t\t\t\t\tconst checkout = res.data as { url: string };\n\n\t\t\t\t\tconst theme =\n\t\t\t\t\t\t(new URL(checkout.url).searchParams.get(\"theme\") as\n\t\t\t\t\t\t\t| \"light\"\n\t\t\t\t\t\t\t| \"dark\"\n\t\t\t\t\t\t\t| undefined) ?? \"light\";\n\n\t\t\t\t\treturn await PolarEmbedCheckout.create(checkout.url, theme);\n\t\t\t\t},\n\t\t\t};\n\t\t},\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 * Redirect to checkout page\n\t */\n\tredirect?: boolean;\n}\n\nexport const CheckoutParams = z.object({\n\tproducts: z.union([z.array(z.string()), z.string()]).optional(),\n\tslug: z.string().optional(),\n\treferenceId: z.string().optional(),\n\tcustomFieldData: z\n\t\t.record(z.string(), z.union([z.string(), z.number(), z.boolean()]))\n\t\t.optional(),\n\tmetadata: z\n\t\t.record(z.string(), z.union([z.string(), z.number(), z.boolean()]))\n\t\t.optional(),\n\tallowDiscountCodes: z.coerce.boolean().optional(),\n\tdiscountId: z.string().optional(),\n\tredirect: z.coerce.boolean().optional(),\n\tembedOrigin: z.string().url().optional(),\n});\n\nexport type CheckoutParams = z.infer<typeof CheckoutParams>;\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: CheckoutParams,\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\tembedOrigin: ctx.body.embedOrigin,\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: ctx.body.redirect ?? 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;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;AAEA,YAAM,IAAI,oBAAS,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;AACH,YAAM,EAAE,QAAQ,kBAAkB,IACjC,MAAM,QAAQ,OAAO,UAAU,KAAK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AAQ,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":["checkout","import_api","polar","import_api","import_plugins","import_zod","polar","checkout","import_api","import_zod","polar","import_api"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -37,6 +37,9 @@ import { WebhookSubscriptionRevokedPayload } from '@polar-sh/sdk/models/componen
|
|
|
37
37
|
import { WebhookSubscriptionUncanceledPayload } from '@polar-sh/sdk/models/components/webhooksubscriptionuncanceledpayload';
|
|
38
38
|
import { WebhookSubscriptionUpdatedPayload } from '@polar-sh/sdk/models/components/webhooksubscriptionupdatedpayload';
|
|
39
39
|
import { validateEvent } from '@polar-sh/sdk/webhooks';
|
|
40
|
+
import * as better_auth_client from 'better-auth/client';
|
|
41
|
+
import { BetterFetchOption } from 'better-auth/client';
|
|
42
|
+
import { PolarEmbedCheckout } from '@polar-sh/checkout/embed';
|
|
40
43
|
|
|
41
44
|
interface CheckoutOptions {
|
|
42
45
|
/**
|
|
@@ -56,14 +59,42 @@ interface CheckoutOptions {
|
|
|
56
59
|
*/
|
|
57
60
|
theme?: "light" | "dark";
|
|
58
61
|
/**
|
|
59
|
-
*
|
|
62
|
+
* Redirect to checkout page
|
|
60
63
|
*/
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Discount ID
|
|
64
|
-
*/
|
|
65
|
-
discountId?: string;
|
|
64
|
+
redirect?: boolean;
|
|
66
65
|
}
|
|
66
|
+
declare const CheckoutParams: z.ZodObject<{
|
|
67
|
+
products: z.ZodOptional<z.ZodUnion<[z.ZodArray<z.ZodString, "many">, z.ZodString]>>;
|
|
68
|
+
slug: z.ZodOptional<z.ZodString>;
|
|
69
|
+
referenceId: z.ZodOptional<z.ZodString>;
|
|
70
|
+
customFieldData: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean]>>>;
|
|
71
|
+
metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean]>>>;
|
|
72
|
+
allowDiscountCodes: z.ZodOptional<z.ZodBoolean>;
|
|
73
|
+
discountId: z.ZodOptional<z.ZodString>;
|
|
74
|
+
redirect: z.ZodOptional<z.ZodBoolean>;
|
|
75
|
+
embedOrigin: z.ZodOptional<z.ZodString>;
|
|
76
|
+
}, "strip", z.ZodTypeAny, {
|
|
77
|
+
products?: string | string[] | undefined;
|
|
78
|
+
slug?: string | undefined;
|
|
79
|
+
referenceId?: string | undefined;
|
|
80
|
+
customFieldData?: Record<string, string | number | boolean> | undefined;
|
|
81
|
+
metadata?: Record<string, string | number | boolean> | undefined;
|
|
82
|
+
allowDiscountCodes?: boolean | undefined;
|
|
83
|
+
discountId?: string | undefined;
|
|
84
|
+
redirect?: boolean | undefined;
|
|
85
|
+
embedOrigin?: string | undefined;
|
|
86
|
+
}, {
|
|
87
|
+
products?: string | string[] | undefined;
|
|
88
|
+
slug?: string | undefined;
|
|
89
|
+
referenceId?: string | undefined;
|
|
90
|
+
customFieldData?: Record<string, string | number | boolean> | undefined;
|
|
91
|
+
metadata?: Record<string, string | number | boolean> | undefined;
|
|
92
|
+
allowDiscountCodes?: boolean | undefined;
|
|
93
|
+
discountId?: string | undefined;
|
|
94
|
+
redirect?: boolean | undefined;
|
|
95
|
+
embedOrigin?: string | undefined;
|
|
96
|
+
}>;
|
|
97
|
+
type CheckoutParams = z.infer<typeof CheckoutParams>;
|
|
67
98
|
declare const checkout: (checkoutOptions?: CheckoutOptions) => (polar: Polar) => {
|
|
68
99
|
checkout: {
|
|
69
100
|
<AsResponse extends boolean = false, ReturnHeaders extends boolean = false>(inputCtx_0: {
|
|
@@ -75,6 +106,8 @@ declare const checkout: (checkoutOptions?: CheckoutOptions) => (polar: Polar) =>
|
|
|
75
106
|
metadata?: Record<string, string | number | boolean> | undefined;
|
|
76
107
|
allowDiscountCodes?: boolean | undefined;
|
|
77
108
|
discountId?: string | undefined;
|
|
109
|
+
redirect?: boolean | undefined;
|
|
110
|
+
embedOrigin?: string | undefined;
|
|
78
111
|
};
|
|
79
112
|
} & {
|
|
80
113
|
method?: "POST" | undefined;
|
|
@@ -112,16 +145,20 @@ declare const checkout: (checkoutOptions?: CheckoutOptions) => (polar: Polar) =>
|
|
|
112
145
|
referenceId: z.ZodOptional<z.ZodString>;
|
|
113
146
|
customFieldData: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean]>>>;
|
|
114
147
|
metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean]>>>;
|
|
115
|
-
allowDiscountCodes: z.
|
|
148
|
+
allowDiscountCodes: z.ZodOptional<z.ZodBoolean>;
|
|
116
149
|
discountId: z.ZodOptional<z.ZodString>;
|
|
150
|
+
redirect: z.ZodOptional<z.ZodBoolean>;
|
|
151
|
+
embedOrigin: z.ZodOptional<z.ZodString>;
|
|
117
152
|
}, "strip", z.ZodTypeAny, {
|
|
118
|
-
allowDiscountCodes: boolean;
|
|
119
153
|
products?: string | string[] | undefined;
|
|
120
154
|
slug?: string | undefined;
|
|
121
155
|
referenceId?: string | undefined;
|
|
122
156
|
customFieldData?: Record<string, string | number | boolean> | undefined;
|
|
123
157
|
metadata?: Record<string, string | number | boolean> | undefined;
|
|
158
|
+
allowDiscountCodes?: boolean | undefined;
|
|
124
159
|
discountId?: string | undefined;
|
|
160
|
+
redirect?: boolean | undefined;
|
|
161
|
+
embedOrigin?: string | undefined;
|
|
125
162
|
}, {
|
|
126
163
|
products?: string | string[] | undefined;
|
|
127
164
|
slug?: string | undefined;
|
|
@@ -130,6 +167,8 @@ declare const checkout: (checkoutOptions?: CheckoutOptions) => (polar: Polar) =>
|
|
|
130
167
|
metadata?: Record<string, string | number | boolean> | undefined;
|
|
131
168
|
allowDiscountCodes?: boolean | undefined;
|
|
132
169
|
discountId?: string | undefined;
|
|
170
|
+
redirect?: boolean | undefined;
|
|
171
|
+
embedOrigin?: string | undefined;
|
|
133
172
|
}>;
|
|
134
173
|
} & {
|
|
135
174
|
use: any[];
|
|
@@ -832,6 +871,9 @@ interface PolarOptions {
|
|
|
832
871
|
declare const polarClient: () => {
|
|
833
872
|
id: "polar-client";
|
|
834
873
|
$InferServerPlugin: ReturnType<typeof polar>;
|
|
874
|
+
getActions: ($fetch: better_auth_client.BetterFetch) => {
|
|
875
|
+
checkoutEmbed: (data: Omit<CheckoutParams, "redirect" | "embedOrigin">, fetchOptions?: BetterFetchOption) => Promise<PolarEmbedCheckout>;
|
|
876
|
+
};
|
|
835
877
|
};
|
|
836
878
|
|
|
837
879
|
declare const polar: <O extends PolarOptions>(options: O) => {
|
|
@@ -847,6 +889,8 @@ declare const polar: <O extends PolarOptions>(options: O) => {
|
|
|
847
889
|
metadata?: Record<string, string | number | boolean> | undefined;
|
|
848
890
|
allowDiscountCodes?: boolean | undefined;
|
|
849
891
|
discountId?: string | undefined;
|
|
892
|
+
redirect?: boolean | undefined;
|
|
893
|
+
embedOrigin?: string | undefined;
|
|
850
894
|
};
|
|
851
895
|
} & {
|
|
852
896
|
method?: "POST" | undefined;
|
|
@@ -884,16 +928,20 @@ declare const polar: <O extends PolarOptions>(options: O) => {
|
|
|
884
928
|
referenceId: zod.ZodOptional<zod.ZodString>;
|
|
885
929
|
customFieldData: zod.ZodOptional<zod.ZodRecord<zod.ZodString, zod.ZodUnion<[zod.ZodString, zod.ZodNumber, zod.ZodBoolean]>>>;
|
|
886
930
|
metadata: zod.ZodOptional<zod.ZodRecord<zod.ZodString, zod.ZodUnion<[zod.ZodString, zod.ZodNumber, zod.ZodBoolean]>>>;
|
|
887
|
-
allowDiscountCodes: zod.
|
|
931
|
+
allowDiscountCodes: zod.ZodOptional<zod.ZodBoolean>;
|
|
888
932
|
discountId: zod.ZodOptional<zod.ZodString>;
|
|
933
|
+
redirect: zod.ZodOptional<zod.ZodBoolean>;
|
|
934
|
+
embedOrigin: zod.ZodOptional<zod.ZodString>;
|
|
889
935
|
}, "strip", zod.ZodTypeAny, {
|
|
890
|
-
allowDiscountCodes: boolean;
|
|
891
936
|
products?: string | string[] | undefined;
|
|
892
937
|
slug?: string | undefined;
|
|
893
938
|
referenceId?: string | undefined;
|
|
894
939
|
customFieldData?: Record<string, string | number | boolean> | undefined;
|
|
895
940
|
metadata?: Record<string, string | number | boolean> | undefined;
|
|
941
|
+
allowDiscountCodes?: boolean | undefined;
|
|
896
942
|
discountId?: string | undefined;
|
|
943
|
+
redirect?: boolean | undefined;
|
|
944
|
+
embedOrigin?: string | undefined;
|
|
897
945
|
}, {
|
|
898
946
|
products?: string | string[] | undefined;
|
|
899
947
|
slug?: string | undefined;
|
|
@@ -902,6 +950,8 @@ declare const polar: <O extends PolarOptions>(options: O) => {
|
|
|
902
950
|
metadata?: Record<string, string | number | boolean> | undefined;
|
|
903
951
|
allowDiscountCodes?: boolean | undefined;
|
|
904
952
|
discountId?: string | undefined;
|
|
953
|
+
redirect?: boolean | undefined;
|
|
954
|
+
embedOrigin?: string | undefined;
|
|
905
955
|
}>;
|
|
906
956
|
} & {
|
|
907
957
|
use: any[];
|
|
@@ -1457,4 +1507,4 @@ declare const polar: <O extends PolarOptions>(options: O) => {
|
|
|
1457
1507
|
};
|
|
1458
1508
|
};
|
|
1459
1509
|
|
|
1460
|
-
export { type CheckoutOptions, type UsageOptions, type WebhooksOptions, checkout, polar, polarClient, portal, usage, webhooks };
|
|
1510
|
+
export { type CheckoutOptions, CheckoutParams, type UsageOptions, type WebhooksOptions, checkout, polar, polarClient, portal, usage, webhooks };
|
package/dist/index.d.ts
CHANGED
|
@@ -37,6 +37,9 @@ import { WebhookSubscriptionRevokedPayload } from '@polar-sh/sdk/models/componen
|
|
|
37
37
|
import { WebhookSubscriptionUncanceledPayload } from '@polar-sh/sdk/models/components/webhooksubscriptionuncanceledpayload';
|
|
38
38
|
import { WebhookSubscriptionUpdatedPayload } from '@polar-sh/sdk/models/components/webhooksubscriptionupdatedpayload';
|
|
39
39
|
import { validateEvent } from '@polar-sh/sdk/webhooks';
|
|
40
|
+
import * as better_auth_client from 'better-auth/client';
|
|
41
|
+
import { BetterFetchOption } from 'better-auth/client';
|
|
42
|
+
import { PolarEmbedCheckout } from '@polar-sh/checkout/embed';
|
|
40
43
|
|
|
41
44
|
interface CheckoutOptions {
|
|
42
45
|
/**
|
|
@@ -56,14 +59,42 @@ interface CheckoutOptions {
|
|
|
56
59
|
*/
|
|
57
60
|
theme?: "light" | "dark";
|
|
58
61
|
/**
|
|
59
|
-
*
|
|
62
|
+
* Redirect to checkout page
|
|
60
63
|
*/
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Discount ID
|
|
64
|
-
*/
|
|
65
|
-
discountId?: string;
|
|
64
|
+
redirect?: boolean;
|
|
66
65
|
}
|
|
66
|
+
declare const CheckoutParams: z.ZodObject<{
|
|
67
|
+
products: z.ZodOptional<z.ZodUnion<[z.ZodArray<z.ZodString, "many">, z.ZodString]>>;
|
|
68
|
+
slug: z.ZodOptional<z.ZodString>;
|
|
69
|
+
referenceId: z.ZodOptional<z.ZodString>;
|
|
70
|
+
customFieldData: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean]>>>;
|
|
71
|
+
metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean]>>>;
|
|
72
|
+
allowDiscountCodes: z.ZodOptional<z.ZodBoolean>;
|
|
73
|
+
discountId: z.ZodOptional<z.ZodString>;
|
|
74
|
+
redirect: z.ZodOptional<z.ZodBoolean>;
|
|
75
|
+
embedOrigin: z.ZodOptional<z.ZodString>;
|
|
76
|
+
}, "strip", z.ZodTypeAny, {
|
|
77
|
+
products?: string | string[] | undefined;
|
|
78
|
+
slug?: string | undefined;
|
|
79
|
+
referenceId?: string | undefined;
|
|
80
|
+
customFieldData?: Record<string, string | number | boolean> | undefined;
|
|
81
|
+
metadata?: Record<string, string | number | boolean> | undefined;
|
|
82
|
+
allowDiscountCodes?: boolean | undefined;
|
|
83
|
+
discountId?: string | undefined;
|
|
84
|
+
redirect?: boolean | undefined;
|
|
85
|
+
embedOrigin?: string | undefined;
|
|
86
|
+
}, {
|
|
87
|
+
products?: string | string[] | undefined;
|
|
88
|
+
slug?: string | undefined;
|
|
89
|
+
referenceId?: string | undefined;
|
|
90
|
+
customFieldData?: Record<string, string | number | boolean> | undefined;
|
|
91
|
+
metadata?: Record<string, string | number | boolean> | undefined;
|
|
92
|
+
allowDiscountCodes?: boolean | undefined;
|
|
93
|
+
discountId?: string | undefined;
|
|
94
|
+
redirect?: boolean | undefined;
|
|
95
|
+
embedOrigin?: string | undefined;
|
|
96
|
+
}>;
|
|
97
|
+
type CheckoutParams = z.infer<typeof CheckoutParams>;
|
|
67
98
|
declare const checkout: (checkoutOptions?: CheckoutOptions) => (polar: Polar) => {
|
|
68
99
|
checkout: {
|
|
69
100
|
<AsResponse extends boolean = false, ReturnHeaders extends boolean = false>(inputCtx_0: {
|
|
@@ -75,6 +106,8 @@ declare const checkout: (checkoutOptions?: CheckoutOptions) => (polar: Polar) =>
|
|
|
75
106
|
metadata?: Record<string, string | number | boolean> | undefined;
|
|
76
107
|
allowDiscountCodes?: boolean | undefined;
|
|
77
108
|
discountId?: string | undefined;
|
|
109
|
+
redirect?: boolean | undefined;
|
|
110
|
+
embedOrigin?: string | undefined;
|
|
78
111
|
};
|
|
79
112
|
} & {
|
|
80
113
|
method?: "POST" | undefined;
|
|
@@ -112,16 +145,20 @@ declare const checkout: (checkoutOptions?: CheckoutOptions) => (polar: Polar) =>
|
|
|
112
145
|
referenceId: z.ZodOptional<z.ZodString>;
|
|
113
146
|
customFieldData: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean]>>>;
|
|
114
147
|
metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean]>>>;
|
|
115
|
-
allowDiscountCodes: z.
|
|
148
|
+
allowDiscountCodes: z.ZodOptional<z.ZodBoolean>;
|
|
116
149
|
discountId: z.ZodOptional<z.ZodString>;
|
|
150
|
+
redirect: z.ZodOptional<z.ZodBoolean>;
|
|
151
|
+
embedOrigin: z.ZodOptional<z.ZodString>;
|
|
117
152
|
}, "strip", z.ZodTypeAny, {
|
|
118
|
-
allowDiscountCodes: boolean;
|
|
119
153
|
products?: string | string[] | undefined;
|
|
120
154
|
slug?: string | undefined;
|
|
121
155
|
referenceId?: string | undefined;
|
|
122
156
|
customFieldData?: Record<string, string | number | boolean> | undefined;
|
|
123
157
|
metadata?: Record<string, string | number | boolean> | undefined;
|
|
158
|
+
allowDiscountCodes?: boolean | undefined;
|
|
124
159
|
discountId?: string | undefined;
|
|
160
|
+
redirect?: boolean | undefined;
|
|
161
|
+
embedOrigin?: string | undefined;
|
|
125
162
|
}, {
|
|
126
163
|
products?: string | string[] | undefined;
|
|
127
164
|
slug?: string | undefined;
|
|
@@ -130,6 +167,8 @@ declare const checkout: (checkoutOptions?: CheckoutOptions) => (polar: Polar) =>
|
|
|
130
167
|
metadata?: Record<string, string | number | boolean> | undefined;
|
|
131
168
|
allowDiscountCodes?: boolean | undefined;
|
|
132
169
|
discountId?: string | undefined;
|
|
170
|
+
redirect?: boolean | undefined;
|
|
171
|
+
embedOrigin?: string | undefined;
|
|
133
172
|
}>;
|
|
134
173
|
} & {
|
|
135
174
|
use: any[];
|
|
@@ -832,6 +871,9 @@ interface PolarOptions {
|
|
|
832
871
|
declare const polarClient: () => {
|
|
833
872
|
id: "polar-client";
|
|
834
873
|
$InferServerPlugin: ReturnType<typeof polar>;
|
|
874
|
+
getActions: ($fetch: better_auth_client.BetterFetch) => {
|
|
875
|
+
checkoutEmbed: (data: Omit<CheckoutParams, "redirect" | "embedOrigin">, fetchOptions?: BetterFetchOption) => Promise<PolarEmbedCheckout>;
|
|
876
|
+
};
|
|
835
877
|
};
|
|
836
878
|
|
|
837
879
|
declare const polar: <O extends PolarOptions>(options: O) => {
|
|
@@ -847,6 +889,8 @@ declare const polar: <O extends PolarOptions>(options: O) => {
|
|
|
847
889
|
metadata?: Record<string, string | number | boolean> | undefined;
|
|
848
890
|
allowDiscountCodes?: boolean | undefined;
|
|
849
891
|
discountId?: string | undefined;
|
|
892
|
+
redirect?: boolean | undefined;
|
|
893
|
+
embedOrigin?: string | undefined;
|
|
850
894
|
};
|
|
851
895
|
} & {
|
|
852
896
|
method?: "POST" | undefined;
|
|
@@ -884,16 +928,20 @@ declare const polar: <O extends PolarOptions>(options: O) => {
|
|
|
884
928
|
referenceId: zod.ZodOptional<zod.ZodString>;
|
|
885
929
|
customFieldData: zod.ZodOptional<zod.ZodRecord<zod.ZodString, zod.ZodUnion<[zod.ZodString, zod.ZodNumber, zod.ZodBoolean]>>>;
|
|
886
930
|
metadata: zod.ZodOptional<zod.ZodRecord<zod.ZodString, zod.ZodUnion<[zod.ZodString, zod.ZodNumber, zod.ZodBoolean]>>>;
|
|
887
|
-
allowDiscountCodes: zod.
|
|
931
|
+
allowDiscountCodes: zod.ZodOptional<zod.ZodBoolean>;
|
|
888
932
|
discountId: zod.ZodOptional<zod.ZodString>;
|
|
933
|
+
redirect: zod.ZodOptional<zod.ZodBoolean>;
|
|
934
|
+
embedOrigin: zod.ZodOptional<zod.ZodString>;
|
|
889
935
|
}, "strip", zod.ZodTypeAny, {
|
|
890
|
-
allowDiscountCodes: boolean;
|
|
891
936
|
products?: string | string[] | undefined;
|
|
892
937
|
slug?: string | undefined;
|
|
893
938
|
referenceId?: string | undefined;
|
|
894
939
|
customFieldData?: Record<string, string | number | boolean> | undefined;
|
|
895
940
|
metadata?: Record<string, string | number | boolean> | undefined;
|
|
941
|
+
allowDiscountCodes?: boolean | undefined;
|
|
896
942
|
discountId?: string | undefined;
|
|
943
|
+
redirect?: boolean | undefined;
|
|
944
|
+
embedOrigin?: string | undefined;
|
|
897
945
|
}, {
|
|
898
946
|
products?: string | string[] | undefined;
|
|
899
947
|
slug?: string | undefined;
|
|
@@ -902,6 +950,8 @@ declare const polar: <O extends PolarOptions>(options: O) => {
|
|
|
902
950
|
metadata?: Record<string, string | number | boolean> | undefined;
|
|
903
951
|
allowDiscountCodes?: boolean | undefined;
|
|
904
952
|
discountId?: string | undefined;
|
|
953
|
+
redirect?: boolean | undefined;
|
|
954
|
+
embedOrigin?: string | undefined;
|
|
905
955
|
}>;
|
|
906
956
|
} & {
|
|
907
957
|
use: any[];
|
|
@@ -1457,4 +1507,4 @@ declare const polar: <O extends PolarOptions>(options: O) => {
|
|
|
1457
1507
|
};
|
|
1458
1508
|
};
|
|
1459
1509
|
|
|
1460
|
-
export { type CheckoutOptions, type UsageOptions, type WebhooksOptions, checkout, polar, polarClient, portal, usage, webhooks };
|
|
1510
|
+
export { type CheckoutOptions, CheckoutParams, type UsageOptions, type WebhooksOptions, checkout, polar, polarClient, portal, usage, webhooks };
|
package/dist/index.js
CHANGED
|
@@ -80,10 +80,32 @@ var onUserUpdate = (options) => async (user, context) => {
|
|
|
80
80
|
};
|
|
81
81
|
|
|
82
82
|
// src/client.ts
|
|
83
|
+
import { PolarEmbedCheckout } from "@polar-sh/checkout/embed";
|
|
83
84
|
var polarClient = () => {
|
|
84
85
|
return {
|
|
85
86
|
id: "polar-client",
|
|
86
|
-
$InferServerPlugin: {}
|
|
87
|
+
$InferServerPlugin: {},
|
|
88
|
+
getActions: ($fetch) => {
|
|
89
|
+
return {
|
|
90
|
+
checkoutEmbed: async (data, fetchOptions) => {
|
|
91
|
+
const res = await $fetch("/checkout", {
|
|
92
|
+
method: "POST",
|
|
93
|
+
body: {
|
|
94
|
+
...data,
|
|
95
|
+
redirect: false,
|
|
96
|
+
embedOrigin: window.location.origin
|
|
97
|
+
},
|
|
98
|
+
...fetchOptions
|
|
99
|
+
});
|
|
100
|
+
if (res.error) {
|
|
101
|
+
throw new Error(res.error.message);
|
|
102
|
+
}
|
|
103
|
+
const checkout2 = res.data;
|
|
104
|
+
const theme = new URL(checkout2.url).searchParams.get("theme") ?? "light";
|
|
105
|
+
return await PolarEmbedCheckout.create(checkout2.url, theme);
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
}
|
|
87
109
|
};
|
|
88
110
|
};
|
|
89
111
|
|
|
@@ -310,27 +332,24 @@ var portal = () => (polar2) => {
|
|
|
310
332
|
import { APIError as APIError3, getSessionFromCtx } from "better-auth/api";
|
|
311
333
|
import { createAuthEndpoint as createAuthEndpoint2 } from "better-auth/plugins";
|
|
312
334
|
import { z as z2 } from "zod";
|
|
335
|
+
var CheckoutParams = z2.object({
|
|
336
|
+
products: z2.union([z2.array(z2.string()), z2.string()]).optional(),
|
|
337
|
+
slug: z2.string().optional(),
|
|
338
|
+
referenceId: z2.string().optional(),
|
|
339
|
+
customFieldData: z2.record(z2.string(), z2.union([z2.string(), z2.number(), z2.boolean()])).optional(),
|
|
340
|
+
metadata: z2.record(z2.string(), z2.union([z2.string(), z2.number(), z2.boolean()])).optional(),
|
|
341
|
+
allowDiscountCodes: z2.coerce.boolean().optional(),
|
|
342
|
+
discountId: z2.string().optional(),
|
|
343
|
+
redirect: z2.coerce.boolean().optional(),
|
|
344
|
+
embedOrigin: z2.string().url().optional()
|
|
345
|
+
});
|
|
313
346
|
var checkout = (checkoutOptions = {}) => (polar2) => {
|
|
314
347
|
return {
|
|
315
348
|
checkout: createAuthEndpoint2(
|
|
316
349
|
"/checkout",
|
|
317
350
|
{
|
|
318
351
|
method: "POST",
|
|
319
|
-
body:
|
|
320
|
-
products: z2.union([z2.array(z2.string()), z2.string()]).optional(),
|
|
321
|
-
slug: z2.string().optional(),
|
|
322
|
-
referenceId: z2.string().optional(),
|
|
323
|
-
customFieldData: z2.record(
|
|
324
|
-
z2.string(),
|
|
325
|
-
z2.union([z2.string(), z2.number(), z2.boolean()])
|
|
326
|
-
).optional(),
|
|
327
|
-
metadata: z2.record(
|
|
328
|
-
z2.string(),
|
|
329
|
-
z2.union([z2.string(), z2.number(), z2.boolean()])
|
|
330
|
-
).optional(),
|
|
331
|
-
allowDiscountCodes: z2.coerce.boolean().optional().default(true),
|
|
332
|
-
discountId: z2.string().optional()
|
|
333
|
-
})
|
|
352
|
+
body: CheckoutParams
|
|
334
353
|
},
|
|
335
354
|
async (ctx) => {
|
|
336
355
|
const session = await getSessionFromCtx(ctx);
|
|
@@ -368,7 +387,8 @@ var checkout = (checkoutOptions = {}) => (polar2) => {
|
|
|
368
387
|
} : ctx.body.metadata,
|
|
369
388
|
customFieldData: ctx.body.customFieldData,
|
|
370
389
|
allowDiscountCodes: ctx.body.allowDiscountCodes ?? true,
|
|
371
|
-
discountId: ctx.body.discountId
|
|
390
|
+
discountId: ctx.body.discountId,
|
|
391
|
+
embedOrigin: ctx.body.embedOrigin
|
|
372
392
|
});
|
|
373
393
|
const redirectUrl = new URL(checkout2.url);
|
|
374
394
|
if (checkoutOptions.theme) {
|
|
@@ -376,7 +396,7 @@ var checkout = (checkoutOptions = {}) => (polar2) => {
|
|
|
376
396
|
}
|
|
377
397
|
return ctx.json({
|
|
378
398
|
url: redirectUrl.toString(),
|
|
379
|
-
redirect: true
|
|
399
|
+
redirect: ctx.body.redirect ?? true
|
|
380
400
|
});
|
|
381
401
|
} catch (e) {
|
|
382
402
|
if (e instanceof Error) {
|
|
@@ -739,6 +759,7 @@ var polar = (options) => {
|
|
|
739
759
|
};
|
|
740
760
|
};
|
|
741
761
|
export {
|
|
762
|
+
CheckoutParams,
|
|
742
763
|
checkout,
|
|
743
764
|
polar,
|
|
744
765
|
polarClient,
|
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 ?? 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,Y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","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 { PolarEmbedCheckout } from \"@polar-sh/checkout/embed\";\nimport type { BetterAuthClientPlugin } from \"better-auth\";\nimport type { BetterFetchOption } from \"better-auth/client\";\nimport type { CheckoutParams, polar } from \"./index\";\n\nexport const polarClient = () => {\n\treturn {\n\t\tid: \"polar-client\",\n\t\t$InferServerPlugin: {} as ReturnType<typeof polar>,\n\t\tgetActions: ($fetch) => {\n\t\t\treturn {\n\t\t\t\tcheckoutEmbed: async (\n\t\t\t\t\tdata: Omit<CheckoutParams, \"redirect\" | \"embedOrigin\">,\n\t\t\t\t\tfetchOptions?: BetterFetchOption,\n\t\t\t\t) => {\n\t\t\t\t\tconst res = await $fetch(\"/checkout\", {\n\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\t...data,\n\t\t\t\t\t\t\tredirect: false,\n\t\t\t\t\t\t\tembedOrigin: window.location.origin,\n\t\t\t\t\t\t},\n\t\t\t\t\t\t...fetchOptions,\n\t\t\t\t\t});\n\n\t\t\t\t\tif (res.error) {\n\t\t\t\t\t\tthrow new Error(res.error.message);\n\t\t\t\t\t}\n\n\t\t\t\t\tconst checkout = res.data as { url: string };\n\n\t\t\t\t\tconst theme =\n\t\t\t\t\t\t(new URL(checkout.url).searchParams.get(\"theme\") as\n\t\t\t\t\t\t\t| \"light\"\n\t\t\t\t\t\t\t| \"dark\"\n\t\t\t\t\t\t\t| undefined) ?? \"light\";\n\n\t\t\t\t\treturn await PolarEmbedCheckout.create(checkout.url, theme);\n\t\t\t\t},\n\t\t\t};\n\t\t},\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 * Redirect to checkout page\n\t */\n\tredirect?: boolean;\n}\n\nexport const CheckoutParams = z.object({\n\tproducts: z.union([z.array(z.string()), z.string()]).optional(),\n\tslug: z.string().optional(),\n\treferenceId: z.string().optional(),\n\tcustomFieldData: z\n\t\t.record(z.string(), z.union([z.string(), z.number(), z.boolean()]))\n\t\t.optional(),\n\tmetadata: z\n\t\t.record(z.string(), z.union([z.string(), z.number(), z.boolean()]))\n\t\t.optional(),\n\tallowDiscountCodes: z.coerce.boolean().optional(),\n\tdiscountId: z.string().optional(),\n\tredirect: z.coerce.boolean().optional(),\n\tembedOrigin: z.string().url().optional(),\n});\n\nexport type CheckoutParams = z.infer<typeof CheckoutParams>;\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: CheckoutParams,\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\tembedOrigin: ctx.body.embedOrigin,\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: ctx.body.redirect ?? 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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C,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":["checkout","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.
|
|
3
|
+
"version": "1.1.7",
|
|
4
4
|
"description": "Polar integration for better-auth",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -43,6 +43,9 @@
|
|
|
43
43
|
"better-auth": "^1.3.9",
|
|
44
44
|
"zod": "^3.24.2 || ^4"
|
|
45
45
|
},
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"@polar-sh/checkout": "^0.1.12"
|
|
48
|
+
},
|
|
46
49
|
"scripts": {
|
|
47
50
|
"test": "vitest",
|
|
48
51
|
"test:watch": "vitest",
|