@dodopayments/tanstack 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,336 @@
1
+ # `@dodopayments/tanstack`
2
+
3
+ A typescript library that exports Handlers for Checkout, Customer Portal, and Webhook routes for easy integration with your Tanstack 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 Tanstack adaptor](https://docs.dodopayments.com/developer-resources/tanstack-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/tanstack
17
+ ```
18
+
19
+ ## Quick Start
20
+
21
+ All the examples below assume you're using Tanstack App Router.
22
+
23
+ ### 1. Checkout
24
+
25
+ ```typescript
26
+ // src/routes/api/checkout.ts
27
+ import { Checkout } from "@dodopayments/tanstack";
28
+ import { createServerFileRoute } from "@tanstack/react-start/server";
29
+
30
+ export const ServerRoute = createServerFileRoute("/api/checkout")
31
+ .methods({
32
+ GET: async ({ request }) => {
33
+ return Checkout({
34
+ bearerToken: process.env.DODO_PAYMENTS_API_KEY,
35
+ returnUrl: process.env.DODO_PAYMENTS_RETURN_URL,
36
+ environment: process.env.DODO_PAYMENTS_ENVIRONMENT,
37
+ type: "static", // optional, defaults to 'static'
38
+ })(request)
39
+ }
40
+ })
41
+
42
+ ```
43
+
44
+ ---
45
+
46
+ ### 2. Customer Portal Route Handler
47
+
48
+ ```typescript
49
+ // src/routes/api/customer-portal.ts
50
+
51
+ import { CustomerPortal } from "@dodopayments/tanstack";
52
+ import { createServerFileRoute } from "@tanstack/react-start/server";
53
+
54
+ export const ServerRoute = createServerFileRoute('/api/customer-portal')
55
+ .methods({
56
+ GET: async ({ request }) => {
57
+ return CustomerPortal({
58
+ bearerToken: process.env.DODO_PAYMENTS_API_KEY,
59
+ environment: process.env.DODO_PAYMENTS_ENVIRONMENT,
60
+ })(request)
61
+ }
62
+ })
63
+
64
+ ```
65
+
66
+ #### Query Parameters
67
+
68
+ - `customer_id` (required): The customer ID for the portal session (e.g., `?customer_id=cus_123`)
69
+ - `send_email` (optional, boolean): If set to `true`, sends an email to the customer with the portal link.
70
+
71
+ Returns 400 if `customer_id` is missing.
72
+
73
+ ---
74
+
75
+ ### 3. Webhook Route Handler
76
+
77
+ ```typescript
78
+ // src/routes/api/customer-portal.ts
79
+ import { Webhooks } from "@dodopayments/tanstack";
80
+ import { createServerFileRoute } from "@tanstack/react-start/server";
81
+
82
+ export const ServerRoute = createServerFileRoute('/api/webhook')
83
+ .methods({
84
+ POST: async ({ request }) => {
85
+ return Webhooks({
86
+ webhookKey: process.env.DODO_PAYMENTS_WEBHOOK_KEY,
87
+ onPayload: async (payload) => {
88
+ // Handle Payload here
89
+ console.log(payload)
90
+ }
91
+ })(request)
92
+ }
93
+ })
94
+
95
+ ```
96
+
97
+ ---
98
+
99
+ ## Prompt for LLM
100
+
101
+ ```
102
+
103
+ You are an expert Tanstack developer assistant. Your task is to guide a user through integrating the @dodopayments/tanstack adapter into their existing Tanstack project.
104
+
105
+ The @dodopayments/tanstack adapter provides route handlers for Dodo Payments' Checkout, Customer Portal, and Webhook functionalities, designed for the Tanstack App Router.
106
+
107
+ 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):
108
+
109
+ npm install @dodopayments/tanstack
110
+
111
+ Here's how you should structure your response:
112
+
113
+ Ask the user which functionalities they want to integrate.
114
+
115
+ "Which parts of the @dodopayments/tanstack adapter would you like to integrate into your project? You can choose one or more of the following:
116
+
117
+ Checkout Route Handler (for handling product checkouts)
118
+
119
+ Customer Portal Route Handler (for managing customer subscriptions/details)
120
+
121
+ Webhook Route Handler (for receiving Dodo Payments webhook events)
122
+
123
+ All (integrate all three)"
124
+
125
+ Based on the user's selection, provide detailed integration steps for each chosen functionality.
126
+
127
+ If Checkout Route Handler is selected:
128
+
129
+ Purpose: This handler redirects users to the Dodo Payments checkout page.
130
+ File Creation: Create a new file at app/checkout/route.ts in your Tanstack project.
131
+
132
+ Code Snippet:
133
+
134
+ // src/routes/api/checkout.ts
135
+ import { Checkout } from "@dodopayments/tanstack";
136
+ import { createServerFileRoute } from "@tanstack/react-start/server";
137
+
138
+ export const ServerRoute = createServerFileRoute("/api/checkout")
139
+ .methods({
140
+ GET: async ({ request }) => {
141
+ return Checkout({
142
+ bearerToken: process.env.DODO_PAYMENTS_API_KEY,
143
+ returnUrl: process.env.DODO_PAYMENTS_RETURN_URL,
144
+ environment: process.env.DODO_PAYMENTS_ENVIRONMENT,
145
+ type: "static", // optional, defaults to 'static'
146
+ })(request)
147
+ },
148
+ POST: async ({ request }) => {
149
+ return Checkout({
150
+ bearerToken: process.env.DODO_PAYMENTS_API_KEY,
151
+ returnUrl: process.env.DODO_PAYMENTS_RETURN_URL,
152
+ environment: process.env.DODO_PAYMENTS_ENVIRONMENT,
153
+ type: "dynamic", // for dynamic checkout
154
+ })(request)
155
+ }
156
+ })
157
+
158
+
159
+ Configuration & Usage:
160
+
161
+ bearerToken: Your Dodo Payments API key. It's recommended to set this via the DODO_PAYMENTS_API_KEY environment variable.
162
+
163
+ returnUrl: (Optional) The URL to redirect the user to after a successful checkout.
164
+
165
+ environment: (Optional) Set to "test_mode" for testing, or omit/set to "live_mode" for production.
166
+
167
+ type: (Optional) Set to "static" for GET/static checkout, "dynamic" for POST/dynamic checkout. Defaults to "static".
168
+
169
+ Static Checkout (GET) Query Parameters:
170
+
171
+ productId (required): Product identifier (e.g., ?productId=pdt_nZuwz45WAs64n3l07zpQR)
172
+
173
+ quantity (optional): Quantity of the product
174
+
175
+ Customer Fields (optional): fullName, firstName, lastName, email, country, addressLine, city, state, zipCode
176
+
177
+ Disable Flags (optional, set to true to disable): disableFullName, disableFirstName, disableLastName, disableEmail, disableCountry, disableAddressLine, disableCity, disableState, disableZipCode
178
+
179
+ Advanced Controls (optional): paymentCurrency, showCurrencySelector, paymentAmount, showDiscounts
180
+
181
+ Metadata (optional): Any query parameter starting with metadata_ (e.g., ?metadata_userId=abc123)
182
+
183
+ 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:
184
+
185
+ Docs - One Time Payment Product: https://docs.dodopayments.com/api-reference/payments/post-payments
186
+
187
+ Docs - Subscription Product: https://docs.dodopayments.com/api-reference/subscriptions/post-subscriptions
188
+
189
+ Error Handling: If productId is missing or other query parameters are invalid, the handler will return a 400 response.
190
+
191
+ If Customer Portal Route Handler is selected:
192
+
193
+ Purpose: This handler redirects authenticated users to their Dodo Payments customer portal.
194
+ File Creation: Create a new file at app/customer-portal/route.ts in your Tanstack project.
195
+
196
+ Code Snippet:
197
+
198
+ // src/routes/api/customer-portal.ts
199
+ import { CustomerPortal } from "@dodopayments/tanstack";
200
+ import { createServerFileRoute } from "@tanstack/react-start/server";
201
+
202
+ export const ServerRoute = createServerFileRoute('/api/customer-portal')
203
+ .methods({
204
+ GET: async ({ request }) => {
205
+ return CustomerPortal({
206
+ bearerToken: process.env.DODO_PAYMENTS_API_KEY,
207
+ environment: process.env.DODO_PAYMENTS_ENVIRONMENT,
208
+ })(request)
209
+ }
210
+ })
211
+
212
+ Query Parameters:
213
+
214
+ customer_id (required): The customer ID for the portal session (e.g., ?customer_id=cus_123)
215
+
216
+ send_email (optional, boolean): If set to true, sends an email to the customer with the portal link.
217
+
218
+ Returns 400 if customer_id is missing.
219
+
220
+ If Webhook Route Handler is selected:
221
+
222
+ Purpose: This handler processes incoming webhook events from Dodo Payments, allowing your application to react to events like successful payments, refunds, or subscription changes.
223
+ File Creation: Create a new file at app/api/webhook/dodo-payments/route.ts in your Tanstack project.
224
+
225
+ Code Snippet:
226
+
227
+ // src/routes/api/webhook.ts
228
+ // routes/api/checkout.ts
229
+ import { Webhooks } from "@dodopayments/tanstack";
230
+ import { createServerFileRoute } from "@tanstack/react-start/server";
231
+
232
+ export const ServerRoute = createServerFileRoute('/api/webhook')
233
+ .methods({
234
+ POST: async ({ request }) => {
235
+ return Webhooks({
236
+ webhookKey: process.env.DODO_PAYMENTS_WEBHOOK_KEY,
237
+ onPayload: async (payload) => {
238
+ console.log(payload)
239
+ }
240
+ })(request)
241
+ }
242
+ })
243
+
244
+ Handler Details:
245
+
246
+ Method: Only POST requests are supported. Other methods return 405.
247
+
248
+ Signature Verification: The handler verifies the webhook signature using the webhookKey and returns 401 if verification fails.
249
+
250
+ Payload Validation: The payload is validated with Zod. Returns 400 for invalid payloads.
251
+
252
+ Error Handling:
253
+
254
+ 401: Invalid signature
255
+
256
+ 400: Invalid payload
257
+
258
+ 500: Internal error during verification
259
+
260
+ Event Routing: Calls the appropriate event handler based on the payload type.
261
+
262
+ Supported Webhook Event Handlers:
263
+
264
+ onPayload?: (payload: WebhookPayload) => Promise<void>
265
+
266
+ onPaymentSucceeded?: (payload: WebhookPayload) => Promise<void>
267
+
268
+ onPaymentFailed?: (payload: WebhookPayload) => Promise<void>
269
+
270
+ onPaymentProcessing?: (payload: WebhookPayload) => Promise<void>
271
+
272
+ onPaymentCancelled?: (payload: WebhookPayload) => Promise<void>
273
+
274
+ onRefundSucceeded?: (payload: WebhookPayload) => Promise<void>
275
+
276
+ onRefundFailed?: (payload: WebhookPayload) => Promise<void>
277
+
278
+ onDisputeOpened?: (payload: WebhookPayload) => Promise<void>
279
+
280
+ onDisputeExpired?: (payload: WebhookPayload) => Promise<void>
281
+
282
+ onDisputeAccepted?: (payload: WebhookPayload) => Promise<void>
283
+
284
+ onDisputeCancelled?: (payload: WebhookPayload) => Promise<void>
285
+
286
+ onDisputeChallenged?: (payload: WebhookPayload) => Promise<void>
287
+
288
+ onDisputeWon?: (payload: WebhookPayload) => Promise<void>
289
+
290
+ onDisputeLost?: (payload: WebhookPayload) => Promise<void>
291
+
292
+ onSubscriptionActive?: (payload: WebhookPayload) => Promise<void>
293
+
294
+ onSubscriptionOnHold?: (payload: WebhookPayload) => Promise<void>
295
+
296
+ onSubscriptionRenewed?: (payload: WebhookPayload) => Promise<void>
297
+
298
+ onSubscriptionPaused?: (payload: WebhookPayload) => Promise<void>
299
+
300
+ onSubscriptionPlanChanged?: (payload: WebhookPayload) => Promise<void>
301
+
302
+ onSubscriptionCancelled?: (payload: WebhookPayload) => Promise<void>
303
+
304
+ onSubscriptionFailed?: (payload: WebhookPayload) => Promise<void>
305
+
306
+ onSubscriptionExpired?: (payload: WebhookPayload) => Promise<void>
307
+
308
+ onLicenseKeyCreated?: (payload: WebhookPayload) => Promise<void>
309
+
310
+ Environment Variable Setup:
311
+
312
+ To ensure the adapter functions correctly, you will need to manually set up the following environment variables in your Tanstack project's deployment environment (e.g., Vercel, Netlify, AWS, etc.):
313
+
314
+ DODO_PAYMENTS_API_KEY: Your Dodo Payments API Key (required for Checkout and Customer Portal).
315
+
316
+ RETURN_URL: (Optional) The URL to redirect to after a successful checkout (for Checkout handler).
317
+
318
+ DODO_PAYMENTS_WEBHOOK_SECRET: Your Dodo Payments Webhook Secret (required for Webhook handler).
319
+
320
+ Example .env file:
321
+
322
+ DODO_PAYMENTS_API_KEY=your-api-key
323
+ DODO_PAYMENTS_WEBHOOK_KEY=your-webhook-secret
324
+ DODO_PAYMENTS_RETURN_URL=your-return-url
325
+ DODO_PAYMENTS_ENVIRONMENT="test" or "live"
326
+
327
+ Usage in your code:
328
+
329
+ bearerToken: process.env.DODO_PAYMENTS_API_KEY
330
+ webhookKey: process.env.DODO_PAYMENTS_WEBHOOK_KEY
331
+
332
+ Important: Never commit sensitive environment variables directly into your version control. Use environment variables for all sensitive information.
333
+
334
+ 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
335
+
336
+ ```
@@ -0,0 +1,7 @@
1
+ import { CheckoutHandlerConfig } from "@dodopayments/core/checkout";
2
+ /**
3
+ * TanStack React Start Checkout handler
4
+ * Usage: export const GET = Checkout(config); export const POST = Checkout(config);
5
+ */
6
+ export declare function Checkout(config: CheckoutHandlerConfig): (request: Request) => Promise<import("undici-types").Response>;
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,EAEL,qBAAqB,EAGtB,MAAM,6BAA6B,CAAC;AAErC;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,qBAAqB,IAC7B,SAAS,OAAO,8CAwDxC"}
@@ -0,0 +1,8 @@
1
+ import { ClientOptions } from "dodopayments";
2
+ export type CustomerPortalConfig = Pick<ClientOptions, "environment" | "bearerToken">;
3
+ /**
4
+ * TanStack React Start Customer Portal handler
5
+ * Usage: export const GET = CustomerPortal(config);
6
+ */
7
+ export declare function CustomerPortal({ bearerToken, environment, }: CustomerPortalConfig): (request: Request) => Promise<import("undici-types").Response>;
8
+ //# 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;;;GAGG;AACH,wBAAgB,cAAc,CAAC,EAC7B,WAAW,EACX,WAAW,GACZ,EAAE,oBAAoB,IACE,SAAS,OAAO,8CAuCxC"}