@dodopayments/better-auth 1.0.1 → 1.1.0

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.
@@ -301,4 +301,3 @@ export declare const checkout: (checkoutOptions?: CheckoutOptions) => (dodopayme
301
301
  path: "/dodopayments/checkout";
302
302
  };
303
303
  };
304
- //# sourceMappingURL=checkout.d.ts.map
@@ -0,0 +1,87 @@
1
+ import { APIError, getSessionFromCtx } from "better-auth/api";
2
+ import { createAuthEndpoint } from "better-auth/plugins";
3
+ import { z } from "zod";
4
+ import { buildCheckoutUrl, dynamicCheckoutBodySchema, } from "@dodopayments/core/checkout";
5
+ export const checkout = (checkoutOptions = {}) => (dodopayments) => {
6
+ return {
7
+ checkout: createAuthEndpoint("/dodopayments/checkout", {
8
+ method: "POST",
9
+ body: dynamicCheckoutBodySchema.extend({
10
+ slug: z.string().optional(),
11
+ referenceId: z.string().optional(),
12
+ }),
13
+ requireRequest: true,
14
+ }, async (ctx) => {
15
+ const session = await getSessionFromCtx(ctx);
16
+ let dodoPaymentsProductId;
17
+ if (ctx.body?.slug) {
18
+ const resolvedProducts = await (typeof checkoutOptions.products ===
19
+ "function"
20
+ ? checkoutOptions.products()
21
+ : checkoutOptions.products);
22
+ const productId = resolvedProducts?.find((product) => product.slug === ctx.body.slug)?.productId;
23
+ if (!productId) {
24
+ throw new APIError("BAD_REQUEST", {
25
+ message: "Product not found",
26
+ });
27
+ }
28
+ dodoPaymentsProductId = productId;
29
+ }
30
+ else {
31
+ dodoPaymentsProductId = ctx.body.product_id;
32
+ }
33
+ if (checkoutOptions.authenticatedUsersOnly && !session?.user.id) {
34
+ throw new APIError("UNAUTHORIZED", {
35
+ message: "You must be logged in to checkout",
36
+ });
37
+ }
38
+ try {
39
+ const checkoutUrl = await buildCheckoutUrl({
40
+ body: {
41
+ ...ctx.body,
42
+ customer: {
43
+ email: session?.user.email,
44
+ name: session?.user.name,
45
+ ...ctx.body.customer,
46
+ },
47
+ product_cart: dodoPaymentsProductId
48
+ ? [
49
+ {
50
+ product_id: dodoPaymentsProductId,
51
+ quantity: 1,
52
+ },
53
+ ]
54
+ : undefined,
55
+ metadata: ctx.body.referenceId
56
+ ? {
57
+ referenceId: ctx.body.referenceId,
58
+ ...ctx.body.metadata,
59
+ }
60
+ : ctx.body.metadata,
61
+ },
62
+ bearerToken: dodopayments.bearerToken,
63
+ environment: dodopayments.baseURL.includes("test")
64
+ ? "test_mode"
65
+ : "live_mode",
66
+ returnUrl: checkoutOptions.successUrl
67
+ ? new URL(checkoutOptions.successUrl, ctx.request?.url).toString()
68
+ : undefined,
69
+ type: "dynamic",
70
+ });
71
+ const redirectUrl = new URL(checkoutUrl);
72
+ return ctx.json({
73
+ url: redirectUrl.toString(),
74
+ redirect: true,
75
+ });
76
+ }
77
+ catch (e) {
78
+ if (e instanceof Error) {
79
+ ctx.context.logger.error(`DodoPayments checkout creation failed. Error: ${e.message}`);
80
+ }
81
+ throw new APIError("INTERNAL_SERVER_ERROR", {
82
+ message: "Checkout creation failed",
83
+ });
84
+ }
85
+ }),
86
+ };
87
+ };
@@ -215,4 +215,3 @@ export declare const portal: () => (dodopayments: DodoPayments) => {
215
215
  path: "/dodopayments/customer/payments/list";
216
216
  };
217
217
  };
