@dodopayments/better-auth 1.4.1 → 1.4.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.
Files changed (47) hide show
  1. package/dist/{chunk-K42Z6SPJ.js → chunk-65YIVTFE.js} +2 -3
  2. package/dist/chunk-65YIVTFE.js.map +1 -0
  3. package/dist/{chunk-CRRND2EH.js → chunk-BII7QAPD.js} +4 -9
  4. package/dist/chunk-BII7QAPD.js.map +1 -0
  5. package/dist/{chunk-PXI4EHZC.js → chunk-HZNE63HR.js} +1 -1
  6. package/dist/chunk-HZNE63HR.js.map +1 -0
  7. package/dist/{chunk-J55PDPLE.js → chunk-KXU6PYZF.js} +2 -4
  8. package/dist/chunk-KXU6PYZF.js.map +1 -0
  9. package/dist/client.cjs.map +1 -1
  10. package/dist/client.d.cts +4 -5
  11. package/dist/client.d.ts +4 -5
  12. package/dist/client.js +1 -1
  13. package/dist/hooks/customer.cjs +3 -8
  14. package/dist/hooks/customer.cjs.map +1 -1
  15. package/dist/hooks/customer.d.cts +3 -4
  16. package/dist/hooks/customer.d.ts +3 -4
  17. package/dist/hooks/customer.js +1 -1
  18. package/dist/index.cjs +39 -47
  19. package/dist/index.cjs.map +1 -1
  20. package/dist/index.d.cts +690 -1056
  21. package/dist/index.d.ts +690 -1056
  22. package/dist/index.js +4 -4
  23. package/dist/plugins/checkout.cjs +2 -3
  24. package/dist/plugins/checkout.cjs.map +1 -1
  25. package/dist/plugins/checkout.d.cts +2 -3
  26. package/dist/plugins/checkout.d.ts +2 -3
  27. package/dist/plugins/checkout.js +1 -1
  28. package/dist/plugins/portal.cjs +6 -8
  29. package/dist/plugins/portal.cjs.map +1 -1
  30. package/dist/plugins/portal.d.cts +2 -3
  31. package/dist/plugins/portal.d.ts +2 -3
  32. package/dist/plugins/portal.js +1 -1
  33. package/dist/plugins/usage.d.cts +95 -165
  34. package/dist/plugins/usage.d.ts +95 -165
  35. package/dist/plugins/webhooks.d.cts +2 -3
  36. package/dist/plugins/webhooks.d.ts +2 -3
  37. package/dist/types-4JE4OwKb.d.cts +701 -0
  38. package/dist/types-B9rx1bt7.d.ts +701 -0
  39. package/dist/types.d.cts +1 -2
  40. package/dist/types.d.ts +1 -2
  41. package/package.json +3 -2
  42. package/dist/chunk-CRRND2EH.js.map +0 -1
  43. package/dist/chunk-J55PDPLE.js.map +0 -1
  44. package/dist/chunk-K42Z6SPJ.js.map +0 -1
  45. package/dist/chunk-PXI4EHZC.js.map +0 -1
  46. package/dist/types-DtNvVoO9.d.cts +0 -996
  47. package/dist/types-VW2PljjI.d.ts +0 -996
@@ -1,6 +1,5 @@
1
1
  // src/plugins/checkout.ts
2
- import { APIError, getSessionFromCtx } from "better-auth/api";
3
- import { createAuthEndpoint } from "better-auth/plugins";
2
+ import { APIError, createAuthEndpoint, getSessionFromCtx } from "better-auth/api";
4
3
  import { z } from "zod/v3";
