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