@polar-sh/better-auth 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -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
@@ -313,7 +319,7 @@ So in order to figure out if a user should have access, pass the user's organiza
313
319
  ```typescript
314
320
  const organizationId = (await authClient.organization.list())?.data?.[0]?.id,
315
321
 
316
- const { data: subscriptions } = await authClient.customer.orders.list({
322
+ const { data: subscriptions } = await authClient.customer.subscriptions.list({
317
323
  query: {
318
324
  page: 1,
319
325
  limit: 10,
package/dist/index.cjs CHANGED
@@ -488,6 +488,136 @@ var usage = (_usageOptions) => (polar2) => {
488
488
  };
489
489
  };
490
490
 
491
+ // ../adapter-utils/dist/index.js
492
+ var handleWebhookPayload = async (payload, { webhookSecret, entitlements, onPayload, ...eventHandlers }) => {
493
+ const promises = [];
494
+ if (onPayload) {
495
+ promises.push(onPayload(payload));
496
+ }
497
+ switch (payload.type) {
498
+ case "checkout.created":
499
+ if (eventHandlers.onCheckoutCreated) {
500
+ promises.push(eventHandlers.onCheckoutCreated(payload));
501
+ }
502
+ break;
503
+ case "checkout.updated":
504
+ if (eventHandlers.onCheckoutUpdated) {
505
+ promises.push(eventHandlers.onCheckoutUpdated(payload));
506
+ }
507
+ break;
508
+ case "order.created":
509
+ if (eventHandlers.onOrderCreated) {
510
+ promises.push(eventHandlers.onOrderCreated(payload));
511
+ }
512
+ break;
513
+ case "order.updated":
514
+ if (eventHandlers.onOrderUpdated) {
515
+ promises.push(eventHandlers.onOrderUpdated(payload));
516
+ }
517
+ break;
518
+ case "order.paid":
519
+ if (eventHandlers.onOrderPaid) {
520
+ promises.push(eventHandlers.onOrderPaid(payload));
521
+ }
522
+ break;
523
+ case "subscription.created":
524
+ if (eventHandlers.onSubscriptionCreated) {
525
+ promises.push(eventHandlers.onSubscriptionCreated(payload));
526
+ }
527
+ break;
528
+ case "subscription.updated":
529
+ if (eventHandlers.onSubscriptionUpdated) {
530
+ promises.push(eventHandlers.onSubscriptionUpdated(payload));
531
+ }
532
+ break;
533
+ case "subscription.active":
534
+ if (eventHandlers.onSubscriptionActive) {
535
+ promises.push(eventHandlers.onSubscriptionActive(payload));
536
+ }
537
+ break;
538
+ case "subscription.canceled":
539
+ if (eventHandlers.onSubscriptionCanceled) {
540
+ promises.push(eventHandlers.onSubscriptionCanceled(payload));
541
+ }
542
+ break;
543
+ case "subscription.revoked":
544
+ if (eventHandlers.onSubscriptionRevoked) {
545
+ promises.push(eventHandlers.onSubscriptionRevoked(payload));
546
+ }
547
+ break;
548
+ case "product.created":
549
+ if (eventHandlers.onProductCreated) {
550
+ promises.push(eventHandlers.onProductCreated(payload));
551
+ }
552
+ break;
553
+ case "product.updated":
554
+ if (eventHandlers.onProductUpdated) {
555
+ promises.push(eventHandlers.onProductUpdated(payload));
556
+ }
557
+ break;
558
+ case "organization.updated":
559
+ if (eventHandlers.onOrganizationUpdated) {
560
+ promises.push(eventHandlers.onOrganizationUpdated(payload));
561
+ }
562
+ break;
563
+ case "benefit.created":
564
+ if (eventHandlers.onBenefitCreated) {
565
+ promises.push(eventHandlers.onBenefitCreated(payload));
566
+ }
567
+ break;
568
+ case "benefit.updated":
569
+ if (eventHandlers.onBenefitUpdated) {
570
+ promises.push(eventHandlers.onBenefitUpdated(payload));
571
+ }
572
+ break;
573
+ case "benefit_grant.created":
574
+ if (eventHandlers.onBenefitGrantCreated) {
575
+ promises.push(eventHandlers.onBenefitGrantCreated(payload));
576
+ }
577
+ break;
578
+ case "benefit_grant.updated":
579
+ if (eventHandlers.onBenefitGrantUpdated) {
580
+ promises.push(eventHandlers.onBenefitGrantUpdated(payload));
581
+ }
582
+ break;
583
+ case "benefit_grant.revoked":
584
+ if (eventHandlers.onBenefitGrantRevoked) {
585
+ promises.push(eventHandlers.onBenefitGrantRevoked(payload));
586
+ }
587
+ break;
588
+ case "customer.created":
589
+ if (eventHandlers.onCustomerCreated) {
590
+ promises.push(eventHandlers.onCustomerCreated(payload));
591
+ }
592
+ break;
593
+ case "customer.updated":
594
+ if (eventHandlers.onCustomerUpdated) {
595
+ promises.push(eventHandlers.onCustomerUpdated(payload));
596
+ }
597
+ break;
598
+ case "customer.deleted":
599
+ if (eventHandlers.onCustomerDeleted) {
600
+ promises.push(eventHandlers.onCustomerDeleted(payload));
601
+ }
602
+ break;
603
+ case "customer.state_changed":
604
+ if (eventHandlers.onCustomerStateChanged) {
605
+ promises.push(eventHandlers.onCustomerStateChanged(payload));
606
+ }
607
+ break;
608
+ }
609
+ switch (payload.type) {
610
+ case "benefit_grant.created":
611
+ case "benefit_grant.revoked":
612
+ if (entitlements) {
613
+ for (const handler of entitlements.handlers) {
614
+ promises.push(handler(payload));
615
+ }
616
+ }
617
+ }
618
+ return Promise.all(promises);
619
+ };
620
+
491
621
  // src/plugins/webhooks.ts
492
622
  var import_webhooks = require("@polar-sh/sdk/webhooks.js");
493
623
  var import_api6 = require("better-auth/api");
@@ -503,35 +633,7 @@ var webhooks = (options) => (_polar) => {
503
633
  cloneRequest: true
504
634
  },
505
635
  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;
636
+ const { secret, ...eventHandlers } = options;
535
637
  if (!ctx.request?.body) {
536
638
  throw new import_api6.APIError("INTERNAL_SERVER_ERROR");
537
639
  }
@@ -565,136 +667,10 @@ var webhooks = (options) => (_polar) => {
565
667
  });
566
668
  }
567
669
  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
- }
670
+ await handleWebhookPayload(event, {
671
+ webhookSecret: secret,
672
+ ...eventHandlers
673
+ });
698
674
  } catch (e) {
699
675
  if (e instanceof Error) {
700
676
  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,YAAI,iBAAiB,eAAe,KAAK,IAAI;AAC5C,gBAAM,QAAQ,OAAO,UAAU,OAAO;AAAA,YACrC,IAAI,iBAAiB;AAAA,YACrB,gBAAgB;AAAA,cACf,YAAY,KAAK;AAAA,cACjB,GAAG;AAAA,YACJ;AAAA,UACD,CAAC;AAAA,QACF;AAAA,MACD,OAAO;AACN,cAAM,WAAW,MAAM,QAAQ,OAAO,UAAU,OAAO;AAAA,UACtD,GAAG;AAAA,UACH,OAAO,KAAK;AAAA,UACZ,MAAM,KAAK;AAAA,UACX,YAAY,KAAK;AAAA,QAClB,CAAC;AAED,gBAAQ,IAAI,QAAQ;AAAA,MACrB;AAAA,IACD,SAAS,GAAY;AACpB,UAAI,aAAa,OAAO;AACvB,cAAM,IAAI,oBAAS,yBAAyB;AAAA,UAC3C,SAAS,0CAA0C,EAAE,OAAO;AAAA,QAC7D,CAAC;AAAA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D,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;;;ANzXO,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"]}
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}\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","// 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,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,OAAO,UAAU,OAAO;AAAA,YACrC,IAAI,iBAAiB;AAAA,YACrB,gBAAgB;AAAA,cACf,YAAY,KAAK;AAAA,cACjB,GAAG;AAAA,YACJ;AAAA,UACD,CAAC;AAAA,QACF;AAAA,MACD,OAAO;AACN,cAAM,WAAW,MAAM,QAAQ,OAAO,UAAU,OAAO;AAAA,UACtD,GAAG;AAAA,UACH,OAAO,KAAK;AAAA,UACZ,MAAM,KAAK;AAAA,UACX,YAAY,KAAK;AAAA,QAClB,CAAC;AAED,gBAAQ,IAAI,QAAQ;AAAA,MACrB;AAAA,IACD,SAAS,GAAY;AACpB,UAAI,aAAa,OAAO;AACvB,cAAM,IAAI,oBAAS,yBAAyB;AAAA,UAC3C,SAAS,0CAA0C,EAAE,OAAO;AAAA,QAC7D,CAAC;AAAA,MACF;A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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.js CHANGED
@@ -461,6 +461,136 @@ var usage = (_usageOptions) => (polar2) => {
461
461
  };
462
462
  };
463
463
 
464
+ // ../adapter-utils/dist/index.js
465
+ var handleWebhookPayload = async (payload, { webhookSecret, entitlements, onPayload, ...eventHandlers }) => {
466
+ const promises = [];
467
+ if (onPayload) {
468
+ promises.push(onPayload(payload));
469
+ }
470
+ switch (payload.type) {
471
+ case "checkout.created":
472
+ if (eventHandlers.onCheckoutCreated) {
473
+ promises.push(eventHandlers.onCheckoutCreated(payload));
474
+ }
475
+ break;
476
+ case "checkout.updated":
477
+ if (eventHandlers.onCheckoutUpdated) {
478
+ promises.push(eventHandlers.onCheckoutUpdated(payload));
479
+ }
480
+ break;
481
+ case "order.created":
482
+ if (eventHandlers.onOrderCreated) {
483
+ promises.push(eventHandlers.onOrderCreated(payload));
484
+ }
485
+ break;
486
+ case "order.updated":
487
+ if (eventHandlers.onOrderUpdated) {
488
+ promises.push(eventHandlers.onOrderUpdated(payload));
489
+ }
490
+ break;
491
+ case "order.paid":
492
+ if (eventHandlers.onOrderPaid) {
493
+ promises.push(eventHandlers.onOrderPaid(payload));
494
+ }
495
+ break;
496
+ case "subscription.created":
497
+ if (eventHandlers.onSubscriptionCreated) {
498
+ promises.push(eventHandlers.onSubscriptionCreated(payload));
499
+ }
500
+ break;
501
+ case "subscription.updated":
502
+ if (eventHandlers.onSubscriptionUpdated) {
503
+ promises.push(eventHandlers.onSubscriptionUpdated(payload));
504
+ }
505
+ break;
506
+ case "subscription.active":
507
+ if (eventHandlers.onSubscriptionActive) {
508
+ promises.push(eventHandlers.onSubscriptionActive(payload));
509
+ }
510
+ break;
511
+ case "subscription.canceled":
512
+ if (eventHandlers.onSubscriptionCanceled) {
513
+ promises.push(eventHandlers.onSubscriptionCanceled(payload));
514
+ }
515
+ break;
516
+ case "subscription.revoked":
517
+ if (eventHandlers.onSubscriptionRevoked) {
518
+ promises.push(eventHandlers.onSubscriptionRevoked(payload));
519
+ }
520
+ break;
521
+ case "product.created":
522
+ if (eventHandlers.onProductCreated) {
523
+ promises.push(eventHandlers.onProductCreated(payload));
524
+ }
525
+ break;
526
+ case "product.updated":
527
+ if (eventHandlers.onProductUpdated) {
528
+ promises.push(eventHandlers.onProductUpdated(payload));
529
+ }
530
+ break;
531
+ case "organization.updated":
532
+ if (eventHandlers.onOrganizationUpdated) {
533
+ promises.push(eventHandlers.onOrganizationUpdated(payload));
534
+ }
535
+ break;
536
+ case "benefit.created":
537
+ if (eventHandlers.onBenefitCreated) {
538
+ promises.push(eventHandlers.onBenefitCreated(payload));
539
+ }
540
+ break;
541
+ case "benefit.updated":
542
+ if (eventHandlers.onBenefitUpdated) {
543
+ promises.push(eventHandlers.onBenefitUpdated(payload));
544
+ }
545
+ break;
546
+ case "benefit_grant.created":
547
+ if (eventHandlers.onBenefitGrantCreated) {
548
+ promises.push(eventHandlers.onBenefitGrantCreated(payload));
549
+ }
550
+ break;
551
+ case "benefit_grant.updated":
552
+ if (eventHandlers.onBenefitGrantUpdated) {
553
+ promises.push(eventHandlers.onBenefitGrantUpdated(payload));
554
+ }
555
+ break;
556
+ case "benefit_grant.revoked":
557
+ if (eventHandlers.onBenefitGrantRevoked) {
558
+ promises.push(eventHandlers.onBenefitGrantRevoked(payload));
559
+ }
560
+ break;
561
+ case "customer.created":
562
+ if (eventHandlers.onCustomerCreated) {
563
+ promises.push(eventHandlers.onCustomerCreated(payload));
564
+ }
565
+ break;
566
+ case "customer.updated":
567
+ if (eventHandlers.onCustomerUpdated) {
568
+ promises.push(eventHandlers.onCustomerUpdated(payload));
569
+ }
570
+ break;
571
+ case "customer.deleted":
572
+ if (eventHandlers.onCustomerDeleted) {
573
+ promises.push(eventHandlers.onCustomerDeleted(payload));
574
+ }
575
+ break;
576
+ case "customer.state_changed":
577
+ if (eventHandlers.onCustomerStateChanged) {
578
+ promises.push(eventHandlers.onCustomerStateChanged(payload));
579
+ }
580
+ break;
581
+ }
582
+ switch (payload.type) {
583
+ case "benefit_grant.created":
584
+ case "benefit_grant.revoked":
585
+ if (entitlements) {
586
+ for (const handler of entitlements.handlers) {
587
+ promises.push(handler(payload));
588
+ }
589
+ }
590
+ }
591
+ return Promise.all(promises);
592
+ };
593
+
464
594
  // src/plugins/webhooks.ts
465
595
  import { validateEvent } from "@polar-sh/sdk/webhooks.js";
466
596
  import { APIError as APIError5, createAuthEndpoint as createAuthEndpoint4 } from "better-auth/api";
@@ -476,35 +606,7 @@ var webhooks = (options) => (_polar) => {
476
606
  cloneRequest: true
477
607
  },
478
608
  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;
609
+ const { secret, ...eventHandlers } = options;
508
610
  if (!ctx.request?.body) {
509
611
  throw new APIError5("INTERNAL_SERVER_ERROR");
510
612
  }
@@ -538,136 +640,10 @@ var webhooks = (options) => (_polar) => {
538
640
  });
539
641
  }
540
642
  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
- }
643
+ await handleWebhookPayload(event, {
644
+ webhookSecret: secret,
645
+ ...eventHandlers
646
+ });
671
647
  } catch (e) {
672
648
  if (e instanceof Error) {
673
649
  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,OAAO,UAAU,OAAO;AAAA,YACrC,IAAI,iBAAiB;AAAA,YACrB,gBAAgB;AAAA,cACf,YAAY,KAAK;AAAA,cACjB,GAAG;AAAA,YACJ;AAAA,UACD,CAAC;AAAA,QACF;AAAA,MACD,OAAO;AACN,cAAM,WAAW,MAAM,QAAQ,OAAO,UAAU,OAAO;AAAA,UACtD,GAAG;AAAA,UACH,OAAO,KAAK;AAAA,UACZ,MAAM,KAAK;AAAA,UACX,YAAY,KAAK;AAAA,QAClB,CAAC;AAED,gBAAQ,IAAI,QAAQ;AAAA,MACrB;AAAA,IACD,SAAS,GAAY;AACpB,UAAI,aAAa,OAAO;AACvB,cAAM,IAAI,SAAS,yBAAyB;AAAA,UAC3C,SA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kB,KAAK;AAAA,cACxB;AACA;AAAA,YACD,KAAK;AACJ,kBAAI,mBAAmB;AACtB,kCAAkB,KAAK;AAAA,cACxB;AACA;AAAA,YACD,KAAK;AACJ,kBAAI,wBAAwB;AAC3B,uCAAuB,KAAK;AAAA,cAC7B;AACA;AAAA,UACF;AAAA,QACD,SAAS,GAAY;AACpB,cAAI,aAAa,OAAO;AACvB,gBAAI,QAAQ,OAAO;AAAA,cAClB,gCAAgC,EAAE,OAAO;AAAA,YAC1C;AAAA,UACD,OAAO;AACN,gBAAI,QAAQ,OAAO,MAAM,gCAAgC,CAAC,EAAE;AAAA,UAC7D;AAEA,gBAAM,IAAIA,UAAS,eAAe;AAAA,YACjC,SAAS;AAAA,UACV,CAAC;AAAA,QACF;AAEA,eAAO,IAAI,KAAK,EAAE,UAAU,KAAK,CAAC;AAAA,MACnC;AAAA,IACD;AAAA,EACD;AACD;;;ACzXO,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":["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}\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","// 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,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,OAAO,UAAU,OAAO;AAAA,YACrC,IAAI,iBAAiB;AAAA,YACrB,gBAAgB;AAAA,cACf,YAAY,KAAK;AAAA,cACjB,GAAG;AAAA,YACJ;AAAA,UACD,CAAC;AAAA,QACF;AAAA,MACD,OAAO;AACN,cAAM,WAAW,MAAM,QAAQ,OAAO,UAAU,OAAO;AAAA,UACtD,GAAG;AAAA,UACH,OAAO,KAAK;AAAA,UACZ,MAAM,KAAK;AAAA,UACX,YAAY,KAAK;AAAA,QAClB,CAAC;AAED,gBAAQ,IAAI,QAAQ;AAAA,MACrB;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AAM,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":["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.3",
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"