5
4
  import {
6
5
  buildCheckoutUrl,
@@ -173,4 +172,4 @@ var checkout = (checkoutOptions = {}) => (dodopayments) => {
173
172
  export {
174
173
  checkout
175
174
  };
176
- //# sourceMappingURL=chunk-K42Z6SPJ.js.map
175
+ //# sourceMappingURL=chunk-65YIVTFE.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/plugins/checkout.ts"],"sourcesContent":["import type DodoPayments from \"dodopayments\";\nimport { APIError, createAuthEndpoint, getSessionFromCtx } from \"better-auth/api\";\nimport { z } from \"zod/v3\";\nimport type { CreateCheckoutResponse, Product } from \"../types\";\nimport {\n buildCheckoutUrl,\n checkoutSessionPayloadSchema,\n dynamicCheckoutBodySchema,\n} from \"@dodopayments/core/checkout\";\n\nexport interface CheckoutOptions {\n /**\n * Optional list of slug -> productId mappings for easy slug checkouts\n */\n products?: Product[] | (() => Promise<Product[]>);\n /**\n * Checkout Success URL\n */\n successUrl?: string;\n /**\n * Only allow authenticated customers to checkout\n */\n authenticatedUsersOnly?: boolean;\n}\n\nexport const checkout =\n (checkoutOptions: CheckoutOptions = {}) =>\n (dodopayments: DodoPayments) => {\n return {\n /**\n * @deprecated\n */\n dodoCheckout: createAuthEndpoint(\n \"/dodopayments/checkout\",\n {\n method: \"POST\",\n body: dynamicCheckoutBodySchema.extend({\n slug: z.string().optional(),\n referenceId: z.string().optional(),\n }),\n requireRequest: true,\n },\n async (ctx): Promise<CreateCheckoutResponse> => {\n const session = await getSessionFromCtx(ctx);\n\n let dodoPaymentsProductId: string | undefined;\n\n if (ctx.body?.slug) {\n const resolvedProducts =\n typeof checkoutOptions.products === \"function\"\n ? await checkoutOptions.products()\n : checkoutOptions.products;\n\n const productId = resolvedProducts?.find(\n (product) => product.slug === ctx.body.slug,\n )?.productId;\n\n if (!productId) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"Product not found\",\n });\n }\n\n dodoPaymentsProductId = productId;\n } else {\n dodoPaymentsProductId = ctx.body.product_id;\n }\n\n if (checkoutOptions.authenticatedUsersOnly && !session?.user.id) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to checkout\",\n });\n }\n\n try {\n const checkoutUrl = await buildCheckoutUrl({\n body: {\n ...ctx.body,\n product_id: dodoPaymentsProductId,\n customer: {\n email: session?.user.email,\n name: session?.user.name,\n ...ctx.body.customer,\n },\n product_cart: dodoPaymentsProductId\n ? [\n {\n product_id: dodoPaymentsProductId,\n quantity: 1,\n },\n ]\n : undefined,\n metadata: ctx.body.referenceId\n ? {\n referenceId: ctx.body.referenceId,\n ...ctx.body.metadata,\n }\n : ctx.body.metadata,\n },\n bearerToken: dodopayments.bearerToken,\n environment: dodopayments.baseURL.includes(\"test\")\n ? \"test_mode\"\n : \"live_mode\",\n returnUrl: checkoutOptions.successUrl\n ? new URL(\n checkoutOptions.successUrl,\n ctx.request?.url,\n ).toString()\n : undefined,\n type: \"dynamic\",\n });\n\n const redirectUrl = new URL(checkoutUrl);\n\n return ctx.json({\n url: redirectUrl.toString(),\n redirect: true,\n });\n } catch (e: unknown) {\n if (e instanceof Error) {\n ctx.context.logger.error(\n `DodoPayments checkout creation failed. Error: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Checkout creation failed\",\n });\n }\n },\n ),\n dodoCheckoutSession: createAuthEndpoint(\n \"/dodopayments/checkout-session\",\n {\n method: \"POST\",\n body: checkoutSessionPayloadSchema\n .extend({\n slug: z.string().optional(),\n referenceId: z.string().optional(),\n })\n .partial({\n product_cart: true,\n }),\n requireRequest: true,\n },\n async (ctx): Promise<CreateCheckoutResponse> => {\n const session = await getSessionFromCtx(ctx);\n\n let dodoPaymentsProductId: string | undefined;\n\n if (ctx.body?.slug) {\n const resolvedProducts =\n typeof checkoutOptions.products === \"function\"\n ? await checkoutOptions.products()\n : checkoutOptions.products;\n\n const productId = resolvedProducts?.find(\n (product) => product.slug === ctx.body.slug,\n )?.productId;\n\n if (!productId) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"Product not found\",\n });\n }\n\n dodoPaymentsProductId = productId;\n }\n\n if (checkoutOptions.authenticatedUsersOnly && !session?.user.id) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to checkout\",\n });\n }\n\n // Ensure we have a product_cart\n const product_cart = dodoPaymentsProductId\n ? [{ product_id: dodoPaymentsProductId, quantity: 1 }]\n : ctx.body.product_cart;\n\n if (!product_cart || product_cart.length === 0) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"Neither product_cart nor slug was provided\",\n });\n }\n\n try {\n const checkoutUrl = await buildCheckoutUrl({\n sessionPayload: {\n ...ctx.body,\n product_cart,\n customer: session?.user.email\n ? {\n email: session?.user.email,\n name: session?.user.name,\n }\n : ctx.body.customer,\n metadata: ctx.body.referenceId\n ? {\n referenceId: ctx.body.referenceId,\n ...ctx.body.metadata,\n }\n : ctx.body.metadata,\n return_url: checkoutOptions.successUrl\n ? new URL(\n checkoutOptions.successUrl,\n ctx.request?.url,\n ).toString()\n : undefined,\n },\n bearerToken: dodopayments.bearerToken,\n environment: dodopayments.baseURL.includes(\"test\")\n ? \"test_mode\"\n : \"live_mode\",\n type: \"session\",\n });\n\n const redirectUrl = new URL(checkoutUrl);\n\n return ctx.json({\n url: redirectUrl.toString(),\n redirect: true,\n });\n } catch (e: unknown) {\n if (e instanceof Error) {\n ctx.context.logger.error(\n `DodoPayments checkout creation failed. Error: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Checkout session creation failed\",\n });\n }\n },\n ),\n };\n };\n"],"mappings":";AACA,SAAS,UAAU,oBAAoB,yBAAyB;AAChE,SAAS,SAAS;AAElB;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAiBA,IAAM,WACX,CAAC,kBAAmC,CAAC,MACrC,CAAC,iBAA+B;AAC9B,SAAO;AAAA;AAAA;AAAA;AAAA,IAIL,cAAc;AAAA,MACZ;AAAA,MACA;AAAA,QACE,QAAQ;AAAA,QACR,MAAM,0BAA0B,OAAO;AAAA,UACrC,MAAM,EAAE,OAAO,EAAE,SAAS;AAAA,UAC1B,aAAa,EAAE,OAAO,EAAE,SAAS;AAAA,QACnC,CAAC;AAAA,QACD,gBAAgB;AAAA,MAClB;AAAA,MACA,OAAO,QAAyC;AAC9C,cAAM,UAAU,MAAM,kBAAkB,GAAG;AAE3C,YAAI;AAEJ,YAAI,IAAI,MAAM,MAAM;AAClB,gBAAM,mBACJ,OAAO,gBAAgB,aAAa,aAChC,MAAM,gBAAgB,SAAS,IAC/B,gBAAgB;AAEtB,gBAAM,YAAY,kBAAkB;AAAA,YAClC,CAAC,YAAY,QAAQ,SAAS,IAAI,KAAK;AAAA,UACzC,GAAG;AAEH,cAAI,CAAC,WAAW;AACd,kBAAM,IAAI,SAAS,eAAe;AAAA,cAChC,SAAS;AAAA,YACX,CAAC;AAAA,UACH;AAEA,kCAAwB;AAAA,QAC1B,OAAO;AACL,kCAAwB,IAAI,KAAK;AAAA,QACnC;AAEA,YAAI,gBAAgB,0BAA0B,CAAC,SAAS,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tB,GAAG,IAAI,KAAK;AAAA,cACd,IACA,IAAI,KAAK;AAAA,cACb,YAAY,gBAAgB,aACxB,IAAI;AAAA,gBACF,gBAAgB;AAAA,gBAChB,IAAI,SAAS;AAAA,cACf,EAAE,SAAS,IACX;AAAA,YACN;AAAA,YACA,aAAa,aAAa;AAAA,YAC1B,aAAa,aAAa,QAAQ,SAAS,MAAM,IAC7C,cACA;AAAA,YACJ,MAAM;AAAA,UACR,CAAC;AAED,gBAAM,cAAc,IAAI,IAAI,WAAW;AAEvC,iBAAO,IAAI,KAAK;AAAA,YACd,KAAK,YAAY,SAAS;AAAA,YAC1B,UAAU;AAAA,UACZ,CAAC;AAAA,QACH,SAAS,GAAY;AACnB,cAAI,aAAa,OAAO;AACtB,gBAAI,QAAQ,OAAO;AAAA,cACjB,iDAAiD,EAAE,OAAO;AAAA,YAC5D;AAAA,UACF;AAEA,gBAAM,IAAI,SAAS,yBAAyB;AAAA,YAC1C,SAAS;AAAA,UACX,CAAC;AAAA,QACH;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;","names":[]}
@@ -8,14 +8,9 @@ var onUserCreate = (options) => async (user, ctx) => {
8
8
  });
9
9
  const existingCustomer = customers.items[0];
10
10
  if (existingCustomer) {
11
- if (existingCustomer.email !== user.email) {
12
- await options.client.customers.update(
13
- existingCustomer.customer_id,
14
- {
15
- name: user.name
16
- }
17
- );
18
- }
11
+ await options.client.customers.update(existingCustomer.customer_id, {
12
+ name: user.name
13
+ });
19
14
  } else {
20
15
  await options.client.customers.create({
21
16
  email: user.email,
@@ -64,4 +59,4 @@ export {
64
59
  onUserCreate,
65
60
  onUserUpdate
66
61
  };
67
- //# sourceMappingURL=chunk-CRRND2EH.js.map
62
+ //# sourceMappingURL=chunk-BII7QAPD.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/hooks/customer.ts"],"sourcesContent":["import type { GenericEndpointContext, User } from \"better-auth\";\nimport { APIError } from \"better-auth/api\";\nimport type { DodoPaymentsOptions } from \"../types\";\n\nexport const onUserCreate =\n (options: DodoPaymentsOptions) =>\n async (user: User, ctx: GenericEndpointContext | null) => {\n if (ctx && options.createCustomerOnSignUp) {\n try {\n const customers = await options.client.customers.list({\n email: user.email,\n });\n const existingCustomer = customers.items[0];\n\n if (existingCustomer) {\n await options.client.customers.update(existingCustomer.customer_id, {\n name: user.name,\n });\n } else {\n // TODO: Add metadata to customer object via\n // getCustomerCreateParams option when it becomes\n // available in the API\n await options.client.customers.create({\n email: user.email,\n name: user.name,\n });\n }\n } catch (e: unknown) {\n if (e instanceof Error) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: `DodoPayments customer creation failed. Error: ${e.message}`,\n });\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: `DodoPayments customer creation failed. Error: ${e}`,\n });\n }\n }\n };\n\nexport const onUserUpdate =\n (options: DodoPaymentsOptions) =>\n async (user: User, ctx: GenericEndpointContext | null) => {\n if (ctx && options.createCustomerOnSignUp) {\n try {\n const customers = await options.client.customers.list({\n email: user.email,\n });\n const existingCustomer = customers.items[0];\n\n if (existingCustomer) {\n // TODO: Add metadata to customer object via\n // getCustomerCreateParams option when it becomes\n // available in the API\n await options.client.customers.update(existingCustomer.customer_id, {\n name: user.name,\n });\n }\n } catch (e: unknown) {\n if (e instanceof Error) {\n ctx.context.logger.error(\n `DodoPayments customer update failed. Error: ${e.message}`,\n );\n } else {\n ctx.context.logger.error(\n `DodoPayments customer update failed. Error: ${e}`,\n );\n }\n }\n }\n };\n"],"mappings":";AACA,SAAS,gBAAgB;AAGlB,IAAM,eACX,CAAC,YACD,OAAO,MAAY,QAAuC;AACxD,MAAI,OAAO,QAAQ,wBAAwB;AACzC,QAAI;AACF,YAAM,YAAY,MAAM,QAAQ,OAAO,UAAU,KAAK;AAAA,QACpD,OAAO,KAAK;AAAA,MACd,CAAC;AACD,YAAM,mBAAmB,UAAU,MAAM,CAAC;AAE1C,UAAI,kBAAkB;AACpB,cAAM,QAAQ,OAAO,UAAU,OAAO,iBAAiB,aAAa;AAAA,UAClE,MAAM,KAAK;AAAA,QACb,CAAC;AAAA,MACH,OAAO;AAIL,cAAM,QAAQ,OAAO,UAAU,OAAO;AAAA,UACpC,OAAO,KAAK;AAAA,UACZ,MAAM,KAAK;AAAA,QACb,CAAC;AAAA,MACH;AAAA,IACF,SAAS,GAAY;AACnB,UAAI,aAAa,OAAO;AACtB,cAAM,IAAI,SAAS,yBAAyB;AAAA,UAC1C,SAAS,iDAAiD,EAAE,OAAO;AAAA,QACrE,CAAC;AAAA,MACH;AAEA,YAAM,IAAI,SAAS,yBAAyB;AAAA,QAC1C,SAAS,iDAAiD,CAAC;AAAA,MAC7D,CAAC;AAAA,IACH;AAAA,EACF;AACF;AAEK,IAAM,eACX,CAAC,YACD,OAAO,MAAY,QAAuC;AACxD,MAAI,OAAO,QAAQ,wBAAwB;AACzC,QAAI;AACF,YAAM,YAAY,MAAM,QAAQ,OAAO,UAAU,KAAK;AAAA,QACpD,OAAO,KAAK;AAAA,MACd,CAAC;AACD,YAAM,mBAAmB,UAAU,MAAM,CAAC;AAE1C,UAAI,kBAAkB;AAIpB,cAAM,QAAQ,OAAO,UAAU,OAAO,iBAAiB,aAAa;AAAA,UAClE,MAAM,KAAK;AAAA,QACb,CAAC;AAAA,MACH;AAAA,IACF,SAAS,GAAY;AACnB,UAAI,aAAa,OAAO;AACtB,YAAI,QAAQ,OAAO;AAAA,UACjB,+CAA+C,EAAE,OAAO;AAAA,QAC1D;AAAA,MACF,OAAO;AACL,YAAI,QAAQ,OAAO;AAAA,UACjB,+CAA+C,CAAC;AAAA,QAClD;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;","names":[]}
@@ -9,4 +9,4 @@ var dodopaymentsClient = () => {
9
9
  export {
10
10
  dodopaymentsClient
11
11
  };
12
- //# sourceMappingURL=chunk-PXI4EHZC.js.map
12
+ //# sourceMappingURL=chunk-HZNE63HR.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/client.ts"],"sourcesContent":["import type { BetterAuthClientPlugin } from \"better-auth\";\nimport type { dodopayments } from \"./index\";\n\nexport const dodopaymentsClient = () => {\n return {\n id: \"dodopayments-client\",\n $InferServerPlugin: {} as ReturnType<typeof dodopayments>,\n } satisfies BetterAuthClientPlugin;\n};\n\nexport type {\n Product,\n DodoPaymentsEndpoints,\n DodoPaymentsOptions,\n PaymentItems,\n SubscriptionItems,\n CustomerPortalResponse,\n CreateCheckoutResponse,\n WebhookResponse,\n} from \"./types\";\n"],"mappings":";AAGO,IAAM,qBAAqB,MAAM;AACtC,SAAO;AAAA,IACL,IAAI;AAAA,IACJ,oBAAoB,CAAC;AAAA,EACvB;AACF;","names":[]}
@@ -1,7 +1,5 @@
1
1
  // src/plugins/portal.ts
2
- import { APIError } from "better-auth/api";
3
- import { sessionMiddleware } from "better-auth/api";
4
- import { createAuthEndpoint } from "better-auth/plugins";
2
+ import { APIError, createAuthEndpoint, sessionMiddleware } from "better-auth/api";
5
3
  import { z } from "zod/v3";
6
4
  var portal = () => (dodopayments) => {
7
5
  return {
@@ -193,4 +191,4 @@ async function createCustomer(dodopayments, email, name) {
193
191
  export {
194
192
  portal
195
193
  };
196
- //# sourceMappingURL=chunk-J55PDPLE.js.map
194
+ //# sourceMappingURL=chunk-KXU6PYZF.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/plugins/portal.ts"],"sourcesContent":["import type { DodoPayments } from \"dodopayments\";\nimport { APIError, createAuthEndpoint, sessionMiddleware } from \"better-auth/api\";\nimport { z } from \"zod/v3\";\nimport {\n CustomerPortalResponse,\n PaymentItems,\n SubscriptionItems,\n} from \"../types\";\n\nexport const portal = () => (dodopayments: DodoPayments) => {\n return {\n dodoPortal: createAuthEndpoint(\n \"/dodopayments/customer/portal\",\n {\n method: \"GET\",\n use: [sessionMiddleware],\n },\n async (ctx): Promise<CustomerPortalResponse> => {\n if (!ctx.context.session?.user.id) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"User not found\",\n });\n }\n\n if (!ctx.context.session?.user.emailVerified) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"User email not verified\",\n });\n }\n\n try {\n const customers = await dodopayments.customers.list({\n email: ctx.context.session?.user.email,\n });\n let customer = customers.items[0];\n\n if (!customer) {\n // upsert the customer, if they don't exist in DodoPayments\n customer = await createCustomer(\n dodopayments,\n ctx.context.session.user.email,\n ctx.context.session.user.name,\n );\n }\n\n const customerSession =\n await dodopayments.customers.customerPortal.create(\n customer.customer_id,\n );\n\n return ctx.json({\n url: customerSession.link,\n redirect: true,\n });\n } catch (e: unknown) {\n if (e instanceof Error) {\n ctx.context.logger.error(\n `DodoPayments customer portal creation failed. Error: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Customer portal creation failed\",\n });\n }\n },\n ),\n dodoSubscriptions: createAuthEndpoint(\n \"/dodopayments/customer/subscriptions/list\",\n {\n method: \"GET\",\n query: z\n .object({\n page: z.coerce.number().optional(),\n limit: z.coerce.number().optional(),\n status: z\n .enum([\n \"active\",\n \"cancelled\",\n \"on_hold\",\n \"pending\",\n \"failed\",\n \"expired\",\n ])\n .optional(),\n })\n .optional(),\n use: [sessionMiddleware],\n },\n async (ctx): Promise<SubscriptionItems> => {\n if (!ctx.context.session.user.id) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"User not found\",\n });\n }\n\n if (!ctx.context.session?.user.emailVerified) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"User email not verified\",\n });\n }\n\n try {\n const customers = await dodopayments.customers.list({\n email: ctx.context.session?.user.email,\n });\n let customer = customers.items[0];\n\n if (!customer) {\n // upsert the customer, if they don't exist in DodoPayments\n customer = await createCustomer(\n dodopayments,\n ctx.context.session.user.email,\n ctx.context.session.user.name,\n );\n }\n\n const subscriptions = await dodopayments.subscriptions.list({\n customer_id: customer.customer_id,\n // page number is 0-indexed\n page_number: ctx.query?.page ? ctx.query.page - 1 : undefined,\n page_size: ctx.query?.limit,\n status: ctx.query?.status,\n });\n\n return ctx.json({ items: subscriptions.items });\n } catch (e: unknown) {\n if (e instanceof Error) {\n ctx.context.logger.error(\n `DodoPayments subscriptions list failed. Error: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"DodoPayments subscriptions list failed\",\n });\n }\n },\n ),\n dodoPayments: createAuthEndpoint(\n \"/dodopayments/customer/payments/list\",\n {\n method: \"GET\",\n query: z\n .object({\n page: z.coerce.number().optional(),\n limit: z.coerce.number().optional(),\n status: z\n .enum([\n \"succeeded\",\n \"failed\",\n \"cancelled\",\n \"processing\",\n \"requires_customer_action\",\n \"requires_merchant_action\",\n \"requires_payment_method\",\n \"requires_confirmation\",\n \"requires_capture\",\n \"partially_captured\",\n \"partially_captured_and_capturable\",\n ])\n .optional(),\n })\n .optional(),\n use: [sessionMiddleware],\n },\n async (ctx): Promise<PaymentItems> => {\n if (!ctx.context.session.user.id) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"User not found\",\n });\n }\n\n if (!ctx.context.session?.user.emailVerified) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"User email not verified\",\n });\n }\n\n try {\n const customers = await dodopayments.customers.list({\n email: ctx.context.session?.user.email,\n });\n let customer = customers.items[0];\n\n if (!customer) {\n // upsert the customer, if they don't exist in DodoPayments\n customer = await createCustomer(\n dodopayments,\n ctx.context.session.user.email,\n ctx.context.session.user.name,\n );\n }\n\n const payments = await dodopayments.payments.list({\n customer_id: customer.customer_id,\n // page number is 0-indexed\n page_number: ctx.query?.page ? ctx.query.page - 1 : undefined,\n page_size: ctx.query?.limit,\n status: ctx.query?.status,\n });\n\n return ctx.json({ items: payments.items });\n } catch (e: unknown) {\n if (e instanceof Error) {\n ctx.context.logger.error(\n `DodoPayments orders list failed. Error: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Orders list failed\",\n });\n }\n },\n ),\n };\n};\n\nasync function createCustomer(\n dodopayments: DodoPayments,\n email: string,\n name: string,\n) {\n const customer = await dodopayments.customers.create({\n email,\n name,\n });\n\n return customer;\n}\n"],"mappings":";AACA,SAAS,UAAU,oBAAoB,yBAAyB;AAChE,SAAS,SAAS;AAOX,IAAM,SAAS,MAAM,CAAC,iBAA+B;AAC1D,SAAO;AAAA,IACL,YAAY;AAAA,MACV;AAAA,MACA;AAAA,QACE,QAAQ;AAAA,QACR,KAAK,CAAC,iBAAiB;AAAA,MACzB;AAAA,MACA,OAAO,QAAyC;AAC9C,YAAI,CAAC,IAAI,QAAQ,SAAS,KAAK,IAAI;AACjC,gBAAM,IAAI,SAAS,eAAe;AAAA,YAChC,SAAS;AAAA,UACX,CAAC;AAAA,QACH;AAEA,YAAI,CAAC,IAAI,QAAQ,SAAS,KAAK,eAAe;AAC5C,gBAAM,IAAI,SAAS,gBAAgB;AAAA,YACjC,SAAS;AAAA,UACX,CAAC;AAAA,QACH;AAEA,YAAI;AACF,gBAAM,YAAY,MAAM,aAAa,UAAU,KAAK;AAAA,YAClD,OAAO,IAAI,QAAQ,SAAS,KAAK;AAAA,UACnC,CAAC;AACD,cAAI,WAAW,UAAU,MAAM,CAAC;AAEhC,cAAI,CAAC,UAAU;AAEb,uBAAW,MAAM;AAAA,cACf;AAAA,cACA,IAAI,QAAQ,QAAQ,KAAK;AAAA,cACzB,IAAI,QAAQ,QAAQ,KAAK;AAAA,YAC3B;AAAA,UACF;AAEA,gBAAM,kBACJ,MAAM,aAAa,UAAU,eAAe;AAAA,YAC1C,SAAS;AAAA,UACX;AAEF,iBAAO,IAAI,KAAK;AAAA,YACd,KAAK,gBAAgB;AAAA,YACrB,UAAU;AAAA,UACZ,CAAC;AAAA,QACH,SAAS,GAAY;AACnB,cAAI,aAAa,OAAO;AACtB,gBAAI,QAAQ,OAAO;AAAA,cACjB,wDAAwD,EAAE,OAAO;AAAA,YACnE;AAAA,UACF;AAEA,gBAAM,IAAI,SAAS,yBAAyB;AAAA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A,IAAI,QAAQ,QAAQ,KAAK;AAAA,cACzB,IAAI,QAAQ,QAAQ,KAAK;AAAA,YAC3B;AAAA,UACF;AAEA,gBAAM,WAAW,MAAM,aAAa,SAAS,KAAK;AAAA,YAChD,aAAa,SAAS;AAAA;AAAA,YAEtB,aAAa,IAAI,OAAO,OAAO,IAAI,MAAM,OAAO,IAAI;AAAA,YACpD,WAAW,IAAI,OAAO;AAAA,YACtB,QAAQ,IAAI,OAAO;AAAA,UACrB,CAAC;AAED,iBAAO,IAAI,KAAK,EAAE,OAAO,SAAS,MAAM,CAAC;AAAA,QAC3C,SAAS,GAAY;AACnB,cAAI,aAAa,OAAO;AACtB,gBAAI,QAAQ,OAAO;AAAA,cACjB,2CAA2C,EAAE,OAAO;AAAA,YACtD;AAAA,UACF;AAEA,gBAAM,IAAI,SAAS,yBAAyB;AAAA,YAC1C,SAAS;AAAA,UACX,CAAC;AAAA,QACH;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;AAEA,eAAe,eACb,cACA,OACA,MACA;AACA,QAAM,WAAW,MAAM,aAAa,UAAU,OAAO;AAAA,IACnD;AAAA,IACA;AAAA,EACF,CAAC;AAED,SAAO;AACT;","names":[]}
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/client.ts"],"sourcesContent":["import type { BetterAuthClientPlugin } from \"better-auth\";\nimport type { dodopayments } from \"./index\";\n\nexport const dodopaymentsClient = () => {\n return {\n id: \"dodopayments-client\",\n $InferServerPlugin: {} as ReturnType<typeof dodopayments>,\n } satisfies BetterAuthClientPlugin;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGO,IAAM,qBAAqB,MAAM;AACtC,SAAO;AAAA,IACL,IAAI;AAAA,IACJ,oBAAoB,CAAC;AAAA,EACvB;AACF;","names":[]}
1
+ {"version":3,"sources":["../src/client.ts"],"sourcesContent":["import type { BetterAuthClientPlugin } from \"better-auth\";\nimport type { dodopayments } from \"./index\";\n\nexport const dodopaymentsClient = () => {\n return {\n id: \"dodopayments-client\",\n $InferServerPlugin: {} as ReturnType<typeof dodopayments>,\n } satisfies BetterAuthClientPlugin;\n};\n\nexport type {\n Product,\n DodoPaymentsEndpoints,\n DodoPaymentsOptions,\n PaymentItems,\n SubscriptionItems,\n CustomerPortalResponse,\n CreateCheckoutResponse,\n WebhookResponse,\n} from \"./types\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGO,IAAM,qBAAqB,MAAM;AACtC,SAAO;AAAA,IACL,IAAI;AAAA,IACJ,oBAAoB,CAAC;AAAA,EACvB;AACF;","names":[]}
package/dist/client.d.cts CHANGED
@@ -1,9 +1,8 @@
1
1
  export { dodopaymentsClient } from './index.cjs';
2
- import 'better-auth';
2
+ export { C as CreateCheckoutResponse, a as CustomerPortalResponse, e as DodoPaymentsEndpoints, D as DodoPaymentsOptions, P as PaymentItems, b as Product, S as SubscriptionItems, W as WebhookResponse } from './types-4JE4OwKb.cjs';
3
3
  import 'dodopayments/resources/usage-events.mjs';
4
- import './types-DtNvVoO9.cjs';
5
- import 'dodopayments';
6
- import 'better-call';
4
+ import 'better-auth';
7
5
  import 'zod/v3';
8
- import '@dodopayments/core/webhook';
9
6
  import './plugins/usage.cjs';
7
+ import 'dodopayments';
8
+ import '@dodopayments/core/webhook';
package/dist/client.d.ts CHANGED
@@ -1,9 +1,8 @@
1
1
  export { dodopaymentsClient } from './index.js';
2
- import 'better-auth';
2
+ export { C as CreateCheckoutResponse, a as CustomerPortalResponse, e as DodoPaymentsEndpoints, D as DodoPaymentsOptions, P as PaymentItems, b as Product, S as SubscriptionItems, W as WebhookResponse } from './types-B9rx1bt7.js';
3
3
  import 'dodopayments/resources/usage-events.mjs';
4
- import './types-VW2PljjI.js';
5
- import 'dodopayments';
6
- import 'better-call';
4
+ import 'better-auth';
7
5
  import 'zod/v3';
8
- import '@dodopayments/core/webhook';
9
6
  import './plugins/usage.js';
7
+ import 'dodopayments';
8
+ import '@dodopayments/core/webhook';
package/dist/client.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  dodopaymentsClient
3
- } from "./chunk-PXI4EHZC.js";
3
+ } from "./chunk-HZNE63HR.js";
4
4
  export {
5
5
  dodopaymentsClient
6
6
  };
@@ -33,14 +33,9 @@ var onUserCreate = (options) => async (user, ctx) => {
33
33
  });
34
34
  const existingCustomer = customers.items[0];
35
35
  if (existingCustomer) {
36
- if (existingCustomer.email !== user.email) {
37
- await options.client.customers.update(
38
- existingCustomer.customer_id,
39
- {
40
- name: user.name
41
- }
42
- );
43
- }
36
+ await options.client.customers.update(existingCustomer.customer_id, {
37
+ name: user.name
38
+ });
44
39
  } else {
45
40
  await options.client.customers.create({
46
41
  email: user.email,
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/hooks/customer.ts"],"sourcesContent":["import type { GenericEndpointContext, User } from \"better-auth\";\nimport { APIError } from \"better-auth/api\";\nimport type { DodoPaymentsOptions } from \"../types\";\n\nexport const onUserCreate =\n (options: DodoPaymentsOptions) =>\n async (user: User, ctx?: GenericEndpointContext) => {\n if (ctx && options.createCustomerOnSignUp) {\n try {\n const customers = await options.client.customers.list({\n email: user.email,\n });\n const existingCustomer = customers.items[0];\n\n if (existingCustomer) {\n if (existingCustomer.email !== user.email) {\n await options.client.customers.update(\n existingCustomer.customer_id,\n {\n name: user.name,\n },\n );\n }\n } else {\n // TODO: Add metadata to customer object via\n // getCustomerCreateParams option when it becomes\n // available in the API\n await options.client.customers.create({\n email: user.email,\n name: user.name,\n });\n }\n } catch (e: unknown) {\n if (e instanceof Error) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: `DodoPayments customer creation failed. Error: ${e.message}`,\n });\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: `DodoPayments customer creation failed. Error: ${e}`,\n });\n }\n }\n };\n\nexport const onUserUpdate =\n (options: DodoPaymentsOptions) =>\n async (user: User, ctx?: GenericEndpointContext) => {\n if (ctx && options.createCustomerOnSignUp) {\n try {\n const customers = await options.client.customers.list({\n email: user.email,\n });\n const existingCustomer = customers.items[0];\n\n if (existingCustomer) {\n // TODO: Add metadata to customer object via\n // getCustomerCreateParams option when it becomes\n // available in the API\n await options.client.customers.update(existingCustomer.customer_id, {\n name: user.name,\n });\n }\n } catch (e: unknown) {\n if (e instanceof Error) {\n ctx.context.logger.error(\n `DodoPayments customer update failed. Error: ${e.message}`,\n );\n } else {\n ctx.context.logger.error(\n `DodoPayments customer update failed. Error: ${e}`,\n );\n }\n }\n }\n };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,iBAAyB;AAGlB,IAAM,eACX,CAAC,YACD,OAAO,MAAY,QAAiC;AAClD,MAAI,OAAO,QAAQ,wBAAwB;AACzC,QAAI;AACF,YAAM,YAAY,MAAM,QAAQ,OAAO,UAAU,KAAK;AAAA,QACpD,OAAO,KAAK;AAAA,MACd,CAAC;AACD,YAAM,mBAAmB,UAAU,MAAM,CAAC;AAE1C,UAAI,kBAAkB;AACpB,YAAI,iBAAiB,UAAU,KAAK,OAAO;AACzC,gBAAM,QAAQ,OAAO,UAAU;AAAA,YAC7B,iBAAiB;AAAA,YACjB;AAAA,cACE,MAAM,KAAK;AAAA,YACb;AAAA,UACF;AAAA,QACF;AAAA,MACF,OAAO;AAIL,cAAM,QAAQ,OAAO,UAAU,OAAO;AAAA,UACpC,OAAO,KAAK;AAAA,UACZ,MAAM,KAAK;AAAA,QACb,CAAC;AAAA,MACH;AAAA,IACF,SAAS,GAAY;AACnB,UAAI,aAAa,OAAO;AACtB,cAAM,IAAI,oBAAS,yBAAyB;AAAA,UAC1C,SAAS,iDAAiD,EAAE,OAAO;AAAA,QACrE,CAAC;AAAA,MACH;AAEA,YAAM,IAAI,oBAAS,yBAAyB;AAAA,QAC1C,SAAS,iDAAiD,CAAC;AAAA,MAC7D,CAAC;AAAA,IACH;AAAA,EACF;AACF;AAEK,IAAM,eACX,CAAC,YACD,OAAO,MAAY,QAAiC;AAClD,MAAI,OAAO,QAAQ,wBAAwB;AACzC,QAAI;AACF,YAAM,YAAY,MAAM,QAAQ,OAAO,UAAU,KAAK;AAAA,QACpD,OAAO,KAAK;AAAA,MACd,CAAC;AACD,YAAM,mBAAmB,UAAU,MAAM,CAAC;AAE1C,UAAI,kBAAkB;AAIpB,cAAM,QAAQ,OAAO,UAAU,OAAO,iBAAiB,aAAa;AAAA,UAClE,MAAM,KAAK;AAAA,QACb,CAAC;AAAA,MACH;AAAA,IACF,SAAS,GAAY;AACnB,UAAI,aAAa,OAAO;AACtB,YAAI,QAAQ,OAAO;AAAA,UACjB,+CAA+C,EAAE,OAAO;AAAA,QAC1D;AAAA,MACF,OAAO;AACL,YAAI,QAAQ,OAAO;AAAA,UACjB,+CAA+C,CAAC;AAAA,QAClD;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;","names":[]}
1
+ {"version":3,"sources":["../../src/hooks/customer.ts"],"sourcesContent":["import type { GenericEndpointContext, User } from \"better-auth\";\nimport { APIError } from \"better-auth/api\";\nimport type { DodoPaymentsOptions } from \"../types\";\n\nexport const onUserCreate =\n (options: DodoPaymentsOptions) =>\n async (user: User, ctx: GenericEndpointContext | null) => {\n if (ctx && options.createCustomerOnSignUp) {\n try {\n const customers = await options.client.customers.list({\n email: user.email,\n });\n const existingCustomer = customers.items[0];\n\n if (existingCustomer) {\n await options.client.customers.update(existingCustomer.customer_id, {\n name: user.name,\n });\n } else {\n // TODO: Add metadata to customer object via\n // getCustomerCreateParams option when it becomes\n // available in the API\n await options.client.customers.create({\n email: user.email,\n name: user.name,\n });\n }\n } catch (e: unknown) {\n if (e instanceof Error) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: `DodoPayments customer creation failed. Error: ${e.message}`,\n });\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: `DodoPayments customer creation failed. Error: ${e}`,\n });\n }\n }\n };\n\nexport const onUserUpdate =\n (options: DodoPaymentsOptions) =>\n async (user: User, ctx: GenericEndpointContext | null) => {\n if (ctx && options.createCustomerOnSignUp) {\n try {\n const customers = await options.client.customers.list({\n email: user.email,\n });\n const existingCustomer = customers.items[0];\n\n if (existingCustomer) {\n // TODO: Add metadata to customer object via\n // getCustomerCreateParams option when it becomes\n // available in the API\n await options.client.customers.update(existingCustomer.customer_id, {\n name: user.name,\n });\n }\n } catch (e: unknown) {\n if (e instanceof Error) {\n ctx.context.logger.error(\n `DodoPayments customer update failed. Error: ${e.message}`,\n );\n } else {\n ctx.context.logger.error(\n `DodoPayments customer update failed. Error: ${e}`,\n );\n }\n }\n }\n };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,iBAAyB;AAGlB,IAAM,eACX,CAAC,YACD,OAAO,MAAY,QAAuC;AACxD,MAAI,OAAO,QAAQ,wBAAwB;AACzC,QAAI;AACF,YAAM,YAAY,MAAM,QAAQ,OAAO,UAAU,KAAK;AAAA,QACpD,OAAO,KAAK;AAAA,MACd,CAAC;AACD,YAAM,mBAAmB,UAAU,MAAM,CAAC;AAE1C,UAAI,kBAAkB;AACpB,cAAM,QAAQ,OAAO,UAAU,OAAO,iBAAiB,aAAa;AAAA,UAClE,MAAM,KAAK;AAAA,QACb,CAAC;AAAA,MACH,OAAO;AAIL,cAAM,QAAQ,OAAO,UAAU,OAAO;AAAA,UACpC,OAAO,KAAK;AAAA,UACZ,MAAM,KAAK;AAAA,QACb,CAAC;AAAA,MACH;AAAA,IACF,SAAS,GAAY;AACnB,UAAI,aAAa,OAAO;AACtB,cAAM,IAAI,oBAAS,yBAAyB;AAAA,UAC1C,SAAS,iDAAiD,EAAE,OAAO;AAAA,QACrE,CAAC;AAAA,MACH;AAEA,YAAM,IAAI,oBAAS,yBAAyB;AAAA,QAC1C,SAAS,iDAAiD,CAAC;AAAA,MAC7D,CAAC;AAAA,IACH;AAAA,EACF;AACF;AAEK,IAAM,eACX,CAAC,YACD,OAAO,MAAY,QAAuC;AACxD,MAAI,OAAO,QAAQ,wBAAwB;AACzC,QAAI;AACF,YAAM,YAAY,MAAM,QAAQ,OAAO,UAAU,KAAK;AAAA,QACpD,OAAO,KAAK;AAAA,MACd,CAAC;AACD,YAAM,mBAAmB,UAAU,MAAM,CAAC;AAE1C,UAAI,kBAAkB;AAIpB,cAAM,QAAQ,OAAO,UAAU,OAAO,iBAAiB,aAAa;AAAA,UAClE,MAAM,KAAK;AAAA,QACb,CAAC;AAAA,MACH;AAAA,IACF,SAAS,GAAY;AACnB,UAAI,aAAa,OAAO;AACtB,YAAI,QAAQ,OAAO;AAAA,UACjB,+CAA+C,EAAE,OAAO;AAAA,QAC1D;AAAA,MACF,OAAO;AACL,YAAI,QAAQ,OAAO;AAAA,UACjB,+CAA+C,CAAC;AAAA,QAClD;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;","names":[]}
@@ -1,13 +1,12 @@
1
1
  import { User, GenericEndpointContext } from 'better-auth';
2
- import { D as DodoPaymentsOptions } from '../types-DtNvVoO9.cjs';
2
+ import { D as DodoPaymentsOptions } from '../types-4JE4OwKb.cjs';
3
3
  import 'dodopayments';
4
- import 'better-call';
5
4
  import 'zod/v3';
6
5
  import '@dodopayments/core/webhook';
7
6
  import '../plugins/usage.cjs';
8
7
  import 'dodopayments/resources/usage-events.mjs';
9
8
 
10
- declare const onUserCreate: (options: DodoPaymentsOptions) => (user: User, ctx?: GenericEndpointContext) => Promise<void>;
11
- declare const onUserUpdate: (options: DodoPaymentsOptions) => (user: User, ctx?: GenericEndpointContext) => Promise<void>;
9
+ declare const onUserCreate: (options: DodoPaymentsOptions) => (user: User, ctx: GenericEndpointContext | null) => Promise<void>;
10
+ declare const onUserUpdate: (options: DodoPaymentsOptions) => (user: User, ctx: GenericEndpointContext | null) => Promise<void>;
12
11
 
13
12
  export { onUserCreate, onUserUpdate };
@@ -1,13 +1,12 @@
1
1
  import { User, GenericEndpointContext } from 'better-auth';
2
- import { D as DodoPaymentsOptions } from '../types-VW2PljjI.js';
2
+ import { D as DodoPaymentsOptions } from '../types-B9rx1bt7.js';
3
3
  import 'dodopayments';
4
- import 'better-call';
5
4
  import 'zod/v3';
6
5
  import '@dodopayments/core/webhook';
7
6
  import '../plugins/usage.js';
8
7
  import 'dodopayments/resources/usage-events.mjs';
9
8
 
10
- declare const onUserCreate: (options: DodoPaymentsOptions) => (user: User, ctx?: GenericEndpointContext) => Promise<void>;
11
- declare const onUserUpdate: (options: DodoPaymentsOptions) => (user: User, ctx?: GenericEndpointContext) => Promise<void>;
9
+ declare const onUserCreate: (options: DodoPaymentsOptions) => (user: User, ctx: GenericEndpointContext | null) => Promise<void>;
10
+ declare const onUserUpdate: (options: DodoPaymentsOptions) => (user: User, ctx: GenericEndpointContext | null) => Promise<void>;
12
11
 
13
12
  export { onUserCreate, onUserUpdate };
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  onUserCreate,
3
3
  onUserUpdate
4
- } from "../chunk-CRRND2EH.js";
4
+ } from "../chunk-BII7QAPD.js";
5
5
  export {
6
6
  onUserCreate,
7
7
  onUserUpdate
package/dist/index.cjs CHANGED
@@ -39,14 +39,9 @@ var onUserCreate = (options) => async (user, ctx) => {
39
39
  });
40
40
  const existingCustomer = customers.items[0];
41
41
  if (existingCustomer) {
42
- if (existingCustomer.email !== user.email) {
43
- await options.client.customers.update(
44
- existingCustomer.customer_id,
45
- {
46
- name: user.name
47
- }
48
- );
49
- }
42
+ await options.client.customers.update(existingCustomer.customer_id, {
43
+ name: user.name
44
+ });
50
45
  } else {
51
46
  await options.client.customers.create({
52
47
  email: user.email,
@@ -101,16 +96,14 @@ var dodopaymentsClient = () => {
101
96
 
102
97
  // src/plugins/portal.ts
103
98
  var import_api2 = require("better-auth/api");
104
- var import_api3 = require("better-auth/api");
105
- var import_plugins = require("better-auth/plugins");
106
99
  var import_v3 = require("zod/v3");
107
100
  var portal = () => (dodopayments2) => {
108
101
  return {
109
- dodoPortal: (0, import_plugins.createAuthEndpoint)(
102
+ dodoPortal: (0, import_api2.createAuthEndpoint)(
110
103
  "/dodopayments/customer/portal",
111
104
  {
112
105
  method: "GET",
113
- use: [import_api3.sessionMiddleware]
106
+ use: [import_api2.sessionMiddleware]
114
107
  },
115
108
  async (ctx) => {
116
109
  if (!ctx.context.session?.user.id) {
@@ -154,7 +147,7 @@ var portal = () => (dodopayments2) => {
154
147
  }
155
148
  }
156
149
  ),
157
- dodoSubscriptions: (0, import_plugins.createAuthEndpoint)(
150
+ dodoSubscriptions: (0, import_api2.createAuthEndpoint)(
158
151
  "/dodopayments/customer/subscriptions/list",
159
152
  {
160
153
  method: "GET",
@@ -170,7 +163,7 @@ var portal = () => (dodopayments2) => {
170
163
  "expired"
171
164
  ]).optional()
172
165
  }).optional(),
173
- use: [import_api3.sessionMiddleware]
166
+ use: [import_api2.sessionMiddleware]
174
167
  },
175
168
  async (ctx) => {
176
169
  if (!ctx.context.session.user.id) {
@@ -215,7 +208,7 @@ var portal = () => (dodopayments2) => {
215
208
  }
216
209
  }
217
210
  ),
218
- dodoPayments: (0, import_plugins.createAuthEndpoint)(
211
+ dodoPayments: (0, import_api2.createAuthEndpoint)(
219
212
  "/dodopayments/customer/payments/list",
220
213
  {
221
214
  method: "GET",
@@ -236,7 +229,7 @@ var portal = () => (dodopayments2) => {
236
229
  "partially_captured_and_capturable"
237
230
  ]).optional()
238
231
  }).optional(),
239
- use: [import_api3.sessionMiddleware]
232
+ use: [import_api2.sessionMiddleware]
240
233
  },
241
234
  async (ctx) => {
242
235
  if (!ctx.context.session.user.id) {
@@ -292,8 +285,7 @@ async function createCustomer(dodopayments2, email, name) {
292
285
  }
293
286
 
294
287
  // src/plugins/checkout.ts
295
- var import_api4 = require("better-auth/api");
296
- var import_plugins2 = require("better-auth/plugins");
288
+ var import_api3 = require("better-auth/api");
297
289
  var import_v32 = require("zod/v3");
298
290
  var import_checkout = require("@dodopayments/core/checkout");
299
291
  var checkout = (checkoutOptions = {}) => (dodopayments2) => {
@@ -301,7 +293,7 @@ var checkout = (checkoutOptions = {}) => (dodopayments2) => {
301
293
  /**
302
294
  * @deprecated
303
295
  */
304
- dodoCheckout: (0, import_plugins2.createAuthEndpoint)(
296
+ dodoCheckout: (0, import_api3.createAuthEndpoint)(
305
297
  "/dodopayments/checkout",
306
298
  {
307
299
  method: "POST",
@@ -312,7 +304,7 @@ var checkout = (checkoutOptions = {}) => (dodopayments2) => {
312
304
  requireRequest: true
313
305
  },
314
306
  async (ctx) => {
315
- const session = await (0, import_api4.getSessionFromCtx)(ctx);
307
+ const session = await (0, import_api3.getSessionFromCtx)(ctx);
316
308
  let dodoPaymentsProductId;
317
309
  if (ctx.body?.slug) {
318
310
  const resolvedProducts = typeof checkoutOptions.products === "function" ? await checkoutOptions.products() : checkoutOptions.products;
@@ -320,7 +312,7 @@ var checkout = (checkoutOptions = {}) => (dodopayments2) => {
320
312
  (product) => product.slug === ctx.body.slug
321
313
  )?.productId;
322
314
  if (!productId) {
323
- throw new import_api4.APIError("BAD_REQUEST", {
315
+ throw new import_api3.APIError("BAD_REQUEST", {
324
316
  message: "Product not found"
325
317
  });
326
318
  }
@@ -329,7 +321,7 @@ var checkout = (checkoutOptions = {}) => (dodopayments2) => {
329
321
  dodoPaymentsProductId = ctx.body.product_id;
330
322
  }
331
323
  if (checkoutOptions.authenticatedUsersOnly && !session?.user.id) {
332
- throw new import_api4.APIError("UNAUTHORIZED", {
324
+ throw new import_api3.APIError("UNAUTHORIZED", {
333
325
  message: "You must be logged in to checkout"
334
326
  });
335
327
  }
@@ -373,13 +365,13 @@ var checkout = (checkoutOptions = {}) => (dodopayments2) => {
373
365
  `DodoPayments checkout creation failed. Error: ${e.message}`
374
366
  );
375
367
  }
376
- throw new import_api4.APIError("INTERNAL_SERVER_ERROR", {
368
+ throw new import_api3.APIError("INTERNAL_SERVER_ERROR", {
377
369
  message: "Checkout creation failed"
378
370
  });
379
371
  }
380
372
  }
381
373
  ),
382
- dodoCheckoutSession: (0, import_plugins2.createAuthEndpoint)(
374
+ dodoCheckoutSession: (0, import_api3.createAuthEndpoint)(
383
375
  "/dodopayments/checkout-session",
384
376
  {
385
377
  method: "POST",
@@ -392,7 +384,7 @@ var checkout = (checkoutOptions = {}) => (dodopayments2) => {
392
384
  requireRequest: true
393
385
  },
394
386
  async (ctx) => {
395
- const session = await (0, import_api4.getSessionFromCtx)(ctx);
387
+ const session = await (0, import_api3.getSessionFromCtx)(ctx);
396
388
  let dodoPaymentsProductId;
397
389
  if (ctx.body?.slug) {
398
390
  const resolvedProducts = typeof checkoutOptions.products === "function" ? await checkoutOptions.products() : checkoutOptions.products;
@@ -400,20 +392,20 @@ var checkout = (checkoutOptions = {}) => (dodopayments2) => {
400
392
  (product) => product.slug === ctx.body.slug
401
393
  )?.productId;
402
394
  if (!productId) {
403
- throw new import_api4.APIError("BAD_REQUEST", {
395
+ throw new import_api3.APIError("BAD_REQUEST", {
404
396
  message: "Product not found"
405
397
  });
406
398
  }
407
399
  dodoPaymentsProductId = productId;
408
400
  }
409
401
  if (checkoutOptions.authenticatedUsersOnly && !session?.user.id) {
410
- throw new import_api4.APIError("UNAUTHORIZED", {
402
+ throw new import_api3.APIError("UNAUTHORIZED", {
411
403
  message: "You must be logged in to checkout"
412
404
  });
413
405
  }
414
406
  const product_cart = dodoPaymentsProductId ? [{ product_id: dodoPaymentsProductId, quantity: 1 }] : ctx.body.product_cart;
415
407
  if (!product_cart || product_cart.length === 0) {
416
- throw new import_api4.APIError("BAD_REQUEST", {
408
+ throw new import_api3.APIError("BAD_REQUEST", {
417
409
  message: "Neither product_cart nor slug was provided"
418
410
  });
419
411
  }
@@ -450,7 +442,7 @@ var checkout = (checkoutOptions = {}) => (dodopayments2) => {
450
442
  `DodoPayments checkout creation failed. Error: ${e.message}`
451
443
  );
452
444
  }
453
- throw new import_api4.APIError("INTERNAL_SERVER_ERROR", {
445
+ throw new import_api3.APIError("INTERNAL_SERVER_ERROR", {
454
446
  message: "Checkout session creation failed"
455
447
  });
456
448
  }
@@ -461,11 +453,11 @@ var checkout = (checkoutOptions = {}) => (dodopayments2) => {
461
453
 
462
454
  // src/plugins/webhooks.ts
463
455
  var import_webhook = require("@dodopayments/core/webhook");
464
- var import_api5 = require("better-auth/api");
456
+ var import_api4 = require("better-auth/api");
465
457
  var import_webhook2 = require("@dodopayments/core/webhook");
466
458
  var webhooks = (options) => (_dodopayments) => {
467
459
  return {
468
- dodopaymentsWebhooks: (0, import_api5.createAuthEndpoint)(
460
+ dodopaymentsWebhooks: (0, import_api4.createAuthEndpoint)(
469
461
  "/dodopayments/webhooks",
470
462
  {
471
463
  method: "POST",
@@ -477,13 +469,13 @@ var webhooks = (options) => (_dodopayments) => {
477
469
  async (ctx) => {
478
470
  const { webhookKey } = options;
479
471
  if (!ctx.request?.body) {
480
- throw new import_api5.APIError("INTERNAL_SERVER_ERROR");
472
+ throw new import_api4.APIError("INTERNAL_SERVER_ERROR");
481
473
  }
482
474
  const buf = await ctx.request.text();
483
475
  let event;
484
476
  try {
485
477
  if (!webhookKey) {
486
- throw new import_api5.APIError("INTERNAL_SERVER_ERROR", {
478
+ throw new import_api4.APIError("INTERNAL_SERVER_ERROR", {
487
479
  message: "DodoPayments webhook webhookKey not found"
488
480
  });
489
481
  }
@@ -504,11 +496,11 @@ var webhooks = (options) => (_dodopayments) => {
504
496
  } catch (err) {
505
497
  if (err instanceof Error) {
506
498
  ctx.context.logger.error(`Webhook Error: ${err.message}`);
507
- throw new import_api5.APIError("BAD_REQUEST", {
499
+ throw new import_api4.APIError("BAD_REQUEST", {
508
500
  message: `Webhook Error: ${err.message}`
509
501
  });
510
502
  }
511
- throw new import_api5.APIError("BAD_REQUEST", {
503
+ throw new import_api4.APIError("BAD_REQUEST", {
512
504
  message: `Webhook Error: ${err}`
513
505
  });
514
506
  }
@@ -523,7 +515,7 @@ var webhooks = (options) => (_dodopayments) => {
523
515
  ctx.context.logger.error(
524
516
  `DodoPayments webhook failed. Error: ${e}`
525
517
  );
526
- throw new import_api5.APIError("BAD_REQUEST", {
518
+ throw new import_api4.APIError("BAD_REQUEST", {
527
519
  message: "Webhook error: See server logs for more information."
528
520
  });
529
521
  }
@@ -534,7 +526,7 @@ var webhooks = (options) => (_dodopayments) => {
534
526
  };
535
527
 
536
528
  // src/plugins/usage.ts
537
- var import_api6 = require("better-auth/api");
529
+ var import_api5 = require("better-auth/api");
538
530
  var import_v33 = require("zod/v3");
539
531
  var EventInputSchema = import_v33.z.object({
540
532
  event_id: import_v33.z.string(),
@@ -547,21 +539,21 @@ var EventInputSchema = import_v33.z.object({
547
539
  var usage = () => (dodopayments2) => {
548
540
  return {
549
541
  // Ingest usage data
550
- dodoUsageIngest: (0, import_api6.createAuthEndpoint)(
542
+ dodoUsageIngest: (0, import_api5.createAuthEndpoint)(
551
543
  "/dodopayments/usage/ingest",
552
544
  {
553
545
  method: "POST",
554
546
  body: EventInputSchema,
555
- use: [import_api6.sessionMiddleware]
547
+ use: [import_api5.sessionMiddleware]
556
548
  },
557
549
  async (ctx) => {
558
550
  if (!ctx.context.session?.user?.id) {
559
- throw new import_api6.APIError("BAD_REQUEST", {
551
+ throw new import_api5.APIError("BAD_REQUEST", {
560
552
  message: "User not found"
561
553
  });
562
554
  }
563
555
  if (!ctx.context.session?.user.emailVerified) {
564
- throw new import_api6.APIError("UNAUTHORIZED", {
556
+ throw new import_api5.APIError("UNAUTHORIZED", {
565
557
  message: "User email not verified"
566
558
  });
567
559
  }
@@ -595,14 +587,14 @@ var usage = () => (dodopayments2) => {
595
587
  `User usage ingestion error: ${e.message}`
596
588
  );
597
589
  }
598
- throw new import_api6.APIError("INTERNAL_SERVER_ERROR", {
590
+ throw new import_api5.APIError("INTERNAL_SERVER_ERROR", {
599
591
  message: "Failed to record the user usage"
600
592
  });
601
593
  }
602
594
  }
603
595
  ),
604
596
  // List usage meters
605
- dodoUsageMetersList: (0, import_api6.createAuthEndpoint)(
597
+ dodoUsageMetersList: (0, import_api5.createAuthEndpoint)(
606
598
  "/dodopayments/usage/meters/list",
607
599
  {
608
600
  method: "GET",
@@ -614,16 +606,16 @@ var usage = () => (dodopayments2) => {
614
606
  start: import_v33.z.string().optional(),
615
607
  end: import_v33.z.string().optional()
616
608
  }).optional(),
617
- use: [import_api6.sessionMiddleware]
609
+ use: [import_api5.sessionMiddleware]
618
610
  },
619
611
  async (ctx) => {
620
612
  if (!ctx.context.session?.user?.id) {
621
- throw new import_api6.APIError("BAD_REQUEST", {
613
+ throw new import_api5.APIError("BAD_REQUEST", {
622
614
  message: "User not found"
623
615
  });
624
616
  }
625
617
  if (!ctx.context.session?.user.emailVerified) {
626
- throw new import_api6.APIError("UNAUTHORIZED", {
618
+ throw new import_api5.APIError("UNAUTHORIZED", {
627
619
  message: "User email not verified"
628
620
  });
629
621
  }
@@ -650,7 +642,7 @@ var usage = () => (dodopayments2) => {
650
642
  `User usage meter list error: ${e.message}`
651
643
  );
652
644
  }
653
- throw new import_api6.APIError("INTERNAL_SERVER_ERROR", {
645
+ throw new import_api5.APIError("INTERNAL_SERVER_ERROR", {
654
646
  message: "Failed to fetch the user usage"
655
647
  });
656
648
  }