@dodopayments/better-auth 0.1.1 → 1.0.2
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 +21 -19
- package/dist/client.d.ts +0 -1
- package/dist/client.js +6 -0
- package/dist/hooks/customer.d.ts +0 -1
- package/dist/hooks/customer.js +63 -0
- package/dist/index.d.ts +158 -757
- package/dist/index.js +6 -15344
- package/dist/plugins/checkout.d.ts +1 -2
- package/dist/plugins/checkout.js +87 -0
- package/dist/plugins/portal.d.ts +12 -13
- package/dist/plugins/portal.js +173 -0
- package/dist/plugins/webhooks.d.ts +1 -2
- package/dist/plugins/webhooks.js +62 -0
- package/dist/types.d.ts +0 -1
- package/dist/types.js +1 -0
- package/package.json +5 -2
- package/dist/client.d.ts.map +0 -1
- package/dist/hooks/customer.d.ts.map +0 -1
- package/dist/index.cjs +0 -15379
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/plugins/checkout.d.ts.map +0 -1
- package/dist/plugins/portal.d.ts.map +0 -1
- package/dist/plugins/webhooks.d.ts.map +0 -1
- package/dist/types.d.ts.map +0 -1
package/README.md
CHANGED
|
@@ -25,7 +25,7 @@ Add the required environment variables to your `.env` file:
|
|
|
25
25
|
```env
|
|
26
26
|
# Get this from your Dodo Payments Dashboard > Developer > API Keys
|
|
27
27
|
DODO_PAYMENTS_API_KEY=your_api_key_here
|
|
28
|
-
# use the webhook endpoint `/api/auth/webhooks
|
|
28
|
+
# use the webhook endpoint `/api/auth/dodopayments/webhooks` to generate a webhook secret
|
|
29
29
|
# from Dodo Payments Dashboard > Developer > Webhooks
|
|
30
30
|
DODO_PAYMENTS_WEBHOOK_SECRET=your_webhook_secret_here # Use
|
|
31
31
|
BETTER_AUTH_URL=http://localhost:3000
|
|
@@ -104,7 +104,7 @@ export const authClient = createAuthClient({
|
|
|
104
104
|
|
|
105
105
|
```typescript
|
|
106
106
|
// Create checkout with customer details
|
|
107
|
-
const { data: checkout, error } = await authClient.checkout({
|
|
107
|
+
const { data: checkout, error } = await authClient.dodopayments.checkout({
|
|
108
108
|
slug: "premium-plan", // The slug you provided in the checkout configuration
|
|
109
109
|
// product_id: "pdt_xxxxxxxxxxxxxxxxxxxxx", // Alternatively, use the product ID
|
|
110
110
|
customer: {
|
|
@@ -129,7 +129,8 @@ window.location.href = checkout.url;
|
|
|
129
129
|
|
|
130
130
|
```typescript
|
|
131
131
|
// Redirect to customer portal (requires authentication)
|
|
132
|
-
const { data: customerPortal, error } =
|
|
132
|
+
const { data: customerPortal, error } =
|
|
133
|
+
await authClient.dodopayments.customer.portal();
|
|
133
134
|
if (customerPortal && customerPortal.redirect) {
|
|
134
135
|
window.location.href = customerPortal.url;
|
|
135
136
|
}
|
|
@@ -140,7 +141,7 @@ if (customerPortal && customerPortal.redirect) {
|
|
|
140
141
|
```typescript
|
|
141
142
|
// Get customer's subscriptions
|
|
142
143
|
const { data: subscriptions, error } =
|
|
143
|
-
await authClient.customer.subscriptions.list({
|
|
144
|
+
await authClient.dodopayments.customer.subscriptions.list({
|
|
144
145
|
query: {
|
|
145
146
|
limit: 10,
|
|
146
147
|
page: 1,
|
|
@@ -149,18 +150,19 @@ const { data: subscriptions, error } =
|
|
|
149
150
|
});
|
|
150
151
|
|
|
151
152
|
// Get customer's payment history
|
|
152
|
-
const { data: payments, error } =
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
}
|
|
153
|
+
const { data: payments, error } =
|
|
154
|
+
await authClient.dodopayments.customer.payments.list({
|
|
155
|
+
query: {
|
|
156
|
+
limit: 10,
|
|
157
|
+
page: 1,
|
|
158
|
+
status: "succeeded", // Filter by payment status
|
|
159
|
+
},
|
|
160
|
+
});
|
|
159
161
|
```
|
|
160
162
|
|
|
161
163
|
## Webhooks
|
|
162
164
|
|
|
163
|
-
The webhooks plugin handles real-time payment events from Dodo Payments with secure signature verification. If you followed the default better-auth setup, the webhook endpoint will be `/api/auth/webhooks
|
|
165
|
+
The webhooks plugin handles real-time payment events from Dodo Payments with secure signature verification. If you followed the default better-auth setup, the webhook endpoint will be `/api/auth/dodopayments/webhooks`. Generate a webhook secret for the URL `https://<your-domain>/api/auth/dodopayments/webhooks` and set it in your ENV as follows and use it in the `webhooks` plugin setup.
|
|
164
166
|
|
|
165
167
|
```env
|
|
166
168
|
DODO_PAYMENTS_WEBHOOK_SECRET=your_webhook_secret_here
|
|
@@ -322,7 +324,7 @@ use: [
|
|
|
322
324
|
],
|
|
323
325
|
|
|
324
326
|
Usage Example:
|
|
325
|
-
const { data: checkout, error } = await authClient.checkout({
|
|
327
|
+
const { data: checkout, error } = await authClient.dodopayments.checkout({
|
|
326
328
|
slug: "premium-plan", // Use the slug from your configuration
|
|
327
329
|
customer: {
|
|
328
330
|
email: "customer@example.com",
|
|
@@ -362,13 +364,13 @@ use: [
|
|
|
362
364
|
|
|
363
365
|
Usage Examples:
|
|
364
366
|
// Access customer portal
|
|
365
|
-
const { data: customerPortal, error } = await authClient.customer.portal();
|
|
367
|
+
const { data: customerPortal, error } = await authClient.dodopayments.customer.portal();
|
|
366
368
|
if (customerPortal && customerPortal.redirect) {
|
|
367
369
|
window.location.href = customerPortal.url;
|
|
368
370
|
}
|
|
369
371
|
|
|
370
372
|
// List customer subscriptions
|
|
371
|
-
const { data: subscriptions, error } = await authClient.customer.subscriptions.list({
|
|
373
|
+
const { data: subscriptions, error } = await authClient.dodopayments.customer.subscriptions.list({
|
|
372
374
|
query: {
|
|
373
375
|
limit: 10,
|
|
374
376
|
page: 1,
|
|
@@ -377,7 +379,7 @@ const { data: subscriptions, error } = await authClient.customer.subscriptions.l
|
|
|
377
379
|
});
|
|
378
380
|
|
|
379
381
|
// List customer payments
|
|
380
|
-
const { data: payments, error } = await authClient.customer.payments.list({
|
|
382
|
+
const { data: payments, error } = await authClient.dodopayments.customer.payments.list({
|
|
381
383
|
query: {
|
|
382
384
|
limit: 10,
|
|
383
385
|
page: 1,
|
|
@@ -401,7 +403,7 @@ Ask the user: What is your domain name? Please provide:
|
|
|
401
403
|
- For development: use "localhost:3000" (or your local port)
|
|
402
404
|
|
|
403
405
|
STEP 2: After receiving the user's domain name, you will:
|
|
404
|
-
- Generate their webhook URL: https://[USER-DOMAIN]/api/auth/webhooks
|
|
406
|
+
- Generate their webhook URL: https://[USER-DOMAIN]/api/auth/dodopayments/webhooks
|
|
405
407
|
- Provide them with a TODO list for webhook setup in Dodo Payments dashboard
|
|
406
408
|
- Give them the exact environment variable setup instructions
|
|
407
409
|
|
|
@@ -409,7 +411,7 @@ WEBHOOK SETUP TODO LIST (provide this after domain input):
|
|
|
409
411
|
1. Configure webhook in Dodo Payments Dashboard:
|
|
410
412
|
- Go to Dodo Payments Dashboard > Developer > Webhooks
|
|
411
413
|
- Click "Add Webhook" or "Create Webhook"
|
|
412
|
-
- Enter webhook URL: https://[USER-DOMAIN]/api/auth/webhooks
|
|
414
|
+
- Enter webhook URL: https://[USER-DOMAIN]/api/auth/dodopayments/webhooks
|
|
413
415
|
- Select events you want to receive (or select all)
|
|
414
416
|
- Copy the generated webhook secret
|
|
415
417
|
|
|
@@ -575,7 +577,7 @@ IMPORTANT NOTES:
|
|
|
575
577
|
- Product creation in Dodo Payments dashboard (for checkout plugin)
|
|
576
578
|
- Webhook setup in Dodo Payments dashboard (for webhooks plugin)
|
|
577
579
|
- Domain name collection for webhook URL generation
|
|
578
|
-
5. For webhook plugin: Ask for the user's domain name and generate the exact webhook URL: https://[user-domain]/api/auth/webhooks
|
|
580
|
+
5. For webhook plugin: Ask for the user's domain name and generate the exact webhook URL: https://[user-domain]/api/auth/dodopayments/webhooks
|
|
579
581
|
6. All client methods return { data, error } objects for proper error handling
|
|
580
582
|
7. Use test_mode for development and live_mode for production
|
|
581
583
|
8. The webhook endpoint is automatically created and secured with signature verification (if webhooks plugin is selected)
|
package/dist/client.d.ts
CHANGED
package/dist/client.js
ADDED
package/dist/hooks/customer.d.ts
CHANGED
|
@@ -2,4 +2,3 @@ import type { GenericEndpointContext, User } from "better-auth";
|
|
|
2
2
|
import type { DodoPaymentsOptions } from "../types";
|
|
3
3
|
export declare const onUserCreate: (options: DodoPaymentsOptions) => (user: User, ctx?: GenericEndpointContext) => Promise<void>;
|
|
4
4
|
export declare const onUserUpdate: (options: DodoPaymentsOptions) => (user: User, ctx?: GenericEndpointContext) => Promise<void>;
|
|
5
|
-
//# sourceMappingURL=customer.d.ts.map
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { APIError } from "better-auth/api";
|
|
2
|
+
export const onUserCreate = (options) => async (user, ctx) => {
|
|
3
|
+
if (ctx && options.createCustomerOnSignUp) {
|
|
4
|
+
try {
|
|
5
|
+
const customers = await options.client.customers.list({
|
|
6
|
+
email: user.email,
|
|
7
|
+
});
|
|
8
|
+
const existingCustomer = customers.items[0];
|
|
9
|
+
if (existingCustomer) {
|
|
10
|
+
if (existingCustomer.email !== user.email) {
|
|
11
|
+
await options.client.customers.update(existingCustomer.customer_id, {
|
|
12
|
+
name: user.name,
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
// TODO: Add metadata to customer object via
|
|
18
|
+
// getCustomerCreateParams option when it becomes
|
|
19
|
+
// available in the API
|
|
20
|
+
await options.client.customers.create({
|
|
21
|
+
email: user.email,
|
|
22
|
+
name: user.name,
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
catch (e) {
|
|
27
|
+
if (e instanceof Error) {
|
|
28
|
+
throw new APIError("INTERNAL_SERVER_ERROR", {
|
|
29
|
+
message: `DodoPayments customer creation failed. Error: ${e.message}`,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
throw new APIError("INTERNAL_SERVER_ERROR", {
|
|
33
|
+
message: `DodoPayments customer creation failed. Error: ${e}`,
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
export const onUserUpdate = (options) => async (user, ctx) => {
|
|
39
|
+
if (ctx && options.createCustomerOnSignUp) {
|
|
40
|
+
try {
|
|
41
|
+
const customers = await options.client.customers.list({
|
|
42
|
+
email: user.email,
|
|
43
|
+
});
|
|
44
|
+
const existingCustomer = customers.items[0];
|
|
45
|
+
if (existingCustomer) {
|
|
46
|
+
// TODO: Add metadata to customer object via
|
|
47
|
+
// getCustomerCreateParams option when it becomes
|
|
48
|
+
// available in the API
|
|
49
|
+
await options.client.customers.update(existingCustomer.customer_id, {
|
|
50
|
+
name: user.name,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
catch (e) {
|
|
55
|
+
if (e instanceof Error) {
|
|
56
|
+
ctx.context.logger.error(`DodoPayments customer update failed. Error: ${e.message}`);
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
ctx.context.logger.error(`DodoPayments customer update failed. Error: ${e}`);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
};
|