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