@dodopayments/bun 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 +383 -0
- package/dist/checkout/checkout.d.ts +3 -0
- package/dist/checkout/checkout.d.ts.map +1 -0
- package/dist/customer-portal/customer-portal.d.ts +4 -0
- package/dist/customer-portal/customer-portal.d.ts.map +1 -0
- package/dist/index.cjs +8798 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8794 -0
- package/dist/index.js.map +1 -0
- package/dist/webhooks/webhooks.d.ts +3 -0
- package/dist/webhooks/webhooks.d.ts.map +1 -0
- package/llm-prompt.txt +93 -0
- package/package.json +57 -0
package/README.md
ADDED
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
# @dodopayments/bun
|
|
2
|
+
|
|
3
|
+
A typescript library that exports Handlers for Checkout, Customer Portal, and Webhook routes for easy integration with your Bun server.
|
|
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 Bun adaptor](https://docs.dodopayments.com/developer-resources/bun-adaptor)
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
You can install this package using Bun:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
bun add @dodopayments/bun
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Quick Start
|
|
20
|
+
|
|
21
|
+
### 1. Checkout
|
|
22
|
+
|
|
23
|
+
```typescript
|
|
24
|
+
import { Checkout } from "@dodopayments/bun";
|
|
25
|
+
|
|
26
|
+
// Static checkout handler (handles GET requests)
|
|
27
|
+
const staticCheckoutHandler = Checkout({
|
|
28
|
+
bearerToken: process.env.DODO_PAYMENTS_API_KEY,
|
|
29
|
+
returnUrl: process.env.DODO_PAYMENTS_RETURN_URL,
|
|
30
|
+
environment: process.env.DODO_PAYMENTS_ENVIRONMENT,
|
|
31
|
+
type: "static", // optional, defaults to 'static'
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// Session checkout handler (handles POST requests)
|
|
35
|
+
const sessionCheckoutHandler = Checkout({
|
|
36
|
+
bearerToken: process.env.DODO_PAYMENTS_API_KEY,
|
|
37
|
+
returnUrl: process.env.DODO_PAYMENTS_RETURN_URL,
|
|
38
|
+
environment: process.env.DODO_PAYMENTS_ENVIRONMENT,
|
|
39
|
+
type: "session",
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
Bun.serve({
|
|
43
|
+
port: 3000,
|
|
44
|
+
fetch(request) {
|
|
45
|
+
const url = new URL(request.url);
|
|
46
|
+
|
|
47
|
+
if (url.pathname === "/api/checkout") {
|
|
48
|
+
if (request.method === "GET") {
|
|
49
|
+
return staticCheckoutHandler(request);
|
|
50
|
+
}
|
|
51
|
+
if (request.method === "POST") {
|
|
52
|
+
return sessionCheckoutHandler(request);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return new Response("Not Found", { status: 404 });
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
**Note:** You can also use `type: "dynamic"` for dynamic checkout links instead of `"session"`.
|
|
62
|
+
|
|
63
|
+
---
|
|
64
|
+
|
|
65
|
+
### 2. Customer Portal Route Handler
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
import { CustomerPortal } from "@dodopayments/bun";
|
|
69
|
+
|
|
70
|
+
const customerPortalHandler = CustomerPortal({
|
|
71
|
+
bearerToken: process.env.DODO_PAYMENTS_API_KEY!,
|
|
72
|
+
environment: process.env.DODO_PAYMENTS_ENVIRONMENT,
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
Bun.serve({
|
|
76
|
+
port: 3000,
|
|
77
|
+
fetch(request) {
|
|
78
|
+
const url = new URL(request.url);
|
|
79
|
+
|
|
80
|
+
if (url.pathname === "/api/customer-portal" && request.method === "GET") {
|
|
81
|
+
return customerPortalHandler(request);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return new Response("Not Found", { status: 404 });
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
#### Query Parameters
|
|
90
|
+
|
|
91
|
+
- `customer_id` (required): The customer ID for the portal session (e.g., `?customer_id=cus_123`)
|
|
92
|
+
- `send_email` (optional, boolean): If set to `true`, sends an email to the customer with the portal link.
|
|
93
|
+
|
|
94
|
+
Returns 400 if `customer_id` is missing.
|
|
95
|
+
|
|
96
|
+
---
|
|
97
|
+
|
|
98
|
+
### 3. Webhook Route Handler
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
import { Webhooks } from "@dodopayments/bun";
|
|
102
|
+
|
|
103
|
+
const webhookHandler = Webhooks({
|
|
104
|
+
webhookKey: process.env.DODO_PAYMENTS_WEBHOOK_KEY!,
|
|
105
|
+
onPayload: async (payload) => {
|
|
106
|
+
// handle the payload
|
|
107
|
+
},
|
|
108
|
+
// ... other event handlers for granular control
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
Bun.serve({
|
|
112
|
+
port: 3000,
|
|
113
|
+
fetch(request) {
|
|
114
|
+
const url = new URL(request.url);
|
|
115
|
+
|
|
116
|
+
if (url.pathname === "/api/webhook" && request.method === "POST") {
|
|
117
|
+
return webhookHandler(request);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return new Response("Not Found", { status: 404 });
|
|
121
|
+
},
|
|
122
|
+
});
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
---
|
|
126
|
+
|
|
127
|
+
## Prompt for LLM
|
|
128
|
+
|
|
129
|
+
```
|
|
130
|
+
You are an expert Bun developer assistant. Your task is to guide a user through integrating the @dodopayments/bun adapter into their existing Bun server project.
|
|
131
|
+
|
|
132
|
+
The @dodopayments/bun adapter provides request handlers for Dodo Payments' Checkout, Customer Portal, and Webhook functionalities, designed to work seamlessly with Bun's native Request/Response APIs.
|
|
133
|
+
|
|
134
|
+
First, install the necessary package:
|
|
135
|
+
|
|
136
|
+
bun add @dodopayments/bun
|
|
137
|
+
|
|
138
|
+
Here's how you should structure your response:
|
|
139
|
+
|
|
140
|
+
1. Ask the user which functionalities they want to integrate.
|
|
141
|
+
|
|
142
|
+
"Which parts of the @dodopayments/bun adapter would you like to integrate into your project? You can choose one or more of the following:
|
|
143
|
+
|
|
144
|
+
Checkout Route Handler (for handling product checkouts)
|
|
145
|
+
|
|
146
|
+
Customer Portal Route Handler (for managing customer subscriptions/details)
|
|
147
|
+
|
|
148
|
+
Webhook Route Handler (for receiving Dodo Payments webhook events)
|
|
149
|
+
|
|
150
|
+
All (integrate all three)"
|
|
151
|
+
|
|
152
|
+
2. Based on the user's selection, provide detailed integration steps for each chosen functionality.
|
|
153
|
+
|
|
154
|
+
If Checkout Route Handler is selected:
|
|
155
|
+
|
|
156
|
+
Purpose: This handler manages different types of checkout flows. All checkout types (static, dynamic, and sessions) return JSON responses with checkout URLs for programmatic handling.
|
|
157
|
+
|
|
158
|
+
Integration: Create handlers in your Bun server for static (GET), dynamic (POST), and checkout sessions (POST).
|
|
159
|
+
|
|
160
|
+
Code Snippet:
|
|
161
|
+
|
|
162
|
+
import { Checkout } from '@dodopayments/bun';
|
|
163
|
+
|
|
164
|
+
const staticCheckoutHandler = Checkout({
|
|
165
|
+
bearerToken: process.env.DODO_PAYMENTS_API_KEY,
|
|
166
|
+
returnUrl: process.env.DODO_PAYMENTS_RETURN_URL,
|
|
167
|
+
environment: process.env.DODO_PAYMENTS_ENVIRONMENT,
|
|
168
|
+
type: "static"
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
const dynamicCheckoutHandler = Checkout({
|
|
172
|
+
bearerToken: process.env.DODO_PAYMENTS_API_KEY,
|
|
173
|
+
returnUrl: process.env.DODO_PAYMENTS_RETURN_URL,
|
|
174
|
+
environment: process.env.DODO_PAYMENTS_ENVIRONMENT,
|
|
175
|
+
type: "dynamic"
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
const sessionCheckoutHandler = Checkout({
|
|
179
|
+
bearerToken: process.env.DODO_PAYMENTS_API_KEY,
|
|
180
|
+
returnUrl: process.env.DODO_PAYMENTS_RETURN_URL,
|
|
181
|
+
environment: process.env.DODO_PAYMENTS_ENVIRONMENT,
|
|
182
|
+
type: "session"
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
Bun.serve({
|
|
186
|
+
port: 3000,
|
|
187
|
+
fetch(request) {
|
|
188
|
+
const url = new URL(request.url);
|
|
189
|
+
|
|
190
|
+
if (url.pathname === "/api/checkout") {
|
|
191
|
+
if (request.method === "GET") {
|
|
192
|
+
return staticCheckoutHandler(request);
|
|
193
|
+
}
|
|
194
|
+
if (request.method === "POST") {
|
|
195
|
+
// Use either dynamicCheckoutHandler or sessionCheckoutHandler based on your needs
|
|
196
|
+
return sessionCheckoutHandler(request);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return new Response("Not Found", { status: 404 });
|
|
201
|
+
},
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
Config Options:
|
|
205
|
+
|
|
206
|
+
bearerToken: Your Dodo Payments API key (recommended to be stored in DODO_PAYMENTS_API_KEY env variable).
|
|
207
|
+
|
|
208
|
+
returnUrl (optional): URL to redirect the user after successful checkout.
|
|
209
|
+
|
|
210
|
+
environment: "test_mode" or "live_mode"
|
|
211
|
+
|
|
212
|
+
type: "static" (GET), "dynamic" (POST), or "session" (POST)
|
|
213
|
+
|
|
214
|
+
GET (static checkout) expects query parameters:
|
|
215
|
+
|
|
216
|
+
productId (required)
|
|
217
|
+
|
|
218
|
+
quantity, customer fields (fullName, email, etc.), and metadata (metadata_*) are optional.
|
|
219
|
+
|
|
220
|
+
Returns: {"checkout_url": "https://checkout.dodopayments.com/..."}
|
|
221
|
+
|
|
222
|
+
POST (dynamic checkout) expects a JSON body with payment details (one-time or subscription). Reference the docs for the full POST schema:
|
|
223
|
+
|
|
224
|
+
One-time payments: https://docs.dodopayments.com/api-reference/payments/post-payments
|
|
225
|
+
|
|
226
|
+
Subscriptions: https://docs.dodopayments.com/api-reference/subscriptions/post-subscriptions
|
|
227
|
+
|
|
228
|
+
Returns: {"checkout_url": "https://checkout.dodopayments.com/..."}
|
|
229
|
+
|
|
230
|
+
POST (checkout sessions) - (Recommended) A more customizable checkout experience:
|
|
231
|
+
|
|
232
|
+
Expects a JSON body with product_cart array and customer details.
|
|
233
|
+
|
|
234
|
+
One-time payments: https://docs.dodopayments.com/api-reference/payments/post-payments
|
|
235
|
+
|
|
236
|
+
Subscriptions: https://docs.dodopayments.com/api-reference/subscriptions/post-subscriptions
|
|
237
|
+
|
|
238
|
+
Required fields for checkout sessions:
|
|
239
|
+
product_cart (array): Array of products with product_id and quantity
|
|
240
|
+
|
|
241
|
+
Returns: {"checkout_url": "https://checkout.dodopayments.com/session/..."}
|
|
242
|
+
|
|
243
|
+
Error Handling: If productId is missing or other parameters are invalid, the handler will return a 400 response.
|
|
244
|
+
|
|
245
|
+
If Customer Portal Route Handler is selected:
|
|
246
|
+
|
|
247
|
+
Purpose: This route allows customers to manage their subscriptions via the Dodo Payments portal.
|
|
248
|
+
|
|
249
|
+
Integration:
|
|
250
|
+
|
|
251
|
+
import { CustomerPortal } from "@dodopayments/bun";
|
|
252
|
+
|
|
253
|
+
const customerPortalHandler = CustomerPortal({
|
|
254
|
+
bearerToken: process.env.DODO_PAYMENTS_API_KEY,
|
|
255
|
+
environment: process.env.DODO_PAYMENTS_ENVIRONMENT,
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
Bun.serve({
|
|
259
|
+
port: 3000,
|
|
260
|
+
fetch(request) {
|
|
261
|
+
const url = new URL(request.url);
|
|
262
|
+
|
|
263
|
+
if (url.pathname === "/api/customer-portal" && request.method === "GET") {
|
|
264
|
+
return customerPortalHandler(request);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
return new Response("Not Found", { status: 404 });
|
|
268
|
+
},
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
Query Parameters:
|
|
272
|
+
|
|
273
|
+
customer_id (required): e.g., ?customer_id=cus_123
|
|
274
|
+
|
|
275
|
+
send_email (optional): if true, customer is emailed the portal link
|
|
276
|
+
|
|
277
|
+
Returns 400 if customer_id is missing.
|
|
278
|
+
|
|
279
|
+
If Webhook Route Handler is selected:
|
|
280
|
+
|
|
281
|
+
Purpose: Processes incoming webhook events from Dodo Payments to trigger events in your app.
|
|
282
|
+
|
|
283
|
+
Integration:
|
|
284
|
+
|
|
285
|
+
import { Webhooks } from "@dodopayments/bun";
|
|
286
|
+
|
|
287
|
+
const webhookHandler = Webhooks({
|
|
288
|
+
webhookKey: process.env.DODO_PAYMENTS_WEBHOOK_KEY,
|
|
289
|
+
onPayload: async (payload) => {
|
|
290
|
+
// Handle generic payload
|
|
291
|
+
},
|
|
292
|
+
// You can also provide fine-grained handlers for each event type below
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
Bun.serve({
|
|
296
|
+
port: 3000,
|
|
297
|
+
fetch(request) {
|
|
298
|
+
const url = new URL(request.url);
|
|
299
|
+
|
|
300
|
+
if (url.pathname === "/api/webhook" && request.method === "POST") {
|
|
301
|
+
return webhookHandler(request);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
return new Response("Not Found", { status: 404 });
|
|
305
|
+
},
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
Features:
|
|
309
|
+
|
|
310
|
+
Only POST method is allowed — others return 405
|
|
311
|
+
|
|
312
|
+
Signature verification is performed using webhookKey. Returns 401 if invalid.
|
|
313
|
+
|
|
314
|
+
Zod-based payload validation. Returns 400 if invalid schema.
|
|
315
|
+
|
|
316
|
+
All handlers are async functions.
|
|
317
|
+
|
|
318
|
+
Supported Webhook Event Handlers:
|
|
319
|
+
|
|
320
|
+
You may pass in any of the following handlers:
|
|
321
|
+
|
|
322
|
+
onPayload?: (payload: WebhookPayload) => Promise<void>
|
|
323
|
+
|
|
324
|
+
onPaymentSucceeded?: (payload: WebhookPayload) => Promise<void>
|
|
325
|
+
|
|
326
|
+
onPaymentFailed?: (payload: WebhookPayload) => Promise<void>
|
|
327
|
+
|
|
328
|
+
onPaymentProcessing?: (payload: WebhookPayload) => Promise<void>
|
|
329
|
+
|
|
330
|
+
onPaymentCancelled?: (payload: WebhookPayload) => Promise<void>
|
|
331
|
+
|
|
332
|
+
onRefundSucceeded?: (payload: WebhookPayload) => Promise<void>
|
|
333
|
+
|
|
334
|
+
onRefundFailed?: (payload: WebhookPayload) => Promise<void>
|
|
335
|
+
|
|
336
|
+
onDisputeOpened?: (payload: WebhookPayload) => Promise<void>
|
|
337
|
+
|
|
338
|
+
onDisputeExpired?: (payload: WebhookPayload) => Promise<void>
|
|
339
|
+
|
|
340
|
+
onDisputeAccepted?: (payload: WebhookPayload) => Promise<void>
|
|
341
|
+
|
|
342
|
+
onDisputeCancelled?: (payload: WebhookPayload) => Promise<void>
|
|
343
|
+
|
|
344
|
+
onDisputeChallenged?: (payload: WebhookPayload) => Promise<void>
|
|
345
|
+
|
|
346
|
+
onDisputeWon?: (payload: WebhookPayload) => Promise<void>
|
|
347
|
+
|
|
348
|
+
onDisputeLost?: (payload: WebhookPayload) => Promise<void>
|
|
349
|
+
|
|
350
|
+
onSubscriptionActive?: (payload: WebhookPayload) => Promise<void>
|
|
351
|
+
|
|
352
|
+
onSubscriptionOnHold?: (payload: WebhookPayload) => Promise<void>
|
|
353
|
+
|
|
354
|
+
onSubscriptionRenewed?: (payload: WebhookPayload) => Promise<void>
|
|
355
|
+
|
|
356
|
+
onSubscriptionPaused?: (payload: WebhookPayload) => Promise<void>
|
|
357
|
+
|
|
358
|
+
onSubscriptionPlanChanged?: (payload: WebhookPayload) => Promise<void>
|
|
359
|
+
|
|
360
|
+
onSubscriptionCancelled?: (payload: WebhookPayload) => Promise<void>
|
|
361
|
+
|
|
362
|
+
onSubscriptionFailed?: (payload: WebhookPayload) => Promise<void>
|
|
363
|
+
|
|
364
|
+
onSubscriptionExpired?: (payload: WebhookPayload) => Promise<void>
|
|
365
|
+
|
|
366
|
+
onLicenseKeyCreated?: (payload: WebhookPayload) => Promise<void>
|
|
367
|
+
|
|
368
|
+
Environment Variable Setup:
|
|
369
|
+
|
|
370
|
+
Make sure to define these environment variables in your project:
|
|
371
|
+
|
|
372
|
+
DODO_PAYMENTS_API_KEY=your-api-key
|
|
373
|
+
DODO_PAYMENTS_WEBHOOK_KEY=your-webhook-secret
|
|
374
|
+
DODO_PAYMENTS_ENVIRONMENT="test_mode" or "live_mode"
|
|
375
|
+
DODO_PAYMENTS_RETURN_URL=your-return-url
|
|
376
|
+
|
|
377
|
+
Use these inside your code as:
|
|
378
|
+
|
|
379
|
+
process.env.DODO_PAYMENTS_API_KEY
|
|
380
|
+
process.env.DODO_PAYMENTS_WEBHOOK_KEY
|
|
381
|
+
|
|
382
|
+
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.).
|
|
383
|
+
```
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"checkout.d.ts","sourceRoot":"","sources":["../../src/checkout/checkout.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,qBAAqB,EAItB,MAAM,6BAA6B,CAAC;AAErC,eAAO,MAAM,QAAQ,GAAI,QAAQ,qBAAqB,MA2F5C,SAAS,OAAO,KAAG,OAAO,CAAC,QAAQ,CAM5C,CAAC"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { ClientOptions } from "dodopayments";
|
|
2
|
+
export type CustomerPortalConfig = Pick<ClientOptions, "environment" | "bearerToken">;
|
|
3
|
+
export declare const CustomerPortal: ({ bearerToken, environment, }: CustomerPortalConfig) => (request: Request) => Promise<Response>;
|
|
4
|
+
//# sourceMappingURL=customer-portal.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"customer-portal.d.ts","sourceRoot":"","sources":["../../src/customer-portal/customer-portal.ts"],"names":[],"mappings":"AAAA,OAAqB,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAE3D,MAAM,MAAM,oBAAoB,GAAG,IAAI,CACrC,aAAa,EACb,aAAa,GAAG,aAAa,CAC9B,CAAC;AAEF,eAAO,MAAM,cAAc,GAAI,+BAG5B,oBAAoB,eACc,OAAO,KAAG,OAAO,CAAC,QAAQ,CAsC9D,CAAC"}
|