@polar-sh/better-auth 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -100,34 +100,40 @@ export const authClient = createAuthClient({
100
100
 
101
101
  ```typescript
102
102
  import { betterAuth } from "better-auth";
103
- import { polar, checkout, portal, usage, webhooks } from "@polar-sh/better-auth";
103
+ import {
104
+ polar,
105
+ checkout,
106
+ portal,
107
+ usage,
108
+ webhooks,
109
+ } from "@polar-sh/better-auth";
104
110
  import { Polar } from "@polar-sh/sdk";
105
111
 
106
112
  const polarClient = new Polar({
107
- accessToken: process.env.POLAR_ACCESS_TOKEN,
108
- // Use 'sandbox' if you're using the Polar Sandbox environment
109
- // Remember that access tokens, products, etc. are completely separated between environments.
110
- // Access tokens obtained in Production are for instance not usable in the Sandbox environment.
111
- server: 'sandbox'
113
+ accessToken: process.env.POLAR_ACCESS_TOKEN,
114
+ // Use 'sandbox' if you're using the Polar Sandbox environment
115
+ // Remember that access tokens, products, etc. are completely separated between environments.
116
+ // Access tokens obtained in Production are for instance not usable in the Sandbox environment.
117
+ server: "sandbox",
112
118
  });
113
119
 
114
120
  const auth = betterAuth({
115
- // ... Better Auth config
116
- plugins: [
117
- polar({
118
- client: polarClient,
119
- createCustomerOnSignUp: true,
120
- getCustomerCreateParams?: ({ user }, request) => ({
121
- metadata: {
122
- myCustomProperty: 123
123
- }
124
- }),
125
- use: [
126
- // This is where you add Polar plugins
127
- ]
128
- })
129
- ]
130
- })
121
+ // ... Better Auth config
122
+ plugins: [
123
+ polar({
124
+ client: polarClient,
125
+ createCustomerOnSignUp: true,
126
+ getCustomerCreateParams: ({ user }, request) => ({
127
+ metadata: {
128
+ myCustomProperty: 123,
129
+ },
130
+ }),
131
+ use: [
132
+ // This is where you add Polar plugins
133
+ ],
134
+ }),
135
+ ],
136
+ });
131
137
  ```
132
138
 
133
139
  ### Required Options
@@ -164,7 +170,9 @@ const auth = betterAuth({
164
170
  // Relative URL to return to when checkout is successfully completed
165
171
  successUrl: "/success?checkout_id={CHECKOUT_ID}",
166
172
  // Wheather you want to allow unauthenticated checkout sessions or not
167
- authenticatedUsersOnly: true
173
+ authenticatedUsersOnly: true,
174
+ // Enforces the theme - System-preferred theme will be set if left omitted
175
+ theme: "dark"
168
176
  })
169
177
  ],
170
178
  })
