@dodopayments/nextjs 0.1.1 → 0.1.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.
- package/README.md +210 -134
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
# `@dodopayments/nextjs`
|
|
2
2
|
|
|
3
|
-
>
|
|
3
|
+
> **AI Agent Integration Guide:** See the AI Agent Prompt section below for detailed instructions and guidance for AI assistants.
|
|
4
4
|
|
|
5
5
|
A typescript library that exports Handlers for Checkout, Customer Portal, and Webhook routes for easy integration with your Next.js app.
|
|
6
6
|
|
|
7
|
+
## Documentation
|
|
8
|
+
Detailed documentation can be found at [Dodo Payments NextJS adaptor](https://docs.dodopayments.com/developer-resources/nextjs-adaptor)
|
|
9
|
+
|
|
7
10
|
## Installation
|
|
8
11
|
|
|
9
12
|
You can install this package via npm or any other package manager of your choice:
|
|
@@ -16,66 +19,7 @@ npm install @dodopayments/nextjs
|
|
|
16
19
|
|
|
17
20
|
All the examples below assume you're using Next.js App Router.
|
|
18
21
|
|
|
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
|
|
22
|
+
### 1. Checkout
|
|
79
23
|
|
|
80
24
|
```typescript
|
|
81
25
|
// app/checkout/route.ts
|
|
@@ -88,19 +32,8 @@ export const GET = Checkout({
|
|
|
88
32
|
type: "static", // explicitly specify type (optional, defaults to 'static')
|
|
89
33
|
});
|
|
90
34
|
|
|
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
37
|
|
|
105
38
|
---
|
|
106
39
|
|
|
@@ -140,78 +73,221 @@ export const POST = Webhooks({
|
|
|
140
73
|
});
|
|
141
74
|
```
|
|
142
75
|
|
|
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:
|
|
76
|
+
---
|
|
77
|
+
|
|
78
|
+
## Prompt for LLM
|
|
190
79
|
|
|
191
|
-
```bash
|
|
192
|
-
turbo run build --filter=@dodopayments/nextjs
|
|
193
80
|
```
|
|
194
81
|
|
|
195
|
-
|
|
82
|
+
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
83
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
84
|
+
The @dodopayments/nextjs adapter provides route handlers for Dodo Payments' Checkout, Customer Portal, and Webhook functionalities, designed for the Next.js App Router.
|
|
85
|
+
|
|
86
|
+
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):
|
|
87
|
+
|
|
88
|
+
npm install @dodopayments/nextjs
|
|
89
|
+
|
|
90
|
+
Here's how you should structure your response:
|
|
91
|
+
|
|
92
|
+
Ask the user which functionalities they want to integrate.
|
|
93
|
+
|
|
94
|
+
"Which parts of the @dodopayments/nextjs adapter would you like to integrate into your project? You can choose one or more of the following:
|
|
95
|
+
|
|
96
|
+
Checkout Route Handler (for handling product checkouts)
|
|
97
|
+
|
|
98
|
+
Customer Portal Route Handler (for managing customer subscriptions/details)
|
|
99
|
+
|
|
100
|
+
Webhook Route Handler (for receiving Dodo Payments webhook events)
|
|
101
|
+
|
|
102
|
+
All (integrate all three)"
|
|
103
|
+
|
|
104
|
+
Based on the user's selection, provide detailed integration steps for each chosen functionality.
|
|
105
|
+
|
|
106
|
+
If Checkout Route Handler is selected:
|
|
107
|
+
|
|
108
|
+
Purpose: This handler redirects users to the Dodo Payments checkout page.
|
|
109
|
+
File Creation: Create a new file at app/checkout/route.ts in your Next.js project.
|
|
110
|
+
|
|
111
|
+
Code Snippet:
|
|
112
|
+
|
|
113
|
+
// app/checkout/route.ts
|
|
114
|
+
import { Checkout } from '@dodopayments/nextjs'
|
|
115
|
+
|
|
116
|
+
export const GET = Checkout({
|
|
117
|
+
bearerToken: process.env.DODO_PAYMENTS_API_KEY!,
|
|
118
|
+
returnUrl: process.env.RETURN_URL!,
|
|
119
|
+
environment: "test_mode",
|
|
120
|
+
type: "static",
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
export const POST = Checkout({
|
|
124
|
+
bearerToken: process.env.DODO_PAYMENTS_API_KEY!,
|
|
125
|
+
returnUrl: process.env.RETURN_URL!,
|
|
126
|
+
environment: "test_mode",
|
|
127
|
+
type: "dynamic",
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
Configuration & Usage:
|
|
131
|
+
|
|
132
|
+
bearerToken: Your Dodo Payments API key. It's recommended to set this via the DODO_PAYMENTS_API_KEY environment variable.
|
|
133
|
+
|
|
134
|
+
returnUrl: (Optional) The URL to redirect the user to after a successful checkout.
|
|
135
|
+
|
|
136
|
+
environment: (Optional) Set to "test_mode" for testing, or omit/set to "live_mode" for production.
|
|
137
|
+
|
|
138
|
+
type: (Optional) Set to "static" for GET/static checkout, "dynamic" for POST/dynamic checkout. Defaults to "static".
|
|
139
|
+
|
|
140
|
+
Static Checkout (GET) Query Parameters:
|
|
141
|
+
|
|
142
|
+
productId (required): Product identifier (e.g., ?productId=pdt_nZuwz45WAs64n3l07zpQR)
|
|
143
|
+
|
|
144
|
+
quantity (optional): Quantity of the product
|
|
145
|
+
|
|
146
|
+
Customer Fields (optional): fullName, firstName, lastName, email, country, addressLine, city, state, zipCode
|
|
147
|
+
|
|
148
|
+
Disable Flags (optional, set to true to disable): disableFullName, disableFirstName, disableLastName, disableEmail, disableCountry, disableAddressLine, disableCity, disableState, disableZipCode
|
|
149
|
+
|
|
150
|
+
Advanced Controls (optional): paymentCurrency, showCurrencySelector, paymentAmount, showDiscounts
|
|
151
|
+
|
|
152
|
+
Metadata (optional): Any query parameter starting with metadata_ (e.g., ?metadata_userId=abc123)
|
|
153
|
+
|
|
154
|
+
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:
|
|
155
|
+
|
|
156
|
+
Docs - One Time Payment Product: https://docs.dodopayments.com/api-reference/payments/post-payments
|
|
157
|
+
|
|
158
|
+
Docs - Subscription Product: https://docs.dodopayments.com/api-reference/subscriptions/post-subscriptions
|
|
200
159
|
|
|
201
|
-
|
|
160
|
+
Error Handling: If productId is missing or other query parameters are invalid, the handler will return a 400 response.
|
|
202
161
|
|
|
203
|
-
|
|
162
|
+
If Customer Portal Route Handler is selected:
|
|
163
|
+
|
|
164
|
+
Purpose: This handler redirects authenticated users to their Dodo Payments customer portal.
|
|
165
|
+
File Creation: Create a new file at app/customer-portal/route.ts in your Next.js project.
|
|
166
|
+
|
|
167
|
+
Code Snippet:
|
|
168
|
+
|
|
169
|
+
// app/customer-portal/route.ts
|
|
170
|
+
import { CustomerPortal } from '@dodopayments/nextjs'
|
|
171
|
+
|
|
172
|
+
export const GET = CustomerPortal({
|
|
173
|
+
bearerToken: process.env.DODO_PAYMENTS_API_KEY!,
|
|
174
|
+
environment: "test_mode",
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
Query Parameters:
|
|
178
|
+
|
|
179
|
+
customer_id (required): The customer ID for the portal session (e.g., ?customer_id=cus_123)
|
|
180
|
+
|
|
181
|
+
send_email (optional, boolean): If set to true, sends an email to the customer with the portal link.
|
|
182
|
+
|
|
183
|
+
Returns 400 if customer_id is missing.
|
|
184
|
+
|
|
185
|
+
If Webhook Route Handler is selected:
|
|
186
|
+
|
|
187
|
+
Purpose: This handler processes incoming webhook events from Dodo Payments, allowing your application to react to events like successful payments, refunds, or subscription changes.
|
|
188
|
+
File Creation: Create a new file at app/api/webhook/dodo-payments/route.ts in your Next.js project.
|
|
189
|
+
|
|
190
|
+
Code Snippet:
|
|
191
|
+
|
|
192
|
+
// app/api/webhook/dodo-payments/route.ts
|
|
193
|
+
import { Webhooks } from '@dodopayments/nextjs'
|
|
194
|
+
|
|
195
|
+
export const POST = Webhooks({
|
|
196
|
+
webhookKey: process.env.DODO_WEBHOOK_SECRET!,
|
|
197
|
+
onPayload: async (payload) => {
|
|
198
|
+
// handle the payload
|
|
199
|
+
},
|
|
200
|
+
// ... other event handlers for granular control
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
Handler Details:
|
|
204
|
+
|
|
205
|
+
Method: Only POST requests are supported. Other methods return 405.
|
|
206
|
+
|
|
207
|
+
Signature Verification: The handler verifies the webhook signature using the webhookKey and returns 401 if verification fails.
|
|
208
|
+
|
|
209
|
+
Payload Validation: The payload is validated with Zod. Returns 400 for invalid payloads.
|
|
210
|
+
|
|
211
|
+
Error Handling:
|
|
212
|
+
|
|
213
|
+
401: Invalid signature
|
|
214
|
+
|
|
215
|
+
400: Invalid payload
|
|
216
|
+
|
|
217
|
+
500: Internal error during verification
|
|
218
|
+
|
|
219
|
+
Event Routing: Calls the appropriate event handler based on the payload type.
|
|
220
|
+
|
|
221
|
+
Supported Webhook Event Handlers:
|
|
222
|
+
|
|
223
|
+
onPayload?: (payload: WebhookPayload) => Promise<void>
|
|
224
|
+
|
|
225
|
+
onPaymentSucceeded?: (payload: WebhookPayload) => Promise<void>
|
|
226
|
+
|
|
227
|
+
onPaymentFailed?: (payload: WebhookPayload) => Promise<void>
|
|
228
|
+
|
|
229
|
+
onPaymentProcessing?: (payload: WebhookPayload) => Promise<void>
|
|
230
|
+
|
|
231
|
+
onPaymentCancelled?: (payload: WebhookPayload) => Promise<void>
|
|
232
|
+
|
|
233
|
+
onRefundSucceeded?: (payload: WebhookPayload) => Promise<void>
|
|
234
|
+
|
|
235
|
+
onRefundFailed?: (payload: WebhookPayload) => Promise<void>
|
|
236
|
+
|
|
237
|
+
onDisputeOpened?: (payload: WebhookPayload) => Promise<void>
|
|
238
|
+
|
|
239
|
+
onDisputeExpired?: (payload: WebhookPayload) => Promise<void>
|
|
240
|
+
|
|
241
|
+
onDisputeAccepted?: (payload: WebhookPayload) => Promise<void>
|
|
242
|
+
|
|
243
|
+
onDisputeCancelled?: (payload: WebhookPayload) => Promise<void>
|
|
244
|
+
|
|
245
|
+
onDisputeChallenged?: (payload: WebhookPayload) => Promise<void>
|
|
246
|
+
|
|
247
|
+
onDisputeWon?: (payload: WebhookPayload) => Promise<void>
|
|
248
|
+
|
|
249
|
+
onDisputeLost?: (payload: WebhookPayload) => Promise<void>
|
|
250
|
+
|
|
251
|
+
onSubscriptionActive?: (payload: WebhookPayload) => Promise<void>
|
|
252
|
+
|
|
253
|
+
onSubscriptionOnHold?: (payload: WebhookPayload) => Promise<void>
|
|
254
|
+
|
|
255
|
+
onSubscriptionRenewed?: (payload: WebhookPayload) => Promise<void>
|
|
256
|
+
|
|
257
|
+
onSubscriptionPaused?: (payload: WebhookPayload) => Promise<void>
|
|
258
|
+
|
|
259
|
+
onSubscriptionPlanChanged?: (payload: WebhookPayload) => Promise<void>
|
|
260
|
+
|
|
261
|
+
onSubscriptionCancelled?: (payload: WebhookPayload) => Promise<void>
|
|
262
|
+
|
|
263
|
+
onSubscriptionFailed?: (payload: WebhookPayload) => Promise<void>
|
|
264
|
+
|
|
265
|
+
onSubscriptionExpired?: (payload: WebhookPayload) => Promise<void>
|
|
266
|
+
|
|
267
|
+
onLicenseKeyCreated?: (payload: WebhookPayload) => Promise<void>
|
|
268
|
+
|
|
269
|
+
Environment Variable Setup:
|
|
270
|
+
|
|
271
|
+
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.):
|
|
272
|
+
|
|
273
|
+
DODO_PAYMENTS_API_KEY: Your Dodo Payments API Key (required for Checkout and Customer Portal).
|
|
274
|
+
|
|
275
|
+
RETURN_URL: (Optional) The URL to redirect to after a successful checkout (for Checkout handler).
|
|
276
|
+
|
|
277
|
+
DODO_WEBHOOK_SECRET: Your Dodo Payments Webhook Secret (required for Webhook handler).
|
|
278
|
+
|
|
279
|
+
Example .env file:
|
|
204
280
|
|
|
205
|
-
**Example `.env` file:**
|
|
206
|
-
```env
|
|
207
281
|
DODO_PAYMENTS_API_KEY=your-api-key
|
|
208
282
|
DODO_WEBHOOK_SECRET=your-webhook-secret
|
|
209
|
-
```
|
|
210
283
|
|
|
211
|
-
|
|
212
|
-
|
|
284
|
+
Usage in your code:
|
|
285
|
+
|
|
213
286
|
bearerToken: process.env.DODO_PAYMENTS_API_KEY!
|
|
214
287
|
webhookKey: process.env.DODO_WEBHOOK_SECRET!
|
|
215
|
-
```
|
|
216
288
|
|
|
217
|
-
|
|
289
|
+
Important: Never commit sensitive environment variables directly into your version control. Use environment variables for all sensitive information.
|
|
290
|
+
|
|
291
|
+
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
|
|
292
|
+
|
|
293
|
+
```
|