@dodopayments/express 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 ADDED
@@ -0,0 +1,241 @@
1
+ # `@dodopayments/express`
2
+
3
+ A typescript library that exports Handlers for Checkout, Customer Portal, and Webhook routes for easy integration with your express app.
4
+
5
+ > **AI Agent Integration Guide:** See the AI Agent Prompt section below for detailed instructions and guidance for AI assistants.
6
+
7
+ ## Documentation
8
+
9
+ Detailed documentation can be found at [Dodo Payments express adaptor](https://docs.dodopayments.com/developer-resources/express-adaptor)
10
+
11
+ ## Installation
12
+
13
+ You can install this package via npm or any other package manager of your choice:
14
+
15
+ ```bash
16
+ npm install @dodopayments/express
17
+ ```
18
+
19
+ ## Quick Start
20
+
21
+
22
+ ### 1. Checkout
23
+
24
+ ```typescript
25
+ import { checkoutHandler } from '@dodopayments/express';
26
+
27
+ app.get('/api/checkout', checkoutHandler({
28
+ bearerToken: process.env.DODO_PAYMENTS_API_KEY!,
29
+ returnUrl: process.env.RETURN_URL!,
30
+ environment: "test_mode",
31
+ type: "static"
32
+ }))
33
+
34
+ app.post('/api/checkout', checkoutHandler({
35
+ bearerToken: process.env.DODO_PAYMENTS_API_KEY!,
36
+ returnUrl: process.env.RETURN_URL!,
37
+ environment: "test_mode",
38
+ type: "dynamic"
39
+ }))
40
+ ```
41
+
42
+ ---
43
+
44
+ ### 2. Customer Portal Route Handler
45
+
46
+ ```typescript
47
+ import { CustomerPortal } from "@dodopayments/express";
48
+
49
+ app.get('/api/customer-portal', CustomerPortal({
50
+ bearerToken: process.env.DODO_PAYMENTS_API_KEY!,
51
+ environment: "test_mode",
52
+ }))
53
+
54
+ ```
55
+
56
+ #### Query Parameters
57
+
58
+ - `customer_id` (required): The customer ID for the portal session (e.g., `?customer_id=cus_123`)
59
+ - `send_email` (optional, boolean): If set to `true`, sends an email to the customer with the portal link.
60
+
61
+ Returns 400 if `customer_id` is missing.
62
+
63
+ ---
64
+
65
+ ### 3. Webhook Route Handler
66
+
67
+ ```typescript
68
+ // app/api/webhook/dodo-payments/route.ts
69
+ import { Webhooks } from "@dodopayments/express";
70
+
71
+ app.post('/api/webhook',
72
+ Webhooks({
73
+ webhookKey: process.env.DODO_PAYMENTS_WEBHOOK_SECRET!,
74
+ onPayload: async (payload) => {
75
+ // handle the payload
76
+ },
77
+ // ... other event handlers for granular control
78
+ }))
79
+ ```
80
+
81
+ ---
82
+
83
+ ```
84
+ You are an expert Express.js developer assistant. Your task is to guide a user through integrating the @dodopayments/express adapter into their existing Express.js project.
85
+
86
+ The @dodopayments/express adapter provides route handlers for Dodo Payments' Checkout, Customer Portal, and Webhook functionalities, designed to plug directly into an Express app.
87
+
88
+ First, install the necessary package. Use the package manager appropriate for the user's project (npm, yarn, or bun):
89
+
90
+ npm install @dodopayments/express
91
+
92
+ ---
93
+
94
+ Here's how you should structure your response:
95
+
96
+ 1. Ask the user which functionalities they want to integrate.
97
+
98
+ "Which parts of the @dodopayments/express adapter would you like to integrate into your project? You can choose one or more of the following:
99
+
100
+ - Checkout Route Handler (for handling product checkouts)
101
+ - Customer Portal Route Handler (for managing customer subscriptions/details)
102
+ - Webhook Route Handler (for receiving Dodo Payments webhook events)
103
+ - All (integrate all three)"
104
+
105
+ ---
106
+
107
+ 2. Based on the user's selection, provide detailed integration steps for each chosen functionality.
108
+
109
+ ---
110
+
111
+ **If Checkout Route Handler is selected:**
112
+
113
+ **Purpose**: This handler redirects users to the Dodo Payments checkout page.
114
+
115
+ **Integration**:
116
+ Create two routes in your Express app — one for static (GET) and one for dynamic (POST) checkout.
117
+
118
+
119
+ import { checkoutHandler } from '@dodopayments/express';
120
+
121
+ app.get('/api/checkout', checkoutHandler({
122
+ bearerToken: process.env.DODO_PAYMENTS_API_KEY!,
123
+ returnUrl: process.env.RETURN_URL!,
124
+ environment: "test_mode",
125
+ type: "static"
126
+ }));
127
+
128
+ app.post('/api/checkout', checkoutHandler({
129
+ bearerToken: process.env.DODO_PAYMENTS_API_KEY!,
130
+ returnUrl: process.env.RETURN_URL!,
131
+ environment: "test_mode",
132
+ type: "dynamic"
133
+ }));
134
+
135
+ Config Options:
136
+
137
+ bearerToken: Your Dodo Payments API key (recommended to be stored in DODO_PAYMENTS_API_KEY env variable).
138
+
139
+ returnUrl (optional): URL to redirect the user after successful checkout.
140
+
141
+ environment: "test_mode" or "live_mode"
142
+
143
+ type: "static" (GET) or "dynamic" (POST)
144
+
145
+ GET (static checkout) expects query parameters:
146
+
147
+ productId (required)
148
+
149
+ quantity, customer fields (fullName, email, etc.), and metadata (metadata_*) are optional.
150
+
151
+ POST (dynamic checkout) expects a JSON body with payment details (one-time or subscription). Reference the docs for the full POST schema:
152
+
153
+ One-time payments: https://docs.dodopayments.com/api-reference/payments/post-payments
154
+
155
+ Subscriptions: https://docs.dodopayments.com/api-reference/subscriptions/post-subscriptions
156
+
157
+ If Customer Portal Route Handler is selected:
158
+
159
+ Purpose: This route allows customers to manage their subscriptions via the Dodo Payments portal.
160
+
161
+ Integration:
162
+
163
+ import { CustomerPortal } from "@dodopayments/express";
164
+
165
+ app.get('/api/customer-portal', CustomerPortal({
166
+ bearerToken: process.env.DODO_PAYMENTS_API_KEY!,
167
+ environment: "test_mode",
168
+ }));
169
+
170
+ Query Parameters:
171
+
172
+ customer_id (required): e.g., ?customer_id=cus_123
173
+
174
+ send_email (optional): if true, customer is emailed the portal link
175
+
176
+ Returns 400 if customer_id is missing.
177
+
178
+ If Webhook Route Handler is selected:
179
+
180
+ Purpose: Processes incoming webhook events from Dodo Payments to trigger events in your app.
181
+
182
+ Integration:
183
+
184
+ import { Webhooks } from "@dodopayments/express";
185
+
186
+ app.post('/api/webhook', Webhooks({
187
+ webhookKey: process.env.DODO_PAYMENTS_WEBHOOK_SECRET!,
188
+ onPayload: async (payload) => {
189
+ // Handle generic payload
190
+ },
191
+ // You can also provide fine-grained handlers for each event type below
192
+ }));
193
+
194
+ Features:
195
+
196
+ Only POST method is allowed — others return 405
197
+
198
+ Signature verification is performed using webhookKey. Returns 401 if invalid.
199
+
200
+ Zod-based payload validation. Returns 400 if invalid schema.
201
+
202
+ All handlers are async functions.
203
+
204
+ Supported Webhook Event Handlers:
205
+
206
+ You may pass in any of the following handlers:
207
+
208
+ onPayload
209
+
210
+ onPaymentSucceeded
211
+
212
+ onPaymentFailed
213
+
214
+ onPaymentProcessing
215
+
216
+ onPaymentCancelled
217
+
218
+ onRefundSucceeded
219
+
220
+ onRefundFailed
221
+
222
+ onDisputeOpened, onDisputeExpired, onDisputeAccepted, onDisputeCancelled, onDisputeChallenged, onDisputeWon, onDisputeLost
223
+
224
+ onSubscriptionActive, onSubscriptionOnHold, onSubscriptionRenewed, onSubscriptionPaused, onSubscriptionPlanChanged, onSubscriptionCancelled, onSubscriptionFailed, onSubscriptionExpired
225
+
226
+ onLicenseKeyCreated
227
+
228
+ Environment Variable Setup:
229
+
230
+ Make sure to define these environment variables in your project:
231
+
232
+ DODO_PAYMENTS_API_KEY=your-api-key
233
+ RETURN_URL=https://yourapp.com/success
234
+ DODO_PAYMENTS_WEBHOOK_SECRET=your-webhook-secret
235
+
236
+ Use these inside your code as:
237
+
238
+ process.env.DODO_PAYMENTS_API_KEY!
239
+ process.env.DODO_PAYMENTS_WEBHOOK_SECRET!
240
+
241
+ Security Note: Do NOT commit secrets to version control. Use .env files locally and secrets managers in deployment environments (e.g., AWS, Vercel, Heroku, etc.).
@@ -0,0 +1,3 @@
1
+ import type { Request, Response } from "express";
2
+ import { CheckoutHandlerConfig } from "@dodopayments/core/checkout";
3
+ export declare function checkoutHandler(config: CheckoutHandlerConfig): (req: Request, res: Response) => Response<any, Record<string, any>> | Promise<void | Response<any, Record<string, any>>>;
@@ -0,0 +1,4 @@
1
+ import { Request, Response } from "express";
2
+ import { ClientOptions } from "dodopayments";
3
+ export type CustomerPortalConfig = Pick<ClientOptions, "environment" | "bearerToken">;
4
+ export declare const CustomerPortal: ({ bearerToken, environment, }: CustomerPortalConfig) => (req: Request, res: Response) => Promise<void | Response<any, Record<string, any>>>;