@dodopayments/nextjs 0.1.2 → 0.1.4
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 +206 -298
- package/package.json +9 -4
- package/dist/index.cjs +0 -32
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -13
- package/dist/index.d.ts +0 -13
- package/dist/index.js +0 -32
- package/dist/index.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
# `@dodopayments/nextjs`
|
|
2
2
|
|
|
3
|
+
A typescript library that exports Handlers for Checkout, Customer Portal, and Webhook routes for easy integration with your Next.js app.
|
|
4
|
+
|
|
3
5
|
> **AI Agent Integration Guide:** See the AI Agent Prompt section below for detailed instructions and guidance for AI assistants.
|
|
4
6
|
|
|
5
|
-
|
|
7
|
+
## Documentation
|
|
8
|
+
|
|
9
|
+
Detailed documentation can be found at [Dodo Payments NextJS adaptor](https://docs.dodopayments.com/developer-resources/nextjs-adaptor)
|
|
6
10
|
|
|
7
11
|
## Installation
|
|
8
12
|
|
|
@@ -16,66 +20,7 @@ npm install @dodopayments/nextjs
|
|
|
16
20
|
|
|
17
21
|
All the examples below assume you're using Next.js App Router.
|
|
18
22
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
### Static Checkout (GET):
|
|
22
|
-
- Used for simple, single-product payments.
|
|
23
|
-
- Parameters are passed as query parameters in the URL.
|
|
24
|
-
- Best for straightforward use cases where the product and quantity are known in advance.
|
|
25
|
-
|
|
26
|
-
#### Query Parameters
|
|
27
|
-
|
|
28
|
-
The checkout route supports the following query parameters:
|
|
29
|
-
|
|
30
|
-
- `productId` - Product identifier (required, e.g., `?productId=pdt_nZuwz45WAs64n3l07zpQR`)
|
|
31
|
-
- `quantity` - Quantity of the product (optional, e.g., `?quantity=1`)
|
|
32
|
-
|
|
33
|
-
**Customer Fields (optional):**
|
|
34
|
-
|
|
35
|
-
- `fullName` - Customer's full name (e.g., `?fullName=John%20Doe`)
|
|
36
|
-
- `firstName` - Customer's first name (e.g., `?firstName=John`)
|
|
37
|
-
- `lastName` - Customer's last name (e.g., `?lastName=Doe`)
|
|
38
|
-
- `email` - Customer's email address (e.g., `?email=john.doe@example.com`)
|
|
39
|
-
- `country` - Customer's country (e.g., `?country=US`)
|
|
40
|
-
- `addressLine` - Customer's address line (e.g., `?addressLine=123%20Main%20St`)
|
|
41
|
-
- `city` - Customer's city (e.g., `?city=Anytown`)
|
|
42
|
-
- `state` - Customer's state/province (e.g., `?state=CA`)
|
|
43
|
-
- `zipCode` - Customer's zip/postal code (e.g., `?zipCode=90210`)
|
|
44
|
-
|
|
45
|
-
**Disable Flags (optional, set to `true` to disable):**
|
|
46
|
-
|
|
47
|
-
- `disableFullName` - Disable full name field (e.g., `?disableFullName=true`)
|
|
48
|
-
- `disableFirstName` - Disable first name field (e.g., `?disableFirstName=true`)
|
|
49
|
-
- `disableLastName` - Disable last name field (e.g., `?disableLastName=true`)
|
|
50
|
-
- `disableEmail` - Disable email field (e.g., `?disableEmail=true`)
|
|
51
|
-
- `disableCountry` - Disable country field (e.g., `?disableCountry=true`)
|
|
52
|
-
- `disableAddressLine` - Disable address line field (e.g., `?disableAddressLine=true`)
|
|
53
|
-
- `disableCity` - Disable city field (e.g., `?disableCity=true`)
|
|
54
|
-
- `disableState` - Disable state field (e.g., `?disableState=true`)
|
|
55
|
-
- `disableZipCode` - Disable zip code field (e.g., `?disableZipCode=true`)
|
|
56
|
-
|
|
57
|
-
**Advanced Controls (optional):**
|
|
58
|
-
|
|
59
|
-
- `paymentCurrency` - Specify the payment currency (e.g., `?paymentCurrency=USD`)
|
|
60
|
-
- `showCurrencySelector` - Show currency selector (e.g., `?showCurrencySelector=true`)
|
|
61
|
-
- `paymentAmount` - Specify the payment amount (e.g., `?paymentAmount=1000` for $10.00)
|
|
62
|
-
- `showDiscounts` - Show discount fields (e.g., `?showDiscounts=true`)
|
|
63
|
-
|
|
64
|
-
**Metadata (optional):**
|
|
65
|
-
Any query parameter starting with `metadata_` will be passed as metadata to the checkout.
|
|
66
|
-
(e.g., `?metadata_userId=abc123&metadata_campaign=summer_sale`)
|
|
67
|
-
|
|
68
|
-
If the `productId` is missing, the handler will return a 400 response. Invalid query parameters will also result in a 400 response.
|
|
69
|
-
|
|
70
|
-
## Dynamic Checkout (POST):
|
|
71
|
-
- Parameters are sent as a JSON body in a POST request.
|
|
72
|
-
- Supports both one-time and recurring payments.
|
|
73
|
-
- For a complete list of supported and POST body fields, refer to the below documentation.
|
|
74
|
-
[Docs - One Time Payment Product](https://docs.dodopayments.com/api-reference/payments/post-payments)
|
|
75
|
-
|
|
76
|
-
[Docs - Subscription Product](https://docs.dodopayments.com/api-reference/subscriptions/post-subscriptions)
|
|
77
|
-
|
|
78
|
-
### 1. Checkout Route Handler
|
|
23
|
+
### 1. Checkout
|
|
79
24
|
|
|
80
25
|
```typescript
|
|
81
26
|
// app/checkout/route.ts
|
|
@@ -87,21 +32,8 @@ export const GET = Checkout({
|
|
|
87
32
|
environment: "test_mode",
|
|
88
33
|
type: "static", // explicitly specify type (optional, defaults to 'static')
|
|
89
34
|
});
|
|
90
|
-
|
|
91
|
-
export const POST = Checkout({
|
|
92
|
-
bearerToken: process.env.DODO_PAYMENTS_API_KEY!,
|
|
93
|
-
returnUrl: process.env.RETURN_URL!,
|
|
94
|
-
environment: "test_mode",
|
|
95
|
-
type: "dynamic", // explicitly specify type for dynamic checkout
|
|
96
|
-
});
|
|
97
35
|
```
|
|
98
36
|
|
|
99
|
-
> **Note:**
|
|
100
|
-
> - Use `GET` for static checkout (single product, simple use cases).
|
|
101
|
-
> - Use `POST` for dynamic checkout (subscriptions, carts, advanced features).
|
|
102
|
-
> - The `type` property can be set to either `"static"` or `"dynamic"`. If not provided, it defaults to `"static"`.
|
|
103
|
-
|
|
104
|
-
|
|
105
37
|
---
|
|
106
38
|
|
|
107
39
|
### 2. Customer Portal Route Handler
|
|
@@ -140,245 +72,221 @@ export const POST = Webhooks({
|
|
|
140
72
|
});
|
|
141
73
|
```
|
|
142
74
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
- **Signature Verification:** The handler verifies the webhook signature using the `webhookKey` and returns 401 if verification fails.
|
|
147
|
-
- **Payload Validation:** The payload is validated with Zod. Returns 400 for invalid payloads.
|
|
148
|
-
- **Error Handling:**
|
|
149
|
-
- 401: Invalid signature
|
|
150
|
-
- 400: Invalid payload
|
|
151
|
-
- 500: Internal error during verification
|
|
152
|
-
- **Event Routing:** Calls the appropriate event handler based on the payload type.
|
|
153
|
-
|
|
154
|
-
#### Supported Webhook Event Handlers
|
|
155
|
-
|
|
156
|
-
The `Webhooks` function accepts an object with various `onEventName` properties, where `EventName` corresponds to the type of webhook event. Each handler is an `async` function that receives the parsed payload for that specific event type. Below is a comprehensive list of all supported event handlers with their function signatures:
|
|
157
|
-
|
|
158
|
-
- `onPayload?: (payload: WebhookPayload) => Promise<void>;`
|
|
159
|
-
- `onPaymentSucceeded?: (payload: WebhookPayload) => Promise<void>;`
|
|
160
|
-
- `onPaymentFailed?: (payload: WebhookPayload) => Promise<void>;`
|
|
161
|
-
- `onPaymentProcessing?: (payload: WebhookPayload) => Promise<void>;`
|
|
162
|
-
- `onPaymentCancelled?: (payload: WebhookPayload) => Promise<void>;`
|
|
163
|
-
- `onRefundSucceeded?: (payload: WebhookPayload) => Promise<void>;`
|
|
164
|
-
- `onRefundFailed?: (payload: WebhookPayload) => Promise<void>;`
|
|
165
|
-
- `onDisputeOpened?: (payload: WebhookPayload) => Promise<void>;`
|
|
166
|
-
- `onDisputeExpired?: (payload: WebhookPayload) => Promise<void>;`
|
|
167
|
-
- `onDisputeAccepted?: (payload: WebhookPayload) => Promise<void>;`
|
|
168
|
-
- `onDisputeCancelled?: (payload: WebhookPayload) => Promise<void>;`
|
|
169
|
-
- `onDisputeChallenged?: (payload: WebhookPayload) => Promise<void>;`
|
|
170
|
-
- `onDisputeWon?: (payload: WebhookPayload) => Promise<void>;`
|
|
171
|
-
- `onDisputeLost?: (payload: WebhookPayload) => Promise<void>;`
|
|
172
|
-
- `onSubscriptionActive?: (payload: WebhookPayload) => Promise<void>;`
|
|
173
|
-
- `onSubscriptionOnHold?: (payload: WebhookPayload) => Promise<void>;`
|
|
174
|
-
- `onSubscriptionRenewed?: (payload: WebhookPayload) => Promise<void>;`
|
|
175
|
-
- `onSubscriptionPaused?: (payload: WebhookPayload) => Promise<void>;`
|
|
176
|
-
- `onSubscriptionPlanChanged?: (payload: WebhookPayload) => Promise<void>;`
|
|
177
|
-
- `onSubscriptionCancelled?: (payload: WebhookPayload) => Promise<void>;`
|
|
178
|
-
- `onSubscriptionFailed?: (payload: WebhookPayload) => Promise<void>;`
|
|
179
|
-
- `onSubscriptionExpired?: (payload: WebhookPayload) => Promise<void>;`
|
|
180
|
-
- `onLicenseKeyCreated?: (payload: WebhookPayload) => Promise<void>;`
|
|
181
|
-
|
|
182
|
-
## Development
|
|
183
|
-
|
|
184
|
-
This library is built with:
|
|
185
|
-
|
|
186
|
-
- **TypeScript** - Type safety and better developer experience
|
|
187
|
-
- **tsup** - Fast TypeScript bundler
|
|
188
|
-
|
|
189
|
-
To build this package, install the turborepo cli and run:
|
|
75
|
+
---
|
|
76
|
+
|
|
77
|
+
## Prompt for LLM
|
|
190
78
|
|
|
191
|
-
```bash
|
|
192
|
-
turbo run build --filter=@dodopayments/nextjs
|
|
193
79
|
```
|
|
194
80
|
|
|
195
|
-
|
|
81
|
+
You are an expert Next.js developer assistant. Your task is to guide a user through integrating the @dodopayments/nextjs adapter into their existing Next.js project.
|
|
196
82
|
|
|
197
|
-
|
|
198
|
-
turbo run dev --filter=@dodopayments/nextjs
|
|
199
|
-
```
|
|
83
|
+
The @dodopayments/nextjs adapter provides route handlers for Dodo Payments' Checkout, Customer Portal, and Webhook functionalities, designed for the Next.js App Router.
|
|
200
84
|
|
|
201
|
-
|
|
85
|
+
First, install the necessary packages. Use the package manager appropriate for your project (npm, yarn, or bun) based on the presence of lock files (e.g., package-lock.json for npm, yarn.lock for yarn, bun.lockb for bun):
|
|
202
86
|
|
|
203
|
-
|
|
87
|
+
npm install @dodopayments/nextjs
|
|
204
88
|
|
|
205
|
-
|
|
206
|
-
```env
|
|
207
|
-
DODO_PAYMENTS_API_KEY=your-api-key
|
|
208
|
-
DODO_WEBHOOK_SECRET=your-webhook-secret
|
|
209
|
-
```
|
|
89
|
+
Here's how you should structure your response:
|
|
210
90
|
|
|
211
|
-
|
|
212
|
-
```typescript
|
|
213
|
-
bearerToken: process.env.DODO_PAYMENTS_API_KEY!
|
|
214
|
-
webhookKey: process.env.DODO_WEBHOOK_SECRET!
|
|
215
|
-
```
|
|
91
|
+
Ask the user which functionalities they want to integrate.
|
|
216
92
|
|
|
217
|
-
|
|
93
|
+
"Which parts of the @dodopayments/nextjs adapter would you like to integrate into your project? You can choose one or more of the following:
|
|
218
94
|
|
|
219
|
-
|
|
95
|
+
Checkout Route Handler (for handling product checkouts)
|
|
220
96
|
|
|
221
|
-
|
|
97
|
+
Customer Portal Route Handler (for managing customer subscriptions/details)
|
|
222
98
|
|
|
223
|
-
|
|
99
|
+
Webhook Route Handler (for receiving Dodo Payments webhook events)
|
|
224
100
|
|
|
225
|
-
|
|
101
|
+
All (integrate all three)"
|
|
226
102
|
|
|
227
|
-
|
|
103
|
+
Based on the user's selection, provide detailed integration steps for each chosen functionality.
|
|
228
104
|
|
|
229
|
-
|
|
230
|
-
npm install @dodopayments/nextjs
|
|
231
|
-
```
|
|
105
|
+
If Checkout Route Handler is selected:
|
|
232
106
|
|
|
233
|
-
|
|
107
|
+
Purpose: This handler redirects users to the Dodo Payments checkout page.
|
|
108
|
+
File Creation: Create a new file at app/checkout/route.ts in your Next.js project.
|
|
234
109
|
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
"Which parts of the `@dodopayments/nextjs` adapter would you like to integrate into your project? You can choose one or more of the following:
|
|
238
|
-
* `Checkout Route Handler` (for handling product checkouts)
|
|
239
|
-
* `Customer Portal Route Handler` (for managing customer subscriptions/details)
|
|
240
|
-
* `Webhook Route Handler` (for receiving Dodo Payments webhook events)
|
|
241
|
-
* `All` (integrate all three)"
|
|
242
|
-
|
|
243
|
-
2. **Based on the user's selection, provide detailed integration steps for each chosen functionality.**
|
|
244
|
-
|
|
245
|
-
---
|
|
246
|
-
**If `Checkout Route Handler` is selected:**
|
|
247
|
-
|
|
248
|
-
**Purpose:** This handler redirects users to the Dodo Payments checkout page.
|
|
249
|
-
**File Creation:** Create a new file at `app/checkout/route.ts` in your Next.js project.
|
|
250
|
-
**Code Snippet:**
|
|
251
|
-
```typescript
|
|
252
|
-
// app/checkout/route.ts
|
|
253
|
-
import { Checkout } from '@dodopayments/nextjs'
|
|
254
|
-
|
|
255
|
-
export const GET = Checkout({
|
|
256
|
-
bearerToken: process.env.DODO_PAYMENTS_API_KEY!,
|
|
257
|
-
returnUrl: process.env.RETURN_URL!,
|
|
258
|
-
environment: "test_mode",
|
|
259
|
-
type: "static", // explicitly specify type (optional, defaults to 'static')
|
|
260
|
-
});
|
|
261
|
-
|
|
262
|
-
export const POST = Checkout({
|
|
263
|
-
bearerToken: process.env.DODO_PAYMENTS_API_KEY!,
|
|
264
|
-
returnUrl: process.env.RETURN_URL!,
|
|
265
|
-
environment: "test_mode",
|
|
266
|
-
type: "dynamic", // explicitly specify type for dynamic checkout
|
|
267
|
-
});
|
|
268
|
-
```
|
|
269
|
-
**Configuration & Usage:**
|
|
270
|
-
* `bearerToken`: Your Dodo Payments API key. It's recommended to set this via the `DODO_PAYMENTS_API_KEY` environment variable.
|
|
271
|
-
* `returnUrl`: (Optional) The URL to redirect the user to after a successful checkout.
|
|
272
|
-
* `environment`: (Optional) Set to `"test_mode"` for testing, or omit/set to `"live_mode"` for production.
|
|
273
|
-
* `type`: (Optional) Set to `"static"` for GET/static checkout, `"dynamic"` for POST/dynamic checkout. Defaults to `"static"`.
|
|
274
|
-
* **Static Checkout (GET) Query Parameters:**
|
|
275
|
-
* `productId` (required): Product identifier (e.g., `?productId=pdt_nZuwz45WAs64n3l07zpQR`)
|
|
276
|
-
* `quantity` (optional): Quantity of the product
|
|
277
|
-
* **Customer Fields (optional):** `fullName`, `firstName`, `lastName`, `email`, `country`, `addressLine`, `city`, `state`, `zipCode`
|
|
278
|
-
* **Disable Flags (optional, set to `true` to disable):** `disableFullName`, `disableFirstName`, `disableLastName`, `disableEmail`, `disableCountry`, `disableAddressLine`, `disableCity`, `disableState`, `disableZipCode`
|
|
279
|
-
* **Advanced Controls (optional):** `paymentCurrency`, `showCurrencySelector`, `paymentAmount`, `showDiscounts`
|
|
280
|
-
* **Metadata (optional):** Any query parameter starting with `metadata_` (e.g., `?metadata_userId=abc123`)
|
|
281
|
-
* **Dynamic Checkout (POST):** Parameters are sent as a JSON body. Supports both one-time and recurring payments. For a complete list of supported POST body fields, refer to:
|
|
282
|
-
* [Docs - One Time Payment Product](https://docs.dodopayments.com/api-reference/payments/post-payments)
|
|
283
|
-
* [Docs - Subscription Product](https://docs.dodopayments.com/api-reference/subscriptions/post-subscriptions)
|
|
284
|
-
* **Error Handling:** If `productId` is missing or other query parameters are invalid, the handler will return a 400 response.
|
|
285
|
-
|
|
286
|
-
---
|
|
287
|
-
**If `Customer Portal Route Handler` is selected:**
|
|
288
|
-
|
|
289
|
-
**Purpose:** This handler redirects authenticated users to their Dodo Payments customer portal.
|
|
290
|
-
**File Creation:** Create a new file at `app/customer-portal/route.ts` in your Next.js project.
|
|
291
|
-
**Code Snippet:**
|
|
292
|
-
```typescript
|
|
293
|
-
// app/customer-portal/route.ts
|
|
294
|
-
import { CustomerPortal } from '@dodopayments/nextjs'
|
|
295
|
-
|
|
296
|
-
export const GET = CustomerPortal({
|
|
297
|
-
bearerToken: process.env.DODO_PAYMENTS_API_KEY!,
|
|
298
|
-
environment: "test_mode",
|
|
299
|
-
});
|
|
300
|
-
```
|
|
301
|
-
**Query Parameters:**
|
|
302
|
-
* `customer_id` (required): The customer ID for the portal session (e.g., `?customer_id=cus_123`)
|
|
303
|
-
* `send_email` (optional, boolean): If set to `true`, sends an email to the customer with the portal link.
|
|
304
|
-
* Returns 400 if `customer_id` is missing.
|
|
305
|
-
|
|
306
|
-
---
|
|
307
|
-
**If `Webhook Route Handler` is selected:**
|
|
308
|
-
|
|
309
|
-
**Purpose:** This handler processes incoming webhook events from Dodo Payments, allowing your application to react to events like successful payments, refunds, or subscription changes.
|
|
310
|
-
**File Creation:** Create a new file at `app/api/webhook/dodo-payments/route.ts` in your Next.js project.
|
|
311
|
-
**Code Snippet:**
|
|
312
|
-
```typescript
|
|
313
|
-
// app/api/webhook/dodo-payments/route.ts
|
|
314
|
-
import { Webhooks } from '@dodopayments/nextjs'
|
|
315
|
-
|
|
316
|
-
export const POST = Webhooks({
|
|
317
|
-
webhookKey: process.env.DODO_WEBHOOK_SECRET!,
|
|
318
|
-
onPayload: async (payload) => {
|
|
319
|
-
// handle the payload
|
|
320
|
-
},
|
|
321
|
-
// ... other event handlers for granular control
|
|
322
|
-
});
|
|
323
|
-
```
|
|
324
|
-
**Handler Details:**
|
|
325
|
-
* **Method:** Only POST requests are supported. Other methods return 405.
|
|
326
|
-
* **Signature Verification:** The handler verifies the webhook signature using the `webhookKey` and returns 401 if verification fails.
|
|
327
|
-
* **Payload Validation:** The payload is validated with Zod. Returns 400 for invalid payloads.
|
|
328
|
-
* **Error Handling:**
|
|
329
|
-
* 401: Invalid signature
|
|
330
|
-
* 400: Invalid payload
|
|
331
|
-
* 500: Internal error during verification
|
|
332
|
-
* **Event Routing:** Calls the appropriate event handler based on the payload type.
|
|
333
|
-
* **Supported Webhook Event Handlers:**
|
|
334
|
-
* `onPayload?: (payload: WebhookPayload) => Promise<void>;`
|
|
335
|
-
* `onPaymentSucceeded?: (payload: WebhookPayload) => Promise<void>;`
|
|
336
|
-
* `onPaymentFailed?: (payload: WebhookPayload) => Promise<void>;`
|
|
337
|
-
* `onPaymentProcessing?: (payload: WebhookPayload) => Promise<void>;`
|
|
338
|
-
* `onPaymentCancelled?: (payload: WebhookPayload) => Promise<void>;`
|
|
339
|
-
* `onRefundSucceeded?: (payload: WebhookPayload) => Promise<void>;`
|
|
340
|
-
* `onRefundFailed?: (payload: WebhookPayload) => Promise<void>;`
|
|
341
|
-
* `onDisputeOpened?: (payload: WebhookPayload) => Promise<void>;`
|
|
342
|
-
* `onDisputeExpired?: (payload: WebhookPayload) => Promise<void>;`
|
|
343
|
-
* `onDisputeAccepted?: (payload: WebhookPayload) => Promise<void>;`
|
|
344
|
-
* `onDisputeCancelled?: (payload: WebhookPayload) => Promise<void>;`
|
|
345
|
-
* `onDisputeChallenged?: (payload: WebhookPayload) => Promise<void>;`
|
|
346
|
-
* `onDisputeWon?: (payload: WebhookPayload) => Promise<void>;`
|
|
347
|
-
* `onDisputeLost?: (payload: WebhookPayload) => Promise<void>;`
|
|
348
|
-
* `onSubscriptionActive?: (payload: WebhookPayload) => Promise<void>;`
|
|
349
|
-
* `onSubscriptionOnHold?: (payload: WebhookPayload) => Promise<void>;`
|
|
350
|
-
* `onSubscriptionRenewed?: (payload: WebhookPayload) => Promise<void>;`
|
|
351
|
-
* `onSubscriptionPaused?: (payload: WebhookPayload) => Promise<void>;`
|
|
352
|
-
* `onSubscriptionPlanChanged?: (payload: WebhookPayload) => Promise<void>;`
|
|
353
|
-
* `onSubscriptionCancelled?: (payload: WebhookPayload) => Promise<void>;`
|
|
354
|
-
* `onSubscriptionFailed?: (payload: WebhookPayload) => Promise<void>;`
|
|
355
|
-
* `onSubscriptionExpired?: (payload: WebhookPayload) => Promise<void>;`
|
|
356
|
-
* `onLicenseKeyCreated?: (payload: WebhookPayload) => Promise<void>;`
|
|
357
|
-
|
|
358
|
-
---
|
|
359
|
-
|
|
360
|
-
3. **Environment Variable Setup:**
|
|
361
|
-
|
|
362
|
-
To ensure the adapter functions correctly, you will need to manually set up the following environment variables in your Next.js project's deployment environment (e.g., Vercel, Netlify, AWS, etc.):
|
|
363
|
-
|
|
364
|
-
* `DODO_PAYMENTS_API_KEY`: Your Dodo Payments API Key (required for Checkout and Customer Portal).
|
|
365
|
-
* `RETURN_URL`: (Optional) The URL to redirect to after a successful checkout (for Checkout handler).
|
|
366
|
-
* `DODO_WEBHOOK_SECRET`: Your Dodo Payments Webhook Secret (required for Webhook handler).
|
|
367
|
-
|
|
368
|
-
**Example `.env` file:**
|
|
369
|
-
```env
|
|
370
|
-
DODO_PAYMENTS_API_KEY=your-api-key
|
|
371
|
-
DODO_WEBHOOK_SECRET=your-webhook-secret
|
|
372
|
-
```
|
|
373
|
-
|
|
374
|
-
**Usage in your code:**
|
|
375
|
-
```typescript
|
|
376
|
-
bearerToken: process.env.DODO_PAYMENTS_API_KEY!
|
|
377
|
-
webhookKey: process.env.DODO_WEBHOOK_SECRET!
|
|
378
|
-
```
|
|
379
|
-
|
|
380
|
-
**Important:** Never commit sensitive environment variables directly into your version control. Use environment variables for all sensitive information.
|
|
381
|
-
|
|
382
|
-
If the user needs assistance setting up environment variables for their specific deployment environment, ask them what platform they are using (e.g., Vercel, Netlify, AWS, etc.), and provide guidance. You can also add comments to their PR or chat depending on the context.
|
|
110
|
+
Code Snippet:
|
|
383
111
|
|
|
384
|
-
|
|
112
|
+
// app/checkout/route.ts
|
|
113
|
+
import { Checkout } from '@dodopayments/nextjs'
|
|
114
|
+
|
|
115
|
+
export const GET = Checkout({
|
|
116
|
+
bearerToken: process.env.DODO_PAYMENTS_API_KEY!,
|
|
117
|
+
returnUrl: process.env.RETURN_URL!,
|
|
118
|
+
environment: "test_mode",
|
|
119
|
+
type: "static",
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
export const POST = Checkout({
|
|
123
|
+
bearerToken: process.env.DODO_PAYMENTS_API_KEY!,
|
|
124
|
+
returnUrl: process.env.RETURN_URL!,
|
|
125
|
+
environment: "test_mode",
|
|
126
|
+
type: "dynamic",
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
Configuration & Usage:
|
|
130
|
+
|
|
131
|
+
bearerToken: Your Dodo Payments API key. It's recommended to set this via the DODO_PAYMENTS_API_KEY environment variable.
|
|
132
|
+
|
|
133
|
+
returnUrl: (Optional) The URL to redirect the user to after a successful checkout.
|
|
134
|
+
|
|
135
|
+
environment: (Optional) Set to "test_mode" for testing, or omit/set to "live_mode" for production.
|
|
136
|
+
|
|
137
|
+
type: (Optional) Set to "static" for GET/static checkout, "dynamic" for POST/dynamic checkout. Defaults to "static".
|
|
138
|
+
|
|
139
|
+
Static Checkout (GET) Query Parameters:
|
|
140
|
+
|
|
141
|
+
productId (required): Product identifier (e.g., ?productId=pdt_nZuwz45WAs64n3l07zpQR)
|
|
142
|
+
|
|
143
|
+
quantity (optional): Quantity of the product
|
|
144
|
+
|
|
145
|
+
Customer Fields (optional): fullName, firstName, lastName, email, country, addressLine, city, state, zipCode
|
|
146
|
+
|
|
147
|
+
Disable Flags (optional, set to true to disable): disableFullName, disableFirstName, disableLastName, disableEmail, disableCountry, disableAddressLine, disableCity, disableState, disableZipCode
|
|
148
|
+
|
|
149
|
+
Advanced Controls (optional): paymentCurrency, showCurrencySelector, paymentAmount, showDiscounts
|
|
150
|
+
|
|
151
|
+
Metadata (optional): Any query parameter starting with metadata_ (e.g., ?metadata_userId=abc123)
|
|
152
|
+
|
|
153
|
+
Dynamic Checkout (POST): Parameters are sent as a JSON body. Supports both one-time and recurring payments. For a complete list of supported POST body fields, refer to:
|
|
154
|
+
|
|
155
|
+
Docs - One Time Payment Product: https://docs.dodopayments.com/api-reference/payments/post-payments
|
|
156
|
+
|
|
157
|
+
Docs - Subscription Product: https://docs.dodopayments.com/api-reference/subscriptions/post-subscriptions
|
|
158
|
+
|
|
159
|
+
Error Handling: If productId is missing or other query parameters are invalid, the handler will return a 400 response.
|
|
160
|
+
|
|
161
|
+
If Customer Portal Route Handler is selected:
|
|
162
|
+
|
|
163
|
+
Purpose: This handler redirects authenticated users to their Dodo Payments customer portal.
|
|
164
|
+
File Creation: Create a new file at app/customer-portal/route.ts in your Next.js project.
|
|
165
|
+
|
|
166
|
+
Code Snippet:
|
|
167
|
+
|
|
168
|
+
// app/customer-portal/route.ts
|
|
169
|
+
import { CustomerPortal } from '@dodopayments/nextjs'
|
|
170
|
+
|
|
171
|
+
export const GET = CustomerPortal({
|
|
172
|
+
bearerToken: process.env.DODO_PAYMENTS_API_KEY!,
|
|
173
|
+
environment: "test_mode",
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
Query Parameters:
|
|
177
|
+
|
|
178
|
+
customer_id (required): The customer ID for the portal session (e.g., ?customer_id=cus_123)
|
|
179
|
+
|
|
180
|
+
send_email (optional, boolean): If set to true, sends an email to the customer with the portal link.
|
|
181
|
+
|
|
182
|
+
Returns 400 if customer_id is missing.
|
|
183
|
+
|
|
184
|
+
If Webhook Route Handler is selected:
|
|
185
|
+
|
|
186
|
+
Purpose: This handler processes incoming webhook events from Dodo Payments, allowing your application to react to events like successful payments, refunds, or subscription changes.
|
|
187
|
+
File Creation: Create a new file at app/api/webhook/dodo-payments/route.ts in your Next.js project.
|
|
188
|
+
|
|
189
|
+
Code Snippet:
|
|
190
|
+
|
|
191
|
+
// app/api/webhook/dodo-payments/route.ts
|
|
192
|
+
import { Webhooks } from '@dodopayments/nextjs'
|
|
193
|
+
|
|
194
|
+
export const POST = Webhooks({
|
|
195
|
+
webhookKey: process.env.DODO_WEBHOOK_SECRET!,
|
|
196
|
+
onPayload: async (payload) => {
|
|
197
|
+
// handle the payload
|
|
198
|
+
},
|
|
199
|
+
// ... other event handlers for granular control
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
Handler Details:
|
|
203
|
+
|
|
204
|
+
Method: Only POST requests are supported. Other methods return 405.
|
|
205
|
+
|
|
206
|
+
Signature Verification: The handler verifies the webhook signature using the webhookKey and returns 401 if verification fails.
|
|
207
|
+
|
|
208
|
+
Payload Validation: The payload is validated with Zod. Returns 400 for invalid payloads.
|
|
209
|
+
|
|
210
|
+
Error Handling:
|
|
211
|
+
|
|
212
|
+
401: Invalid signature
|
|
213
|
+
|
|
214
|
+
400: Invalid payload
|
|
215
|
+
|
|
216
|
+
500: Internal error during verification
|
|
217
|
+
|
|
218
|
+
Event Routing: Calls the appropriate event handler based on the payload type.
|
|
219
|
+
|
|
220
|
+
Supported Webhook Event Handlers:
|
|
221
|
+
|
|
222
|
+
onPayload?: (payload: WebhookPayload) => Promise<void>
|
|
223
|
+
|
|
224
|
+
onPaymentSucceeded?: (payload: WebhookPayload) => Promise<void>
|
|
225
|
+
|
|
226
|
+
onPaymentFailed?: (payload: WebhookPayload) => Promise<void>
|
|
227
|
+
|
|
228
|
+
onPaymentProcessing?: (payload: WebhookPayload) => Promise<void>
|
|
229
|
+
|
|
230
|
+
onPaymentCancelled?: (payload: WebhookPayload) => Promise<void>
|
|
231
|
+
|
|
232
|
+
onRefundSucceeded?: (payload: WebhookPayload) => Promise<void>
|
|
233
|
+
|
|
234
|
+
onRefundFailed?: (payload: WebhookPayload) => Promise<void>
|
|
235
|
+
|
|
236
|
+
onDisputeOpened?: (payload: WebhookPayload) => Promise<void>
|
|
237
|
+
|
|
238
|
+
onDisputeExpired?: (payload: WebhookPayload) => Promise<void>
|
|
239
|
+
|
|
240
|
+
onDisputeAccepted?: (payload: WebhookPayload) => Promise<void>
|
|
241
|
+
|
|
242
|
+
onDisputeCancelled?: (payload: WebhookPayload) => Promise<void>
|
|
243
|
+
|
|
244
|
+
onDisputeChallenged?: (payload: WebhookPayload) => Promise<void>
|
|
245
|
+
|
|
246
|
+
onDisputeWon?: (payload: WebhookPayload) => Promise<void>
|
|
247
|
+
|
|
248
|
+
onDisputeLost?: (payload: WebhookPayload) => Promise<void>
|
|
249
|
+
|
|
250
|
+
onSubscriptionActive?: (payload: WebhookPayload) => Promise<void>
|
|
251
|
+
|
|
252
|
+
onSubscriptionOnHold?: (payload: WebhookPayload) => Promise<void>
|
|
253
|
+
|
|
254
|
+
onSubscriptionRenewed?: (payload: WebhookPayload) => Promise<void>
|
|
255
|
+
|
|
256
|
+
onSubscriptionPaused?: (payload: WebhookPayload) => Promise<void>
|
|
257
|
+
|
|
258
|
+
onSubscriptionPlanChanged?: (payload: WebhookPayload) => Promise<void>
|
|
259
|
+
|
|
260
|
+
onSubscriptionCancelled?: (payload: WebhookPayload) => Promise<void>
|
|
261
|
+
|
|
262
|
+
onSubscriptionFailed?: (payload: WebhookPayload) => Promise<void>
|
|
263
|
+
|
|
264
|
+
onSubscriptionExpired?: (payload: WebhookPayload) => Promise<void>
|
|
265
|
+
|
|
266
|
+
onLicenseKeyCreated?: (payload: WebhookPayload) => Promise<void>
|
|
267
|
+
|
|
268
|
+
Environment Variable Setup:
|
|
269
|
+
|
|
270
|
+
To ensure the adapter functions correctly, you will need to manually set up the following environment variables in your Next.js project's deployment environment (e.g., Vercel, Netlify, AWS, etc.):
|
|
271
|
+
|
|
272
|
+
DODO_PAYMENTS_API_KEY: Your Dodo Payments API Key (required for Checkout and Customer Portal).
|
|
273
|
+
|
|
274
|
+
RETURN_URL: (Optional) The URL to redirect to after a successful checkout (for Checkout handler).
|
|
275
|
+
|
|
276
|
+
DODO_WEBHOOK_SECRET: Your Dodo Payments Webhook Secret (required for Webhook handler).
|
|
277
|
+
|
|
278
|
+
Example .env file:
|
|
279
|
+
|
|
280
|
+
DODO_PAYMENTS_API_KEY=your-api-key
|
|
281
|
+
DODO_WEBHOOK_SECRET=your-webhook-secret
|
|
282
|
+
|
|
283
|
+
Usage in your code:
|
|
284
|
+
|
|
285
|
+
bearerToken: process.env.DODO_PAYMENTS_API_KEY!
|
|
286
|
+
webhookKey: process.env.DODO_WEBHOOK_SECRET!
|
|
287
|
+
|
|
288
|
+
Important: Never commit sensitive environment variables directly into your version control. Use environment variables for all sensitive information.
|
|
289
|
+
|
|
290
|
+
If the user needs assistance setting up environment variables for their specific deployment environment, ask them what platform they are using (e.g., Vercel, Netlify, AWS, etc.), and provide guidance. You can also add comments to their PR or chat depending on the context
|
|
291
|
+
|
|
292
|
+
```
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dodopayments/nextjs",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
7
7
|
"scripts": {
|
|
8
|
-
"build": "
|
|
9
|
-
"dev": "
|
|
8
|
+
"build": "rollup -c",
|
|
9
|
+
"dev": "rollup -c -w",
|
|
10
10
|
"format": "prettier --write .",
|
|
11
11
|
"check-format": "prettier --check .",
|
|
12
12
|
"check-types": "tsc --noEmit"
|
|
@@ -37,7 +37,12 @@
|
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
39
|
"@dodo/typescript-config": "*",
|
|
40
|
-
"
|
|
40
|
+
"rollup": "^4.0.0",
|
|
41
|
+
"@rollup/plugin-typescript": "^11.0.0",
|
|
42
|
+
"@rollup/plugin-node-resolve": "^15.0.0",
|
|
43
|
+
"@rollup/plugin-commonjs": "^25.0.0",
|
|
44
|
+
"rollup-plugin-dts": "^6.1.0",
|
|
45
|
+
"rimraf": "^5.0.0"
|
|
41
46
|
},
|
|
42
47
|
"peerDependencies": {
|
|
43
48
|
"next": "^15.3.4",
|