@@ -313,7 +321,7 @@ So in order to figure out if a user should have access, pass the user's organiza
313
321
  ```typescript
314
322
  const organizationId = (await authClient.organization.list())?.data?.[0]?.id,
315
323
 
316
- const { data: subscriptions } = await authClient.customer.orders.list({
324
+ const { data: subscriptions } = await authClient.customer.subscriptions.list({
317
325
  query: {
318
326
  page: 1,
319
327
  limit: 10,
package/dist/index.cjs CHANGED
@@ -379,8 +379,12 @@ var checkout = (checkoutOptions = {}) => (polar2) => {
379
379
  } : ctx.body.metadata,
380
380
  customFieldData: ctx.body.customFieldData
381
381
  });
382
+ const redirectUrl = new URL(checkout2.url);
383
+ if (checkoutOptions.theme) {
384
+ redirectUrl.searchParams.set("theme", checkoutOptions.theme);
385
+ }
382
386
  return ctx.json({
383
- url: checkout2.url,
387
+ url: redirectUrl.toString(),
384
388
  redirect: true
385
389
  });
386
390
  } catch (e) {
@@ -488,6 +492,136 @@ var usage = (_usageOptions) => (polar2) => {
488
492
  };
489
493
  };
490
494
 
495
+ // ../adapter-utils/dist/index.js
496
+ var handleWebhookPayload = async (payload, { webhookSecret, entitlements, onPayload, ...eventHandlers }) => {
497
+ const promises = [];
498
+ if (onPayload) {
499
+ promises.push(onPayload(payload));
500
+ }
501
+ switch (payload.type) {
502
+ case "checkout.created":
503
+ if (eventHandlers.onCheckoutCreated) {
504
+ promises.push(eventHandlers.onCheckoutCreated(payload));
505
+ }
506
+ break;
507
+ case "checkout.updated":
508
+ if (eventHandlers.onCheckoutUpdated) {
509
+ promises.push(eventHandlers.onCheckoutUpdated(payload));
510
+ }
511
+ break;
512
+ case "order.created":
513
+ if (eventHandlers.onOrderCreated) {
514
+ promises.push(eventHandlers.onOrderCreated(payload));
515
+ }
516
+ break;
517
+ case "order.updated":
518
+ if (eventHandlers.onOrderUpdated) {
519
+ promises.push(eventHandlers.onOrderUpdated(payload));
520
+ }
521
+ break;
522
+ case "order.paid":
523
+ if (eventHandlers.onOrderPaid) {
524
+ promises.push(eventHandlers.onOrderPaid(payload));
525
+ }
526
+ break;
527
+ case "subscription.created":
528
+ if (eventHandlers.onSubscriptionCreated) {
529
+ promises.push(eventHandlers.onSubscriptionCreated(payload));
530
+ }
531
+ break;
532
+ case "subscription.updated":
533
+ if (eventHandlers.onSubscriptionUpdated) {
534
+ promises.push(eventHandlers.onSubscriptionUpdated(payload));
535
+ }
536
+ break;
537
+ case "subscription.active":
538
+ if (eventHandlers.onSubscriptionActive) {
539
+ promises.push(eventHandlers.onSubscriptionActive(payload));
540
+ }
541
+ break;
542
+ case "subscription.canceled":
543
+ if (eventHandlers.onSubscriptionCanceled) {
544
+ promises.push(eventHandlers.onSubscriptionCanceled(payload));
545
+ }
546
+ break;
547
+ case "subscription.revoked":
548
+ if (eventHandlers.onSubscriptionRevoked) {
549
+ promises.push(eventHandlers.onSubscriptionRevoked(payload));
550
+ }
551
+ break;
552
+ case "product.created":
553
+ if (eventHandlers.onProductCreated) {
554
+ promises.push(eventHandlers.onProductCreated(payload));
555
+ }
556
+ break;
557
+ case "product.updated":
558
+ if (eventHandlers.onProductUpdated) {
559
+ promises.push(eventHandlers.onProductUpdated(payload));
560
+ }
561
+ break;
562
+ case "organization.updated":
563
+ if (eventHandlers.onOrganizationUpdated) {
564
+ promises.push(eventHandlers.onOrganizationUpdated(payload));
565
+ }
566
+ break;
567
+ case "benefit.created":
568
+ if (eventHandlers.onBenefitCreated) {
569
+ promises.push(eventHandlers.onBenefitCreated(payload));
570
+ }
571
+ break;
572
+ case "benefit.updated":
573
+ if (eventHandlers.onBenefitUpdated) {
574
+ promises.push(eventHandlers.onBenefitUpdated(payload));
575
+ }
576
+ break;
577
+ case "benefit_grant.created":
578
+ if (eventHandlers.onBenefitGrantCreated) {
579
+ promises.push(eventHandlers.onBenefitGrantCreated(payload));
580
+ }
581
+ break;
582
+ case "benefit_grant.updated":
583
+ if (eventHandlers.onBenefitGrantUpdated) {
584
+ promises.push(eventHandlers.onBenefitGrantUpdated(payload));
585
+ }
586
+ break;
587
+ case "benefit_grant.revoked":
588
+ if (eventHandlers.onBenefitGrantRevoked) {
589
+ promises.push(eventHandlers.onBenefitGrantRevoked(payload));
590
+ }
591
+ break;
592
+ case "customer.created":
593
+ if (eventHandlers.onCustomerCreated) {
594
+ promises.push(eventHandlers.onCustomerCreated(payload));
595
+ }
596
+ break;
597
+ case "customer.updated":
598
+ if (eventHandlers.onCustomerUpdated) {
599
+ promises.push(eventHandlers.onCustomerUpdated(payload));
600
+ }
601
+ break;
602
+ case "customer.deleted":
603
+ if (eventHandlers.onCustomerDeleted) {
604
+ promises.push(eventHandlers.onCustomerDeleted(payload));
605
+ }
606
+ break;
607
+ case "customer.state_changed":
608
+ if (eventHandlers.onCustomerStateChanged) {
609
+ promises.push(eventHandlers.onCustomerStateChanged(payload));
610
+ }
611
+ break;
612
+ }
613
+ switch (payload.type) {
614
+ case "benefit_grant.created":
615
+ case "benefit_grant.revoked":
616
+ if (entitlements) {
617
+ for (const handler of entitlements.handlers) {
618
+ promises.push(handler(payload));
619
+ }
620
+ }
621
+ }
622
+ return Promise.all(promises);
623
+ };
624
+
491
625
  // src/plugins/webhooks.ts
492
626
  var import_webhooks = require("@polar-sh/sdk/webhooks.js");
493
627
  var import_api6 = require("better-auth/api");
@@ -503,35 +637,7 @@ var webhooks = (options) => (_polar) => {
503
637
  cloneRequest: true
504
638
  },
505
639
  async (ctx) => {
506
- const {
507
- secret,
508
- onPayload,
509
- onCheckoutCreated,
510
- onCheckoutUpdated,
511
- onOrderCreated,
512
- onOrderPaid,
513
- onOrderRefunded,
514
- onRefundCreated,
515
- onRefundUpdated,
516
- onSubscriptionCreated,
517
- onSubscriptionUpdated,
518
- onSubscriptionActive,
519
- onSubscriptionCanceled,
520
- onSubscriptionRevoked,
521
- onSubscriptionUncanceled,
522
- onProductCreated,
523
- onProductUpdated,
524
- onOrganizationUpdated,
525
- onBenefitCreated,
526
- onBenefitUpdated,
527
- onBenefitGrantCreated,
528
- onBenefitGrantUpdated,
529
- onBenefitGrantRevoked,
530
- onCustomerCreated,
531
- onCustomerUpdated,
532
- onCustomerDeleted,
533
- onCustomerStateChanged
534
- } = options;
640
+ const { secret, ...eventHandlers } = options;
535
641
  if (!ctx.request?.body) {
536
642
  throw new import_api6.APIError("INTERNAL_SERVER_ERROR");
537
643
  }
@@ -565,136 +671,10 @@ var webhooks = (options) => (_polar) => {
565
671
  });
566
672
  }
567
673
  try {
568
- if (onPayload) {
569
- onPayload(event);
570
- }
571
- switch (event.type) {
572
- case "checkout.created":
573
- if (onCheckoutCreated) {
574
- onCheckoutCreated(event);
575
- }
576
- break;
577
- case "checkout.updated":
578
- if (onCheckoutUpdated) {
579
- onCheckoutUpdated(event);
580
- }
581
- break;
582
- case "order.created":
583
- if (onOrderCreated) {
584
- onOrderCreated(event);
585
- }
586
- break;
587
- case "order.paid":
588
- if (onOrderPaid) {
589
- onOrderPaid(event);
590
- }
591
- break;
592
- case "subscription.created":
593
- if (onSubscriptionCreated) {
594
- onSubscriptionCreated(event);
595
- }
596
- break;
597
- case "subscription.updated":
598
- if (onSubscriptionUpdated) {
599
- onSubscriptionUpdated(event);
600
- }
601
- break;
602
- case "subscription.active":
603
- if (onSubscriptionActive) {
604
- onSubscriptionActive(event);
605
- }
606
- break;
607
- case "subscription.canceled":
608
- if (onSubscriptionCanceled) {
609
- onSubscriptionCanceled(event);
610
- }
611
- break;
612
- case "subscription.uncanceled":
613
- if (onSubscriptionUncanceled) {
614
- onSubscriptionUncanceled(event);
615
- }
616
- break;
617
- case "subscription.revoked":
618
- if (onSubscriptionRevoked) {
619
- onSubscriptionRevoked(event);
620
- }
621
- break;
622
- case "product.created":
623
- if (onProductCreated) {
624
- onProductCreated(event);
625
- }
626
- break;
627
- case "product.updated":
628
- if (onProductUpdated) {
629
- onProductUpdated(event);
630
- }
631
- break;
632
- case "organization.updated":
633
- if (onOrganizationUpdated) {
634
- onOrganizationUpdated(event);
635
- }
636
- break;
637
- case "benefit.created":
638
- if (onBenefitCreated) {
639
- onBenefitCreated(event);
640
- }
641
- break;
642
- case "benefit.updated":
643
- if (onBenefitUpdated) {
644
- onBenefitUpdated(event);
645
- }
646
- break;
647
- case "benefit_grant.created":
648
- if (onBenefitGrantCreated) {
649
- onBenefitGrantCreated(event);
650
- }
651
- break;
652
- case "benefit_grant.updated":
653
- if (onBenefitGrantUpdated) {
654
- onBenefitGrantUpdated(event);
655
- }
656
- break;
657
- case "benefit_grant.revoked":
658
- if (onBenefitGrantRevoked) {
659
- onBenefitGrantRevoked(event);
660
- }
661
- break;
662
- case "order.refunded":
663
- if (onOrderRefunded) {
664
- onOrderRefunded(event);
665
- }
666
- break;
667
- case "refund.created":
668
- if (onRefundCreated) {
669
- onRefundCreated(event);
670
- }
671
- break;
672
- case "refund.updated":
673
- if (onRefundUpdated) {
674
- onRefundUpdated(event);
675
- }
676
- break;
677
- case "customer.created":
678
- if (onCustomerCreated) {
679
- onCustomerCreated(event);
680
- }
681
- break;
682
- case "customer.updated":
683
- if (onCustomerUpdated) {
684
- onCustomerUpdated(event);
685
- }
686
- break;
687
- case "customer.deleted":
688
- if (onCustomerDeleted) {
689
- onCustomerDeleted(event);
690
- }
691
- break;
692
- case "customer.state_changed":
693
- if (onCustomerStateChanged) {
694
- onCustomerStateChanged(event);
695
- }
696
- break;
697
- }
674
+ await handleWebhookPayload(event, {
675
+ webhookSecret: secret,
676
+ ...eventHandlers
677
+ });
698
678
  } catch (e) {
699
679
  if (e instanceof Error) {
700
680
  ctx.context.logger.error(
@@ -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","../src/plugins/webhooks.ts"],"sourcesContent":["import type { BetterAuthPlugin } from \"better-auth\";\nimport { onUserCreate, onUserUpdate } 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\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 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 = 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\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\t...params,\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\tconst customer = await 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\n\t\t\t\t\tconsole.log(customer);\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, 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\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\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 { 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}\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}),\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tconst session = await getSessionFromCtx(ctx);\n\n\t\t\t\t\tlet productIds: string[] = [];\n\n\t\t\t\t\tif (ctx.body.slug) {\n\t\t\t\t\t\tconst resolvedProducts = await (typeof checkoutOptions.products ===\n\t\t\t\t\t\t\"function\"\n\t\t\t\t\t\t\t? checkoutOptions.products()\n\t\t\t\t\t\t\t: checkoutOptions.products);\n\n\t\t\t\t\t\tconst productId = resolvedProducts?.find(\n\t\t\t\t\t\t\t(product) => product.slug === ctx.body.slug,\n\t\t\t\t\t\t)?.productId;\n\n\t\t\t\t\t\tif (!productId) {\n\t\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\t\tmessage: \"Product not found\",\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tproductIds = [productId];\n\t\t\t\t\t} else {\n\t\t\t\t\t\tproductIds = Array.isArray(ctx.body.products)\n\t\t\t\t\t\t\t? ctx.body.products.filter((id) => id !== undefined)\n\t\t\t\t\t\t\t: [ctx.body.products].filter((id) => id !== undefined);\n\t\t\t\t\t}\n\n\t\t\t\t\tif (checkoutOptions.authenticatedUsersOnly && !session?.user.id) {\n\t\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\t\tmessage: \"You must be logged in to checkout\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst checkout = await polar.checkouts.create({\n\t\t\t\t\t\t\texternalCustomerId: session?.user.id,\n\t\t\t\t\t\t\tproducts: productIds,\n\t\t\t\t\t\t\tsuccessUrl: checkoutOptions.successUrl\n\t\t\t\t\t\t\t\t? new URL(\n\t\t\t\t\t\t\t\t\t\tcheckoutOptions.successUrl,\n\t\t\t\t\t\t\t\t\t\tctx.request?.url,\n\t\t\t\t\t\t\t\t\t).toString()\n\t\t\t\t\t\t\t\t: undefined,\n\t\t\t\t\t\t\tmetadata: ctx.body.referenceId\n\t\t\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\t\t\treferenceId: ctx.body.referenceId,\n\t\t\t\t\t\t\t\t\t\t...ctx.body.metadata,\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t: ctx.body.metadata,\n\t\t\t\t\t\t\tcustomFieldData: ctx.body.customFieldData,\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\treturn ctx.json({\n\t\t\t\t\t\t\turl: checkout.url,\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","import type { Polar } from \"@polar-sh/sdk\";\nimport type { WebhookBenefitCreatedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitcreatedpayload.js\";\nimport type { WebhookBenefitGrantCreatedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitgrantcreatedpayload.js\";\nimport type { WebhookBenefitGrantRevokedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitgrantrevokedpayload.js\";\nimport type { WebhookBenefitGrantUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitgrantupdatedpayload.js\";\nimport type { WebhookBenefitUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitupdatedpayload.js\";\nimport type { WebhookCheckoutCreatedPayload } from \"@polar-sh/sdk/models/components/webhookcheckoutcreatedpayload.js\";\nimport type { WebhookCheckoutUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookcheckoutupdatedpayload.js\";\nimport type { WebhookCustomerCreatedPayload } from \"@polar-sh/sdk/models/components/webhookcustomercreatedpayload.js\";\nimport type { WebhookCustomerDeletedPayload } from \"@polar-sh/sdk/models/components/webhookcustomerdeletedpayload.js\";\nimport type { WebhookCustomerStateChangedPayload } from \"@polar-sh/sdk/models/components/webhookcustomerstatechangedpayload.js\";\nimport type { WebhookCustomerUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookcustomerupdatedpayload.js\";\nimport type { WebhookOrderCreatedPayload } from \"@polar-sh/sdk/models/components/webhookordercreatedpayload.js\";\nimport type { WebhookOrderPaidPayload } from \"@polar-sh/sdk/models/components/webhookorderpaidpayload.js\";\nimport type { WebhookOrderRefundedPayload } from \"@polar-sh/sdk/models/components/webhookorderrefundedpayload.js\";\nimport type { WebhookOrganizationUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookorganizationupdatedpayload.js\";\nimport type { WebhookProductCreatedPayload } from \"@polar-sh/sdk/models/components/webhookproductcreatedpayload.js\";\nimport type { WebhookProductUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookproductupdatedpayload.js\";\nimport type { WebhookRefundCreatedPayload } from \"@polar-sh/sdk/models/components/webhookrefundcreatedpayload.js\";\nimport type { WebhookRefundUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookrefundupdatedpayload.js\";\nimport type { WebhookSubscriptionActivePayload } from \"@polar-sh/sdk/models/components/webhooksubscriptionactivepayload.js\";\nimport type { WebhookSubscriptionCanceledPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptioncanceledpayload.js\";\nimport type { WebhookSubscriptionCreatedPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptioncreatedpayload.js\";\nimport type { WebhookSubscriptionRevokedPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptionrevokedpayload.js\";\nimport type { WebhookSubscriptionUncanceledPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptionuncanceledpayload.js\";\nimport type { WebhookSubscriptionUpdatedPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptionupdatedpayload.js\";\nimport { validateEvent } from \"@polar-sh/sdk/webhooks.js\";\nimport { APIError, createAuthEndpoint } from \"better-auth/api\";\n\nexport interface WebhooksOptions {\n\t/**\n\t * Webhook Secret\n\t */\n\tsecret: string;\n\t/**\n\t * Generic handler for all webhooks\n\t */\n\tonPayload?: (payload: ReturnType<typeof validateEvent>) => Promise<void>;\n\t/**\n\t * Webhook for checkout created\n\t */\n\tonCheckoutCreated?: (payload: WebhookCheckoutCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for checkout updated\n\t */\n\tonCheckoutUpdated?: (payload: WebhookCheckoutUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for order created\n\t */\n\tonOrderCreated?: (payload: WebhookOrderCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for order refunded\n\t */\n\tonOrderRefunded?: (payload: WebhookOrderRefundedPayload) => Promise<void>;\n\t/**\n\t * Webhook for order paid\n\t */\n\tonOrderPaid?: (payload: WebhookOrderPaidPayload) => Promise<void>;\n\t/**\n\t * Webhook for refund created\n\t */\n\tonRefundCreated?: (payload: WebhookRefundCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for refund updated\n\t */\n\tonRefundUpdated?: (payload: WebhookRefundUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for subscription created\n\t */\n\tonSubscriptionCreated?: (\n\t\tpayload: WebhookSubscriptionCreatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription updated\n\t */\n\tonSubscriptionUpdated?: (\n\t\tpayload: WebhookSubscriptionUpdatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription active\n\t */\n\tonSubscriptionActive?: (\n\t\tpayload: WebhookSubscriptionActivePayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription canceled\n\t */\n\tonSubscriptionCanceled?: (\n\t\tpayload: WebhookSubscriptionCanceledPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription revoked\n\t */\n\tonSubscriptionRevoked?: (\n\t\tpayload: WebhookSubscriptionRevokedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription uncanceled\n\t */\n\tonSubscriptionUncanceled?: (\n\t\tpayload: WebhookSubscriptionUncanceledPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for product created\n\t */\n\tonProductCreated?: (payload: WebhookProductCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for product updated\n\t */\n\tonProductUpdated?: (payload: WebhookProductUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for organization updated\n\t */\n\tonOrganizationUpdated?: (\n\t\tpayload: WebhookOrganizationUpdatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for benefit created\n\t */\n\tonBenefitCreated?: (payload: WebhookBenefitCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for benefit updated\n\t */\n\tonBenefitUpdated?: (payload: WebhookBenefitUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for benefit grant created\n\t */\n\tonBenefitGrantCreated?: (\n\t\tpayload: WebhookBenefitGrantCreatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for benefit grant updated\n\t */\n\tonBenefitGrantUpdated?: (\n\t\tpayload: WebhookBenefitGrantUpdatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for benefit grant revoked\n\t */\n\tonBenefitGrantRevoked?: (\n\t\tpayload: WebhookBenefitGrantRevokedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for customer created\n\t */\n\tonCustomerCreated?: (payload: WebhookCustomerCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for customer updated\n\t */\n\tonCustomerUpdated?: (payload: WebhookCustomerUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for customer deleted\n\t */\n\tonCustomerDeleted?: (payload: WebhookCustomerDeletedPayload) => Promise<void>;\n\t/**\n\t * Webhook for customer state changed\n\t */\n\tonCustomerStateChanged?: (\n\t\tpayload: WebhookCustomerStateChangedPayload,\n\t) => Promise<void>;\n}\n\nexport const webhooks = (options: WebhooksOptions) => (_polar: Polar) => {\n\treturn {\n\t\tpolarWebhooks: createAuthEndpoint(\n\t\t\t\"/polar/webhooks\",\n\t\t\t{\n\t\t\t\tmethod: \"POST\",\n\t\t\t\tmetadata: {\n\t\t\t\t\tisAction: false,\n\t\t\t\t},\n\t\t\t\tcloneRequest: true,\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tconst {\n\t\t\t\t\tsecret,\n\t\t\t\t\tonPayload,\n\t\t\t\t\tonCheckoutCreated,\n\t\t\t\t\tonCheckoutUpdated,\n\t\t\t\t\tonOrderCreated,\n\t\t\t\t\tonOrderPaid,\n\t\t\t\t\tonOrderRefunded,\n\t\t\t\t\tonRefundCreated,\n\t\t\t\t\tonRefundUpdated,\n\t\t\t\t\tonSubscriptionCreated,\n\t\t\t\t\tonSubscriptionUpdated,\n\t\t\t\t\tonSubscriptionActive,\n\t\t\t\t\tonSubscriptionCanceled,\n\t\t\t\t\tonSubscriptionRevoked,\n\t\t\t\t\tonSubscriptionUncanceled,\n\t\t\t\t\tonProductCreated,\n\t\t\t\t\tonProductUpdated,\n\t\t\t\t\tonOrganizationUpdated,\n\t\t\t\t\tonBenefitCreated,\n\t\t\t\t\tonBenefitUpdated,\n\t\t\t\t\tonBenefitGrantCreated,\n\t\t\t\t\tonBenefitGrantUpdated,\n\t\t\t\t\tonBenefitGrantRevoked,\n\t\t\t\t\tonCustomerCreated,\n\t\t\t\t\tonCustomerUpdated,\n\t\t\t\t\tonCustomerDeleted,\n\t\t\t\t\tonCustomerStateChanged,\n\t\t\t\t} = 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\t\t\t\ttry {\n\t\t\t\t\tif (onPayload) {\n\t\t\t\t\t\tonPayload(event);\n\t\t\t\t\t}\n\n\t\t\t\t\tswitch (event.type) {\n\t\t\t\t\t\tcase \"checkout.created\":\n\t\t\t\t\t\t\tif (onCheckoutCreated) {\n\t\t\t\t\t\t\t\tonCheckoutCreated(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"checkout.updated\":\n\t\t\t\t\t\t\tif (onCheckoutUpdated) {\n\t\t\t\t\t\t\t\tonCheckoutUpdated(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"order.created\":\n\t\t\t\t\t\t\tif (onOrderCreated) {\n\t\t\t\t\t\t\t\tonOrderCreated(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"order.paid\":\n\t\t\t\t\t\t\tif (onOrderPaid) {\n\t\t\t\t\t\t\t\tonOrderPaid(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"subscription.created\":\n\t\t\t\t\t\t\tif (onSubscriptionCreated) {\n\t\t\t\t\t\t\t\tonSubscriptionCreated(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"subscription.updated\":\n\t\t\t\t\t\t\tif (onSubscriptionUpdated) {\n\t\t\t\t\t\t\t\tonSubscriptionUpdated(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"subscription.active\":\n\t\t\t\t\t\t\tif (onSubscriptionActive) {\n\t\t\t\t\t\t\t\tonSubscriptionActive(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"subscription.canceled\":\n\t\t\t\t\t\t\tif (onSubscriptionCanceled) {\n\t\t\t\t\t\t\t\tonSubscriptionCanceled(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"subscription.uncanceled\":\n\t\t\t\t\t\t\tif (onSubscriptionUncanceled) {\n\t\t\t\t\t\t\t\tonSubscriptionUncanceled(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"subscription.revoked\":\n\t\t\t\t\t\t\tif (onSubscriptionRevoked) {\n\t\t\t\t\t\t\t\tonSubscriptionRevoked(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"product.created\":\n\t\t\t\t\t\t\tif (onProductCreated) {\n\t\t\t\t\t\t\t\tonProductCreated(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"product.updated\":\n\t\t\t\t\t\t\tif (onProductUpdated) {\n\t\t\t\t\t\t\t\tonProductUpdated(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"organization.updated\":\n\t\t\t\t\t\t\tif (onOrganizationUpdated) {\n\t\t\t\t\t\t\t\tonOrganizationUpdated(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"benefit.created\":\n\t\t\t\t\t\t\tif (onBenefitCreated) {\n\t\t\t\t\t\t\t\tonBenefitCreated(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"benefit.updated\":\n\t\t\t\t\t\t\tif (onBenefitUpdated) {\n\t\t\t\t\t\t\t\tonBenefitUpdated(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"benefit_grant.created\":\n\t\t\t\t\t\t\tif (onBenefitGrantCreated) {\n\t\t\t\t\t\t\t\tonBenefitGrantCreated(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"benefit_grant.updated\":\n\t\t\t\t\t\t\tif (onBenefitGrantUpdated) {\n\t\t\t\t\t\t\t\tonBenefitGrantUpdated(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"benefit_grant.revoked\":\n\t\t\t\t\t\t\tif (onBenefitGrantRevoked) {\n\t\t\t\t\t\t\t\tonBenefitGrantRevoked(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"order.refunded\":\n\t\t\t\t\t\t\tif (onOrderRefunded) {\n\t\t\t\t\t\t\t\tonOrderRefunded(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"refund.created\":\n\t\t\t\t\t\t\tif (onRefundCreated) {\n\t\t\t\t\t\t\t\tonRefundCreated(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"refund.updated\":\n\t\t\t\t\t\t\tif (onRefundUpdated) {\n\t\t\t\t\t\t\t\tonRefundUpdated(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"customer.created\":\n\t\t\t\t\t\t\tif (onCustomerCreated) {\n\t\t\t\t\t\t\t\tonCustomerCreated(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"customer.updated\":\n\t\t\t\t\t\t\tif (onCustomerUpdated) {\n\t\t\t\t\t\t\t\tonCustomerUpdated(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"customer.deleted\":\n\t\t\t\t\t\t\tif (onCustomerDeleted) {\n\t\t\t\t\t\t\t\tonCustomerDeleted(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"customer.state_changed\":\n\t\t\t\t\t\t\tif (onCustomerStateChanged) {\n\t\t\t\t\t\t\t\tonCustomerStateChanged(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\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,eACZ,CAAC,YACD,OAAO,MAAY,QAAiC;AACnD,MAAI,OAAO,QAAQ,wBAAwB;AAC1C,QAAI;AACH,YAAM,SAAS,QAAQ,0BACpB,MAAM,QAAQ,wBAAwB;AAAA,QACtC;AAAA,MACD,CAAC,IACA,CAAC;AAEJ,YAAM,EAAE,QAAQ,kBAAkB,IACjC,MAAM,QAAQ,OAAO,UAAU,KAAK,EAAE,OAAO,KAAK,MAAM,CAAC;AAC1D,YAAM,mBAAmB,kBAAkB,MAAM,CAAC;AAElD,UAAI,kBAAkB;AACrB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AAA,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":["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 { onUserCreate, onUserUpdate } 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\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 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 = 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\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\t...params,\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\tconst customer = await 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\n\t\t\t\t\tconsole.log(customer);\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, 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\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\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 { 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}\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}),\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tconst session = await getSessionFromCtx(ctx);\n\n\t\t\t\t\tlet productIds: string[] = [];\n\n\t\t\t\t\tif (ctx.body.slug) {\n\t\t\t\t\t\tconst resolvedProducts = await (typeof checkoutOptions.products ===\n\t\t\t\t\t\t\"function\"\n\t\t\t\t\t\t\t? checkoutOptions.products()\n\t\t\t\t\t\t\t: checkoutOptions.products);\n\n\t\t\t\t\t\tconst productId = resolvedProducts?.find(\n\t\t\t\t\t\t\t(product) => product.slug === ctx.body.slug,\n\t\t\t\t\t\t)?.productId;\n\n\t\t\t\t\t\tif (!productId) {\n\t\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\t\tmessage: \"Product not found\",\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tproductIds = [productId];\n\t\t\t\t\t} else {\n\t\t\t\t\t\tproductIds = Array.isArray(ctx.body.products)\n\t\t\t\t\t\t\t? ctx.body.products.filter((id) => id !== undefined)\n\t\t\t\t\t\t\t: [ctx.body.products].filter((id) => id !== undefined);\n\t\t\t\t\t}\n\n\t\t\t\t\tif (checkoutOptions.authenticatedUsersOnly && !session?.user.id) {\n\t\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\t\tmessage: \"You must be logged in to checkout\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst checkout = await polar.checkouts.create({\n\t\t\t\t\t\t\texternalCustomerId: session?.user.id,\n\t\t\t\t\t\t\tproducts: productIds,\n\t\t\t\t\t\t\tsuccessUrl: checkoutOptions.successUrl\n\t\t\t\t\t\t\t\t? new URL(\n\t\t\t\t\t\t\t\t\t\tcheckoutOptions.successUrl,\n\t\t\t\t\t\t\t\t\t\tctx.request?.url,\n\t\t\t\t\t\t\t\t\t).toString()\n\t\t\t\t\t\t\t\t: undefined,\n\t\t\t\t\t\t\tmetadata: ctx.body.referenceId\n\t\t\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\t\t\treferenceId: ctx.body.referenceId,\n\t\t\t\t\t\t\t\t\t\t...ctx.body.metadata,\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t: ctx.body.metadata,\n\t\t\t\t\t\t\tcustomFieldData: ctx.body.customFieldData,\n\t\t\t\t\t\t});\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.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 }\n switch (payload.type) {\n case \"benefit_grant.created\":\n case \"benefit_grant.revoked\":\n if (entitlements) {\n for (const handler of entitlements.handlers) {\n promises.push(handler(payload));\n }\n }\n }\n return Promise.all(promises);\n};\n\n// src/entitlement/entitlement.ts\nvar EntitlementStrategy = class {\n grantCallbacks = [];\n revokeCallbacks = [];\n grant(callback) {\n this.grantCallbacks.push(callback);\n return this;\n }\n revoke(callback) {\n this.revokeCallbacks.push(callback);\n return this;\n }\n handler(slug) {\n return async (payload) => {\n if (payload.data.benefit.description === slug) {\n switch (payload.type) {\n case \"benefit_grant.created\":\n await Promise.all(\n this.grantCallbacks.map(\n (callback) => callback({\n customer: payload.data.customer,\n properties: payload.data.properties,\n payload\n })\n )\n );\n break;\n case \"benefit_grant.revoked\":\n await Promise.all(\n this.revokeCallbacks.map(\n (callback) => callback({\n customer: payload.data.customer,\n properties: payload.data.properties,\n payload\n })\n )\n );\n break;\n }\n }\n };\n }\n};\nvar Entitlements = class {\n static handlers = [];\n static use(slug, strategy) {\n this.handlers.push(strategy.handler(slug));\n return this;\n }\n};\nexport {\n EntitlementStrategy,\n Entitlements,\n handleWebhookPayload\n};\n","import { handleWebhookPayload } from \"@polar-sh/adapter-utils\";\nimport type { Polar } from \"@polar-sh/sdk\";\nimport type { WebhookBenefitCreatedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitcreatedpayload.js\";\nimport type { WebhookBenefitGrantCreatedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitgrantcreatedpayload.js\";\nimport type { WebhookBenefitGrantRevokedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitgrantrevokedpayload.js\";\nimport type { WebhookBenefitGrantUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitgrantupdatedpayload.js\";\nimport type { WebhookBenefitUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitupdatedpayload.js\";\nimport type { WebhookCheckoutCreatedPayload } from \"@polar-sh/sdk/models/components/webhookcheckoutcreatedpayload.js\";\nimport type { WebhookCheckoutUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookcheckoutupdatedpayload.js\";\nimport type { WebhookCustomerCreatedPayload } from \"@polar-sh/sdk/models/components/webhookcustomercreatedpayload.js\";\nimport type { WebhookCustomerDeletedPayload } from \"@polar-sh/sdk/models/components/webhookcustomerdeletedpayload.js\";\nimport type { WebhookCustomerStateChangedPayload } from \"@polar-sh/sdk/models/components/webhookcustomerstatechangedpayload.js\";\nimport type { WebhookCustomerUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookcustomerupdatedpayload.js\";\nimport type { WebhookOrderCreatedPayload } from \"@polar-sh/sdk/models/components/webhookordercreatedpayload.js\";\nimport type { WebhookOrderPaidPayload } from \"@polar-sh/sdk/models/components/webhookorderpaidpayload.js\";\nimport type { WebhookOrderRefundedPayload } from \"@polar-sh/sdk/models/components/webhookorderrefundedpayload.js\";\nimport type { WebhookOrganizationUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookorganizationupdatedpayload.js\";\nimport type { WebhookProductCreatedPayload } from \"@polar-sh/sdk/models/components/webhookproductcreatedpayload.js\";\nimport type { WebhookProductUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookproductupdatedpayload.js\";\nimport type { WebhookRefundCreatedPayload } from \"@polar-sh/sdk/models/components/webhookrefundcreatedpayload.js\";\nimport type { WebhookRefundUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookrefundupdatedpayload.js\";\nimport type { WebhookSubscriptionActivePayload } from \"@polar-sh/sdk/models/components/webhooksubscriptionactivepayload.js\";\nimport type { WebhookSubscriptionCanceledPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptioncanceledpayload.js\";\nimport type { WebhookSubscriptionCreatedPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptioncreatedpayload.js\";\nimport type { WebhookSubscriptionRevokedPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptionrevokedpayload.js\";\nimport type { WebhookSubscriptionUncanceledPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptionuncanceledpayload.js\";\nimport type { WebhookSubscriptionUpdatedPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptionupdatedpayload.js\";\nimport { validateEvent } from \"@polar-sh/sdk/webhooks.js\";\nimport { APIError, createAuthEndpoint } from \"better-auth/api\";\n\nexport interface WebhooksOptions {\n\t/**\n\t * Webhook Secret\n\t */\n\tsecret: string;\n\t/**\n\t * Generic handler for all webhooks\n\t */\n\tonPayload?: (payload: ReturnType<typeof validateEvent>) => Promise<void>;\n\t/**\n\t * Webhook for checkout created\n\t */\n\tonCheckoutCreated?: (payload: WebhookCheckoutCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for checkout updated\n\t */\n\tonCheckoutUpdated?: (payload: WebhookCheckoutUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for order created\n\t */\n\tonOrderCreated?: (payload: WebhookOrderCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for order refunded\n\t */\n\tonOrderRefunded?: (payload: WebhookOrderRefundedPayload) => Promise<void>;\n\t/**\n\t * Webhook for order paid\n\t */\n\tonOrderPaid?: (payload: WebhookOrderPaidPayload) => Promise<void>;\n\t/**\n\t * Webhook for refund created\n\t */\n\tonRefundCreated?: (payload: WebhookRefundCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for refund updated\n\t */\n\tonRefundUpdated?: (payload: WebhookRefundUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for subscription created\n\t */\n\tonSubscriptionCreated?: (\n\t\tpayload: WebhookSubscriptionCreatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription updated\n\t */\n\tonSubscriptionUpdated?: (\n\t\tpayload: WebhookSubscriptionUpdatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription active\n\t */\n\tonSubscriptionActive?: (\n\t\tpayload: WebhookSubscriptionActivePayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription canceled\n\t */\n\tonSubscriptionCanceled?: (\n\t\tpayload: WebhookSubscriptionCanceledPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription revoked\n\t */\n\tonSubscriptionRevoked?: (\n\t\tpayload: WebhookSubscriptionRevokedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription uncanceled\n\t */\n\tonSubscriptionUncanceled?: (\n\t\tpayload: WebhookSubscriptionUncanceledPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for product created\n\t */\n\tonProductCreated?: (payload: WebhookProductCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for product updated\n\t */\n\tonProductUpdated?: (payload: WebhookProductUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for organization updated\n\t */\n\tonOrganizationUpdated?: (\n\t\tpayload: WebhookOrganizationUpdatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for benefit created\n\t */\n\tonBenefitCreated?: (payload: WebhookBenefitCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for benefit updated\n\t */\n\tonBenefitUpdated?: (payload: WebhookBenefitUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for benefit grant created\n\t */\n\tonBenefitGrantCreated?: (\n\t\tpayload: WebhookBenefitGrantCreatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for benefit grant updated\n\t */\n\tonBenefitGrantUpdated?: (\n\t\tpayload: WebhookBenefitGrantUpdatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for benefit grant revoked\n\t */\n\tonBenefitGrantRevoked?: (\n\t\tpayload: WebhookBenefitGrantRevokedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for customer created\n\t */\n\tonCustomerCreated?: (payload: WebhookCustomerCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for customer updated\n\t */\n\tonCustomerUpdated?: (payload: WebhookCustomerUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for customer deleted\n\t */\n\tonCustomerDeleted?: (payload: WebhookCustomerDeletedPayload) => Promise<void>;\n\t/**\n\t * Webhook for customer state changed\n\t */\n\tonCustomerStateChanged?: (\n\t\tpayload: WebhookCustomerStateChangedPayload,\n\t) => Promise<void>;\n}\n\nexport const webhooks = (options: WebhooksOptions) => (_polar: Polar) => {\n\treturn {\n\t\tpolarWebhooks: createAuthEndpoint(\n\t\t\t\"/polar/webhooks\",\n\t\t\t{\n\t\t\t\tmethod: \"POST\",\n\t\t\t\tmetadata: {\n\t\t\t\t\tisAction: false,\n\t\t\t\t},\n\t\t\t\tcloneRequest: true,\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tconst { secret, ...eventHandlers } = options;\n\n\t\t\t\tif (!ctx.request?.body) {\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\");\n\t\t\t\t}\n\t\t\t\tconst buf = await ctx.request.text();\n\t\t\t\tlet event: ReturnType<typeof validateEvent>;\n\t\t\t\ttry {\n\t\t\t\t\tif (!secret) {\n\t\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\t\tmessage: \"Polar webhook secret not found\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\tconst headers = {\n\t\t\t\t\t\t\"webhook-id\": ctx.request.headers.get(\"webhook-id\") as string,\n\t\t\t\t\t\t\"webhook-timestamp\": ctx.request.headers.get(\n\t\t\t\t\t\t\t\"webhook-timestamp\",\n\t\t\t\t\t\t) as string,\n\t\t\t\t\t\t\"webhook-signature\": ctx.request.headers.get(\n\t\t\t\t\t\t\t\"webhook-signature\",\n\t\t\t\t\t\t) as string,\n\t\t\t\t\t};\n\n\t\t\t\t\tevent = validateEvent(buf, headers, secret);\n\t\t\t\t} catch (err: unknown) {\n\t\t\t\t\tif (err instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(`${err.message}`);\n\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\tmessage: `Webhook Error: ${err.message}`,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: `Webhook Error: ${err}`,\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tawait handleWebhookPayload(event, {\n\t\t\t\t\t\twebhookSecret: secret,\n\t\t\t\t\t\t...eventHandlers,\n\t\t\t\t\t});\n\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t`Polar webhook failed. Error: ${e.message}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t} else {\n\t\t\t\t\t\tctx.context.logger.error(`Polar webhook failed. Error: ${e}`);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"Webhook error: See server logs for more information.\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\treturn ctx.json({ received: true });\n\t\t\t},\n\t\t),\n\t};\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCA,iBAAyB;AAGlB,IAAM,eACZ,CAAC,YACD,OAAO,MAAY,QAAiC;AACnD,MAAI,OAAO,QAAQ,wBAAwB;AAC1C,QAAI;AACH,YAAM,SAAS,QAAQ,0BACpB,MAAM,QAAQ,wBAAwB;AAAA,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A,UACF;AACA,gBAAM,IAAI,qBAAS,eAAe;AAAA,YACjC,SAAS,kBAAkB,GAAG;AAAA,UAC/B,CAAC;AAAA,QACF;AAEA,YAAI;AACH,gBAAM,qBAAqB,OAAO;AAAA,YACjC,eAAe;AAAA,YACf,GAAG;AAAA,UACJ,CAAC;AAAA,QACF,SAAS,GAAY;AACpB,cAAI,aAAa,OAAO;AACvB,gBAAI,QAAQ,OAAO;AAAA,cAClB,gCAAgC,EAAE,OAAO;AAAA,YAC1C;AAAA,UACD,OAAO;AACN,gBAAI,QAAQ,OAAO,MAAM,gCAAgC,CAAC,EAAE;AAAA,UAC7D;AAEA,gBAAM,IAAI,qBAAS,eAAe;AAAA,YACjC,SAAS;AAAA,UACV,CAAC;AAAA,QACF;AAEA,eAAO,IAAI,KAAK,EAAE,UAAU,KAAK,CAAC;AAAA,MACnC;AAAA,IACD;AAAA,EACD;AACD;;;APhOO,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,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":["import_api","polar","import_api","import_plugins","import_zod","polar","checkout","import_api","import_zod","polar","import_api"]}
package/dist/index.d.cts CHANGED
@@ -51,6 +51,10 @@ interface CheckoutOptions {
51
51
  * Only allow authenticated customers to checkout
52
52
  */
53
53
  authenticatedUsersOnly?: boolean;
54
+ /**
55
+ * Checkout theme
56
+ */
57
+ theme?: "light" | "dark";
54
58
  }
