@polar-sh/better-auth 0.0.5 → 0.0.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/dist/index.cjs +19 -10
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +19 -10
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -367,7 +367,6 @@ var webhooks = (options) => (0, import_plugins4.createAuthEndpoint)(
|
|
|
367
367
|
);
|
|
368
368
|
|
|
369
369
|
// src/hooks/customer.ts
|
|
370
|
-
var import_api5 = require("better-auth/api");
|
|
371
370
|
var onUserCreate = (options) => async (user, ctx) => {
|
|
372
371
|
if (ctx && options.createCustomerOnSignUp) {
|
|
373
372
|
try {
|
|
@@ -375,12 +374,25 @@ var onUserCreate = (options) => async (user, ctx) => {
|
|
|
375
374
|
user,
|
|
376
375
|
session: ctx.context.session.session
|
|
377
376
|
}) : {};
|
|
378
|
-
await options.client.customers.
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
377
|
+
const { result: existingCustomers } = await options.client.customers.list({ email: user.email });
|
|
378
|
+
const existingCustomer = existingCustomers.items[0];
|
|
379
|
+
if (existingCustomer) {
|
|
380
|
+
if (existingCustomer.externalId !== user.id) {
|
|
381
|
+
await options.client.customers.update({
|
|
382
|
+
id: existingCustomer.id,
|
|
383
|
+
customerUpdate: {
|
|
384
|
+
externalId: user.id
|
|
385
|
+
}
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
} else {
|
|
389
|
+
await options.client.customers.create({
|
|
390
|
+
...params,
|
|
391
|
+
email: user.email,
|
|
392
|
+
name: user.name,
|
|
393
|
+
externalId: user.id
|
|
394
|
+
});
|
|
395
|
+
}
|
|
384
396
|
} catch (e) {
|
|
385
397
|
if (e instanceof Error) {
|
|
386
398
|
ctx.context.logger.error(
|
|
@@ -391,9 +403,6 @@ var onUserCreate = (options) => async (user, ctx) => {
|
|
|
391
403
|
`Polar customer creation failed. Error: ${e}`
|
|
392
404
|
);
|
|
393
405
|
}
|
|
394
|
-
throw new import_api5.APIError("BAD_REQUEST", {
|
|
395
|
-
message: "Polar customer creation failed. See server logs for more information."
|
|
396
|
-
});
|
|
397
406
|
}
|
|
398
407
|
}
|
|
399
408
|
};
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/endpoints/checkout.ts","../src/endpoints/customerPortal.ts","../src/endpoints/customerState.ts","../src/endpoints/webhooks.ts","../src/hooks/customer.ts"],"sourcesContent":["import type { BetterAuthPlugin } from \"better-auth\";\nimport { checkout } from \"./endpoints/checkout\";\nimport { customerPortal } from \"./endpoints/customerPortal\";\nimport { customerState } from \"./endpoints/customerState\";\nimport { webhooks } from \"./endpoints/webhooks\";\nimport { onUserCreate, onUserUpdate } from \"./hooks/customer\";\nimport type { PolarOptions } from \"./types\";\n\nexport const polar = <O extends PolarOptions>(options: O) => {\n\treturn {\n\t\tid: \"polar\",\n\t\tendpoints: {\n\t\t\tpolarCheckout: checkout(options),\n\t\t\tpolarWebhooks: webhooks(options),\n\t\t\tpolarCustomerPortal: customerPortal(options),\n\t\t\tpolarCustomerState: customerState(options),\n\t\t},\n\t\tinit() {\n\t\t\treturn {\n\t\t\t\toptions: {\n\t\t\t\t\tdatabaseHooks: {\n\t\t\t\t\t\tuser: {\n\t\t\t\t\t\t\tcreate: {\n\t\t\t\t\t\t\t\tafter: onUserCreate(options),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\tupdate: {\n\t\t\t\t\t\t\t\tafter: onUserUpdate(options),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t};\n\t\t},\n\t} satisfies BetterAuthPlugin;\n};\n","import { APIError, getSessionFromCtx } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport { z } from \"zod\";\nimport type { PolarOptions } from \"../types\";\n\nexport const checkout = (options: PolarOptions) =>\n\tcreateAuthEndpoint(\n\t\t\"/checkout/:slug\",\n\t\t{\n\t\t\tmethod: \"GET\",\n\t\t\tquery: z.object({\n\t\t\t\tproductId: z.string().optional(),\n\t\t\t}),\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tif (!options.checkout?.enabled) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"Checkout is not enabled\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tlet productId = ctx.query?.productId;\n\n\t\t\tif (ctx.params.slug) {\n\t\t\t\tconst products = await (typeof options.checkout.products === \"function\"\n\t\t\t\t\t? options.checkout.products()\n\t\t\t\t\t: options.checkout.products);\n\n\t\t\t\tproductId = products.find(\n\t\t\t\t\t(product) => product.slug === ctx.params.slug,\n\t\t\t\t)?.productId;\n\t\t\t}\n\n\t\t\tif (!productId) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"Product Id not found\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tconst session = await getSessionFromCtx(ctx);\n\n\t\t\ttry {\n\t\t\t\tconst checkout = await options.client.checkouts.create({\n\t\t\t\t\tcustomerExternalId: session?.user.id,\n\t\t\t\t\tproductId,\n\t\t\t\t\tsuccessUrl: options.checkout.successUrl\n\t\t\t\t\t\t? new URL(options.checkout.successUrl, ctx.request?.url).toString()\n\t\t\t\t\t\t: undefined,\n\t\t\t\t});\n\n\t\t\t\treturn ctx.redirect(checkout.url);\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar checkout creation failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\tmessage: \"Checkout creation failed\",\n\t\t\t\t});\n\t\t\t}\n\t\t},\n\t);\n","import { APIError, sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport type { PolarOptions } from \"../types\";\n\nexport const customerPortal = (options: PolarOptions) =>\n\tcreateAuthEndpoint(\n\t\t\"/portal\",\n\t\t{\n\t\t\tmethod: \"GET\",\n\t\t\tuse: [sessionMiddleware],\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tif (!options.enableCustomerPortal) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"Customer portal is not enabled\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tif (!ctx.context.session?.user.id) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\ttry {\n\t\t\t\tconst customerSession = await options.client.customerSessions.create({\n\t\t\t\t\tcustomerExternalId: ctx.context.session?.user.id,\n\t\t\t\t});\n\n\t\t\t\treturn ctx.redirect(customerSession.customerPortalUrl);\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar customer portal creation failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\tmessage: \"Customer portal creation failed\",\n\t\t\t\t});\n\t\t\t}\n\t\t},\n\t);\n","import { APIError, sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport type { PolarOptions } from \"../types\";\n\nexport const customerState = (options: PolarOptions) =>\n\tcreateAuthEndpoint(\n\t\t\"/state\",\n\t\t{\n\t\t\tmethod: \"GET\",\n\t\t\tuse: [sessionMiddleware],\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\ttry {\n\t\t\t\tconst state = await options.client.customers.getStateExternal({\n\t\t\t\t\texternalId: ctx.context.session?.user.id,\n\t\t\t\t});\n\n\t\t\t\treturn ctx.json(state);\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar subscriptions list failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\tmessage: \"Subscriptions list failed\",\n\t\t\t\t});\n\t\t\t}\n\t\t},\n\t);\n","import { validateEvent } from \"@polar-sh/sdk/webhooks.js\";\nimport { APIError } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport type { PolarOptions } from \"../types\";\n\nexport const webhooks = (options: PolarOptions) =>\n\tcreateAuthEndpoint(\n\t\t\"/polar/webhooks\",\n\t\t{\n\t\t\tmethod: \"POST\",\n\t\t\tmetadata: {\n\t\t\t\tisAction: false,\n\t\t\t},\n\t\t\tcloneRequest: true,\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tconst { webhooks } = options;\n\n\t\t\tif (!webhooks) {\n\t\t\t\tthrow new APIError(\"NOT_FOUND\", {\n\t\t\t\t\tmessage: \"Webhooks not enabled\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tconst {\n\t\t\t\tsecret,\n\t\t\t\tonPayload,\n\t\t\t\tonCheckoutCreated,\n\t\t\t\tonCheckoutUpdated,\n\t\t\t\tonOrderCreated,\n\t\t\t\tonOrderRefunded,\n\t\t\t\tonRefundCreated,\n\t\t\t\tonRefundUpdated,\n\t\t\t\tonSubscriptionCreated,\n\t\t\t\tonSubscriptionUpdated,\n\t\t\t\tonSubscriptionActive,\n\t\t\t\tonSubscriptionCanceled,\n\t\t\t\tonSubscriptionRevoked,\n\t\t\t\tonSubscriptionUncanceled,\n\t\t\t\tonProductCreated,\n\t\t\t\tonProductUpdated,\n\t\t\t\tonOrganizationUpdated,\n\t\t\t\tonBenefitCreated,\n\t\t\t\tonBenefitUpdated,\n\t\t\t\tonBenefitGrantCreated,\n\t\t\t\tonBenefitGrantUpdated,\n\t\t\t\tonBenefitGrantRevoked,\n\t\t\t\tonCustomerCreated,\n\t\t\t\tonCustomerUpdated,\n\t\t\t\tonCustomerDeleted,\n\t\t\t\tonCustomerStateChanged,\n\t\t\t} = webhooks;\n\n\t\t\tif (!ctx.request?.body) {\n\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\");\n\t\t\t}\n\t\t\tconst buf = await ctx.request.text();\n\t\t\tlet event: ReturnType<typeof validateEvent>;\n\t\t\ttry {\n\t\t\t\tif (!secret) {\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: \"Polar webhook secret not found\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\tconst headers = {\n\t\t\t\t\t\"webhook-id\": ctx.request.headers.get(\"webhook-id\") as string,\n\t\t\t\t\t\"webhook-timestamp\": ctx.request.headers.get(\n\t\t\t\t\t\t\"webhook-timestamp\",\n\t\t\t\t\t) as string,\n\t\t\t\t\t\"webhook-signature\": ctx.request.headers.get(\n\t\t\t\t\t\t\"webhook-signature\",\n\t\t\t\t\t) as string,\n\t\t\t\t};\n\n\t\t\t\tevent = validateEvent(buf, headers, secret);\n\t\t\t} catch (err: unknown) {\n\t\t\t\tif (err instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(`${err.message}`);\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: `Webhook Error: ${err.message}`,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: `Webhook Error: ${err}`,\n\t\t\t\t});\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tif (onPayload) {\n\t\t\t\t\tonPayload(event);\n\t\t\t\t}\n\n\t\t\t\tswitch (event.type) {\n\t\t\t\t\tcase \"checkout.created\":\n\t\t\t\t\t\tif (onCheckoutCreated) {\n\t\t\t\t\t\t\tonCheckoutCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"checkout.updated\":\n\t\t\t\t\t\tif (onCheckoutUpdated) {\n\t\t\t\t\t\t\tonCheckoutUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"order.created\":\n\t\t\t\t\t\tif (onOrderCreated) {\n\t\t\t\t\t\t\tonOrderCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.created\":\n\t\t\t\t\t\tif (onSubscriptionCreated) {\n\t\t\t\t\t\t\tonSubscriptionCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.updated\":\n\t\t\t\t\t\tif (onSubscriptionUpdated) {\n\t\t\t\t\t\t\tonSubscriptionUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.active\":\n\t\t\t\t\t\tif (onSubscriptionActive) {\n\t\t\t\t\t\t\tonSubscriptionActive(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.canceled\":\n\t\t\t\t\t\tif (onSubscriptionCanceled) {\n\t\t\t\t\t\t\tonSubscriptionCanceled(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.uncanceled\":\n\t\t\t\t\t\tif (onSubscriptionUncanceled) {\n\t\t\t\t\t\t\tonSubscriptionUncanceled(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.revoked\":\n\t\t\t\t\t\tif (onSubscriptionRevoked) {\n\t\t\t\t\t\t\tonSubscriptionRevoked(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"product.created\":\n\t\t\t\t\t\tif (onProductCreated) {\n\t\t\t\t\t\t\tonProductCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"product.updated\":\n\t\t\t\t\t\tif (onProductUpdated) {\n\t\t\t\t\t\t\tonProductUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"organization.updated\":\n\t\t\t\t\t\tif (onOrganizationUpdated) {\n\t\t\t\t\t\t\tonOrganizationUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"benefit.created\":\n\t\t\t\t\t\tif (onBenefitCreated) {\n\t\t\t\t\t\t\tonBenefitCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"benefit.updated\":\n\t\t\t\t\t\tif (onBenefitUpdated) {\n\t\t\t\t\t\t\tonBenefitUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"benefit_grant.created\":\n\t\t\t\t\t\tif (onBenefitGrantCreated) {\n\t\t\t\t\t\t\tonBenefitGrantCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"benefit_grant.updated\":\n\t\t\t\t\t\tif (onBenefitGrantUpdated) {\n\t\t\t\t\t\t\tonBenefitGrantUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"benefit_grant.revoked\":\n\t\t\t\t\t\tif (onBenefitGrantRevoked) {\n\t\t\t\t\t\t\tonBenefitGrantRevoked(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"order.refunded\":\n\t\t\t\t\t\tif (onOrderRefunded) {\n\t\t\t\t\t\t\tonOrderRefunded(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"refund.created\":\n\t\t\t\t\t\tif (onRefundCreated) {\n\t\t\t\t\t\t\tonRefundCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"refund.updated\":\n\t\t\t\t\t\tif (onRefundUpdated) {\n\t\t\t\t\t\t\tonRefundUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"customer.created\":\n\t\t\t\t\t\tif (onCustomerCreated) {\n\t\t\t\t\t\t\tonCustomerCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"customer.updated\":\n\t\t\t\t\t\tif (onCustomerUpdated) {\n\t\t\t\t\t\t\tonCustomerUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"customer.deleted\":\n\t\t\t\t\t\tif (onCustomerDeleted) {\n\t\t\t\t\t\t\tonCustomerDeleted(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"customer.state_changed\":\n\t\t\t\t\t\tif (onCustomerStateChanged) {\n\t\t\t\t\t\t\tonCustomerStateChanged(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(`Polar webhook failed. Error: ${e.message}`);\n\t\t\t\t} else {\n\t\t\t\t\tctx.context.logger.error(`Polar webhook failed. Error: ${e}`);\n\t\t\t\t}\n\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"Webhook error: See server logs for more information.\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\treturn ctx.json({ received: true });\n\t\t},\n\t);\n","import type { GenericEndpointContext, User } from \"better-auth\";\nimport { APIError } from \"better-auth/api\";\nimport type { PolarOptions } from \"../types\";\n\nexport const onUserCreate =\n\t(options: PolarOptions) =>\n\tasync (user: User, ctx?: GenericEndpointContext) => {\n\t\tif (ctx && options.createCustomerOnSignUp) {\n\t\t\ttry {\n\t\t\t\tconst params =\n\t\t\t\t\toptions.getCustomerCreateParams && ctx.context.session\n\t\t\t\t\t\t? await options.getCustomerCreateParams({\n\t\t\t\t\t\t\t\tuser,\n\t\t\t\t\t\t\t\tsession: ctx.context.session.session,\n\t\t\t\t\t\t\t})\n\t\t\t\t\t\t: {};\n\n\t\t\t\tawait options.client.customers.create({\n\t\t\t\t\t...params,\n\t\t\t\t\temail: user.email,\n\t\t\t\t\tname: user.name,\n\t\t\t\t\texternalId: user.id,\n\t\t\t\t});\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar customer creation failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar customer creation failed. Error: ${e}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage:\n\t\t\t\t\t\t\"Polar customer creation failed. See server logs for more information.\",\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t};\n\nexport const onUserUpdate =\n\t(options: PolarOptions) =>\n\tasync (user: User, ctx?: GenericEndpointContext) => {\n\t\tif (ctx && options.createCustomerOnSignUp) {\n\t\t\ttry {\n\t\t\t\tawait options.client.customers.updateExternal({\n\t\t\t\t\texternalId: user.id,\n\t\t\t\t\tcustomerUpdate: {\n\t\t\t\t\t\temail: user.email,\n\t\t\t\t\t\tname: user.name,\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar customer update failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\tctx.context.logger.error(`Polar customer update failed. Error: ${e}`);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,iBAA4C;AAC5C,qBAAmC;AACnC,iBAAkB;AAGX,IAAM,WAAW,CAAC,gBACxB;AAAA,EACC;AAAA,EACA;AAAA,IACC,QAAQ;AAAA,IACR,OAAO,aAAE,OAAO;AAAA,MACf,WAAW,aAAE,OAAO,EAAE,SAAS;AAAA,IAChC,CAAC;AAAA,EACF;AAAA,EACA,OAAO,QAAQ;AACd,QAAI,CAAC,QAAQ,UAAU,SAAS;AAC/B,YAAM,IAAI,oBAAS,eAAe;AAAA,QACjC,SAAS;AAAA,MACV,CAAC;AAAA,IACF;AAEA,QAAI,YAAY,IAAI,OAAO;AAE3B,QAAI,IAAI,OAAO,MAAM;AACpB,YAAM,WAAW,OAAO,OAAO,QAAQ,SAAS,aAAa,aAC1D,QAAQ,SAAS,SAAS,IAC1B,QAAQ,SAAS;AAEpB,kBAAY,SAAS;AAAA,QACpB,CAAC,YAAY,QAAQ,SAAS,IAAI,OAAO;AAAA,MAC1C,GAAG;AAAA,IACJ;AAEA,QAAI,CAAC,WAAW;AACf,YAAM,IAAI,oBAAS,eAAe;AAAA,QACjC,SAAS;AAAA,MACV,CAAC;AAAA,IACF;AAEA,UAAM,UAAU,UAAM,8BAAkB,GAAG;AAE3C,QAAI;AACH,YAAMA,YAAW,MAAM,QAAQ,OAAO,UAAU,OAAO;AAAA,QACtD,oBAAoB,SAAS,KAAK;AAAA,QAClC;AAAA,QACA,YAAY,QAAQ,SAAS,aAC1B,IAAI,IAAI,QAAQ,SAAS,YAAY,IAAI,SAAS,GAAG,EAAE,SAAS,IAChE;AAAA,MACJ,CAAC;AAED,aAAO,IAAI,SAASA,UAAS,GAA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AA,EACD;AACD;AAEM,IAAM,eACZ,CAAC,YACD,OAAO,MAAY,QAAiC;AACnD,MAAI,OAAO,QAAQ,wBAAwB;AAC1C,QAAI;AACH,YAAM,QAAQ,OAAO,UAAU,eAAe;AAAA,QAC7C,YAAY,KAAK;AAAA,QACjB,gBAAgB;AAAA,UACf,OAAO,KAAK;AAAA,UACZ,MAAM,KAAK;AAAA,QACZ;AAAA,MACD,CAAC;AAAA,IACF,SAAS,GAAY;AACpB,UAAI,aAAa,OAAO;AACvB,YAAI,QAAQ,OAAO;AAAA,UAClB,wCAAwC,EAAE,OAAO;AAAA,QAClD;AAAA,MACD,OAAO;AACN,YAAI,QAAQ,OAAO,MAAM,wCAAwC,CAAC,EAAE;AAAA,MACrE;AAAA,IACD;AAAA,EACD;AACD;;;ALxDM,IAAM,QAAQ,CAAyB,YAAe;AAC5D,SAAO;AAAA,IACN,IAAI;AAAA,IACJ,WAAW;AAAA,MACV,eAAe,SAAS,OAAO;AAAA,MAC/B,eAAe,SAAS,OAAO;AAAA,MAC/B,qBAAqB,eAAe,OAAO;AAAA,MAC3C,oBAAoB,cAAc,OAAO;AAAA,IAC1C;AAAA,IACA,OAAO;AACN,aAAO;AAAA,QACN,SAAS;AAAA,UACR,eAAe;AAAA,YACd,MAAM;AAAA,cACL,QAAQ;AAAA,gBACP,OAAO,aAAa,OAAO;AAAA,cAC5B;AAAA,cACA,QAAQ;AAAA,gBACP,OAAO,aAAa,OAAO;AAAA,cAC5B;AAAA,YACD;AAAA,UACD;AAAA,QACD;AAAA,MACD;AAAA,IACD;AAAA,EACD;AACD;","names":["checkout","import_api","import_plugins","import_api","import_plugins","import_api","import_plugins","webhooks","import_api"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/endpoints/checkout.ts","../src/endpoints/customerPortal.ts","../src/endpoints/customerState.ts","../src/endpoints/webhooks.ts","../src/hooks/customer.ts"],"sourcesContent":["import type { BetterAuthPlugin } from \"better-auth\";\nimport { checkout } from \"./endpoints/checkout\";\nimport { customerPortal } from \"./endpoints/customerPortal\";\nimport { customerState } from \"./endpoints/customerState\";\nimport { webhooks } from \"./endpoints/webhooks\";\nimport { onUserCreate, onUserUpdate } from \"./hooks/customer\";\nimport type { PolarOptions } from \"./types\";\n\nexport const polar = <O extends PolarOptions>(options: O) => {\n\treturn {\n\t\tid: \"polar\",\n\t\tendpoints: {\n\t\t\tpolarCheckout: checkout(options),\n\t\t\tpolarWebhooks: webhooks(options),\n\t\t\tpolarCustomerPortal: customerPortal(options),\n\t\t\tpolarCustomerState: customerState(options),\n\t\t},\n\t\tinit() {\n\t\t\treturn {\n\t\t\t\toptions: {\n\t\t\t\t\tdatabaseHooks: {\n\t\t\t\t\t\tuser: {\n\t\t\t\t\t\t\tcreate: {\n\t\t\t\t\t\t\t\tafter: onUserCreate(options),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\tupdate: {\n\t\t\t\t\t\t\t\tafter: onUserUpdate(options),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t};\n\t\t},\n\t} satisfies BetterAuthPlugin;\n};\n","import { APIError, getSessionFromCtx } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport { z } from \"zod\";\nimport type { PolarOptions } from \"../types\";\n\nexport const checkout = (options: PolarOptions) =>\n\tcreateAuthEndpoint(\n\t\t\"/checkout/:slug\",\n\t\t{\n\t\t\tmethod: \"GET\",\n\t\t\tquery: z.object({\n\t\t\t\tproductId: z.string().optional(),\n\t\t\t}),\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tif (!options.checkout?.enabled) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"Checkout is not enabled\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tlet productId = ctx.query?.productId;\n\n\t\t\tif (ctx.params.slug) {\n\t\t\t\tconst products = await (typeof options.checkout.products === \"function\"\n\t\t\t\t\t? options.checkout.products()\n\t\t\t\t\t: options.checkout.products);\n\n\t\t\t\tproductId = products.find(\n\t\t\t\t\t(product) => product.slug === ctx.params.slug,\n\t\t\t\t)?.productId;\n\t\t\t}\n\n\t\t\tif (!productId) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"Product Id not found\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tconst session = await getSessionFromCtx(ctx);\n\n\t\t\ttry {\n\t\t\t\tconst checkout = await options.client.checkouts.create({\n\t\t\t\t\tcustomerExternalId: session?.user.id,\n\t\t\t\t\tproductId,\n\t\t\t\t\tsuccessUrl: options.checkout.successUrl\n\t\t\t\t\t\t? new URL(options.checkout.successUrl, ctx.request?.url).toString()\n\t\t\t\t\t\t: undefined,\n\t\t\t\t});\n\n\t\t\t\treturn ctx.redirect(checkout.url);\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar checkout creation failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\tmessage: \"Checkout creation failed\",\n\t\t\t\t});\n\t\t\t}\n\t\t},\n\t);\n","import { APIError, sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport type { PolarOptions } from \"../types\";\n\nexport const customerPortal = (options: PolarOptions) =>\n\tcreateAuthEndpoint(\n\t\t\"/portal\",\n\t\t{\n\t\t\tmethod: \"GET\",\n\t\t\tuse: [sessionMiddleware],\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tif (!options.enableCustomerPortal) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"Customer portal is not enabled\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tif (!ctx.context.session?.user.id) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\ttry {\n\t\t\t\tconst customerSession = await options.client.customerSessions.create({\n\t\t\t\t\tcustomerExternalId: ctx.context.session?.user.id,\n\t\t\t\t});\n\n\t\t\t\treturn ctx.redirect(customerSession.customerPortalUrl);\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar customer portal creation failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\tmessage: \"Customer portal creation failed\",\n\t\t\t\t});\n\t\t\t}\n\t\t},\n\t);\n","import { APIError, sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport type { PolarOptions } from \"../types\";\n\nexport const customerState = (options: PolarOptions) =>\n\tcreateAuthEndpoint(\n\t\t\"/state\",\n\t\t{\n\t\t\tmethod: \"GET\",\n\t\t\tuse: [sessionMiddleware],\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\ttry {\n\t\t\t\tconst state = await options.client.customers.getStateExternal({\n\t\t\t\t\texternalId: ctx.context.session?.user.id,\n\t\t\t\t});\n\n\t\t\t\treturn ctx.json(state);\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar subscriptions list failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\tmessage: \"Subscriptions list failed\",\n\t\t\t\t});\n\t\t\t}\n\t\t},\n\t);\n","import { validateEvent } from \"@polar-sh/sdk/webhooks.js\";\nimport { APIError } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport type { PolarOptions } from \"../types\";\n\nexport const webhooks = (options: PolarOptions) =>\n\tcreateAuthEndpoint(\n\t\t\"/polar/webhooks\",\n\t\t{\n\t\t\tmethod: \"POST\",\n\t\t\tmetadata: {\n\t\t\t\tisAction: false,\n\t\t\t},\n\t\t\tcloneRequest: true,\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tconst { webhooks } = options;\n\n\t\t\tif (!webhooks) {\n\t\t\t\tthrow new APIError(\"NOT_FOUND\", {\n\t\t\t\t\tmessage: \"Webhooks not enabled\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tconst {\n\t\t\t\tsecret,\n\t\t\t\tonPayload,\n\t\t\t\tonCheckoutCreated,\n\t\t\t\tonCheckoutUpdated,\n\t\t\t\tonOrderCreated,\n\t\t\t\tonOrderRefunded,\n\t\t\t\tonRefundCreated,\n\t\t\t\tonRefundUpdated,\n\t\t\t\tonSubscriptionCreated,\n\t\t\t\tonSubscriptionUpdated,\n\t\t\t\tonSubscriptionActive,\n\t\t\t\tonSubscriptionCanceled,\n\t\t\t\tonSubscriptionRevoked,\n\t\t\t\tonSubscriptionUncanceled,\n\t\t\t\tonProductCreated,\n\t\t\t\tonProductUpdated,\n\t\t\t\tonOrganizationUpdated,\n\t\t\t\tonBenefitCreated,\n\t\t\t\tonBenefitUpdated,\n\t\t\t\tonBenefitGrantCreated,\n\t\t\t\tonBenefitGrantUpdated,\n\t\t\t\tonBenefitGrantRevoked,\n\t\t\t\tonCustomerCreated,\n\t\t\t\tonCustomerUpdated,\n\t\t\t\tonCustomerDeleted,\n\t\t\t\tonCustomerStateChanged,\n\t\t\t} = webhooks;\n\n\t\t\tif (!ctx.request?.body) {\n\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\");\n\t\t\t}\n\t\t\tconst buf = await ctx.request.text();\n\t\t\tlet event: ReturnType<typeof validateEvent>;\n\t\t\ttry {\n\t\t\t\tif (!secret) {\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: \"Polar webhook secret not found\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\tconst headers = {\n\t\t\t\t\t\"webhook-id\": ctx.request.headers.get(\"webhook-id\") as string,\n\t\t\t\t\t\"webhook-timestamp\": ctx.request.headers.get(\n\t\t\t\t\t\t\"webhook-timestamp\",\n\t\t\t\t\t) as string,\n\t\t\t\t\t\"webhook-signature\": ctx.request.headers.get(\n\t\t\t\t\t\t\"webhook-signature\",\n\t\t\t\t\t) as string,\n\t\t\t\t};\n\n\t\t\t\tevent = validateEvent(buf, headers, secret);\n\t\t\t} catch (err: unknown) {\n\t\t\t\tif (err instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(`${err.message}`);\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: `Webhook Error: ${err.message}`,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: `Webhook Error: ${err}`,\n\t\t\t\t});\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tif (onPayload) {\n\t\t\t\t\tonPayload(event);\n\t\t\t\t}\n\n\t\t\t\tswitch (event.type) {\n\t\t\t\t\tcase \"checkout.created\":\n\t\t\t\t\t\tif (onCheckoutCreated) {\n\t\t\t\t\t\t\tonCheckoutCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"checkout.updated\":\n\t\t\t\t\t\tif (onCheckoutUpdated) {\n\t\t\t\t\t\t\tonCheckoutUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"order.created\":\n\t\t\t\t\t\tif (onOrderCreated) {\n\t\t\t\t\t\t\tonOrderCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.created\":\n\t\t\t\t\t\tif (onSubscriptionCreated) {\n\t\t\t\t\t\t\tonSubscriptionCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.updated\":\n\t\t\t\t\t\tif (onSubscriptionUpdated) {\n\t\t\t\t\t\t\tonSubscriptionUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.active\":\n\t\t\t\t\t\tif (onSubscriptionActive) {\n\t\t\t\t\t\t\tonSubscriptionActive(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.canceled\":\n\t\t\t\t\t\tif (onSubscriptionCanceled) {\n\t\t\t\t\t\t\tonSubscriptionCanceled(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.uncanceled\":\n\t\t\t\t\t\tif (onSubscriptionUncanceled) {\n\t\t\t\t\t\t\tonSubscriptionUncanceled(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.revoked\":\n\t\t\t\t\t\tif (onSubscriptionRevoked) {\n\t\t\t\t\t\t\tonSubscriptionRevoked(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"product.created\":\n\t\t\t\t\t\tif (onProductCreated) {\n\t\t\t\t\t\t\tonProductCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"product.updated\":\n\t\t\t\t\t\tif (onProductUpdated) {\n\t\t\t\t\t\t\tonProductUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"organization.updated\":\n\t\t\t\t\t\tif (onOrganizationUpdated) {\n\t\t\t\t\t\t\tonOrganizationUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"benefit.created\":\n\t\t\t\t\t\tif (onBenefitCreated) {\n\t\t\t\t\t\t\tonBenefitCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"benefit.updated\":\n\t\t\t\t\t\tif (onBenefitUpdated) {\n\t\t\t\t\t\t\tonBenefitUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"benefit_grant.created\":\n\t\t\t\t\t\tif (onBenefitGrantCreated) {\n\t\t\t\t\t\t\tonBenefitGrantCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"benefit_grant.updated\":\n\t\t\t\t\t\tif (onBenefitGrantUpdated) {\n\t\t\t\t\t\t\tonBenefitGrantUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"benefit_grant.revoked\":\n\t\t\t\t\t\tif (onBenefitGrantRevoked) {\n\t\t\t\t\t\t\tonBenefitGrantRevoked(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"order.refunded\":\n\t\t\t\t\t\tif (onOrderRefunded) {\n\t\t\t\t\t\t\tonOrderRefunded(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"refund.created\":\n\t\t\t\t\t\tif (onRefundCreated) {\n\t\t\t\t\t\t\tonRefundCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"refund.updated\":\n\t\t\t\t\t\tif (onRefundUpdated) {\n\t\t\t\t\t\t\tonRefundUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"customer.created\":\n\t\t\t\t\t\tif (onCustomerCreated) {\n\t\t\t\t\t\t\tonCustomerCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"customer.updated\":\n\t\t\t\t\t\tif (onCustomerUpdated) {\n\t\t\t\t\t\t\tonCustomerUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"customer.deleted\":\n\t\t\t\t\t\tif (onCustomerDeleted) {\n\t\t\t\t\t\t\tonCustomerDeleted(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"customer.state_changed\":\n\t\t\t\t\t\tif (onCustomerStateChanged) {\n\t\t\t\t\t\t\tonCustomerStateChanged(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(`Polar webhook failed. Error: ${e.message}`);\n\t\t\t\t} else {\n\t\t\t\t\tctx.context.logger.error(`Polar webhook failed. Error: ${e}`);\n\t\t\t\t}\n\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"Webhook error: See server logs for more information.\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\treturn ctx.json({ received: true });\n\t\t},\n\t);\n","import type { GenericEndpointContext, User } from \"better-auth\";\nimport type { PolarOptions } from \"../types\";\n\nexport const onUserCreate =\n\t(options: PolarOptions) =>\n\tasync (user: User, ctx?: GenericEndpointContext) => {\n\t\tif (ctx && options.createCustomerOnSignUp) {\n\t\t\ttry {\n\t\t\t\tconst params =\n\t\t\t\t\toptions.getCustomerCreateParams && ctx.context.session\n\t\t\t\t\t\t? await options.getCustomerCreateParams({\n\t\t\t\t\t\t\t\tuser,\n\t\t\t\t\t\t\t\tsession: ctx.context.session.session,\n\t\t\t\t\t\t\t})\n\t\t\t\t\t\t: {};\n\n\t\t\t\tconst { result: existingCustomers } =\n\t\t\t\t\tawait options.client.customers.list({ email: user.email });\n\t\t\t\tconst existingCustomer = existingCustomers.items[0];\n\n\t\t\t\tif (existingCustomer) {\n\t\t\t\t\tif (existingCustomer.externalId !== user.id) {\n\t\t\t\t\t\tawait options.client.customers.update({\n\t\t\t\t\t\t\tid: existingCustomer.id,\n\t\t\t\t\t\t\tcustomerUpdate: {\n\t\t\t\t\t\t\t\texternalId: user.id,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tawait options.client.customers.create({\n\t\t\t\t\t\t...params,\n\t\t\t\t\t\temail: user.email,\n\t\t\t\t\t\tname: user.name,\n\t\t\t\t\t\texternalId: user.id,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar customer creation failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar customer creation failed. Error: ${e}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t};\n\nexport const onUserUpdate =\n\t(options: PolarOptions) =>\n\tasync (user: User, ctx?: GenericEndpointContext) => {\n\t\tif (ctx && options.createCustomerOnSignUp) {\n\t\t\ttry {\n\t\t\t\tawait options.client.customers.updateExternal({\n\t\t\t\t\texternalId: user.id,\n\t\t\t\t\tcustomerUpdate: {\n\t\t\t\t\t\temail: user.email,\n\t\t\t\t\t\tname: user.name,\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar customer update failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\tctx.context.logger.error(`Polar customer update failed. Error: ${e}`);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;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ACD,OAAO;AACN,YAAI,QAAQ,OAAO;AAAA,UAClB,0CAA0C,CAAC;AAAA,QAC5C;AAAA,MACD;AAAA,IACD;AAAA,EACD;AACD;AAEM,IAAM,eACZ,CAAC,YACD,OAAO,MAAY,QAAiC;AACnD,MAAI,OAAO,QAAQ,wBAAwB;AAC1C,QAAI;AACH,YAAM,QAAQ,OAAO,UAAU,eAAe;AAAA,QAC7C,YAAY,KAAK;AAAA,QACjB,gBAAgB;AAAA,UACf,OAAO,KAAK;AAAA,UACZ,MAAM,KAAK;AAAA,QACZ;AAAA,MACD,CAAC;AAAA,IACF,SAAS,GAAY;AACpB,UAAI,aAAa,OAAO;AACvB,YAAI,QAAQ,OAAO;AAAA,UAClB,wCAAwC,EAAE,OAAO;AAAA,QAClD;AAAA,MACD,OAAO;AACN,YAAI,QAAQ,OAAO,MAAM,wCAAwC,CAAC,EAAE;AAAA,MACrE;AAAA,IACD;AAAA,EACD;AACD;;;ALjEM,IAAM,QAAQ,CAAyB,YAAe;AAC5D,SAAO;AAAA,IACN,IAAI;AAAA,IACJ,WAAW;AAAA,MACV,eAAe,SAAS,OAAO;AAAA,MAC/B,eAAe,SAAS,OAAO;AAAA,MAC/B,qBAAqB,eAAe,OAAO;AAAA,MAC3C,oBAAoB,cAAc,OAAO;AAAA,IAC1C;AAAA,IACA,OAAO;AACN,aAAO;AAAA,QACN,SAAS;AAAA,UACR,eAAe;AAAA,YACd,MAAM;AAAA,cACL,QAAQ;AAAA,gBACP,OAAO,aAAa,OAAO;AAAA,cAC5B;AAAA,cACA,QAAQ;AAAA,gBACP,OAAO,aAAa,OAAO;AAAA,cAC5B;AAAA,YACD;AAAA,UACD;AAAA,QACD;AAAA,MACD;AAAA,IACD;AAAA,EACD;AACD;","names":["checkout","import_api","import_plugins","import_api","import_plugins","import_api","import_plugins","webhooks"]}
|
package/dist/index.js
CHANGED
|
@@ -341,7 +341,6 @@ var webhooks = (options) => createAuthEndpoint4(
|
|
|
341
341
|
);
|
|
342
342
|
|
|
343
343
|
// src/hooks/customer.ts
|
|
344
|
-
import { APIError as APIError5 } from "better-auth/api";
|
|
345
344
|
var onUserCreate = (options) => async (user, ctx) => {
|
|
346
345
|
if (ctx && options.createCustomerOnSignUp) {
|
|
347
346
|
try {
|
|
@@ -349,12 +348,25 @@ var onUserCreate = (options) => async (user, ctx) => {
|
|
|
349
348
|
user,
|
|
350
349
|
session: ctx.context.session.session
|
|
351
350
|
}) : {};
|
|
352
|
-
await options.client.customers.
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
351
|
+
const { result: existingCustomers } = await options.client.customers.list({ email: user.email });
|
|
352
|
+
const existingCustomer = existingCustomers.items[0];
|
|
353
|
+
if (existingCustomer) {
|
|
354
|
+
if (existingCustomer.externalId !== user.id) {
|
|
355
|
+
await options.client.customers.update({
|
|
356
|
+
id: existingCustomer.id,
|
|
357
|
+
customerUpdate: {
|
|
358
|
+
externalId: user.id
|
|
359
|
+
}
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
} else {
|
|
363
|
+
await options.client.customers.create({
|
|
364
|
+
...params,
|
|
365
|
+
email: user.email,
|
|
366
|
+
name: user.name,
|
|
367
|
+
externalId: user.id
|
|
368
|
+
});
|
|
369
|
+
}
|
|
358
370
|
} catch (e) {
|
|
359
371
|
if (e instanceof Error) {
|
|
360
372
|
ctx.context.logger.error(
|
|
@@ -365,9 +377,6 @@ var onUserCreate = (options) => async (user, ctx) => {
|
|
|
365
377
|
`Polar customer creation failed. Error: ${e}`
|
|
366
378
|
);
|
|
367
379
|
}
|
|
368
|
-
throw new APIError5("BAD_REQUEST", {
|
|
369
|
-
message: "Polar customer creation failed. See server logs for more information."
|
|
370
|
-
});
|
|
371
380
|
}
|
|
372
381
|
}
|
|
373
382
|
};
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/endpoints/checkout.ts","../src/endpoints/customerPortal.ts","../src/endpoints/customerState.ts","../src/endpoints/webhooks.ts","../src/hooks/customer.ts","../src/index.ts"],"sourcesContent":["import { APIError, getSessionFromCtx } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport { z } from \"zod\";\nimport type { PolarOptions } from \"../types\";\n\nexport const checkout = (options: PolarOptions) =>\n\tcreateAuthEndpoint(\n\t\t\"/checkout/:slug\",\n\t\t{\n\t\t\tmethod: \"GET\",\n\t\t\tquery: z.object({\n\t\t\t\tproductId: z.string().optional(),\n\t\t\t}),\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tif (!options.checkout?.enabled) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"Checkout is not enabled\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tlet productId = ctx.query?.productId;\n\n\t\t\tif (ctx.params.slug) {\n\t\t\t\tconst products = await (typeof options.checkout.products === \"function\"\n\t\t\t\t\t? options.checkout.products()\n\t\t\t\t\t: options.checkout.products);\n\n\t\t\t\tproductId = products.find(\n\t\t\t\t\t(product) => product.slug === ctx.params.slug,\n\t\t\t\t)?.productId;\n\t\t\t}\n\n\t\t\tif (!productId) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"Product Id not found\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tconst session = await getSessionFromCtx(ctx);\n\n\t\t\ttry {\n\t\t\t\tconst checkout = await options.client.checkouts.create({\n\t\t\t\t\tcustomerExternalId: session?.user.id,\n\t\t\t\t\tproductId,\n\t\t\t\t\tsuccessUrl: options.checkout.successUrl\n\t\t\t\t\t\t? new URL(options.checkout.successUrl, ctx.request?.url).toString()\n\t\t\t\t\t\t: undefined,\n\t\t\t\t});\n\n\t\t\t\treturn ctx.redirect(checkout.url);\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar checkout creation failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\tmessage: \"Checkout creation failed\",\n\t\t\t\t});\n\t\t\t}\n\t\t},\n\t);\n","import { APIError, sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport type { PolarOptions } from \"../types\";\n\nexport const customerPortal = (options: PolarOptions) =>\n\tcreateAuthEndpoint(\n\t\t\"/portal\",\n\t\t{\n\t\t\tmethod: \"GET\",\n\t\t\tuse: [sessionMiddleware],\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tif (!options.enableCustomerPortal) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"Customer portal is not enabled\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tif (!ctx.context.session?.user.id) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\ttry {\n\t\t\t\tconst customerSession = await options.client.customerSessions.create({\n\t\t\t\t\tcustomerExternalId: ctx.context.session?.user.id,\n\t\t\t\t});\n\n\t\t\t\treturn ctx.redirect(customerSession.customerPortalUrl);\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar customer portal creation failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\tmessage: \"Customer portal creation failed\",\n\t\t\t\t});\n\t\t\t}\n\t\t},\n\t);\n","import { APIError, sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport type { PolarOptions } from \"../types\";\n\nexport const customerState = (options: PolarOptions) =>\n\tcreateAuthEndpoint(\n\t\t\"/state\",\n\t\t{\n\t\t\tmethod: \"GET\",\n\t\t\tuse: [sessionMiddleware],\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\ttry {\n\t\t\t\tconst state = await options.client.customers.getStateExternal({\n\t\t\t\t\texternalId: ctx.context.session?.user.id,\n\t\t\t\t});\n\n\t\t\t\treturn ctx.json(state);\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar subscriptions list failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\tmessage: \"Subscriptions list failed\",\n\t\t\t\t});\n\t\t\t}\n\t\t},\n\t);\n","import { validateEvent } from \"@polar-sh/sdk/webhooks.js\";\nimport { APIError } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport type { PolarOptions } from \"../types\";\n\nexport const webhooks = (options: PolarOptions) =>\n\tcreateAuthEndpoint(\n\t\t\"/polar/webhooks\",\n\t\t{\n\t\t\tmethod: \"POST\",\n\t\t\tmetadata: {\n\t\t\t\tisAction: false,\n\t\t\t},\n\t\t\tcloneRequest: true,\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tconst { webhooks } = options;\n\n\t\t\tif (!webhooks) {\n\t\t\t\tthrow new APIError(\"NOT_FOUND\", {\n\t\t\t\t\tmessage: \"Webhooks not enabled\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tconst {\n\t\t\t\tsecret,\n\t\t\t\tonPayload,\n\t\t\t\tonCheckoutCreated,\n\t\t\t\tonCheckoutUpdated,\n\t\t\t\tonOrderCreated,\n\t\t\t\tonOrderRefunded,\n\t\t\t\tonRefundCreated,\n\t\t\t\tonRefundUpdated,\n\t\t\t\tonSubscriptionCreated,\n\t\t\t\tonSubscriptionUpdated,\n\t\t\t\tonSubscriptionActive,\n\t\t\t\tonSubscriptionCanceled,\n\t\t\t\tonSubscriptionRevoked,\n\t\t\t\tonSubscriptionUncanceled,\n\t\t\t\tonProductCreated,\n\t\t\t\tonProductUpdated,\n\t\t\t\tonOrganizationUpdated,\n\t\t\t\tonBenefitCreated,\n\t\t\t\tonBenefitUpdated,\n\t\t\t\tonBenefitGrantCreated,\n\t\t\t\tonBenefitGrantUpdated,\n\t\t\t\tonBenefitGrantRevoked,\n\t\t\t\tonCustomerCreated,\n\t\t\t\tonCustomerUpdated,\n\t\t\t\tonCustomerDeleted,\n\t\t\t\tonCustomerStateChanged,\n\t\t\t} = webhooks;\n\n\t\t\tif (!ctx.request?.body) {\n\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\");\n\t\t\t}\n\t\t\tconst buf = await ctx.request.text();\n\t\t\tlet event: ReturnType<typeof validateEvent>;\n\t\t\ttry {\n\t\t\t\tif (!secret) {\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: \"Polar webhook secret not found\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\tconst headers = {\n\t\t\t\t\t\"webhook-id\": ctx.request.headers.get(\"webhook-id\") as string,\n\t\t\t\t\t\"webhook-timestamp\": ctx.request.headers.get(\n\t\t\t\t\t\t\"webhook-timestamp\",\n\t\t\t\t\t) as string,\n\t\t\t\t\t\"webhook-signature\": ctx.request.headers.get(\n\t\t\t\t\t\t\"webhook-signature\",\n\t\t\t\t\t) as string,\n\t\t\t\t};\n\n\t\t\t\tevent = validateEvent(buf, headers, secret);\n\t\t\t} catch (err: unknown) {\n\t\t\t\tif (err instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(`${err.message}`);\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: `Webhook Error: ${err.message}`,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: `Webhook Error: ${err}`,\n\t\t\t\t});\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tif (onPayload) {\n\t\t\t\t\tonPayload(event);\n\t\t\t\t}\n\n\t\t\t\tswitch (event.type) {\n\t\t\t\t\tcase \"checkout.created\":\n\t\t\t\t\t\tif (onCheckoutCreated) {\n\t\t\t\t\t\t\tonCheckoutCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"checkout.updated\":\n\t\t\t\t\t\tif (onCheckoutUpdated) {\n\t\t\t\t\t\t\tonCheckoutUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"order.created\":\n\t\t\t\t\t\tif (onOrderCreated) {\n\t\t\t\t\t\t\tonOrderCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.created\":\n\t\t\t\t\t\tif (onSubscriptionCreated) {\n\t\t\t\t\t\t\tonSubscriptionCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.updated\":\n\t\t\t\t\t\tif (onSubscriptionUpdated) {\n\t\t\t\t\t\t\tonSubscriptionUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.active\":\n\t\t\t\t\t\tif (onSubscriptionActive) {\n\t\t\t\t\t\t\tonSubscriptionActive(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.canceled\":\n\t\t\t\t\t\tif (onSubscriptionCanceled) {\n\t\t\t\t\t\t\tonSubscriptionCanceled(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.uncanceled\":\n\t\t\t\t\t\tif (onSubscriptionUncanceled) {\n\t\t\t\t\t\t\tonSubscriptionUncanceled(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.revoked\":\n\t\t\t\t\t\tif (onSubscriptionRevoked) {\n\t\t\t\t\t\t\tonSubscriptionRevoked(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"product.created\":\n\t\t\t\t\t\tif (onProductCreated) {\n\t\t\t\t\t\t\tonProductCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"product.updated\":\n\t\t\t\t\t\tif (onProductUpdated) {\n\t\t\t\t\t\t\tonProductUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"organization.updated\":\n\t\t\t\t\t\tif (onOrganizationUpdated) {\n\t\t\t\t\t\t\tonOrganizationUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"benefit.created\":\n\t\t\t\t\t\tif (onBenefitCreated) {\n\t\t\t\t\t\t\tonBenefitCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"benefit.updated\":\n\t\t\t\t\t\tif (onBenefitUpdated) {\n\t\t\t\t\t\t\tonBenefitUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"benefit_grant.created\":\n\t\t\t\t\t\tif (onBenefitGrantCreated) {\n\t\t\t\t\t\t\tonBenefitGrantCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"benefit_grant.updated\":\n\t\t\t\t\t\tif (onBenefitGrantUpdated) {\n\t\t\t\t\t\t\tonBenefitGrantUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"benefit_grant.revoked\":\n\t\t\t\t\t\tif (onBenefitGrantRevoked) {\n\t\t\t\t\t\t\tonBenefitGrantRevoked(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"order.refunded\":\n\t\t\t\t\t\tif (onOrderRefunded) {\n\t\t\t\t\t\t\tonOrderRefunded(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"refund.created\":\n\t\t\t\t\t\tif (onRefundCreated) {\n\t\t\t\t\t\t\tonRefundCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"refund.updated\":\n\t\t\t\t\t\tif (onRefundUpdated) {\n\t\t\t\t\t\t\tonRefundUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"customer.created\":\n\t\t\t\t\t\tif (onCustomerCreated) {\n\t\t\t\t\t\t\tonCustomerCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"customer.updated\":\n\t\t\t\t\t\tif (onCustomerUpdated) {\n\t\t\t\t\t\t\tonCustomerUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"customer.deleted\":\n\t\t\t\t\t\tif (onCustomerDeleted) {\n\t\t\t\t\t\t\tonCustomerDeleted(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"customer.state_changed\":\n\t\t\t\t\t\tif (onCustomerStateChanged) {\n\t\t\t\t\t\t\tonCustomerStateChanged(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(`Polar webhook failed. Error: ${e.message}`);\n\t\t\t\t} else {\n\t\t\t\t\tctx.context.logger.error(`Polar webhook failed. Error: ${e}`);\n\t\t\t\t}\n\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"Webhook error: See server logs for more information.\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\treturn ctx.json({ received: true });\n\t\t},\n\t);\n","import type { GenericEndpointContext, User } from \"better-auth\";\nimport { APIError } from \"better-auth/api\";\nimport type { PolarOptions } from \"../types\";\n\nexport const onUserCreate =\n\t(options: PolarOptions) =>\n\tasync (user: User, ctx?: GenericEndpointContext) => {\n\t\tif (ctx && options.createCustomerOnSignUp) {\n\t\t\ttry {\n\t\t\t\tconst params =\n\t\t\t\t\toptions.getCustomerCreateParams && ctx.context.session\n\t\t\t\t\t\t? await options.getCustomerCreateParams({\n\t\t\t\t\t\t\t\tuser,\n\t\t\t\t\t\t\t\tsession: ctx.context.session.session,\n\t\t\t\t\t\t\t})\n\t\t\t\t\t\t: {};\n\n\t\t\t\tawait options.client.customers.create({\n\t\t\t\t\t...params,\n\t\t\t\t\temail: user.email,\n\t\t\t\t\tname: user.name,\n\t\t\t\t\texternalId: user.id,\n\t\t\t\t});\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar customer creation failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar customer creation failed. Error: ${e}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage:\n\t\t\t\t\t\t\"Polar customer creation failed. See server logs for more information.\",\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t};\n\nexport const onUserUpdate =\n\t(options: PolarOptions) =>\n\tasync (user: User, ctx?: GenericEndpointContext) => {\n\t\tif (ctx && options.createCustomerOnSignUp) {\n\t\t\ttry {\n\t\t\t\tawait options.client.customers.updateExternal({\n\t\t\t\t\texternalId: user.id,\n\t\t\t\t\tcustomerUpdate: {\n\t\t\t\t\t\temail: user.email,\n\t\t\t\t\t\tname: user.name,\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar customer update failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\tctx.context.logger.error(`Polar customer update failed. Error: ${e}`);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t};\n","import type { BetterAuthPlugin } from \"better-auth\";\nimport { checkout } from \"./endpoints/checkout\";\nimport { customerPortal } from \"./endpoints/customerPortal\";\nimport { customerState } from \"./endpoints/customerState\";\nimport { webhooks } from \"./endpoints/webhooks\";\nimport { onUserCreate, onUserUpdate } from \"./hooks/customer\";\nimport type { PolarOptions } from \"./types\";\n\nexport const polar = <O extends PolarOptions>(options: O) => {\n\treturn {\n\t\tid: \"polar\",\n\t\tendpoints: {\n\t\t\tpolarCheckout: checkout(options),\n\t\t\tpolarWebhooks: webhooks(options),\n\t\t\tpolarCustomerPortal: customerPortal(options),\n\t\t\tpolarCustomerState: customerState(options),\n\t\t},\n\t\tinit() {\n\t\t\treturn {\n\t\t\t\toptions: {\n\t\t\t\t\tdatabaseHooks: {\n\t\t\t\t\t\tuser: {\n\t\t\t\t\t\t\tcreate: {\n\t\t\t\t\t\t\t\tafter: onUserCreate(options),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\tupdate: {\n\t\t\t\t\t\t\t\tafter: onUserUpdate(options),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t};\n\t\t},\n\t} satisfies BetterAuthPlugin;\n};\n"],"mappings":";AAAA,SAAS,UAAU,yBAAyB;AAC5C,SAAS,0BAA0B;AACnC,SAAS,SAAS;AAGX,IAAM,WAAW,CAAC,YACxB;AAAA,EACC;AAAA,EACA;AAAA,IACC,QAAQ;AAAA,IACR,OAAO,E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AY;AACpB,UAAI,aAAa,OAAO;AACvB,YAAI,QAAQ,OAAO;AAAA,UAClB,wCAAwC,EAAE,OAAO;AAAA,QAClD;AAAA,MACD,OAAO;AACN,YAAI,QAAQ,OAAO,MAAM,wCAAwC,CAAC,EAAE;AAAA,MACrE;AAAA,IACD;AAAA,EACD;AACD;;;ACxDM,IAAM,QAAQ,CAAyB,YAAe;AAC5D,SAAO;AAAA,IACN,IAAI;AAAA,IACJ,WAAW;AAAA,MACV,eAAe,SAAS,OAAO;AAAA,MAC/B,eAAe,SAAS,OAAO;AAAA,MAC/B,qBAAqB,eAAe,OAAO;AAAA,MAC3C,oBAAoB,cAAc,OAAO;AAAA,IAC1C;AAAA,IACA,OAAO;AACN,aAAO;AAAA,QACN,SAAS;AAAA,UACR,eAAe;AAAA,YACd,MAAM;AAAA,cACL,QAAQ;AAAA,gBACP,OAAO,aAAa,OAAO;AAAA,cAC5B;AAAA,cACA,QAAQ;AAAA,gBACP,OAAO,aAAa,OAAO;AAAA,cAC5B;AAAA,YACD;AAAA,UACD;AAAA,QACD;AAAA,MACD;AAAA,IACD;AAAA,EACD;AACD;","names":["checkout","APIError","createAuthEndpoint","APIError","sessionMiddleware","createAuthEndpoint","APIError","createAuthEndpoint","webhooks","APIError"]}
|
|
1
|
+
{"version":3,"sources":["../src/endpoints/checkout.ts","../src/endpoints/customerPortal.ts","../src/endpoints/customerState.ts","../src/endpoints/webhooks.ts","../src/hooks/customer.ts","../src/index.ts"],"sourcesContent":["import { APIError, getSessionFromCtx } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport { z } from \"zod\";\nimport type { PolarOptions } from \"../types\";\n\nexport const checkout = (options: PolarOptions) =>\n\tcreateAuthEndpoint(\n\t\t\"/checkout/:slug\",\n\t\t{\n\t\t\tmethod: \"GET\",\n\t\t\tquery: z.object({\n\t\t\t\tproductId: z.string().optional(),\n\t\t\t}),\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tif (!options.checkout?.enabled) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"Checkout is not enabled\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tlet productId = ctx.query?.productId;\n\n\t\t\tif (ctx.params.slug) {\n\t\t\t\tconst products = await (typeof options.checkout.products === \"function\"\n\t\t\t\t\t? options.checkout.products()\n\t\t\t\t\t: options.checkout.products);\n\n\t\t\t\tproductId = products.find(\n\t\t\t\t\t(product) => product.slug === ctx.params.slug,\n\t\t\t\t)?.productId;\n\t\t\t}\n\n\t\t\tif (!productId) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"Product Id not found\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tconst session = await getSessionFromCtx(ctx);\n\n\t\t\ttry {\n\t\t\t\tconst checkout = await options.client.checkouts.create({\n\t\t\t\t\tcustomerExternalId: session?.user.id,\n\t\t\t\t\tproductId,\n\t\t\t\t\tsuccessUrl: options.checkout.successUrl\n\t\t\t\t\t\t? new URL(options.checkout.successUrl, ctx.request?.url).toString()\n\t\t\t\t\t\t: undefined,\n\t\t\t\t});\n\n\t\t\t\treturn ctx.redirect(checkout.url);\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar checkout creation failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\tmessage: \"Checkout creation failed\",\n\t\t\t\t});\n\t\t\t}\n\t\t},\n\t);\n","import { APIError, sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport type { PolarOptions } from \"../types\";\n\nexport const customerPortal = (options: PolarOptions) =>\n\tcreateAuthEndpoint(\n\t\t\"/portal\",\n\t\t{\n\t\t\tmethod: \"GET\",\n\t\t\tuse: [sessionMiddleware],\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tif (!options.enableCustomerPortal) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"Customer portal is not enabled\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tif (!ctx.context.session?.user.id) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\ttry {\n\t\t\t\tconst customerSession = await options.client.customerSessions.create({\n\t\t\t\t\tcustomerExternalId: ctx.context.session?.user.id,\n\t\t\t\t});\n\n\t\t\t\treturn ctx.redirect(customerSession.customerPortalUrl);\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar customer portal creation failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\tmessage: \"Customer portal creation failed\",\n\t\t\t\t});\n\t\t\t}\n\t\t},\n\t);\n","import { APIError, sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport type { PolarOptions } from \"../types\";\n\nexport const customerState = (options: PolarOptions) =>\n\tcreateAuthEndpoint(\n\t\t\"/state\",\n\t\t{\n\t\t\tmethod: \"GET\",\n\t\t\tuse: [sessionMiddleware],\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\ttry {\n\t\t\t\tconst state = await options.client.customers.getStateExternal({\n\t\t\t\t\texternalId: ctx.context.session?.user.id,\n\t\t\t\t});\n\n\t\t\t\treturn ctx.json(state);\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar subscriptions list failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\tmessage: \"Subscriptions list failed\",\n\t\t\t\t});\n\t\t\t}\n\t\t},\n\t);\n","import { validateEvent } from \"@polar-sh/sdk/webhooks.js\";\nimport { APIError } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport type { PolarOptions } from \"../types\";\n\nexport const webhooks = (options: PolarOptions) =>\n\tcreateAuthEndpoint(\n\t\t\"/polar/webhooks\",\n\t\t{\n\t\t\tmethod: \"POST\",\n\t\t\tmetadata: {\n\t\t\t\tisAction: false,\n\t\t\t},\n\t\t\tcloneRequest: true,\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tconst { webhooks } = options;\n\n\t\t\tif (!webhooks) {\n\t\t\t\tthrow new APIError(\"NOT_FOUND\", {\n\t\t\t\t\tmessage: \"Webhooks not enabled\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tconst {\n\t\t\t\tsecret,\n\t\t\t\tonPayload,\n\t\t\t\tonCheckoutCreated,\n\t\t\t\tonCheckoutUpdated,\n\t\t\t\tonOrderCreated,\n\t\t\t\tonOrderRefunded,\n\t\t\t\tonRefundCreated,\n\t\t\t\tonRefundUpdated,\n\t\t\t\tonSubscriptionCreated,\n\t\t\t\tonSubscriptionUpdated,\n\t\t\t\tonSubscriptionActive,\n\t\t\t\tonSubscriptionCanceled,\n\t\t\t\tonSubscriptionRevoked,\n\t\t\t\tonSubscriptionUncanceled,\n\t\t\t\tonProductCreated,\n\t\t\t\tonProductUpdated,\n\t\t\t\tonOrganizationUpdated,\n\t\t\t\tonBenefitCreated,\n\t\t\t\tonBenefitUpdated,\n\t\t\t\tonBenefitGrantCreated,\n\t\t\t\tonBenefitGrantUpdated,\n\t\t\t\tonBenefitGrantRevoked,\n\t\t\t\tonCustomerCreated,\n\t\t\t\tonCustomerUpdated,\n\t\t\t\tonCustomerDeleted,\n\t\t\t\tonCustomerStateChanged,\n\t\t\t} = webhooks;\n\n\t\t\tif (!ctx.request?.body) {\n\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\");\n\t\t\t}\n\t\t\tconst buf = await ctx.request.text();\n\t\t\tlet event: ReturnType<typeof validateEvent>;\n\t\t\ttry {\n\t\t\t\tif (!secret) {\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: \"Polar webhook secret not found\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\tconst headers = {\n\t\t\t\t\t\"webhook-id\": ctx.request.headers.get(\"webhook-id\") as string,\n\t\t\t\t\t\"webhook-timestamp\": ctx.request.headers.get(\n\t\t\t\t\t\t\"webhook-timestamp\",\n\t\t\t\t\t) as string,\n\t\t\t\t\t\"webhook-signature\": ctx.request.headers.get(\n\t\t\t\t\t\t\"webhook-signature\",\n\t\t\t\t\t) as string,\n\t\t\t\t};\n\n\t\t\t\tevent = validateEvent(buf, headers, secret);\n\t\t\t} catch (err: unknown) {\n\t\t\t\tif (err instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(`${err.message}`);\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: `Webhook Error: ${err.message}`,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: `Webhook Error: ${err}`,\n\t\t\t\t});\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tif (onPayload) {\n\t\t\t\t\tonPayload(event);\n\t\t\t\t}\n\n\t\t\t\tswitch (event.type) {\n\t\t\t\t\tcase \"checkout.created\":\n\t\t\t\t\t\tif (onCheckoutCreated) {\n\t\t\t\t\t\t\tonCheckoutCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"checkout.updated\":\n\t\t\t\t\t\tif (onCheckoutUpdated) {\n\t\t\t\t\t\t\tonCheckoutUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"order.created\":\n\t\t\t\t\t\tif (onOrderCreated) {\n\t\t\t\t\t\t\tonOrderCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.created\":\n\t\t\t\t\t\tif (onSubscriptionCreated) {\n\t\t\t\t\t\t\tonSubscriptionCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.updated\":\n\t\t\t\t\t\tif (onSubscriptionUpdated) {\n\t\t\t\t\t\t\tonSubscriptionUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.active\":\n\t\t\t\t\t\tif (onSubscriptionActive) {\n\t\t\t\t\t\t\tonSubscriptionActive(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.canceled\":\n\t\t\t\t\t\tif (onSubscriptionCanceled) {\n\t\t\t\t\t\t\tonSubscriptionCanceled(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.uncanceled\":\n\t\t\t\t\t\tif (onSubscriptionUncanceled) {\n\t\t\t\t\t\t\tonSubscriptionUncanceled(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"subscription.revoked\":\n\t\t\t\t\t\tif (onSubscriptionRevoked) {\n\t\t\t\t\t\t\tonSubscriptionRevoked(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"product.created\":\n\t\t\t\t\t\tif (onProductCreated) {\n\t\t\t\t\t\t\tonProductCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"product.updated\":\n\t\t\t\t\t\tif (onProductUpdated) {\n\t\t\t\t\t\t\tonProductUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"organization.updated\":\n\t\t\t\t\t\tif (onOrganizationUpdated) {\n\t\t\t\t\t\t\tonOrganizationUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"benefit.created\":\n\t\t\t\t\t\tif (onBenefitCreated) {\n\t\t\t\t\t\t\tonBenefitCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"benefit.updated\":\n\t\t\t\t\t\tif (onBenefitUpdated) {\n\t\t\t\t\t\t\tonBenefitUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"benefit_grant.created\":\n\t\t\t\t\t\tif (onBenefitGrantCreated) {\n\t\t\t\t\t\t\tonBenefitGrantCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"benefit_grant.updated\":\n\t\t\t\t\t\tif (onBenefitGrantUpdated) {\n\t\t\t\t\t\t\tonBenefitGrantUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"benefit_grant.revoked\":\n\t\t\t\t\t\tif (onBenefitGrantRevoked) {\n\t\t\t\t\t\t\tonBenefitGrantRevoked(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"order.refunded\":\n\t\t\t\t\t\tif (onOrderRefunded) {\n\t\t\t\t\t\t\tonOrderRefunded(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"refund.created\":\n\t\t\t\t\t\tif (onRefundCreated) {\n\t\t\t\t\t\t\tonRefundCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"refund.updated\":\n\t\t\t\t\t\tif (onRefundUpdated) {\n\t\t\t\t\t\t\tonRefundUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"customer.created\":\n\t\t\t\t\t\tif (onCustomerCreated) {\n\t\t\t\t\t\t\tonCustomerCreated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"customer.updated\":\n\t\t\t\t\t\tif (onCustomerUpdated) {\n\t\t\t\t\t\t\tonCustomerUpdated(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"customer.deleted\":\n\t\t\t\t\t\tif (onCustomerDeleted) {\n\t\t\t\t\t\t\tonCustomerDeleted(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"customer.state_changed\":\n\t\t\t\t\t\tif (onCustomerStateChanged) {\n\t\t\t\t\t\t\tonCustomerStateChanged(event);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(`Polar webhook failed. Error: ${e.message}`);\n\t\t\t\t} else {\n\t\t\t\t\tctx.context.logger.error(`Polar webhook failed. Error: ${e}`);\n\t\t\t\t}\n\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"Webhook error: See server logs for more information.\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\treturn ctx.json({ received: true });\n\t\t},\n\t);\n","import type { GenericEndpointContext, User } from \"better-auth\";\nimport type { PolarOptions } from \"../types\";\n\nexport const onUserCreate =\n\t(options: PolarOptions) =>\n\tasync (user: User, ctx?: GenericEndpointContext) => {\n\t\tif (ctx && options.createCustomerOnSignUp) {\n\t\t\ttry {\n\t\t\t\tconst params =\n\t\t\t\t\toptions.getCustomerCreateParams && ctx.context.session\n\t\t\t\t\t\t? await options.getCustomerCreateParams({\n\t\t\t\t\t\t\t\tuser,\n\t\t\t\t\t\t\t\tsession: ctx.context.session.session,\n\t\t\t\t\t\t\t})\n\t\t\t\t\t\t: {};\n\n\t\t\t\tconst { result: existingCustomers } =\n\t\t\t\t\tawait options.client.customers.list({ email: user.email });\n\t\t\t\tconst existingCustomer = existingCustomers.items[0];\n\n\t\t\t\tif (existingCustomer) {\n\t\t\t\t\tif (existingCustomer.externalId !== user.id) {\n\t\t\t\t\t\tawait options.client.customers.update({\n\t\t\t\t\t\t\tid: existingCustomer.id,\n\t\t\t\t\t\t\tcustomerUpdate: {\n\t\t\t\t\t\t\t\texternalId: user.id,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tawait options.client.customers.create({\n\t\t\t\t\t\t...params,\n\t\t\t\t\t\temail: user.email,\n\t\t\t\t\t\tname: user.name,\n\t\t\t\t\t\texternalId: user.id,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar customer creation failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar customer creation failed. Error: ${e}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t};\n\nexport const onUserUpdate =\n\t(options: PolarOptions) =>\n\tasync (user: User, ctx?: GenericEndpointContext) => {\n\t\tif (ctx && options.createCustomerOnSignUp) {\n\t\t\ttry {\n\t\t\t\tawait options.client.customers.updateExternal({\n\t\t\t\t\texternalId: user.id,\n\t\t\t\t\tcustomerUpdate: {\n\t\t\t\t\t\temail: user.email,\n\t\t\t\t\t\tname: user.name,\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t`Polar customer update failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\tctx.context.logger.error(`Polar customer update failed. Error: ${e}`);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t};\n","import type { BetterAuthPlugin } from \"better-auth\";\nimport { checkout } from \"./endpoints/checkout\";\nimport { customerPortal } from \"./endpoints/customerPortal\";\nimport { customerState } from \"./endpoints/customerState\";\nimport { webhooks } from \"./endpoints/webhooks\";\nimport { onUserCreate, onUserUpdate } from \"./hooks/customer\";\nimport type { PolarOptions } from \"./types\";\n\nexport const polar = <O extends PolarOptions>(options: O) => {\n\treturn {\n\t\tid: \"polar\",\n\t\tendpoints: {\n\t\t\tpolarCheckout: checkout(options),\n\t\t\tpolarWebhooks: webhooks(options),\n\t\t\tpolarCustomerPortal: customerPortal(options),\n\t\t\tpolarCustomerState: customerState(options),\n\t\t},\n\t\tinit() {\n\t\t\treturn {\n\t\t\t\toptions: {\n\t\t\t\t\tdatabaseHooks: {\n\t\t\t\t\t\tuser: {\n\t\t\t\t\t\t\tcreate: {\n\t\t\t\t\t\t\t\tafter: onUserCreate(options),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\tupdate: {\n\t\t\t\t\t\t\t\tafter: onUserUpdate(options),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t};\n\t\t},\n\t} satisfies BetterAuthPlugin;\n};\n"],"mappings":";AAAA,SAAS,UAAU,yBAAyB;AAC5C,SAAS,0BAA0B;AACnC,SAAS,SAAS;AAGX,IAAM,WAAW,CAAC,YACxB;AAAA,EACC;AAAA,EACA;AAAA,IACC,QAAQ;AAAA,IACR,OAAO,EAAE,OAAO;AAAA,MACf,WAAW,EAAE,OAAO,EAAE,SAAS;AAAA,IAChC,CAAC;AAAA,EACF;AAAA,EACA,OAAO,QAAQ;AACd,QAAI,CAAC,QAAQ,UAAU,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U,eAAe;AAAA,QAC7C,YAAY,KAAK;AAAA,QACjB,gBAAgB;AAAA,UACf,OAAO,KAAK;AAAA,UACZ,MAAM,KAAK;AAAA,QACZ;AAAA,MACD,CAAC;AAAA,IACF,SAAS,GAAY;AACpB,UAAI,aAAa,OAAO;AACvB,YAAI,QAAQ,OAAO;AAAA,UAClB,wCAAwC,EAAE,OAAO;AAAA,QAClD;AAAA,MACD,OAAO;AACN,YAAI,QAAQ,OAAO,MAAM,wCAAwC,CAAC,EAAE;AAAA,MACrE;AAAA,IACD;AAAA,EACD;AACD;;;ACjEM,IAAM,QAAQ,CAAyB,YAAe;AAC5D,SAAO;AAAA,IACN,IAAI;AAAA,IACJ,WAAW;AAAA,MACV,eAAe,SAAS,OAAO;AAAA,MAC/B,eAAe,SAAS,OAAO;AAAA,MAC/B,qBAAqB,eAAe,OAAO;AAAA,MAC3C,oBAAoB,cAAc,OAAO;AAAA,IAC1C;AAAA,IACA,OAAO;AACN,aAAO;AAAA,QACN,SAAS;AAAA,UACR,eAAe;AAAA,YACd,MAAM;AAAA,cACL,QAAQ;AAAA,gBACP,OAAO,aAAa,OAAO;AAAA,cAC5B;AAAA,cACA,QAAQ;AAAA,gBACP,OAAO,aAAa,OAAO;AAAA,cAC5B;AAAA,YACD;AAAA,UACD;AAAA,QACD;AAAA,MACD;AAAA,IACD;AAAA,EACD;AACD;","names":["checkout","APIError","createAuthEndpoint","APIError","sessionMiddleware","createAuthEndpoint","APIError","createAuthEndpoint","webhooks"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@polar-sh/better-auth",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.7",
|
|
4
4
|
"description": "Polar integration for better-auth",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
],
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@biomejs/biome": "1.9.4",
|
|
33
|
-
"@polar-sh/sdk": "^0.29.
|
|
33
|
+
"@polar-sh/sdk": "^0.29.3",
|
|
34
34
|
"@sindresorhus/tsconfig": "^7.0.0",
|
|
35
35
|
"@types/node": "^20.0.0",
|
|
36
36
|
"better-auth": "^1.2.0",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"zod": "^3.24.2"
|
|
42
42
|
},
|
|
43
43
|
"peerDependencies": {
|
|
44
|
-
"@polar-sh/sdk": "^0.29.
|
|
44
|
+
"@polar-sh/sdk": "^0.29.3",
|
|
45
45
|
"better-auth": "^1.2.0"
|
|
46
46
|
},
|
|
47
47
|
"scripts": {
|