@dodopayments/nextjs 0.1.0

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 ADDED
@@ -0,0 +1,169 @@
1
+ # `@dodopayments/nextjs`
2
+
3
+ > For AI Agents, see [llm-prompt.txt](llm-prompt.txt)
4
+
5
+ A typescript library that exports Handlers for Checkout, Customer Portal, and Webhook routes for easy integration with your Next.js app.
6
+
7
+ ## Installation
8
+
9
+ You can install this package via npm or any other package manager of your choice:
10
+
11
+ ```bash
12
+ npm install @dodopayments/nextjs zod next
13
+ ```
14
+
15
+ ## Quick Start
16
+
17
+ All the examples below assume you're using Next.js App Router.
18
+
19
+ ### 1. Checkout Route Handler
20
+
21
+ ```typescript
22
+ // app/checkout/route.ts
23
+ import { Checkout } from "@dodopayments/nextjs";
24
+
25
+ export const GET = Checkout({
26
+ // Can be omitted if DODO_PAYMENTS_API_KEY environment variable is set.
27
+ bearerToken: process.env.DODO_PAYMENTS_API_KEY!,
28
+ // URL to redirect to after successful checkout, can be omitted.
29
+ successUrl: process.env.SUCCESS_URL!,
30
+ // Omit or set to "live_mode" for production
31
+ environment: "test_mode",
32
+ });
33
+ ```
34
+
35
+ #### Query Parameters
36
+
37
+ The checkout route supports the following query parameters:
38
+
39
+ - `productId` - Product identifier (required, e.g., `?productId=pdt_nZuwz45WAs64n3l07zpQR`)
40
+ - `quantity` - Quantity of the product (optional, e.g., `?quantity=1`)
41
+
42
+ **Customer Fields (optional):**
43
+
44
+ - `fullName` - Customer's full name (e.g., `?fullName=John%20Doe`)
45
+ - `firstName` - Customer's first name (e.g., `?firstName=John`)
46
+ - `lastName` - Customer's last name (e.g., `?lastName=Doe`)
47
+ - `email` - Customer's email address (e.g., `?email=john.doe@example.com`)
48
+ - `country` - Customer's country (e.g., `?country=US`)
49
+ - `addressLine` - Customer's address line (e.g., `?addressLine=123%20Main%20St`)
50
+ - `city` - Customer's city (e.g., `?city=Anytown`)
51
+ - `state` - Customer's state/province (e.g., `?state=CA`)
52
+ - `zipCode` - Customer's zip/postal code (e.g., `?zipCode=90210`)
53
+
54
+ **Disable Flags (optional, set to `true` to disable):**
55
+
56
+ - `disableFullName` - Disable full name field (e.g., `?disableFullName=true`)
57
+ - `disableFirstName` - Disable first name field (e.g., `?disableFirstName=true`)
58
+ - `disableLastName` - Disable last name field (e.g., `?disableLastName=true`)
59
+ - `disableEmail` - Disable email field (e.g., `?disableEmail=true`)
60
+ - `disableCountry` - Disable country field (e.g., `?disableCountry=true`)
61
+ - `disableAddressLine` - Disable address line field (e.g., `?disableAddressLine=true`)
62
+ - `disableCity` - Disable city field (e.g., `?disableCity=true`)
63
+ - `disableState` - Disable state field (e.g., `?disableState=true`)
64
+ - `disableZipCode` - Disable zip code field (e.g., `?disableZipCode=true`)
65
+
66
+ **Advanced Controls (optional):**
67
+
68
+ - `paymentCurrency` - Specify the payment currency (e.g., `?paymentCurrency=USD`)
69
+ - `showCurrencySelector` - Show currency selector (e.g., `?showCurrencySelector=true`)
70
+ - `paymentAmount` - Specify the payment amount (e.g., `?paymentAmount=1000` for $10.00)
71
+ - `showDiscounts` - Show discount fields (e.g., `?showDiscounts=true`)
72
+
73
+ **Metadata (optional):**
74
+ Any query parameter starting with `metadata_` will be passed as metadata to the checkout.
75
+ (e.g., `?metadata_userId=abc123&metadata_campaign=summer_sale`)
76
+
77
+ If the `productId` is missing, the handler will return a 400 response. Invalid query parameters will also result in a 400 response.
78
+
79
+ ### 2. Customer Portal Route Handler
80
+
81
+ ```typescript
82
+ // app/customer-portal/route.ts
83
+ import { CustomerPortal } from "@dodopayments/nextjs";
84
+ import { NextRequest } from "next/server";
85
+
86
+ export const GET = CustomerPortal({
87
+ // Can be omitted if DODO_PAYMENTS_API_KEY environment variable is set.
88
+ bearerToken: process.env.DODO_PAYMENTS_API_KEY!,
89
+ // Omit or set to "live_mode" for production
90
+ environment: "test_mode",
91
+ // Write logic to get customerId from request here
92
+ getCustomerId: async (req: NextRequest) => {
93
+ // Implement your logic to get the customer ID from the request
94
+ // (e.g., from session, auth token, etc.)
95
+ return ""; // Return the customer ID
96
+ },
97
+ });
98
+ ```
99
+
100
+ ### 3. Webhook Route Handler
101
+
102
+ ```typescript
103
+ // app/api/webhook/dodo-payments/route.ts
104
+ import { Webhooks } from "@dodopayments/nextjs";
105
+
106
+ export const POST = Webhooks({
107
+ webhookKey: process.env.DODO_WEBHOOK_SECRET!,
108
+ onPayload: async (payload) => {
109
+ // handle the payload
110
+ },
111
+ // ... other event handlers for granular control
112
+ });
113
+ ```
114
+
115
+ #### Supported Webhook Event Handlers
116
+
117
+ 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:
118
+
119
+ - `onPayload?: (payload: WebhookPayload) => Promise<void>;`
120
+ - `onPaymentSucceeded?: (payload: WebhookPayload) => Promise<void>;`
121
+ - `onPaymentFailed?: (payload: WebhookPayload) => Promise<void>;`
122
+ - `onPaymentProcessing?: (payload: WebhookPayload) => Promise<void>;`
123
+ - `onPaymentCancelled?: (payload: WebhookPayload) => Promise<void>;`
124
+ - `onRefundSucceeded?: (payload: WebhookPayload) => Promise<void>;`
125
+ - `onRefundFailed?: (payload: WebhookPayload) => Promise<void>;`
126
+ - `onDisputeOpened?: (payload: WebhookPayload) => Promise<void>;`
127
+ - `onDisputeExpired?: (payload: WebhookPayload) => Promise<void>;`
128
+ - `onDisputeAccepted?: (payload: WebhookPayload) => Promise<void>;`
129
+ - `onDisputeCancelled?: (payload: WebhookPayload) => Promise<void>;`
130
+ - `onDisputeChallenged?: (payload: WebhookPayload) => Promise<void>;`
131
+ - `onDisputeWon?: (payload: WebhookPayload) => Promise<void>;`
132
+ - `onDisputeLost?: (payload: WebhookPayload) => Promise<void>;`
133
+ - `onSubscriptionActive?: (payload: WebhookPayload) => Promise<void>;`
134
+ - `onSubscriptionOnHold?: (payload: WebhookPayload) => Promise<void>;`
135
+ - `onSubscriptionRenewed?: (payload: WebhookPayload) => Promise<void>;`
136
+ - `onSubscriptionPaused?: (payload: WebhookPayload) => Promise<void>;`
137
+ - `onSubscriptionPlanChanged?: (payload: WebhookPayload) => Promise<void>;`
138
+ - `onSubscriptionCancelled?: (payload: WebhookPayload) => Promise<void>;`
139
+ - `onSubscriptionFailed?: (payload: WebhookPayload) => Promise<void>;`
140
+ - `onSubscriptionExpired?: (payload: WebhookPayload) => Promise<void>;`
141
+ - `onLicenseKeyCreated?: (payload: WebhookPayload) => Promise<void>;`
142
+
143
+ ## Development
144
+
145
+ This library is built with:
146
+
147
+ - **TypeScript** - Type safety and better developer experience
148
+ - **tsup** - Fast TypeScript bundler
149
+
150
+ To build this package, install the turborepo cli and run:
151
+
152
+ ```bash
153
+ turbo run build --filter=@dodopayments/nextjs
154
+ ```
155
+
156
+ To run in development mode:
157
+
158
+ ```bash
159
+ turbo run dev --filter=@dodopayments/nextjs
160
+ ```
161
+
162
+ ## Environment Variables
163
+
164
+ You can set `DODO_PAYMENTS_API_KEY` environment variable to use your API key and omit the `bearerToken` parameter.
165
+
166
+ ```env
167
+ DODO_PAYMENTS_API_KEY=your-api-key
168
+ DODO_WEBHOOK_SECRET=your-webhook-secret
169
+ ```