55
59
  declare const checkout: (checkoutOptions?: CheckoutOptions) => (polar: Polar) => {
56
60
  checkout: {
package/dist/index.d.ts CHANGED
@@ -51,6 +51,10 @@ interface CheckoutOptions {
51
51
  * Only allow authenticated customers to checkout
52
52
  */
53
53
  authenticatedUsersOnly?: boolean;
54
+ /**
55
+ * Checkout theme
56
+ */
57
+ theme?: "light" | "dark";
54
58
  }
55
59
  declare const checkout: (checkoutOptions?: CheckoutOptions) => (polar: Polar) => {
56
60
  checkout: {
package/dist/index.js CHANGED
@@ -348,8 +348,12 @@ var checkout = (checkoutOptions = {}) => (polar2) => {
348
348
  } : ctx.body.metadata,
349
349
  customFieldData: ctx.body.customFieldData
350
350
  });
351
+ const redirectUrl = new URL(checkout2.url);
352
+ if (checkoutOptions.theme) {
353
+ redirectUrl.searchParams.set("theme", checkoutOptions.theme);
354
+ }
351
355
  return ctx.json({
352
- url: checkout2.url,
356
+ url: redirectUrl.toString(),
353
357
  redirect: true
354
358
  });
355
359
  } catch (e) {
@@ -461,6 +465,136 @@ var usage = (_usageOptions) => (polar2) => {
461
465
  };
462
466
  };
463
467
 
468
+ // ../adapter-utils/dist/index.js
469
+ var handleWebhookPayload = async (payload, { webhookSecret, entitlements, onPayload, ...eventHandlers }) => {
470
+ const promises = [];
471
+ if (onPayload) {
472
+ promises.push(onPayload(payload));
473
+ }
474
+ switch (payload.type) {
475
+ case "checkout.created":
476
+ if (eventHandlers.onCheckoutCreated) {
477
+ promises.push(eventHandlers.onCheckoutCreated(payload));
478
+ }
479
+ break;
480
+ case "checkout.updated":
481
+ if (eventHandlers.onCheckoutUpdated) {
482
+ promises.push(eventHandlers.onCheckoutUpdated(payload));
483
+ }
484
+ break;
485
+ case "order.created":
486
+ if (eventHandlers.onOrderCreated) {
487
+ promises.push(eventHandlers.onOrderCreated(payload));
488
+ }
489
+ break;
490
+ case "order.updated":
491
+ if (eventHandlers.onOrderUpdated) {
492
+ promises.push(eventHandlers.onOrderUpdated(payload));
493
+ }
494
+ break;
495
+ case "order.paid":
496
+ if (eventHandlers.onOrderPaid) {
497
+ promises.push(eventHandlers.onOrderPaid(payload));
498
+ }
499
+ break;
500
+ case "subscription.created":
501
+ if (eventHandlers.onSubscriptionCreated) {
502
+ promises.push(eventHandlers.onSubscriptionCreated(payload));
503
+ }
504
+ break;
505
+ case "subscription.updated":
506
+ if (eventHandlers.onSubscriptionUpdated) {
507
+ promises.push(eventHandlers.onSubscriptionUpdated(payload));
508
+ }
509
+ break;
510
+ case "subscription.active":
511
+ if (eventHandlers.onSubscriptionActive) {
512
+ promises.push(eventHandlers.onSubscriptionActive(payload));
513
+ }
514
+ break;
515
+ case "subscription.canceled":
516
+ if (eventHandlers.onSubscriptionCanceled) {
517
+ promises.push(eventHandlers.onSubscriptionCanceled(payload));
518
+ }
519
+ break;
520
+ case "subscription.revoked":
521
+ if (eventHandlers.onSubscriptionRevoked) {
522
+ promises.push(eventHandlers.onSubscriptionRevoked(payload));
523
+ }
524
+ break;
525
+ case "product.created":
526
+ if (eventHandlers.onProductCreated) {
527
+ promises.push(eventHandlers.onProductCreated(payload));
528
+ }
529
+ break;
530
+ case "product.updated":
531
+ if (eventHandlers.onProductUpdated) {
532
+ promises.push(eventHandlers.onProductUpdated(payload));
533
+ }
534
+ break;
535
+ case "organization.updated":
536
+ if (eventHandlers.onOrganizationUpdated) {
537
+ promises.push(eventHandlers.onOrganizationUpdated(payload));
538
+ }
539
+ break;
540
+ case "benefit.created":
541
+ if (eventHandlers.onBenefitCreated) {
542
+ promises.push(eventHandlers.onBenefitCreated(payload));
543
+ }
544
+ break;
545
+ case "benefit.updated":
546
+ if (eventHandlers.onBenefitUpdated) {
547
+ promises.push(eventHandlers.onBenefitUpdated(payload));
548
+ }
549
+ break;
550
+ case "benefit_grant.created":
551
+ if (eventHandlers.onBenefitGrantCreated) {
552
+ promises.push(eventHandlers.onBenefitGrantCreated(payload));
553
+ }
554
+ break;
555
+ case "benefit_grant.updated":
556
+ if (eventHandlers.onBenefitGrantUpdated) {
557
+ promises.push(eventHandlers.onBenefitGrantUpdated(payload));
558
+ }
559
+ break;
560
+ case "benefit_grant.revoked":
561
+ if (eventHandlers.onBenefitGrantRevoked) {
562
+ promises.push(eventHandlers.onBenefitGrantRevoked(payload));
563
+ }
564
+ break;
565
+ case "customer.created":
566
+ if (eventHandlers.onCustomerCreated) {
567
+ promises.push(eventHandlers.onCustomerCreated(payload));
568
+ }
569
+ break;
570
+ case "customer.updated":
571
+ if (eventHandlers.onCustomerUpdated) {
572
+ promises.push(eventHandlers.onCustomerUpdated(payload));
573
+ }
574
+ break;
575
+ case "customer.deleted":
576
+ if (eventHandlers.onCustomerDeleted) {
577
+ promises.push(eventHandlers.onCustomerDeleted(payload));
578
+ }
579
+ break;
580
+ case "customer.state_changed":
581
+ if (eventHandlers.onCustomerStateChanged) {
582
+ promises.push(eventHandlers.onCustomerStateChanged(payload));
583
+ }
584
+ break;
585
+ }
586
+ switch (payload.type) {
587
+ case "benefit_grant.created":
588
+ case "benefit_grant.revoked":
589
+ if (entitlements) {
590
+ for (const handler of entitlements.handlers) {
591
+ promises.push(handler(payload));
592
+ }
593
+ }
594
+ }
595
+ return Promise.all(promises);
596
+ };
597
+
464
598
  // src/plugins/webhooks.ts
465
599
  import { validateEvent } from "@polar-sh/sdk/webhooks.js";
466
600
  import { APIError as APIError5, createAuthEndpoint as createAuthEndpoint4 } from "better-auth/api";
@@ -476,35 +610,7 @@ var webhooks = (options) => (_polar) => {
476
610
  cloneRequest: true
477
611
  },
478
612
  async (ctx) => {
479
- const {
480
- secret,
481
- onPayload,
482
- onCheckoutCreated,
483
- onCheckoutUpdated,
484
- onOrderCreated,
485
- onOrderPaid,
486
- onOrderRefunded,
487
- onRefundCreated,
488
- onRefundUpdated,
489
- onSubscriptionCreated,
490
- onSubscriptionUpdated,
491
- onSubscriptionActive,
492
- onSubscriptionCanceled,
493
- onSubscriptionRevoked,
494
- onSubscriptionUncanceled,
495
- onProductCreated,
496
- onProductUpdated,
497
- onOrganizationUpdated,
498
- onBenefitCreated,
499
- onBenefitUpdated,
500
- onBenefitGrantCreated,
501
- onBenefitGrantUpdated,
502
- onBenefitGrantRevoked,
503
- onCustomerCreated,
504
- onCustomerUpdated,
505
- onCustomerDeleted,
506
- onCustomerStateChanged
507
- } = options;
613
+ const { secret, ...eventHandlers } = options;
508
614
  if (!ctx.request?.body) {
509
615
  throw new APIError5("INTERNAL_SERVER_ERROR");
510
616
  }
@@ -538,136 +644,10 @@ var webhooks = (options) => (_polar) => {
538
644
  });
539
645
  }
540
646
  try {
541
- if (onPayload) {
542
- onPayload(event);
543
- }
544
- switch (event.type) {
545
- case "checkout.created":
546
- if (onCheckoutCreated) {
547
- onCheckoutCreated(event);
548
- }
549
- break;
550
- case "checkout.updated":
551
- if (onCheckoutUpdated) {
552
- onCheckoutUpdated(event);
553
- }
554
- break;
555
- case "order.created":
556
- if (onOrderCreated) {
557
- onOrderCreated(event);
558
- }
559
- break;
560
- case "order.paid":
561
- if (onOrderPaid) {
562
- onOrderPaid(event);
563
- }
564
- break;
565
- case "subscription.created":
566
- if (onSubscriptionCreated) {
567
- onSubscriptionCreated(event);
568
- }
569
- break;
570
- case "subscription.updated":
571
- if (onSubscriptionUpdated) {
572
- onSubscriptionUpdated(event);
573
- }
574
- break;
575
- case "subscription.active":
576
- if (onSubscriptionActive) {
577
- onSubscriptionActive(event);
578
- }
579
- break;
580
- case "subscription.canceled":
581
- if (onSubscriptionCanceled) {
582
- onSubscriptionCanceled(event);
583
- }
584
- break;
585
- case "subscription.uncanceled":
586
- if (onSubscriptionUncanceled) {
587
- onSubscriptionUncanceled(event);
588
- }
589
- break;
590
- case "subscription.revoked":
591
- if (onSubscriptionRevoked) {
592
- onSubscriptionRevoked(event);
593
- }
594
- break;
595
- case "product.created":
596
- if (onProductCreated) {
597
- onProductCreated(event);
598
- }
599
- break;
600
- case "product.updated":
601
- if (onProductUpdated) {
602
- onProductUpdated(event);
603
- }
604
- break;
605
- case "organization.updated":
606
- if (onOrganizationUpdated) {
607
- onOrganizationUpdated(event);
608
- }
609
- break;
610
- case "benefit.created":
611
- if (onBenefitCreated) {
612
- onBenefitCreated(event);
613
- }
614
- break;
615
- case "benefit.updated":
616
- if (onBenefitUpdated) {
617
- onBenefitUpdated(event);
618
- }
619
- break;
620
- case "benefit_grant.created":
621
- if (onBenefitGrantCreated) {
622
- onBenefitGrantCreated(event);
623
- }
624
- break;
625
- case "benefit_grant.updated":
626
- if (onBenefitGrantUpdated) {
627
- onBenefitGrantUpdated(event);
628
- }
629
- break;
630
- case "benefit_grant.revoked":
631
- if (onBenefitGrantRevoked) {
632
- onBenefitGrantRevoked(event);
633
- }
634
- break;
635
- case "order.refunded":
636
- if (onOrderRefunded) {
637
- onOrderRefunded(event);
638
- }
639
- break;
640
- case "refund.created":
641
- if (onRefundCreated) {
642
- onRefundCreated(event);
643
- }
644
- break;
645
- case "refund.updated":
646
- if (onRefundUpdated) {
647
- onRefundUpdated(event);
648
- }
649
- break;
650
- case "customer.created":
651
- if (onCustomerCreated) {
652
- onCustomerCreated(event);
653
- }
654
- break;
655
- case "customer.updated":
656
- if (onCustomerUpdated) {
657
- onCustomerUpdated(event);
658
- }
659
- break;
660
- case "customer.deleted":
661
- if (onCustomerDeleted) {
662
- onCustomerDeleted(event);
663
- }
664
- break;
665
- case "customer.state_changed":
666
- if (onCustomerStateChanged) {
667
- onCustomerStateChanged(event);
668
- }
669
- break;
670
- }
647
+ await handleWebhookPayload(event, {
648
+ webhookSecret: secret,
649
+ ...eventHandlers
650
+ });
671
651
  } catch (e) {
672
652
  if (e instanceof Error) {
673
653
  ctx.context.logger.error(
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","../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 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 = 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\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\t...params,\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\tconst customer = await 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\n\t\t\t\t\tconsole.log(customer);\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, 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\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\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 { 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}\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}),\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tconst session = await getSessionFromCtx(ctx);\n\n\t\t\t\t\tlet productIds: string[] = [];\n\n\t\t\t\t\tif (ctx.body.slug) {\n\t\t\t\t\t\tconst resolvedProducts = await (typeof checkoutOptions.products ===\n\t\t\t\t\t\t\"function\"\n\t\t\t\t\t\t\t? checkoutOptions.products()\n\t\t\t\t\t\t\t: checkoutOptions.products);\n\n\t\t\t\t\t\tconst productId = resolvedProducts?.find(\n\t\t\t\t\t\t\t(product) => product.slug === ctx.body.slug,\n\t\t\t\t\t\t)?.productId;\n\n\t\t\t\t\t\tif (!productId) {\n\t\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\t\tmessage: \"Product not found\",\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tproductIds = [productId];\n\t\t\t\t\t} else {\n\t\t\t\t\t\tproductIds = Array.isArray(ctx.body.products)\n\t\t\t\t\t\t\t? ctx.body.products.filter((id) => id !== undefined)\n\t\t\t\t\t\t\t: [ctx.body.products].filter((id) => id !== undefined);\n\t\t\t\t\t}\n\n\t\t\t\t\tif (checkoutOptions.authenticatedUsersOnly && !session?.user.id) {\n\t\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\t\tmessage: \"You must be logged in to checkout\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst checkout = await polar.checkouts.create({\n\t\t\t\t\t\t\texternalCustomerId: session?.user.id,\n\t\t\t\t\t\t\tproducts: productIds,\n\t\t\t\t\t\t\tsuccessUrl: checkoutOptions.successUrl\n\t\t\t\t\t\t\t\t? new URL(\n\t\t\t\t\t\t\t\t\t\tcheckoutOptions.successUrl,\n\t\t\t\t\t\t\t\t\t\tctx.request?.url,\n\t\t\t\t\t\t\t\t\t).toString()\n\t\t\t\t\t\t\t\t: undefined,\n\t\t\t\t\t\t\tmetadata: ctx.body.referenceId\n\t\t\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\t\t\treferenceId: ctx.body.referenceId,\n\t\t\t\t\t\t\t\t\t\t...ctx.body.metadata,\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t: ctx.body.metadata,\n\t\t\t\t\t\t\tcustomFieldData: ctx.body.customFieldData,\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\treturn ctx.json({\n\t\t\t\t\t\t\turl: checkout.url,\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","import type { Polar } from \"@polar-sh/sdk\";\nimport type { WebhookBenefitCreatedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitcreatedpayload.js\";\nimport type { WebhookBenefitGrantCreatedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitgrantcreatedpayload.js\";\nimport type { WebhookBenefitGrantRevokedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitgrantrevokedpayload.js\";\nimport type { WebhookBenefitGrantUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitgrantupdatedpayload.js\";\nimport type { WebhookBenefitUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitupdatedpayload.js\";\nimport type { WebhookCheckoutCreatedPayload } from \"@polar-sh/sdk/models/components/webhookcheckoutcreatedpayload.js\";\nimport type { WebhookCheckoutUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookcheckoutupdatedpayload.js\";\nimport type { WebhookCustomerCreatedPayload } from \"@polar-sh/sdk/models/components/webhookcustomercreatedpayload.js\";\nimport type { WebhookCustomerDeletedPayload } from \"@polar-sh/sdk/models/components/webhookcustomerdeletedpayload.js\";\nimport type { WebhookCustomerStateChangedPayload } from \"@polar-sh/sdk/models/components/webhookcustomerstatechangedpayload.js\";\nimport type { WebhookCustomerUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookcustomerupdatedpayload.js\";\nimport type { WebhookOrderCreatedPayload } from \"@polar-sh/sdk/models/components/webhookordercreatedpayload.js\";\nimport type { WebhookOrderPaidPayload } from \"@polar-sh/sdk/models/components/webhookorderpaidpayload.js\";\nimport type { WebhookOrderRefundedPayload } from \"@polar-sh/sdk/models/components/webhookorderrefundedpayload.js\";\nimport type { WebhookOrganizationUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookorganizationupdatedpayload.js\";\nimport type { WebhookProductCreatedPayload } from \"@polar-sh/sdk/models/components/webhookproductcreatedpayload.js\";\nimport type { WebhookProductUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookproductupdatedpayload.js\";\nimport type { WebhookRefundCreatedPayload } from \"@polar-sh/sdk/models/components/webhookrefundcreatedpayload.js\";\nimport type { WebhookRefundUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookrefundupdatedpayload.js\";\nimport type { WebhookSubscriptionActivePayload } from \"@polar-sh/sdk/models/components/webhooksubscriptionactivepayload.js\";\nimport type { WebhookSubscriptionCanceledPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptioncanceledpayload.js\";\nimport type { WebhookSubscriptionCreatedPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptioncreatedpayload.js\";\nimport type { WebhookSubscriptionRevokedPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptionrevokedpayload.js\";\nimport type { WebhookSubscriptionUncanceledPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptionuncanceledpayload.js\";\nimport type { WebhookSubscriptionUpdatedPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptionupdatedpayload.js\";\nimport { validateEvent } from \"@polar-sh/sdk/webhooks.js\";\nimport { APIError, createAuthEndpoint } from \"better-auth/api\";\n\nexport interface WebhooksOptions {\n\t/**\n\t * Webhook Secret\n\t */\n\tsecret: string;\n\t/**\n\t * Generic handler for all webhooks\n\t */\n\tonPayload?: (payload: ReturnType<typeof validateEvent>) => Promise<void>;\n\t/**\n\t * Webhook for checkout created\n\t */\n\tonCheckoutCreated?: (payload: WebhookCheckoutCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for checkout updated\n\t */\n\tonCheckoutUpdated?: (payload: WebhookCheckoutUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for order created\n\t */\n\tonOrderCreated?: (payload: WebhookOrderCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for order refunded\n\t */\n\tonOrderRefunded?: (payload: WebhookOrderRefundedPayload) => Promise<void>;\n\t/**\n\t * Webhook for order paid\n\t */\n\tonOrderPaid?: (payload: WebhookOrderPaidPayload) => Promise<void>;\n\t/**\n\t * Webhook for refund created\n\t */\n\tonRefundCreated?: (payload: WebhookRefundCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for refund updated\n\t */\n\tonRefundUpdated?: (payload: WebhookRefundUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for subscription created\n\t */\n\tonSubscriptionCreated?: (\n\t\tpayload: WebhookSubscriptionCreatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription updated\n\t */\n\tonSubscriptionUpdated?: (\n\t\tpayload: WebhookSubscriptionUpdatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription active\n\t */\n\tonSubscriptionActive?: (\n\t\tpayload: WebhookSubscriptionActivePayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription canceled\n\t */\n\tonSubscriptionCanceled?: (\n\t\tpayload: WebhookSubscriptionCanceledPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription revoked\n\t */\n\tonSubscriptionRevoked?: (\n\t\tpayload: WebhookSubscriptionRevokedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription uncanceled\n\t */\n\tonSubscriptionUncanceled?: (\n\t\tpayload: WebhookSubscriptionUncanceledPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for product created\n\t */\n\tonProductCreated?: (payload: WebhookProductCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for product updated\n\t */\n\tonProductUpdated?: (payload: WebhookProductUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for organization updated\n\t */\n\tonOrganizationUpdated?: (\n\t\tpayload: WebhookOrganizationUpdatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for benefit created\n\t */\n\tonBenefitCreated?: (payload: WebhookBenefitCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for benefit updated\n\t */\n\tonBenefitUpdated?: (payload: WebhookBenefitUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for benefit grant created\n\t */\n\tonBenefitGrantCreated?: (\n\t\tpayload: WebhookBenefitGrantCreatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for benefit grant updated\n\t */\n\tonBenefitGrantUpdated?: (\n\t\tpayload: WebhookBenefitGrantUpdatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for benefit grant revoked\n\t */\n\tonBenefitGrantRevoked?: (\n\t\tpayload: WebhookBenefitGrantRevokedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for customer created\n\t */\n\tonCustomerCreated?: (payload: WebhookCustomerCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for customer updated\n\t */\n\tonCustomerUpdated?: (payload: WebhookCustomerUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for customer deleted\n\t */\n\tonCustomerDeleted?: (payload: WebhookCustomerDeletedPayload) => Promise<void>;\n\t/**\n\t * Webhook for customer state changed\n\t */\n\tonCustomerStateChanged?: (\n\t\tpayload: WebhookCustomerStateChangedPayload,\n\t) => Promise<void>;\n}\n\nexport const webhooks = (options: WebhooksOptions) => (_polar: Polar) => {\n\treturn {\n\t\tpolarWebhooks: createAuthEndpoint(\n\t\t\t\"/polar/webhooks\",\n\t\t\t{\n\t\t\t\tmethod: \"POST\",\n\t\t\t\tmetadata: {\n\t\t\t\t\tisAction: false,\n\t\t\t\t},\n\t\t\t\tcloneRequest: true,\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tconst {\n\t\t\t\t\tsecret,\n\t\t\t\t\tonPayload,\n\t\t\t\t\tonCheckoutCreated,\n\t\t\t\t\tonCheckoutUpdated,\n\t\t\t\t\tonOrderCreated,\n\t\t\t\t\tonOrderPaid,\n\t\t\t\t\tonOrderRefunded,\n\t\t\t\t\tonRefundCreated,\n\t\t\t\t\tonRefundUpdated,\n\t\t\t\t\tonSubscriptionCreated,\n\t\t\t\t\tonSubscriptionUpdated,\n\t\t\t\t\tonSubscriptionActive,\n\t\t\t\t\tonSubscriptionCanceled,\n\t\t\t\t\tonSubscriptionRevoked,\n\t\t\t\t\tonSubscriptionUncanceled,\n\t\t\t\t\tonProductCreated,\n\t\t\t\t\tonProductUpdated,\n\t\t\t\t\tonOrganizationUpdated,\n\t\t\t\t\tonBenefitCreated,\n\t\t\t\t\tonBenefitUpdated,\n\t\t\t\t\tonBenefitGrantCreated,\n\t\t\t\t\tonBenefitGrantUpdated,\n\t\t\t\t\tonBenefitGrantRevoked,\n\t\t\t\t\tonCustomerCreated,\n\t\t\t\t\tonCustomerUpdated,\n\t\t\t\t\tonCustomerDeleted,\n\t\t\t\t\tonCustomerStateChanged,\n\t\t\t\t} = 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\t\t\t\ttry {\n\t\t\t\t\tif (onPayload) {\n\t\t\t\t\t\tonPayload(event);\n\t\t\t\t\t}\n\n\t\t\t\t\tswitch (event.type) {\n\t\t\t\t\t\tcase \"checkout.created\":\n\t\t\t\t\t\t\tif (onCheckoutCreated) {\n\t\t\t\t\t\t\t\tonCheckoutCreated(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"checkout.updated\":\n\t\t\t\t\t\t\tif (onCheckoutUpdated) {\n\t\t\t\t\t\t\t\tonCheckoutUpdated(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"order.created\":\n\t\t\t\t\t\t\tif (onOrderCreated) {\n\t\t\t\t\t\t\t\tonOrderCreated(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"order.paid\":\n\t\t\t\t\t\t\tif (onOrderPaid) {\n\t\t\t\t\t\t\t\tonOrderPaid(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"subscription.created\":\n\t\t\t\t\t\t\tif (onSubscriptionCreated) {\n\t\t\t\t\t\t\t\tonSubscriptionCreated(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"subscription.updated\":\n\t\t\t\t\t\t\tif (onSubscriptionUpdated) {\n\t\t\t\t\t\t\t\tonSubscriptionUpdated(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"subscription.active\":\n\t\t\t\t\t\t\tif (onSubscriptionActive) {\n\t\t\t\t\t\t\t\tonSubscriptionActive(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"subscription.canceled\":\n\t\t\t\t\t\t\tif (onSubscriptionCanceled) {\n\t\t\t\t\t\t\t\tonSubscriptionCanceled(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"subscription.uncanceled\":\n\t\t\t\t\t\t\tif (onSubscriptionUncanceled) {\n\t\t\t\t\t\t\t\tonSubscriptionUncanceled(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"subscription.revoked\":\n\t\t\t\t\t\t\tif (onSubscriptionRevoked) {\n\t\t\t\t\t\t\t\tonSubscriptionRevoked(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"product.created\":\n\t\t\t\t\t\t\tif (onProductCreated) {\n\t\t\t\t\t\t\t\tonProductCreated(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"product.updated\":\n\t\t\t\t\t\t\tif (onProductUpdated) {\n\t\t\t\t\t\t\t\tonProductUpdated(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"organization.updated\":\n\t\t\t\t\t\t\tif (onOrganizationUpdated) {\n\t\t\t\t\t\t\t\tonOrganizationUpdated(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"benefit.created\":\n\t\t\t\t\t\t\tif (onBenefitCreated) {\n\t\t\t\t\t\t\t\tonBenefitCreated(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"benefit.updated\":\n\t\t\t\t\t\t\tif (onBenefitUpdated) {\n\t\t\t\t\t\t\t\tonBenefitUpdated(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"benefit_grant.created\":\n\t\t\t\t\t\t\tif (onBenefitGrantCreated) {\n\t\t\t\t\t\t\t\tonBenefitGrantCreated(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"benefit_grant.updated\":\n\t\t\t\t\t\t\tif (onBenefitGrantUpdated) {\n\t\t\t\t\t\t\t\tonBenefitGrantUpdated(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"benefit_grant.revoked\":\n\t\t\t\t\t\t\tif (onBenefitGrantRevoked) {\n\t\t\t\t\t\t\t\tonBenefitGrantRevoked(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"order.refunded\":\n\t\t\t\t\t\t\tif (onOrderRefunded) {\n\t\t\t\t\t\t\t\tonOrderRefunded(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"refund.created\":\n\t\t\t\t\t\t\tif (onRefundCreated) {\n\t\t\t\t\t\t\t\tonRefundCreated(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"refund.updated\":\n\t\t\t\t\t\t\tif (onRefundUpdated) {\n\t\t\t\t\t\t\t\tonRefundUpdated(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"customer.created\":\n\t\t\t\t\t\t\tif (onCustomerCreated) {\n\t\t\t\t\t\t\t\tonCustomerCreated(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"customer.updated\":\n\t\t\t\t\t\t\tif (onCustomerUpdated) {\n\t\t\t\t\t\t\t\tonCustomerUpdated(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"customer.deleted\":\n\t\t\t\t\t\t\tif (onCustomerDeleted) {\n\t\t\t\t\t\t\t\tonCustomerDeleted(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase \"customer.state_changed\":\n\t\t\t\t\t\t\tif (onCustomerStateChanged) {\n\t\t\t\t\t\t\t\tonCustomerStateChanged(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\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 { onUserCreate, onUserUpdate } 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\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":";AACA,SAAS,gBAAgB;AAGlB,IAAM,eACZ,CAAC,YACD,OAAO,MAAY,QAAiC;AACnD,MAAI,OAAO,QAAQ,wBAAwB;AAC1C,QAAI;AACH,YAAM,SAAS,QAAQ,0BACpB,MAAM,QAAQ,wBAAwB;AAAA,QACtC;AAAA,MACD,CAAC,IACA,CAAC;AAEJ,YAAM,EAAE,QAAQ,kBAAkB,IACjC,MAAM,QAAQ,OAAO,UAAU,KAAK,EAAE,OAAO,KAAK,MAAM,CAAC;AAC1D,YAAM,mBAAmB,kBAAkB,MAAM,CAAC;AAElD,UAAI,kBAAkB;AACrB,YAAI,iBAAiB,eAAe,KAAK,IAAI;AAC5C,gBAAM,QAAQ,OA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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,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":["APIError","polar","APIError","createAuthEndpoint","z","polar","checkout","APIError","createAuthEndpoint","sessionMiddleware","z","polar","APIError","createAuthEndpoint"]}
1
+ {"version":3,"sources":["../src/hooks/customer.ts","../src/client.ts","../src/plugins/portal.ts","../src/plugins/checkout.ts","../src/plugins/usage.ts","../../adapter-utils/dist/index.js","../src/plugins/webhooks.ts","../src/index.ts"],"sourcesContent":["import type { GenericEndpointContext, User } from \"better-auth\";\nimport { APIError } from \"better-auth/api\";\nimport type { PolarOptions } from \"../types\";\n\nexport const 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 = 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\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\t...params,\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\tconst customer = await 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\n\t\t\t\t\tconsole.log(customer);\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, 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\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\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 { 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}\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}),\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tconst session = await getSessionFromCtx(ctx);\n\n\t\t\t\t\tlet productIds: string[] = [];\n\n\t\t\t\t\tif (ctx.body.slug) {\n\t\t\t\t\t\tconst resolvedProducts = await (typeof checkoutOptions.products ===\n\t\t\t\t\t\t\"function\"\n\t\t\t\t\t\t\t? checkoutOptions.products()\n\t\t\t\t\t\t\t: checkoutOptions.products);\n\n\t\t\t\t\t\tconst productId = resolvedProducts?.find(\n\t\t\t\t\t\t\t(product) => product.slug === ctx.body.slug,\n\t\t\t\t\t\t)?.productId;\n\n\t\t\t\t\t\tif (!productId) {\n\t\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\t\tmessage: \"Product not found\",\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tproductIds = [productId];\n\t\t\t\t\t} else {\n\t\t\t\t\t\tproductIds = Array.isArray(ctx.body.products)\n\t\t\t\t\t\t\t? ctx.body.products.filter((id) => id !== undefined)\n\t\t\t\t\t\t\t: [ctx.body.products].filter((id) => id !== undefined);\n\t\t\t\t\t}\n\n\t\t\t\t\tif (checkoutOptions.authenticatedUsersOnly && !session?.user.id) {\n\t\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\t\tmessage: \"You must be logged in to checkout\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst checkout = await polar.checkouts.create({\n\t\t\t\t\t\t\texternalCustomerId: session?.user.id,\n\t\t\t\t\t\t\tproducts: productIds,\n\t\t\t\t\t\t\tsuccessUrl: checkoutOptions.successUrl\n\t\t\t\t\t\t\t\t? new URL(\n\t\t\t\t\t\t\t\t\t\tcheckoutOptions.successUrl,\n\t\t\t\t\t\t\t\t\t\tctx.request?.url,\n\t\t\t\t\t\t\t\t\t).toString()\n\t\t\t\t\t\t\t\t: undefined,\n\t\t\t\t\t\t\tmetadata: ctx.body.referenceId\n\t\t\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\t\t\treferenceId: ctx.body.referenceId,\n\t\t\t\t\t\t\t\t\t\t...ctx.body.metadata,\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t: ctx.body.metadata,\n\t\t\t\t\t\t\tcustomFieldData: ctx.body.customFieldData,\n\t\t\t\t\t\t});\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.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 }\n switch (payload.type) {\n case \"benefit_grant.created\":\n case \"benefit_grant.revoked\":\n if (entitlements) {\n for (const handler of entitlements.handlers) {\n promises.push(handler(payload));\n }\n }\n }\n return Promise.all(promises);\n};\n\n// src/entitlement/entitlement.ts\nvar EntitlementStrategy = class {\n grantCallbacks = [];\n revokeCallbacks = [];\n grant(callback) {\n this.grantCallbacks.push(callback);\n return this;\n }\n revoke(callback) {\n this.revokeCallbacks.push(callback);\n return this;\n }\n handler(slug) {\n return async (payload) => {\n if (payload.data.benefit.description === slug) {\n switch (payload.type) {\n case \"benefit_grant.created\":\n await Promise.all(\n this.grantCallbacks.map(\n (callback) => callback({\n customer: payload.data.customer,\n properties: payload.data.properties,\n payload\n })\n )\n );\n break;\n case \"benefit_grant.revoked\":\n await Promise.all(\n this.revokeCallbacks.map(\n (callback) => callback({\n customer: payload.data.customer,\n properties: payload.data.properties,\n payload\n })\n )\n );\n break;\n }\n }\n };\n }\n};\nvar Entitlements = class {\n static handlers = [];\n static use(slug, strategy) {\n this.handlers.push(strategy.handler(slug));\n return this;\n }\n};\nexport {\n EntitlementStrategy,\n Entitlements,\n handleWebhookPayload\n};\n","import { handleWebhookPayload } from \"@polar-sh/adapter-utils\";\nimport type { Polar } from \"@polar-sh/sdk\";\nimport type { WebhookBenefitCreatedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitcreatedpayload.js\";\nimport type { WebhookBenefitGrantCreatedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitgrantcreatedpayload.js\";\nimport type { WebhookBenefitGrantRevokedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitgrantrevokedpayload.js\";\nimport type { WebhookBenefitGrantUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitgrantupdatedpayload.js\";\nimport type { WebhookBenefitUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitupdatedpayload.js\";\nimport type { WebhookCheckoutCreatedPayload } from \"@polar-sh/sdk/models/components/webhookcheckoutcreatedpayload.js\";\nimport type { WebhookCheckoutUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookcheckoutupdatedpayload.js\";\nimport type { WebhookCustomerCreatedPayload } from \"@polar-sh/sdk/models/components/webhookcustomercreatedpayload.js\";\nimport type { WebhookCustomerDeletedPayload } from \"@polar-sh/sdk/models/components/webhookcustomerdeletedpayload.js\";\nimport type { WebhookCustomerStateChangedPayload } from \"@polar-sh/sdk/models/components/webhookcustomerstatechangedpayload.js\";\nimport type { WebhookCustomerUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookcustomerupdatedpayload.js\";\nimport type { WebhookOrderCreatedPayload } from \"@polar-sh/sdk/models/components/webhookordercreatedpayload.js\";\nimport type { WebhookOrderPaidPayload } from \"@polar-sh/sdk/models/components/webhookorderpaidpayload.js\";\nimport type { WebhookOrderRefundedPayload } from \"@polar-sh/sdk/models/components/webhookorderrefundedpayload.js\";\nimport type { WebhookOrganizationUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookorganizationupdatedpayload.js\";\nimport type { WebhookProductCreatedPayload } from \"@polar-sh/sdk/models/components/webhookproductcreatedpayload.js\";\nimport type { WebhookProductUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookproductupdatedpayload.js\";\nimport type { WebhookRefundCreatedPayload } from \"@polar-sh/sdk/models/components/webhookrefundcreatedpayload.js\";\nimport type { WebhookRefundUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookrefundupdatedpayload.js\";\nimport type { WebhookSubscriptionActivePayload } from \"@polar-sh/sdk/models/components/webhooksubscriptionactivepayload.js\";\nimport type { WebhookSubscriptionCanceledPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptioncanceledpayload.js\";\nimport type { WebhookSubscriptionCreatedPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptioncreatedpayload.js\";\nimport type { WebhookSubscriptionRevokedPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptionrevokedpayload.js\";\nimport type { WebhookSubscriptionUncanceledPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptionuncanceledpayload.js\";\nimport type { WebhookSubscriptionUpdatedPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptionupdatedpayload.js\";\nimport { validateEvent } from \"@polar-sh/sdk/webhooks.js\";\nimport { APIError, createAuthEndpoint } from \"better-auth/api\";\n\nexport interface WebhooksOptions {\n\t/**\n\t * Webhook Secret\n\t */\n\tsecret: string;\n\t/**\n\t * Generic handler for all webhooks\n\t */\n\tonPayload?: (payload: ReturnType<typeof validateEvent>) => Promise<void>;\n\t/**\n\t * Webhook for checkout created\n\t */\n\tonCheckoutCreated?: (payload: WebhookCheckoutCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for checkout updated\n\t */\n\tonCheckoutUpdated?: (payload: WebhookCheckoutUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for order created\n\t */\n\tonOrderCreated?: (payload: WebhookOrderCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for order refunded\n\t */\n\tonOrderRefunded?: (payload: WebhookOrderRefundedPayload) => Promise<void>;\n\t/**\n\t * Webhook for order paid\n\t */\n\tonOrderPaid?: (payload: WebhookOrderPaidPayload) => Promise<void>;\n\t/**\n\t * Webhook for refund created\n\t */\n\tonRefundCreated?: (payload: WebhookRefundCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for refund updated\n\t */\n\tonRefundUpdated?: (payload: WebhookRefundUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for subscription created\n\t */\n\tonSubscriptionCreated?: (\n\t\tpayload: WebhookSubscriptionCreatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription updated\n\t */\n\tonSubscriptionUpdated?: (\n\t\tpayload: WebhookSubscriptionUpdatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription active\n\t */\n\tonSubscriptionActive?: (\n\t\tpayload: WebhookSubscriptionActivePayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription canceled\n\t */\n\tonSubscriptionCanceled?: (\n\t\tpayload: WebhookSubscriptionCanceledPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription revoked\n\t */\n\tonSubscriptionRevoked?: (\n\t\tpayload: WebhookSubscriptionRevokedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription uncanceled\n\t */\n\tonSubscriptionUncanceled?: (\n\t\tpayload: WebhookSubscriptionUncanceledPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for product created\n\t */\n\tonProductCreated?: (payload: WebhookProductCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for product updated\n\t */\n\tonProductUpdated?: (payload: WebhookProductUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for organization updated\n\t */\n\tonOrganizationUpdated?: (\n\t\tpayload: WebhookOrganizationUpdatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for benefit created\n\t */\n\tonBenefitCreated?: (payload: WebhookBenefitCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for benefit updated\n\t */\n\tonBenefitUpdated?: (payload: WebhookBenefitUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for benefit grant created\n\t */\n\tonBenefitGrantCreated?: (\n\t\tpayload: WebhookBenefitGrantCreatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for benefit grant updated\n\t */\n\tonBenefitGrantUpdated?: (\n\t\tpayload: WebhookBenefitGrantUpdatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for benefit grant revoked\n\t */\n\tonBenefitGrantRevoked?: (\n\t\tpayload: WebhookBenefitGrantRevokedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for customer created\n\t */\n\tonCustomerCreated?: (payload: WebhookCustomerCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for customer updated\n\t */\n\tonCustomerUpdated?: (payload: WebhookCustomerUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for customer deleted\n\t */\n\tonCustomerDeleted?: (payload: WebhookCustomerDeletedPayload) => Promise<void>;\n\t/**\n\t * Webhook for customer state changed\n\t */\n\tonCustomerStateChanged?: (\n\t\tpayload: WebhookCustomerStateChangedPayload,\n\t) => Promise<void>;\n}\n\nexport const webhooks = (options: WebhooksOptions) => (_polar: Polar) => {\n\treturn {\n\t\tpolarWebhooks: createAuthEndpoint(\n\t\t\t\"/polar/webhooks\",\n\t\t\t{\n\t\t\t\tmethod: \"POST\",\n\t\t\t\tmetadata: {\n\t\t\t\t\tisAction: false,\n\t\t\t\t},\n\t\t\t\tcloneRequest: true,\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tconst { secret, ...eventHandlers } = options;\n\n\t\t\t\tif (!ctx.request?.body) {\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\");\n\t\t\t\t}\n\t\t\t\tconst buf = await ctx.request.text();\n\t\t\t\tlet event: ReturnType<typeof validateEvent>;\n\t\t\t\ttry {\n\t\t\t\t\tif (!secret) {\n\t\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\t\tmessage: \"Polar webhook secret not found\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\tconst headers = {\n\t\t\t\t\t\t\"webhook-id\": ctx.request.headers.get(\"webhook-id\") as string,\n\t\t\t\t\t\t\"webhook-timestamp\": ctx.request.headers.get(\n\t\t\t\t\t\t\t\"webhook-timestamp\",\n\t\t\t\t\t\t) as string,\n\t\t\t\t\t\t\"webhook-signature\": ctx.request.headers.get(\n\t\t\t\t\t\t\t\"webhook-signature\",\n\t\t\t\t\t\t) as string,\n\t\t\t\t\t};\n\n\t\t\t\t\tevent = validateEvent(buf, headers, secret);\n\t\t\t\t} catch (err: unknown) {\n\t\t\t\t\tif (err instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(`${err.message}`);\n\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\tmessage: `Webhook Error: ${err.message}`,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: `Webhook Error: ${err}`,\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tawait handleWebhookPayload(event, {\n\t\t\t\t\t\twebhookSecret: secret,\n\t\t\t\t\t\t...eventHandlers,\n\t\t\t\t\t});\n\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t`Polar webhook failed. Error: ${e.message}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t} else {\n\t\t\t\t\t\tctx.context.logger.error(`Polar webhook failed. Error: ${e}`);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"Webhook error: See server logs for more information.\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\treturn ctx.json({ received: true });\n\t\t\t},\n\t\t),\n\t};\n};\n","import type { BetterAuthPlugin } from \"better-auth\";\nimport { onUserCreate, onUserUpdate } 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\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":";AACA,SAAS,gBAAgB;AAGlB,IAAM,eACZ,CAAC,YACD,OAAO,MAAY,QAAiC;AACnD,MAAI,OAAO,QAAQ,wBAAwB;AAC1C,QAAI;AACH,YAAM,SAA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AAA,cAC5B;AAAA,YACD;AAAA,UACD;AAAA,QACD;AAAA,MACD;AAAA,IACD;AAAA,EACD;AACD;","names":["APIError","polar","APIError","createAuthEndpoint","z","polar","checkout","APIError","createAuthEndpoint","sessionMiddleware","z","polar","APIError","createAuthEndpoint"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polar-sh/better-auth",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "Polar integration for better-auth",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
@@ -35,7 +35,8 @@
35
35
  "@types/node": "^20.0.0",
36
36
  "better-auth": "^1.2.7",
37
37
  "tsup": "^8.3.5",
38
- "vitest": "^2.1.8"
38
+ "vitest": "^2.1.8",
39
+ "@polar-sh/adapter-utils": "0.2.1"
39
40
  },
40
41
  "dependencies": {
41
42
  "zod": "^3.24.2"