@dodopayments/nextjs 0.1.1 → 0.1.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.
Files changed (2) hide show
  1. package/README.md +169 -2
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # `@dodopayments/nextjs`
2
2
 
3
- > For AI Agents, see [llm-prompt.txt](llm-prompt.txt)
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
 
@@ -67,7 +67,7 @@ Any query parameter starting with `metadata_` will be passed as metadata to the
67
67
 
68
68
  If the `productId` is missing, the handler will return a 400 response. Invalid query parameters will also result in a 400 response.
69
69
 
70
- ## Dynamic Checkout** (POST):
70
+ ## Dynamic Checkout (POST):
71
71
  - Parameters are sent as a JSON body in a POST request.
72
72
  - Supports both one-time and recurring payments.
73
73
  - For a complete list of supported and POST body fields, refer to the below documentation.
@@ -215,3 +215,170 @@ webhookKey: process.env.DODO_WEBHOOK_SECRET!
215
215
  ```
216
216
 
217
217
  > **Tip:** Never commit your `.env` file or secrets to version control. Use environment variables for all sensitive information.
218
+
219
+ ---
220
+
221
+ ## AI Agent Prompt for Next.js Integration
222
+
223
+ 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.
224
+
225
+ The `@dodopayments/nextjs` adapter provides route handlers for Dodo Payments' Checkout, Customer Portal, and Webhook functionalities, designed for the Next.js App Router.
226
+
227
+ 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):
228
+
229
+ ```bash
230
+ npm install @dodopayments/nextjs
231
+ ```
232
+
233
+ Here's how you should structure your response:
234
+
235
+ 1. **Ask the user which functionalities they want to integrate.**
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.
383
+
384
+ ---
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dodopayments/nextjs",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },