@dodopayments/fastify 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,262 @@
1
+ # `@dodopayments/fastify`
2
+
3
+ A typescript library that exports Handlers for Checkout, Customer Portal, and Webhook routes for easy integration with your Fastify 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 Fastify adaptor](https://docs.dodopayments.com/developer-resources/fastify-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/fastify
17
+ ```
18
+
19
+ ## Quick Start
20
+
21
+
22
+ ### 1. Checkout
23
+
24
+ ```typescript
25
+ import { Checkout } from '@dodopayments/fastify';
26
+
27
+ const fastify = Fastify({})
28
+ const checkoutGet = Checkout({
29
+ bearerToken: process.env.DODO_PAYMENTS_API_KEY,
30
+ environment: process.env.DODO_PAYMENTS_ENVIRONMENT,
31
+ returnUrl: process.env.DODO_PAYMENTS_RETURN_URL,
32
+ type: 'static'
33
+ });
34
+
35
+ fastify.get('/api/checkout', checkoutGet.getHandler);
36
+ ```
37
+
38
+ ---
39
+
40
+ ### 2. Customer Portal Route Handler
41
+
42
+ ```typescript
43
+ import { CustomerPortal } from "@dodopayments/fastify";
44
+
45
+ const fastify = Fastify({})
46
+ const customerPortalHandler = CustomerPortal({
47
+ bearerToken: process.env.DODO_PAYMENTS_API_KEY,
48
+ environment: process.env.DODO_PAYMENTS_ENVIRONMENT
49
+ });
50
+ fastify.get('/api/customer-portal', customerPortalHandler);
51
+
52
+ ```
53
+
54
+ #### Query Parameters
55
+
56
+ - `customer_id` (required): The customer ID for the portal session (e.g., `?customer_id=cus_123`)
57
+ - `send_email` (optional, boolean): If set to `true`, sends an email to the customer with the portal link.
58
+
59
+ Returns 400 if `customer_id` is missing.
60
+
61
+ ---
62
+
63
+ ### 3. Webhook Route Handler
64
+
65
+ ```typescript
66
+
67
+ import Fastify from 'fastify'
68
+ import { Webhooks } from '@dodopayments/fastify'
69
+
70
+ const fastify = Fastify({})
71
+ fastify.addContentTypeParser('application/json', { parseAs: 'string' }, function (req, body, done) {
72
+ done(null, body)
73
+ })
74
+ fastify.post('/api/webhooks', Webhooks({
75
+ webhookKey: process.env.DODO_PAYMENTS_WEBHOOK_KEY,
76
+ onPayload: async (payload) => {
77
+ // Handle Payload Here
78
+ console.log(payload)
79
+ }
80
+ }));
81
+
82
+ ```
83
+
84
+ ---
85
+
86
+ ## Prompt for LLM
87
+
88
+ ```
89
+ You are an expert Fastify developer assistant. Your task is to guide a user through integrating the @dodopayments/fastify adapter into their existing Fastify project.
90
+
91
+ The @dodopayments/fastify adapter provides route handlers for Dodo Payments' Checkout, Customer Portal, and Webhook functionalities, designed to plug directly into an Fastify app.
92
+
93
+ First, install the necessary package. Use the package manager appropriate for the user's project (npm, yarn, or bun):
94
+
95
+ npm install @dodopayments/fastify
96
+
97
+ ---
98
+
99
+ Here's how you should structure your response:
100
+
101
+ 1. Ask the user which functionalities they want to integrate.
102
+
103
+ "Which parts of the @dodopayments/fastify adapter would you like to integrate into your project? You can choose one or more of the following:
104
+
105
+ - Checkout Route Handler (for handling product checkouts)
106
+ - Customer Portal Route Handler (for managing customer subscriptions/details)
107
+ - Webhook Route Handler (for receiving Dodo Payments webhook events)
108
+ - All (integrate all three)"
109
+
110
+ ---
111
+
112
+ 2. Based on the user's selection, provide detailed integration steps for each chosen functionality.
113
+
114
+ ---
115
+
116
+ **If Checkout Route Handler is selected:**
117
+
118
+ **Purpose**: This handler redirects users to the Dodo Payments checkout page.
119
+
120
+ **Integration**:
121
+ Create two routes in your Fastify app — one for static (GET) and one for dynamic (POST) checkout.
122
+
123
+
124
+ import { Checkout } from '@dodopayments/fastify';
125
+ import Fastify from 'fastify'
126
+
127
+ const fastify = Fastify({})
128
+ const checkoutGet = Checkout({
129
+ bearerToken: process.env.DODO_PAYMENTS_API_KEY,
130
+ environment: process.env.DODO_PAYMENTS_ENVIRONMENT,
131
+ returnUrl: process.env.DODO_PAYMENTS_RETURN_URL,
132
+ type: 'static'
133
+ });
134
+
135
+ const checkoutPost = Checkout({
136
+ bearerToken: process.env.DODO_PAYMENTS_API_KEY,
137
+ environment: process.env.DODO_PAYMENTS_ENVIRONMENT,
138
+ returnUrl: process.env.DODO_PAYMENTS_RETURN_URL,
139
+ type: 'dynamic'
140
+ });
141
+
142
+ fastify.get('/api/checkout', checkoutGet.getHandler);
143
+ fastify.post('/api/checkout', checkoutGet.postHandler);
144
+
145
+
146
+ Config Options:
147
+
148
+ bearerToken: Your Dodo Payments API key (recommended to be stored in DODO_PAYMENTS_API_KEY env variable).
149
+
150
+ returnUrl (optional): URL to redirect the user after successful checkout.
151
+
152
+ environment: "test_mode" or "live_mode"
153
+
154
+ type: "static" (GET) or "dynamic" (POST)
155
+
156
+ GET (static checkout) expects query parameters:
157
+
158
+ productId (required)
159
+
160
+ quantity, customer fields (fullName, email, etc.), and metadata (metadata_*) are optional.
161
+
162
+ POST (dynamic checkout) expects a JSON body with payment details (one-time or subscription). Reference the docs for the full POST schema:
163
+
164
+ One-time payments: https://docs.dodopayments.com/api-reference/payments/post-payments
165
+
166
+ Subscriptions: https://docs.dodopayments.com/api-reference/subscriptions/post-subscriptions
167
+
168
+ If Customer Portal Route Handler is selected:
169
+
170
+ Purpose: This route allows customers to manage their subscriptions via the Dodo Payments portal.
171
+
172
+ Integration:
173
+
174
+ import { CustomerPortal } from "@dodopayments/fastify";
175
+ import Fastify from 'fastify'
176
+
177
+ const fastify = Fastify({})
178
+ const customerPortalHandler = CustomerPortal({
179
+ bearerToken: process.env.DODO_PAYMENTS_API_KEY,
180
+ environment: process.env.DODO_PAYMENTS_ENVIRONMENT
181
+ });
182
+ fastify.get('/api/customer-portal', customerPortalHandler);
183
+
184
+ Query Parameters:
185
+
186
+ customer_id (required): e.g., ?customer_id=cus_123
187
+
188
+ send_email (optional): if true, customer is emailed the portal link
189
+
190
+ Returns 400 if customer_id is missing.
191
+
192
+ If Webhook Route Handler is selected:
193
+
194
+ Purpose: Processes incoming webhook events from Dodo Payments to trigger events in your app.
195
+
196
+ Integration:
197
+
198
+ import Fastify from 'fastify'
199
+ import { Webhooks } from '@dodopayments/fastify'
200
+
201
+ const fastify = Fastify({})
202
+ fastify.addContentTypeParser('application/json', { parseAs: 'string' }, function (req, body, done) {
203
+ done(null, body)
204
+ })
205
+
206
+ fastify.post('/api/webhooks', Webhooks({
207
+ webhookKey: process.env.DODO_PAYMENTS_WEBHOOK_KEY,
208
+ onPayload: async (payload) => {
209
+ // Handle Payload Here
210
+ console.log(payload)
211
+ }
212
+ }));
213
+
214
+ Features:
215
+
216
+ Only POST method is allowed — others return 405
217
+
218
+ Signature verification is performed using webhookKey. Returns 401 if invalid.
219
+
220
+ Zod-based payload validation. Returns 400 if invalid schema.
221
+
222
+ All handlers are async functions.
223
+
224
+ Supported Webhook Event Handlers:
225
+
226
+ You may pass in any of the following handlers:
227
+
228
+ onPayload
229
+
230
+ onPaymentSucceeded
231
+
232
+ onPaymentFailed
233
+
234
+ onPaymentProcessing
235
+
236
+ onPaymentCancelled
237
+
238
+ onRefundSucceeded
239
+
240
+ onRefundFailed
241
+
242
+ onDisputeOpened, onDisputeExpired, onDisputeAccepted, onDisputeCancelled, onDisputeChallenged, onDisputeWon, onDisputeLost
243
+
244
+ onSubscriptionActive, onSubscriptionOnHold, onSubscriptionRenewed, onSubscriptionPaused, onSubscriptionPlanChanged, onSubscriptionCancelled, onSubscriptionFailed, onSubscriptionExpired
245
+
246
+ onLicenseKeyCreated
247
+
248
+ Environment Variable Setup:
249
+
250
+ Make sure to define these environment variables in your project:
251
+
252
+ DODO_PAYMENTS_API_KEY=your-api-key
253
+ DODO_PAYMENTS_RETURN_URL=https://yourapp.com/success
254
+ DODO_PAYMENTS_WEBHOOK_KEY=your-webhook-secret
255
+ DODO_PAYMENTS_ENVIRONMENT="test"or"live"
256
+
257
+ Use these inside your code as:
258
+
259
+ process.env.DODO_PAYMENTS_API_KEY
260
+ process.env.DODO_PAYMENTS_WEBHOOK_KEY
261
+
262
+ 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,7 @@
1
+ import { FastifyRequest, FastifyReply } from "fastify";
2
+ import { CheckoutHandlerConfig } from "@dodopayments/core/checkout";
3
+ export declare const Checkout: (config: CheckoutHandlerConfig) => {
4
+ getHandler: (request: FastifyRequest, reply: FastifyReply) => Promise<never>;
5
+ postHandler: (request: FastifyRequest, reply: FastifyReply) => Promise<never>;
6
+ };
7
+ //# sourceMappingURL=checkout.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"checkout.d.ts","sourceRoot":"","sources":["../../src/checkout/checkout.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvD,OAAO,EAEL,qBAAqB,EAGtB,MAAM,6BAA6B,CAAC;AAErC,eAAO,MAAM,QAAQ,GAAI,QAAQ,qBAAqB;0BAEjB,cAAc,SAAS,YAAY;2BA4BlC,cAAc,SAAS,YAAY;CAuBxE,CAAC"}
@@ -0,0 +1,5 @@
1
+ import { FastifyRequest, FastifyReply } from "fastify";
2
+ import { ClientOptions } from "dodopayments";
3
+ export type CustomerPortalConfig = Pick<ClientOptions, "environment" | "bearerToken">;
4
+ export declare const CustomerPortal: ({ bearerToken, environment, }: CustomerPortalConfig) => (request: FastifyRequest, reply: FastifyReply) => Promise<never>;
5
+ //# 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,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvD,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,cAAc,SAAS,YAAY,mBAoCvE,CAAC"}