@dodopayments/better-auth 0.1.1
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 +585 -0
- package/dist/client.d.ts +6 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/hooks/customer.d.ts +5 -0
- package/dist/hooks/customer.d.ts.map +1 -0
- package/dist/index.cjs +15379 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +1162 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +15373 -0
- package/dist/index.js.map +1 -0
- package/dist/plugins/checkout.d.ts +304 -0
- package/dist/plugins/checkout.d.ts.map +1 -0
- package/dist/plugins/portal.d.ts +218 -0
- package/dist/plugins/portal.d.ts.map +1 -0
- package/dist/plugins/webhooks.d.ts +45 -0
- package/dist/plugins/webhooks.d.ts.map +1 -0
- package/dist/types.d.ts +33 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +56 -0
package/README.md
ADDED
|
@@ -0,0 +1,585 @@
|
|
|
1
|
+
# @dodopayments/better-auth
|
|
2
|
+
|
|
3
|
+
A Better Auth plugin for integrating Dodo Payments into your authentication flow.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Automatic Customer Management** - Creates Dodo Payments customers when users sign up
|
|
8
|
+
- **Secure Checkout Integration** - Type-safe checkout flows with product slug mapping
|
|
9
|
+
- **Customer Portal Access** - Self-service portal for subscription and payment management
|
|
10
|
+
- **Webhook Event Handling** - Real-time payment event processing with signature verification
|
|
11
|
+
- **TypeScript Support** - Full type safety with TypeScript definitions
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install @dodopayments/better-auth dodopayments better-auth zod
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Setup
|
|
20
|
+
|
|
21
|
+
### 1. Environment Variables
|
|
22
|
+
|
|
23
|
+
Add the required environment variables to your `.env` file:
|
|
24
|
+
|
|
25
|
+
```env
|
|
26
|
+
# Get this from your Dodo Payments Dashboard > Developer > API Keys
|
|
27
|
+
DODO_PAYMENTS_API_KEY=your_api_key_here
|
|
28
|
+
# use the webhook endpoint `/api/auth/webhooks/dodopayments` to generate a webhook secret
|
|
29
|
+
# from Dodo Payments Dashboard > Developer > Webhooks
|
|
30
|
+
DODO_PAYMENTS_WEBHOOK_SECRET=your_webhook_secret_here # Use
|
|
31
|
+
BETTER_AUTH_URL=http://localhost:3000
|
|
32
|
+
BETTER_AUTH_SECRET=your_better_auth_secret_here
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### 2. Server Configuration
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
// src/lib/auth.ts
|
|
39
|
+
import { BetterAuth } from "better-auth";
|
|
40
|
+
import {
|
|
41
|
+
dodopayments,
|
|
42
|
+
checkout,
|
|
43
|
+
portal,
|
|
44
|
+
webhooks,
|
|
45
|
+
} from "@dodopayments/better-auth";
|
|
46
|
+
import DodoPayments from "dodopayments";
|
|
47
|
+
|
|
48
|
+
// Create a DodoPayments client instance
|
|
49
|
+
export const dodoPayments = new DodoPayments({
|
|
50
|
+
bearerToken: process.env.DODO_PAYMENTS_API_KEY!,
|
|
51
|
+
environment: "test_mode", // or "live_mode" for production
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// Configure the dodopayments adapter
|
|
55
|
+
export const { auth, endpoints, client } = BetterAuth({
|
|
56
|
+
plugins: [
|
|
57
|
+
dodopayments({
|
|
58
|
+
client: dodoPayments,
|
|
59
|
+
createCustomerOnSignUp: true, // Auto-create customers on user signup
|
|
60
|
+
use: [
|
|
61
|
+
checkout({
|
|
62
|
+
products: [
|
|
63
|
+
{
|
|
64
|
+
productId: "pdt_xxxxxxxxxxxxxxxxxxxxx", // Your product ID
|
|
65
|
+
slug: "premium-plan", // Friendly slug for checkout
|
|
66
|
+
},
|
|
67
|
+
],
|
|
68
|
+
successUrl: "/dashboard/success",
|
|
69
|
+
authenticatedUsersOnly: true, // Require login for checkout
|
|
70
|
+
}),
|
|
71
|
+
portal(),
|
|
72
|
+
webhooks({
|
|
73
|
+
webhookKey: process.env.DODO_PAYMENTS_WEBHOOK_SECRET!,
|
|
74
|
+
onPayload: async (payload) => {
|
|
75
|
+
// Handle all webhook payloads
|
|
76
|
+
console.log("Received webhook:", payload.event_type);
|
|
77
|
+
},
|
|
78
|
+
// ...or use one of the other granular event handlers
|
|
79
|
+
}),
|
|
80
|
+
],
|
|
81
|
+
}),
|
|
82
|
+
],
|
|
83
|
+
});
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### 3. Client Configuration
|
|
87
|
+
|
|
88
|
+
Initialize the client in your application to interact with the payment endpoints:
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
// src/lib/auth-client.ts
|
|
92
|
+
import { createAuthClient } from "better-auth/react";
|
|
93
|
+
import { dodopaymentsClient } from "@dodopayments/better-auth";
|
|
94
|
+
|
|
95
|
+
export const authClient = createAuthClient({
|
|
96
|
+
baseURL: process.env.BETTER_AUTH_URL || "http://localhost:3000",
|
|
97
|
+
plugins: [dodopaymentsClient()],
|
|
98
|
+
});
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Usage
|
|
102
|
+
|
|
103
|
+
### Creating a Checkout Session
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
// Create checkout with customer details
|
|
107
|
+
const { data: checkout, error } = await authClient.checkout({
|
|
108
|
+
slug: "premium-plan", // The slug you provided in the checkout configuration
|
|
109
|
+
// product_id: "pdt_xxxxxxxxxxxxxxxxxxxxx", // Alternatively, use the product ID
|
|
110
|
+
customer: {
|
|
111
|
+
email: "customer@example.com",
|
|
112
|
+
name: "John Doe",
|
|
113
|
+
},
|
|
114
|
+
billing: {
|
|
115
|
+
city: "San Francisco",
|
|
116
|
+
country: "US",
|
|
117
|
+
state: "CA",
|
|
118
|
+
street: "123 Market St",
|
|
119
|
+
zipcode: "94103",
|
|
120
|
+
},
|
|
121
|
+
referenceId: "order_123", // Optional reference for your records
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
// Redirect to checkout URL
|
|
125
|
+
window.location.href = checkout.url;
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### Accessing Customer Portal
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
// Redirect to customer portal (requires authentication)
|
|
132
|
+
const { data: customerPortal, error } = await authClient.customer.portal();
|
|
133
|
+
if (customerPortal && customerPortal.redirect) {
|
|
134
|
+
window.location.href = customerPortal.url;
|
|
135
|
+
}
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### Listing Customer Data
|
|
139
|
+
|
|
140
|
+
```typescript
|
|
141
|
+
// Get customer's subscriptions
|
|
142
|
+
const { data: subscriptions, error } =
|
|
143
|
+
await authClient.customer.subscriptions.list({
|
|
144
|
+
query: {
|
|
145
|
+
limit: 10,
|
|
146
|
+
page: 1,
|
|
147
|
+
active: true, // Filter for active subscriptions only
|
|
148
|
+
},
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
// Get customer's payment history
|
|
152
|
+
const { data: payments, error } = await authClient.customer.payments.list({
|
|
153
|
+
query: {
|
|
154
|
+
limit: 10,
|
|
155
|
+
page: 1,
|
|
156
|
+
status: "succeeded", // Filter by payment status
|
|
157
|
+
},
|
|
158
|
+
});
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
## Webhooks
|
|
162
|
+
|
|
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/dodopayments`. Generate a webhook secret for the URL `https://<your-domain>/api/auth/webhooks/dodopayments` and set it in your ENV as follows and use it in the `webhooks` plugin setup.
|
|
164
|
+
|
|
165
|
+
```env
|
|
166
|
+
DODO_PAYMENTS_WEBHOOK_SECRET=your_webhook_secret_here
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### Event Handlers
|
|
170
|
+
|
|
171
|
+
```typescript
|
|
172
|
+
webhooks({
|
|
173
|
+
webhookKey: process.env.DODO_PAYMENTS_WEBHOOK_SECRET!,
|
|
174
|
+
// Generic handler for all webhook payloads
|
|
175
|
+
onPayload: async (payload) => {
|
|
176
|
+
console.log("Received webhook:", payload.event_type);
|
|
177
|
+
},
|
|
178
|
+
// Specific event handlers
|
|
179
|
+
});
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
## Configuration
|
|
183
|
+
|
|
184
|
+
### Plugin Options
|
|
185
|
+
|
|
186
|
+
- **`client`** (required) - DodoPayments client instance
|
|
187
|
+
- **`createCustomerOnSignUp`** (optional) - Auto-create customers on user signup
|
|
188
|
+
- **`use`** (required) - Array of plugins to enable (checkout, portal, webhooks)
|
|
189
|
+
|
|
190
|
+
### Checkout Plugin Options
|
|
191
|
+
|
|
192
|
+
- **`products`** - Array of products or async function returning products
|
|
193
|
+
- **`successUrl`** - URL to redirect after successful payment
|
|
194
|
+
- **`authenticatedUsersOnly`** - Require user authentication (default: false)
|
|
195
|
+
|
|
196
|
+
## Prompt for LLMs
|
|
197
|
+
|
|
198
|
+
```
|
|
199
|
+
You are a skilled developer helping to integrate the @dodopayments/better-auth adapter into a typescript web application with better-auth. This adapter enables seamless payment processing through Dodo Payments with automatic customer management, checkout flows, and webhook handling.
|
|
200
|
+
|
|
201
|
+
STAGE 1: BASIC SETUP
|
|
202
|
+
|
|
203
|
+
This stage covers the foundational setup needed before implementing any plugins. Complete this stage first.
|
|
204
|
+
|
|
205
|
+
STEP 1: Installation
|
|
206
|
+
Install the required dependencies:
|
|
207
|
+
npm install @dodopayments/better-auth dodopayments better-auth zod
|
|
208
|
+
|
|
209
|
+
STEP 2: Environment Variables Setup
|
|
210
|
+
You will need to complete these external setup tasks. You will provide the user with a TODO list for the actions they need to take outside of the code:
|
|
211
|
+
|
|
212
|
+
TODO LIST FOR USER:
|
|
213
|
+
1. Generate Dodo Payments API Key:
|
|
214
|
+
- Go to your Dodo Payments Dashboard > Developer > API Keys
|
|
215
|
+
- Create a new API key (or use existing)
|
|
216
|
+
- Copy the API key value
|
|
217
|
+
- Set environment variable: DODO_PAYMENTS_API_KEY=your_api_key_here
|
|
218
|
+
|
|
219
|
+
2. Generate Better Auth Secret:
|
|
220
|
+
- Generate a random secret key (32+ characters)
|
|
221
|
+
- Set environment variable: BETTER_AUTH_SECRET=your_better_auth_secret_here
|
|
222
|
+
|
|
223
|
+
3. Set Application URL:
|
|
224
|
+
- For development: BETTER_AUTH_URL=http://localhost:3000
|
|
225
|
+
- For production: BETTER_AUTH_URL=https://your-domain.com
|
|
226
|
+
|
|
227
|
+
4. Webhook Secret (only if implementing webhooks plugin):
|
|
228
|
+
- This will be provided after you specify your domain name in Stage 2
|
|
229
|
+
- Set environment variable: DODO_PAYMENTS_WEBHOOK_SECRET=your_webhook_secret_here
|
|
230
|
+
|
|
231
|
+
Add these environment variables to your .env file:
|
|
232
|
+
DODO_PAYMENTS_API_KEY=your_api_key_here
|
|
233
|
+
DODO_PAYMENTS_WEBHOOK_SECRET=your_webhook_secret_here
|
|
234
|
+
BETTER_AUTH_URL=http://localhost:3000
|
|
235
|
+
BETTER_AUTH_SECRET=your_better_auth_secret_here
|
|
236
|
+
|
|
237
|
+
STEP 3: Server Configuration
|
|
238
|
+
Create or update your better-auth setup file (src/lib/auth.ts):
|
|
239
|
+
|
|
240
|
+
import { BetterAuth } from "better-auth";
|
|
241
|
+
import { dodopayments } from "@dodopayments/better-auth";
|
|
242
|
+
import DodoPayments from "dodopayments";
|
|
243
|
+
|
|
244
|
+
// Create DodoPayments client
|
|
245
|
+
export const dodoPayments = new DodoPayments({
|
|
246
|
+
bearerToken: process.env.DODO_PAYMENTS_API_KEY!,
|
|
247
|
+
environment: "test_mode", // Change to "live_mode" for production
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
// Configure better-auth with dodopayments adapter
|
|
251
|
+
export const { auth, endpoints, client } = BetterAuth({
|
|
252
|
+
plugins: [
|
|
253
|
+
dodopayments({
|
|
254
|
+
client: dodoPayments,
|
|
255
|
+
createCustomerOnSignUp: true, // Auto-create customers on signup
|
|
256
|
+
use: [], // We'll add plugins here in Stage 2
|
|
257
|
+
}),
|
|
258
|
+
],
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
STEP 4: Client Configuration
|
|
262
|
+
Create or update your auth client file (src/lib/auth-client.ts):
|
|
263
|
+
|
|
264
|
+
import { createAuthClient } from "better-auth/react";
|
|
265
|
+
import { dodopaymentsClient } from "@dodopayments/better-auth";
|
|
266
|
+
|
|
267
|
+
export const authClient = createAuthClient({
|
|
268
|
+
baseURL: process.env.BETTER_AUTH_URL || "http://localhost:3000",
|
|
269
|
+
plugins: [dodopaymentsClient()],
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
STAGE 2: PLUGIN IMPLEMENTATION
|
|
273
|
+
|
|
274
|
+
After completing Stage 1, you can selectively implement any of these plugins based on your needs. Each plugin is independent and can be added or removed as needed.
|
|
275
|
+
|
|
276
|
+
PLUGIN SELECTION:
|
|
277
|
+
Before implementing any plugins, ask the user which plugins they want to implement:
|
|
278
|
+
|
|
279
|
+
"Which plugins would you like to implement? You can choose any combination of:
|
|
280
|
+
1. CHECKOUT - Enables secure payment processing and checkout flows
|
|
281
|
+
2. PORTAL - Provides customer self-service portal for subscriptions and payments
|
|
282
|
+
3. WEBHOOKS - Handles real-time payment events from Dodo Payments
|
|
283
|
+
|
|
284
|
+
Please specify which plugins you want (e.g., 'checkout and webhooks', 'all three', 'just portal', etc.)"
|
|
285
|
+
|
|
286
|
+
If the user doesn't respond or you cannot prompt the user, implement all three plugins by default.
|
|
287
|
+
|
|
288
|
+
Based on the user's selection, implement only the requested plugins from the sections below:
|
|
289
|
+
|
|
290
|
+
CHECKOUT PLUGIN
|
|
291
|
+
|
|
292
|
+
Purpose: Enables secure payment processing with product slug mapping and session integration.
|
|
293
|
+
|
|
294
|
+
SETUP TODO LIST FOR USER:
|
|
295
|
+
1. Create products in Dodo Payments Dashboard:
|
|
296
|
+
- Go to Dodo Payments Dashboard > Products
|
|
297
|
+
- Create your products (e.g., subscription plans, one-time purchases)
|
|
298
|
+
- Copy each product ID (starts with "pdt_")
|
|
299
|
+
- Note down the product names for creating friendly slugs
|
|
300
|
+
|
|
301
|
+
2. Plan your checkout URLs:
|
|
302
|
+
- Decide on your success URL (e.g., "/dashboard/success", "/thank-you")
|
|
303
|
+
- Ensure this URL exists in your application
|
|
304
|
+
|
|
305
|
+
Configuration:
|
|
306
|
+
Add checkout to your imports in src/lib/auth.ts:
|
|
307
|
+
import { dodopayments, checkout } from "@dodopayments/better-auth";
|
|
308
|
+
|
|
309
|
+
Add checkout plugin to the use array in your dodopayments configuration:
|
|
310
|
+
use: [
|
|
311
|
+
checkout({
|
|
312
|
+
products: [
|
|
313
|
+
{
|
|
314
|
+
productId: "pdt_xxxxxxxxxxxxxxxxxxxxx", // Your actual product ID from Dodo Payments
|
|
315
|
+
slug: "premium-plan", // Friendly slug for checkout
|
|
316
|
+
},
|
|
317
|
+
// Add more products as needed
|
|
318
|
+
],
|
|
319
|
+
successUrl: "/dashboard/success", // Your success page URL
|
|
320
|
+
authenticatedUsersOnly: true, // Require login for checkout
|
|
321
|
+
}),
|
|
322
|
+
],
|
|
323
|
+
|
|
324
|
+
Usage Example:
|
|
325
|
+
const { data: checkout, error } = await authClient.checkout({
|
|
326
|
+
slug: "premium-plan", // Use the slug from your configuration
|
|
327
|
+
customer: {
|
|
328
|
+
email: "customer@example.com",
|
|
329
|
+
name: "John Doe",
|
|
330
|
+
},
|
|
331
|
+
billing: {
|
|
332
|
+
city: "San Francisco",
|
|
333
|
+
country: "US",
|
|
334
|
+
state: "CA",
|
|
335
|
+
street: "123 Market St",
|
|
336
|
+
zipcode: "94103",
|
|
337
|
+
},
|
|
338
|
+
referenceId: "order_123", // Optional reference
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
if (checkout) {
|
|
342
|
+
window.location.href = checkout.url;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
Options:
|
|
346
|
+
- products: Array of products or async function returning products
|
|
347
|
+
- successUrl: URL to redirect after successful payment
|
|
348
|
+
- authenticatedUsersOnly: Require user authentication (default: false)
|
|
349
|
+
|
|
350
|
+
PORTAL PLUGIN
|
|
351
|
+
|
|
352
|
+
Purpose: Provides customer self-service capabilities for managing subscriptions and viewing payment history.
|
|
353
|
+
|
|
354
|
+
Configuration:
|
|
355
|
+
Add portal to your imports in src/lib/auth.ts:
|
|
356
|
+
import { dodopayments, portal } from "@dodopayments/better-auth";
|
|
357
|
+
|
|
358
|
+
Add portal plugin to the use array in your dodopayments configuration:
|
|
359
|
+
use: [
|
|
360
|
+
portal(),
|
|
361
|
+
],
|
|
362
|
+
|
|
363
|
+
Usage Examples:
|
|
364
|
+
// Access customer portal
|
|
365
|
+
const { data: customerPortal, error } = await authClient.customer.portal();
|
|
366
|
+
if (customerPortal && customerPortal.redirect) {
|
|
367
|
+
window.location.href = customerPortal.url;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
// List customer subscriptions
|
|
371
|
+
const { data: subscriptions, error } = await authClient.customer.subscriptions.list({
|
|
372
|
+
query: {
|
|
373
|
+
limit: 10,
|
|
374
|
+
page: 1,
|
|
375
|
+
active: true,
|
|
376
|
+
},
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
// List customer payments
|
|
380
|
+
const { data: payments, error } = await authClient.customer.payments.list({
|
|
381
|
+
query: {
|
|
382
|
+
limit: 10,
|
|
383
|
+
page: 1,
|
|
384
|
+
status: "succeeded",
|
|
385
|
+
},
|
|
386
|
+
});
|
|
387
|
+
|
|
388
|
+
Note: All portal methods require user authentication.
|
|
389
|
+
|
|
390
|
+
WEBHOOKS PLUGIN
|
|
391
|
+
|
|
392
|
+
Purpose: Handles real-time payment events from Dodo Payments with secure signature verification.
|
|
393
|
+
|
|
394
|
+
BEFORE CONFIGURATION - Setup Webhook URL:
|
|
395
|
+
First, you need the user's domain name to generate the webhook URL and provide them with setup instructions.
|
|
396
|
+
|
|
397
|
+
STEP 1: Domain Name Input
|
|
398
|
+
Ask the user: What is your domain name? Please provide:
|
|
399
|
+
- For production: your domain name (e.g., "myapp.com", "api.mycompany.com")
|
|
400
|
+
- For staging: your staging domain (e.g., "staging.myapp.com")
|
|
401
|
+
- For development: use "localhost:3000" (or your local port)
|
|
402
|
+
|
|
403
|
+
STEP 2: After receiving the user's domain name, you will:
|
|
404
|
+
- Generate their webhook URL: https://[USER-DOMAIN]/api/auth/webhooks/dodopayments
|
|
405
|
+
- Provide them with a TODO list for webhook setup in Dodo Payments dashboard
|
|
406
|
+
- Give them the exact environment variable setup instructions
|
|
407
|
+
|
|
408
|
+
WEBHOOK SETUP TODO LIST (provide this after domain input):
|
|
409
|
+
1. Configure webhook in Dodo Payments Dashboard:
|
|
410
|
+
- Go to Dodo Payments Dashboard > Developer > Webhooks
|
|
411
|
+
- Click "Add Webhook" or "Create Webhook"
|
|
412
|
+
- Enter webhook URL: https://[USER-DOMAIN]/api/auth/webhooks/dodopayments
|
|
413
|
+
- Select events you want to receive (or select all)
|
|
414
|
+
- Copy the generated webhook secret
|
|
415
|
+
|
|
416
|
+
2. Set webhook secret in your environment:
|
|
417
|
+
- For production: Set DODO_PAYMENTS_WEBHOOK_SECRET in your hosting provider environment
|
|
418
|
+
- For staging: Set DODO_PAYMENTS_WEBHOOK_SECRET in your staging environment
|
|
419
|
+
- For development: Add DODO_PAYMENTS_WEBHOOK_SECRET=your_webhook_secret_here to your .env file
|
|
420
|
+
|
|
421
|
+
3. Deploy your application with the webhook secret configured
|
|
422
|
+
|
|
423
|
+
STEP 3: Add webhooks to your imports in src/lib/auth.ts:
|
|
424
|
+
import { dodopayments, webhooks } from "@dodopayments/better-auth";
|
|
425
|
+
|
|
426
|
+
STEP 4: Add webhooks plugin to the use array in your dodopayments configuration:
|
|
427
|
+
use: [
|
|
428
|
+
webhooks({
|
|
429
|
+
webhookKey: process.env.DODO_PAYMENTS_WEBHOOK_SECRET!,
|
|
430
|
+
// Generic handler for all webhook events
|
|
431
|
+
onPayload: async (payload) => {
|
|
432
|
+
console.log("Received webhook:", payload.event_type);
|
|
433
|
+
},
|
|
434
|
+
// Payment event handlers
|
|
435
|
+
onPaymentSucceeded: async (payload) => {
|
|
436
|
+
console.log("Payment succeeded:", payload);
|
|
437
|
+
},
|
|
438
|
+
onPaymentFailed: async (payload) => {
|
|
439
|
+
console.log("Payment failed:", payload);
|
|
440
|
+
},
|
|
441
|
+
onPaymentProcessing: async (payload) => {
|
|
442
|
+
console.log("Payment processing:", payload);
|
|
443
|
+
},
|
|
444
|
+
onPaymentCancelled: async (payload) => {
|
|
445
|
+
console.log("Payment cancelled:", payload);
|
|
446
|
+
},
|
|
447
|
+
// Refund event handlers
|
|
448
|
+
onRefundSucceeded: async (payload) => {
|
|
449
|
+
console.log("Refund succeeded:", payload);
|
|
450
|
+
},
|
|
451
|
+
onRefundFailed: async (payload) => {
|
|
452
|
+
console.log("Refund failed:", payload);
|
|
453
|
+
},
|
|
454
|
+
// Dispute event handlers
|
|
455
|
+
onDisputeOpened: async (payload) => {
|
|
456
|
+
console.log("Dispute opened:", payload);
|
|
457
|
+
},
|
|
458
|
+
onDisputeExpired: async (payload) => {
|
|
459
|
+
console.log("Dispute expired:", payload);
|
|
460
|
+
},
|
|
461
|
+
onDisputeAccepted: async (payload) => {
|
|
462
|
+
console.log("Dispute accepted:", payload);
|
|
463
|
+
},
|
|
464
|
+
onDisputeCancelled: async (payload) => {
|
|
465
|
+
console.log("Dispute cancelled:", payload);
|
|
466
|
+
},
|
|
467
|
+
onDisputeChallenged: async (payload) => {
|
|
468
|
+
console.log("Dispute challenged:", payload);
|
|
469
|
+
},
|
|
470
|
+
onDisputeWon: async (payload) => {
|
|
471
|
+
console.log("Dispute won:", payload);
|
|
472
|
+
},
|
|
473
|
+
onDisputeLost: async (payload) => {
|
|
474
|
+
console.log("Dispute lost:", payload);
|
|
475
|
+
},
|
|
476
|
+
// Subscription event handlers
|
|
477
|
+
onSubscriptionActive: async (payload) => {
|
|
478
|
+
console.log("Subscription active:", payload);
|
|
479
|
+
},
|
|
480
|
+
onSubscriptionOnHold: async (payload) => {
|
|
481
|
+
console.log("Subscription on hold:", payload);
|
|
482
|
+
},
|
|
483
|
+
onSubscriptionRenewed: async (payload) => {
|
|
484
|
+
console.log("Subscription renewed:", payload);
|
|
485
|
+
},
|
|
486
|
+
onSubscriptionPaused: async (payload) => {
|
|
487
|
+
console.log("Subscription paused:", payload);
|
|
488
|
+
},
|
|
489
|
+
onSubscriptionPlanChanged: async (payload) => {
|
|
490
|
+
console.log("Subscription plan changed:", payload);
|
|
491
|
+
},
|
|
492
|
+
onSubscriptionCancelled: async (payload) => {
|
|
493
|
+
console.log("Subscription cancelled:", payload);
|
|
494
|
+
},
|
|
495
|
+
onSubscriptionFailed: async (payload) => {
|
|
496
|
+
console.log("Subscription failed:", payload);
|
|
497
|
+
},
|
|
498
|
+
onSubscriptionExpired: async (payload) => {
|
|
499
|
+
console.log("Subscription expired:", payload);
|
|
500
|
+
},
|
|
501
|
+
// License key event handlers
|
|
502
|
+
onLicenseKeyCreated: async (payload) => {
|
|
503
|
+
console.log("License key created:", payload);
|
|
504
|
+
},
|
|
505
|
+
}),
|
|
506
|
+
],
|
|
507
|
+
|
|
508
|
+
Supported Webhook Event Handlers:
|
|
509
|
+
- onPayload: Generic handler for all webhook events
|
|
510
|
+
- onPaymentSucceeded: Payment completed successfully
|
|
511
|
+
- onPaymentFailed: Payment failed
|
|
512
|
+
- onPaymentProcessing: Payment is being processed
|
|
513
|
+
- onPaymentCancelled: Payment was cancelled
|
|
514
|
+
- onRefundSucceeded: Refund completed successfully
|
|
515
|
+
- onRefundFailed: Refund failed
|
|
516
|
+
- onDisputeOpened: Dispute was opened
|
|
517
|
+
- onDisputeExpired: Dispute expired
|
|
518
|
+
- onDisputeAccepted: Dispute was accepted
|
|
519
|
+
- onDisputeCancelled: Dispute was cancelled
|
|
520
|
+
- onDisputeChallenged: Dispute was challenged
|
|
521
|
+
- onDisputeWon: Dispute was won
|
|
522
|
+
- onDisputeLost: Dispute was lost
|
|
523
|
+
- onSubscriptionActive: Subscription became active
|
|
524
|
+
- onSubscriptionOnHold: Subscription was put on hold
|
|
525
|
+
- onSubscriptionRenewed: Subscription was renewed
|
|
526
|
+
- onSubscriptionPaused: Subscription was paused
|
|
527
|
+
- onSubscriptionPlanChanged: Subscription plan was changed
|
|
528
|
+
- onSubscriptionCancelled: Subscription was cancelled
|
|
529
|
+
- onSubscriptionFailed: Subscription failed
|
|
530
|
+
- onSubscriptionExpired: Subscription expired
|
|
531
|
+
- onLicenseKeyCreated: License key was created
|
|
532
|
+
|
|
533
|
+
COMBINING SELECTED PLUGINS:
|
|
534
|
+
|
|
535
|
+
After implementing the user's selected plugins, update your src/lib/auth.ts file to include all chosen plugins in the imports and use array:
|
|
536
|
+
|
|
537
|
+
Example for all three plugins:
|
|
538
|
+
import { dodopayments, checkout, portal, webhooks } from "@dodopayments/better-auth";
|
|
539
|
+
|
|
540
|
+
use: [
|
|
541
|
+
checkout({
|
|
542
|
+
// checkout configuration
|
|
543
|
+
}),
|
|
544
|
+
portal(),
|
|
545
|
+
webhooks({
|
|
546
|
+
// webhook configuration
|
|
547
|
+
}),
|
|
548
|
+
],
|
|
549
|
+
|
|
550
|
+
Example for just checkout and portal:
|
|
551
|
+
import { dodopayments, checkout, portal } from "@dodopayments/better-auth";
|
|
552
|
+
|
|
553
|
+
use: [
|
|
554
|
+
checkout({
|
|
555
|
+
// checkout configuration
|
|
556
|
+
}),
|
|
557
|
+
portal(),
|
|
558
|
+
],
|
|
559
|
+
|
|
560
|
+
Example for just webhooks:
|
|
561
|
+
import { dodopayments, webhooks } from "@dodopayments/better-auth";
|
|
562
|
+
|
|
563
|
+
use: [
|
|
564
|
+
webhooks({
|
|
565
|
+
// webhook configuration
|
|
566
|
+
}),
|
|
567
|
+
],
|
|
568
|
+
|
|
569
|
+
IMPORTANT NOTES:
|
|
570
|
+
1. Complete Stage 1 before implementing any plugins
|
|
571
|
+
2. Ask the user which plugins they want to implement, or implement all three if no response
|
|
572
|
+
3. Only implement the plugins the user specifically requested
|
|
573
|
+
4. ALWAYS provide TODO lists for external actions the user needs to complete:
|
|
574
|
+
- API key generation and environment variable setup
|
|
575
|
+
- Product creation in Dodo Payments dashboard (for checkout plugin)
|
|
576
|
+
- Webhook setup in Dodo Payments dashboard (for webhooks plugin)
|
|
577
|
+
- 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/dodopayments
|
|
579
|
+
6. All client methods return { data, error } objects for proper error handling
|
|
580
|
+
7. Use test_mode for development and live_mode for production
|
|
581
|
+
8. The webhook endpoint is automatically created and secured with signature verification (if webhooks plugin is selected)
|
|
582
|
+
9. Customer portal and subscription listing require user authentication (if portal plugin is selected)
|
|
583
|
+
10. Handle errors appropriately and test webhook functionality in development before going live
|
|
584
|
+
11. Present all external setup tasks as clear TODO lists with specific environment variable names
|
|
585
|
+
```
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
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"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { GenericEndpointContext, User } from "better-auth";
|
|
2
|
+
import type { DodoPaymentsOptions } from "../types";
|
|
3
|
+
export declare const onUserCreate: (options: DodoPaymentsOptions) => (user: User, ctx?: GenericEndpointContext) => Promise<void>;
|
|
4
|
+
export declare const onUserUpdate: (options: DodoPaymentsOptions) => (user: User, ctx?: GenericEndpointContext) => Promise<void>;
|
|
5
|
+
//# sourceMappingURL=customer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
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"}
|