218
- //# sourceMappingURL=portal.d.ts.map
@@ -0,0 +1,173 @@
1
+ import { APIError } from "better-auth/api";
2
+ import { sessionMiddleware } from "better-auth/api";
3
+ import { createAuthEndpoint } from "better-auth/plugins";
4
+ import { z } from "zod";
5
+ export const portal = () => (dodopayments) => {
6
+ return {
7
+ portal: createAuthEndpoint("/dodopayments/customer/portal", {
8
+ method: "GET",
9
+ use: [sessionMiddleware],
10
+ }, async (ctx) => {
11
+ if (!ctx.context.session?.user.id) {
12
+ throw new APIError("BAD_REQUEST", {
13
+ message: "User not found",
14
+ });
15
+ }
16
+ if (!ctx.context.session?.user.emailVerified) {
17
+ throw new APIError("UNAUTHORIZED", {
18
+ message: "User email not verified",
19
+ });
20
+ }
21
+ try {
22
+ const customers = await dodopayments.customers.list({
23
+ email: ctx.context.session?.user.email,
24
+ });
25
+ let customer = customers.items[0];
26
+ if (!customer) {
27
+ // upsert the customer, if they don't exist in DodoPayments
28
+ customer = await createCustomer(dodopayments, ctx.context.session.user.email, ctx.context.session.user.name);
29
+ }
30
+ const customerSession = await dodopayments.customers.customerPortal.create(customer.customer_id);
31
+ return ctx.json({
32
+ url: customerSession.link,
33
+ redirect: true,
34
+ });
35
+ }
36
+ catch (e) {
37
+ if (e instanceof Error) {
38
+ ctx.context.logger.error(`DodoPayments customer portal creation failed. Error: ${e.message}`);
39
+ }
40
+ throw new APIError("INTERNAL_SERVER_ERROR", {
41
+ message: "Customer portal creation failed",
42
+ });
43
+ }
44
+ }),
45
+ subscriptions: createAuthEndpoint("/dodopayments/customer/subscriptions/list", {
46
+ method: "GET",
47
+ query: z
48
+ .object({
49
+ page: z.coerce.number().optional(),
50
+ limit: z.coerce.number().optional(),
51
+ status: z
52
+ .enum([
53
+ "active",
54
+ "cancelled",
55
+ "on_hold",
56
+ "pending",
57
+ "paused",
58
+ "failed",
59
+ "expired",
60
+ ])
61
+ .optional(),
62
+ })
63
+ .optional(),
64
+ use: [sessionMiddleware],
65
+ }, async (ctx) => {
66
+ if (!ctx.context.session.user.id) {
67
+ throw new APIError("BAD_REQUEST", {
68
+ message: "User not found",
69
+ });
70
+ }
71
+ if (!ctx.context.session?.user.emailVerified) {
72
+ throw new APIError("UNAUTHORIZED", {
73
+ message: "User email not verified",
74
+ });
75
+ }
76
+ try {
77
+ const customers = await dodopayments.customers.list({
78
+ email: ctx.context.session?.user.email,
79
+ });
80
+ let customer = customers.items[0];
81
+ if (!customer) {
82
+ // upsert the customer, if they don't exist in DodoPayments
83
+ customer = await createCustomer(dodopayments, ctx.context.session.user.email, ctx.context.session.user.name);
84
+ }
85
+ const subscriptions = await dodopayments.subscriptions.list({
86
+ customer_id: customer.customer_id,
87
+ // page number is 0-indexed
88
+ page_number: ctx.query?.page ? ctx.query.page - 1 : undefined,
89
+ page_size: ctx.query?.limit,
90
+ status: ctx.query?.status,
91
+ });
92
+ return ctx.json({ items: subscriptions.items });
93
+ }
94
+ catch (e) {
95
+ if (e instanceof Error) {
96
+ ctx.context.logger.error(`DodoPayments subscriptions list failed. Error: ${e.message}`);
97
+ }
98
+ throw new APIError("INTERNAL_SERVER_ERROR", {
99
+ message: "DodoPayments subscriptions list failed",
100
+ });
101
+ }
102
+ }),
103
+ payments: createAuthEndpoint("/dodopayments/customer/payments/list", {
104
+ method: "GET",
105
+ query: z
106
+ .object({
107
+ page: z.coerce.number().optional(),
108
+ limit: z.coerce.number().optional(),
109
+ status: z
110
+ .enum([
111
+ "succeeded",
112
+ "failed",
113
+ "cancelled",
114
+ "processing",
115
+ "requires_customer_action",
116
+ "requires_merchant_action",
117
+ "requires_payment_method",
118
+ "requires_confirmation",
119
+ "requires_capture",
120
+ "partially_captured",
121
+ "partially_captured_and_capturable",
122
+ ])
123
+ .optional(),
124
+ })
125
+ .optional(),
126
+ use: [sessionMiddleware],
127
+ }, async (ctx) => {
128
+ if (!ctx.context.session.user.id) {
129
+ throw new APIError("BAD_REQUEST", {
130
+ message: "User not found",
131
+ });
132
+ }
133
+ if (!ctx.context.session?.user.emailVerified) {
134
+ throw new APIError("UNAUTHORIZED", {
135
+ message: "User email not verified",
136
+ });
137
+ }
138
+ try {
139
+ const customers = await dodopayments.customers.list({
140
+ email: ctx.context.session?.user.email,
141
+ });
142
+ let customer = customers.items[0];
143
+ if (!customer) {
144
+ // upsert the customer, if they don't exist in DodoPayments
145
+ customer = await createCustomer(dodopayments, ctx.context.session.user.email, ctx.context.session.user.name);
146
+ }
147
+ const payments = await dodopayments.payments.list({
148
+ customer_id: customer.customer_id,
149
+ // page number is 0-indexed
150
+ page_number: ctx.query?.page ? ctx.query.page - 1 : undefined,
151
+ page_size: ctx.query?.limit,
152
+ status: ctx.query?.status,
153
+ });
154
+ return ctx.json({ items: payments.items });
155
+ }
156
+ catch (e) {
157
+ if (e instanceof Error) {
158
+ ctx.context.logger.error(`DodoPayments orders list failed. Error: ${e.message}`);
159
+ }
160
+ throw new APIError("INTERNAL_SERVER_ERROR", {
161
+ message: "Orders list failed",
162
+ });
163
+ }
164
+ }),
165
+ };
166
+ };
167
+ async function createCustomer(dodopayments, email, name) {
168
+ const customer = await dodopayments.customers.create({
169
+ email,
170
+ name,
171
+ });
172
+ return customer;
173
+ }
@@ -42,4 +42,3 @@ export declare const webhooks: (options: WebhookHandlerConfig) => (_dodopayments
42
42
  path: "/dodopayments/webhooks";
43
43
  };
