@dodopayments/remix 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,287 @@
1
+ # `@dodopayments/remix`
2
+
3
+ A typescript library that exports Handlers for Checkout, Customer Portal, and Webhook routes for easy integration with your Next.js 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 remix adaptor](https://docs.dodopayments.com/developer-resources/remix-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/remix
17
+ ```
18
+
19
+ ## Quick Start
20
+
21
+ All the examples below assume you're using Next.js App Router.
22
+
23
+ ### 1. Checkout
24
+
25
+ ```typescript
26
+ // app/routes/api.checkout.tsx
27
+ import { Checkout } from "@dodopayments/remix";
28
+ import type { LoaderFunctionArgs } from "@remix-run/node";
29
+
30
+ const checkoutGetHandler = Checkout({
31
+ bearerToken: process.env.DODO_PAYMENTS_API_KEY,
32
+ returnUrl: process.env.DODO_PAYMENTS_RETURN_URL,
33
+ environment: process.env.DODO_PAYMENTS_ENVIRONMENT,
34
+ type: "static", // optional, defaults to 'static'
35
+ });
36
+
37
+ export const loader = ({ request }: LoaderFunctionArgs) => checkoutGetHandler(request);
38
+ ```
39
+
40
+ ---
41
+
42
+ ### 2. Customer Portal Route Handler
43
+
44
+ ```typescript
45
+ // app/routes/api.customer-portal.tsx
46
+ import { CustomerPortal } from "@dodopayments/remix";
47
+ import type { LoaderFunctionArgs } from "@remix-run/node";
48
+
49
+
50
+ const customerPortalHandler = CustomerPortal({
51
+ bearerToken: process.env.DODO_PAYMENTS_API_KEY,
52
+ environment: process.env.DODO_PAYMENTS_ENVIRONMENT
53
+ });
54
+
55
+ export const loader = ({ request }: LoaderFunctionArgs) => customerPortalHandler(request);
56
+ ```
57
+
58
+ #### Query Parameters
59
+
60
+ - `customer_id` (required): The customer ID for the portal session (e.g., `?customer_id=cus_123`)
61
+ - `send_email` (optional, boolean): If set to `true`, sends an email to the customer with the portal link.
62
+
63
+ Returns 400 if `customer_id` is missing.
64
+
65
+ ---
66
+
67
+ ### 3. Webhook Route Handler
68
+
69
+ ```typescript
70
+ // app/routes/api.webhook.tsx
71
+ import { Webhooks } from "@dodopayments/remix";
72
+ import type { LoaderFunctionArgs } from "@remix-run/node";
73
+
74
+
75
+ const webhookHandler = Webhooks({
76
+ webhookKey: process.env.DODO_PAYMENTS_WEBHOOK_SECRET,
77
+ onPayload: async (payload) => {
78
+ //Handle Payload Here
79
+ console.log(payload)
80
+ }
81
+ });
82
+
83
+
84
+ export const action = ({ request }: LoaderFunctionArgs) => webhookHandler(request);
85
+ ```
86
+
87
+ ---
88
+
89
+ ## Prompt for LLM
90
+
91
+ ```
92
+
93
+ You are an expert Remix developer assistant. Your task is to guide a user through integrating the @dodopayments/remix adapter into their existing Remix project.
94
+
95
+ The @dodopayments/remix adapter provides route handlers for Dodo Payments' Checkout, Customer Portal, and Webhook functionalities, designed for Remix route handlers.
96
+
97
+ 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):
98
+
99
+ npm install @dodopayments/remix
100
+
101
+ Here's how you should structure your response:
102
+
103
+ Ask the user which functionalities they want to integrate.
104
+
105
+ "Which parts of the @dodopayments/remix adapter would you like to integrate into your project? You can choose one or more of the following:
106
+
107
+ Checkout Route Handler (for handling product checkouts)
108
+
109
+ Customer Portal Route Handler (for managing customer subscriptions/details)
110
+
111
+ Webhook Route Handler (for receiving Dodo Payments webhook events)
112
+
113
+ All (integrate all three)"
114
+
115
+ Based on the user's selection, provide detailed integration steps for each chosen functionality.
116
+
117
+ If Checkout Route Handler is selected:
118
+
119
+ Purpose: This handler redirects users to the Dodo Payments checkout page.
120
+ File Creation: Create a new file at app/routes/api.checkout.tsx in your Remix project.
121
+
122
+ Code Snippet:
123
+
124
+ // app/routes/api.checkout.tsx
125
+ import { Checkout } from "@dodopayments/remix";
126
+ import type { LoaderFunctionArgs } from "@remix-run/node";
127
+
128
+ const checkoutGetHandler = Checkout({
129
+ bearerToken: process.env.DODO_PAYMENTS_API_KEY,
130
+ returnUrl: process.env.DODO_PAYMENTS_RETURN_URL,
131
+ environment: process.env.DODO_PAYMENTS_ENVIRONMENT,
132
+ type: "static", // optional, defaults to 'static'
133
+ });
134
+
135
+ const checkoutPostHandler = Checkout({
136
+ bearerToken: process.env.DODO_PAYMENTS_API_KEY,
137
+ returnUrl: process.env.DODO_PAYMENTS_RETURN_URL,
138
+ environment: process.env.DODO_PAYMENTS_ENVIRONMENT,
139
+ type: "dynamic", // optional, defaults to 'static'
140
+ });
141
+
142
+ export const loader = ({ request }: LoaderFunctionArgs) => checkoutGetHandler(request);
143
+
144
+ export const action = ({ request }: LoaderFunctionArgs) => checkoutPostHandler(request);
145
+
146
+ If Customer Portal Route Handler is selected:
147
+
148
+ Purpose: This handler redirects authenticated users to their Dodo Payments customer portal.
149
+ File Creation: Create a new file at app/routes/api.customer-portal.tsx in your Remix project.
150
+
151
+ Code Snippet:
152
+
153
+ // app/routes/api.customer-portal.tsx
154
+ import { CustomerPortal } from "@dodopayments/remix";
155
+ import type { LoaderFunctionArgs } from "@remix-run/node";
156
+
157
+ const customerPortalHandler = CustomerPortal({
158
+ bearerToken: process.env.DODO_PAYMENTS_API_KEY,
159
+ environment: process.env.DODO_PAYMENTS_ENVIRONMENT
160
+ });
161
+
162
+ export const loader = ({ request }: LoaderFunctionArgs) => customerPortalHandler(request);
163
+
164
+ Query Parameters:
165
+
166
+ customer_id (required): The customer ID for the portal session (e.g., ?customer_id=cus_123)
167
+
168
+ send_email (optional, boolean): If set to true, sends an email to the customer with the portal link.
169
+
170
+ Returns 400 if customer_id is missing.
171
+
172
+ If Webhook Route Handler is selected:
173
+
174
+ Purpose: This handler processes incoming webhook events from Dodo Payments, allowing your application to react to events like successful payments, refunds, or subscription changes.
175
+ File Creation: Create a new file at app/routes/api.webhook.tsx in your Remix project.
176
+
177
+ Code Snippet:
178
+
179
+ // app/routes/api.webhook.tsx
180
+ import { Webhooks } from "@dodopayments/remix";
181
+ import type { LoaderFunctionArgs } from "@remix-run/node";
182
+
183
+ const webhookHandler = Webhooks({
184
+ webhookKey: process.env.DODO_PAYMENTS_WEBHOOK_SECRET,
185
+ onPayload: async (payload) => {
186
+ //Handle Payload Here
187
+ console.log(payload)
188
+ }
189
+ });
190
+
191
+ export const action = ({ request }: LoaderFunctionArgs) => webhookHandler(request);
192
+
193
+ Handler Details:
194
+
195
+ Method: Only POST requests are supported. Other methods return 405.
196
+
197
+ Signature Verification: The handler verifies the webhook signature using the webhookKey and returns 401 if verification fails.
198
+
199
+ Payload Validation: The payload is validated with Zod. Returns 400 for invalid payloads.
200
+
201
+ Error Handling:
202
+
203
+ 401: Invalid signature
204
+
205
+ 400: Invalid payload
206
+
207
+ 500: Internal error during verification
208
+
209
+ Event Routing: Calls the appropriate event handler based on the payload type.
210
+
211
+ Supported Webhook Event Handlers:
212
+
213
+ onPayload?: (payload: WebhookPayload) => Promise<void>
214
+
215
+ onPaymentSucceeded?: (payload: WebhookPayload) => Promise<void>
216
+
217
+ onPaymentFailed?: (payload: WebhookPayload) => Promise<void>
218
+
219
+ onPaymentProcessing?: (payload: WebhookPayload) => Promise<void>
220
+
221
+ onPaymentCancelled?: (payload: WebhookPayload) => Promise<void>
222
+
223
+ onRefundSucceeded?: (payload: WebhookPayload) => Promise<void>
224
+
225
+ onRefundFailed?: (payload: WebhookPayload) => Promise<void>
226
+
227
+ onDisputeOpened?: (payload: WebhookPayload) => Promise<void>
228
+
229
+ onDisputeExpired?: (payload: WebhookPayload) => Promise<void>
230
+
231
+ onDisputeAccepted?: (payload: WebhookPayload) => Promise<void>
232
+
233
+ onDisputeCancelled?: (payload: WebhookPayload) => Promise<void>
234
+
235
+ onDisputeChallenged?: (payload: WebhookPayload) => Promise<void>
236
+
237
+ onDisputeWon?: (payload: WebhookPayload) => Promise<void>
238
+
239
+ onDisputeLost?: (payload: WebhookPayload) => Promise<void>
240
+
241
+ onSubscriptionActive?: (payload: WebhookPayload) => Promise<void>
242
+
243
+ onSubscriptionOnHold?: (payload: WebhookPayload) => Promise<void>
244
+
245
+ onSubscriptionRenewed?: (payload: WebhookPayload) => Promise<void>
246
+
247
+ onSubscriptionPaused?: (payload: WebhookPayload) => Promise<void>
248
+
249
+ onSubscriptionPlanChanged?: (payload: WebhookPayload) => Promise<void>
250
+
251
+ onSubscriptionCancelled?: (payload: WebhookPayload) => Promise<void>
252
+
253
+ onSubscriptionFailed?: (payload: WebhookPayload) => Promise<void>
254
+
255
+ onSubscriptionExpired?: (payload: WebhookPayload) => Promise<void>
256
+
257
+ onLicenseKeyCreated?: (payload: WebhookPayload) => Promise<void>
258
+
259
+ Environment Variable Setup:
260
+
261
+ To ensure the adapter functions correctly, you will need to manually set up the following environment variables in your Remix project's deployment environment (e.g., Vercel, Netlify, AWS, etc.):
262
+
263
+ DODO_PAYMENTS_API_KEY: Your Dodo Payments API Key (required for Checkout and Customer Portal).
264
+
265
+ DODO_PAYMENTS_RETURN_URL: (Optional) The URL to redirect to after a successful checkout (for Checkout handler).
266
+
267
+ DODO_PAYMENTS_ENVIRONMENT: (Optional) The environment for the API (e.g., test or live).
268
+
269
+ DODO_PAYMENTS_WEBHOOK_SECRET: Your Dodo Payments Webhook Secret (required for Webhook handler).
270
+
271
+ Example .env file:
272
+
273
+ DODO_PAYMENTS_API_KEY=your-api-key
274
+ DODO_PAYMENTS_RETURN_URL=your-return-url
275
+ DODO_PAYMENTS_ENVIRONMENT=test
276
+ DODO_PAYMENTS_WEBHOOK_SECRET=your-webhook-secret
277
+
278
+ Usage in your code:
279
+
280
+ bearerToken: process.env.DODO_PAYMENTS_API_KEY
281
+ webhookKey: process.env.DODO_PAYMENTS_WEBHOOK_SECRET
282
+
283
+ Important: Never commit sensitive environment variables directly into your version control. Use environment variables for all sensitive information.
284
+
285
+ 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
286
+
287
+ ```
@@ -0,0 +1,7 @@
1
+ import { CheckoutHandlerConfig } from "@dodopayments/core/checkout";
2
+ /**
3
+ * Remix Checkout handler
4
+ * Usage: export const loader = Checkout(config); export const action = 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,8CAmDxC"}
@@ -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<import("undici-types").Response>;
4
+ //# sourceMappingURL=customer-portal.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"customer-portal.d.ts","sourceRoot":"","sources":["../../src/customerPortal/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,MACE,SAAS,OAAO,6CAqCxC,CAAC"}