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