44
44
  };
45
- //# sourceMappingURL=webhooks.d.ts.map
@@ -0,0 +1,62 @@
1
+ import { handleWebhookPayload, } from "@dodopayments/core/webhook";
2
+ import { APIError, createAuthEndpoint } from "better-auth/api";
3
+ import { verifyWebhookPayload } from "@dodopayments/core/webhook";
4
+ export const webhooks = (options) => (_dodopayments) => {
5
+ return {
6
+ dodopaymentsWebhooks: createAuthEndpoint("/dodopayments/webhooks", {
7
+ method: "POST",
8
+ metadata: {
9
+ isAction: false,
10
+ },
11
+ cloneRequest: true,
12
+ }, async (ctx) => {
13
+ const { webhookKey } = options;
14
+ if (!ctx.request?.body) {
15
+ throw new APIError("INTERNAL_SERVER_ERROR");
16
+ }
17
+ const buf = await ctx.request.text();
18
+ let event;
19
+ try {
20
+ if (!webhookKey) {
21
+ throw new APIError("INTERNAL_SERVER_ERROR", {
22
+ message: "DodoPayments webhook webhookKey not found",
23
+ });
24
+ }
25
+ const headers = {
26
+ "webhook-id": ctx.request.headers.get("webhook-id"),
27
+ "webhook-timestamp": ctx.request.headers.get("webhook-timestamp"),
28
+ "webhook-signature": ctx.request.headers.get("webhook-signature"),
29
+ };
30
+ event = await verifyWebhookPayload({
31
+ webhookKey,
32
+ headers,
33
+ body: buf,
34
+ });
35
+ }
36
+ catch (err) {
37
+ if (err instanceof Error) {
38
+ ctx.context.logger.error(`Webhook Error: ${err.message}`);
39
+ throw new APIError("BAD_REQUEST", {
40
+ message: `Webhook Error: ${err.message}`,
41
+ });
42
+ }
43
+ throw new APIError("BAD_REQUEST", {
44
+ message: `Webhook Error: ${err}`,
45
+ });
46
+ }
47
+ try {
48
+ await handleWebhookPayload(event, options);
49
+ }
50
+ catch (e) {
51
+ if (e instanceof Error) {
52
+ ctx.context.logger.error(`DodoPayments webhook failed. Error: ${e.message}`);
53
+ }
54
+ ctx.context.logger.error(`DodoPayments webhook failed. Error: ${e}`);
55
+ throw new APIError("BAD_REQUEST", {
56
+ message: "Webhook error: See server logs for more information.",
57
+ });
58
+ }
59
+ return ctx.json({ received: true });
60
+ }),
61
+ };
62
+ };
package/dist/types.d.ts CHANGED
@@ -30,4 +30,3 @@ export interface DodoPaymentsOptions {
30
30
  */
31
31
  use: DodoPaymentsPlugins;
32
32
  }
33
- //# sourceMappingURL=types.d.ts.map
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@dodopayments/better-auth",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
7
  "scripts": {
8
- "build": "rollup -c",
8
+ "build": "tsc",
9
9
  "dev": "rollup -c -w",
10
10
  "format": "prettier --write .",
11
11
  "check-format": "prettier --check .",
@@ -19,7 +19,10 @@
19
19
  }
20
20
  },
21
21
  "dependencies": {
22
- "@dodopayments/core": "*"
22
+ "@dodopayments/core": "^0.1.14"
23
+ },
24
+ "engines": {
25
+ "node": ">=16.0.0"
23
26
  },
24
27
  "keywords": [
25
28
  "dodopayments",
@@ -1 +0,0 @@
1
- {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAE5C,eAAO,MAAM,kBAAkB;;wBAGD,UAAU,CAAC,OAAO,YAAY,CAAC;CAE5D,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"customer.d.ts","sourceRoot":"","sources":["../../src/hooks/customer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAEhE,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,UAAU,CAAC;AAEpD,eAAO,MAAM,YAAY,GACtB,SAAS,mBAAmB,MACtB,MAAM,IAAI,EAAE,MAAM,sBAAsB,kBAsC9C,CAAC;AAEJ,eAAO,MAAM,YAAY,GACtB,SAAS,mBAAmB,MACtB,MAAM,IAAI,EAAE,MAAM,sBAAsB,kBA4B9C,CAAC"}