@dodopayments/opencode-plugin 0.3.2 → 0.4.0
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/.mcp.json +4 -2
- package/README.md +92 -12
- package/opencode-plugin/README.md +22 -3
- package/opencode-plugin/index.js +19 -12
- package/package.json +1 -1
- package/skills/best-practices/SKILL.md +335 -179
- package/skills/better-auth-integration/SKILL.md +214 -0
- package/skills/billing-sdk/SKILL.md +357 -370
- package/skills/checkout-integration/SKILL.md +580 -329
- package/skills/credit-based-billing/SKILL.md +448 -775
- package/skills/customer-management/SKILL.md +281 -0
- package/skills/discounts-and-promotions/SKILL.md +464 -0
- package/skills/framework-adapters/SKILL.md +644 -0
- package/skills/license-keys/SKILL.md +324 -199
- package/skills/localized-pricing/SKILL.md +230 -0
- package/skills/mobile-checkout/SKILL.md +521 -0
- package/skills/product-catalog-management/SKILL.md +311 -0
- package/skills/refunds-and-disputes/SKILL.md +333 -0
- package/skills/subscription-integration/SKILL.md +323 -404
- package/skills/testing-and-go-live/SKILL.md +335 -0
- package/skills/usage-based-billing/SKILL.md +339 -479
- package/skills/webhook-integration/SKILL.md +600 -402
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: better-auth-integration
|
|
3
|
+
description: Guide only for applications using @dodopayments/better-auth, covering authenticated customer sync, checkout, portal access, usage ingestion, and verified webhook callbacks.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Better Auth Integration
|
|
7
|
+
|
|
8
|
+
Use `@dodopayments/better-auth` to synchronize Better Auth users with Dodo Payments and expose authenticated checkout, customer portal, subscription, payment, usage, and webhook endpoints.
|
|
9
|
+
|
|
10
|
+
## When to use this skill
|
|
11
|
+
|
|
12
|
+
- Create a Dodo customer automatically when a Better Auth user signs up.
|
|
13
|
+
- Start checkout for a signed-in user without exposing a Dodo API key.
|
|
14
|
+
- Let users open their customer portal or list their subscriptions and payments.
|
|
15
|
+
- Submit metered usage for the signed-in customer.
|
|
16
|
+
- Receive signature-verified Dodo webhooks through Better Auth.
|
|
17
|
+
|
|
18
|
+
## Install
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install @dodopayments/better-auth dodopayments better-auth
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
The package exports the lowercase server functions `dodopayments`, `checkout`, `portal`, `usage`, and `webhooks`. Its client entry point exports `dodopaymentsClient`.
|
|
25
|
+
|
|
26
|
+
## Server setup
|
|
27
|
+
|
|
28
|
+
Every endpoint is registered by a feature in `use`. Include every feature that the application calls; omitting one makes its route return 404.
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
import { betterAuth } from "better-auth";
|
|
32
|
+
import DodoPayments from "dodopayments";
|
|
33
|
+
import { checkout, dodopayments, portal, usage, webhooks } from "@dodopayments/better-auth";
|
|
34
|
+
|
|
35
|
+
const dodoPayments = new DodoPayments({
|
|
36
|
+
bearerToken: process.env.DODO_PAYMENTS_API_KEY,
|
|
37
|
+
environment: "test_mode",
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
export const auth = betterAuth({
|
|
41
|
+
database: {
|
|
42
|
+
// your database config
|
|
43
|
+
},
|
|
44
|
+
plugins: [
|
|
45
|
+
dodopayments({
|
|
46
|
+
client: dodoPayments,
|
|
47
|
+
createCustomerOnSignUp: true,
|
|
48
|
+
// `user` here is Better Auth's base user: id, name, email, emailVerified,
|
|
49
|
+
// image, createdAt, updatedAt. Anything else (phone number, company, plan)
|
|
50
|
+
// must first be declared via `user.additionalFields` in your Better Auth
|
|
51
|
+
// config, otherwise it does not exist on the type and will be undefined.
|
|
52
|
+
getCustomerParams: (user) => ({
|
|
53
|
+
metadata: { better_auth_user_id: user.id },
|
|
54
|
+
}),
|
|
55
|
+
use: [
|
|
56
|
+
checkout({
|
|
57
|
+
products: [
|
|
58
|
+
{ productId: "pdt_premium", slug: "premium-plan" },
|
|
59
|
+
],
|
|
60
|
+
successUrl: "https://app.example.com/billing/success",
|
|
61
|
+
authenticatedUsersOnly: true,
|
|
62
|
+
}),
|
|
63
|
+
portal(),
|
|
64
|
+
usage(),
|
|
65
|
+
webhooks({
|
|
66
|
+
webhookKey: process.env.DODO_PAYMENTS_WEBHOOK_KEY,
|
|
67
|
+
onPayload: async (payload) => {
|
|
68
|
+
console.log("Verified Dodo webhook:", payload.type);
|
|
69
|
+
},
|
|
70
|
+
}),
|
|
71
|
+
],
|
|
72
|
+
}),
|
|
73
|
+
],
|
|
74
|
+
});
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
`products` maps an application slug to a Dodo product ID. `authenticatedUsersOnly: true` requires a Better Auth session for checkout. `getCustomerParams` adds application metadata when customer synchronization runs.
|
|
78
|
+
|
|
79
|
+
## Browser client setup
|
|
80
|
+
|
|
81
|
+
```typescript
|
|
82
|
+
import { createAuthClient } from "better-auth/react";
|
|
83
|
+
import { dodopaymentsClient } from "@dodopayments/better-auth/client";
|
|
84
|
+
|
|
85
|
+
export const authClient = createAuthClient({
|
|
86
|
+
plugins: [dodopaymentsClient()],
|
|
87
|
+
});
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
These are browser methods. They send the Better Auth session cookie automatically; do not pass `request.headers` to them.
|
|
91
|
+
|
|
92
|
+
## Create a checkout session
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
import { authClient } from "@/lib/auth-client";
|
|
96
|
+
|
|
97
|
+
async function startCheckout() {
|
|
98
|
+
const { data: session, error } = await authClient.dodopayments.checkoutSession({
|
|
99
|
+
slug: "premium-plan",
|
|
100
|
+
referenceId: "order_123",
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
if (error) {
|
|
104
|
+
console.error("Checkout error:", error);
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (session) {
|
|
109
|
+
window.location.href = session.url;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
`slug` must match a product configured in `checkout({ products: [...] })`. `referenceId` is optional reconciliation metadata.
|
|
115
|
+
|
|
116
|
+
## Open the customer portal
|
|
117
|
+
|
|
118
|
+
```typescript
|
|
119
|
+
const { data: portalSession, error } =
|
|
120
|
+
await authClient.dodopayments.customer.portal();
|
|
121
|
+
|
|
122
|
+
if (error) {
|
|
123
|
+
console.error("Portal error:", error);
|
|
124
|
+
} else if (portalSession?.redirect) {
|
|
125
|
+
window.location.href = portalSession.url;
|
|
126
|
+
}
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
The portal endpoint requires a signed-in, verified user and resolves that user's synchronized Dodo customer.
|
|
130
|
+
|
|
131
|
+
## List subscriptions and payments
|
|
132
|
+
|
|
133
|
+
```typescript
|
|
134
|
+
const { data: subscriptions, error: subscriptionsError } =
|
|
135
|
+
await authClient.dodopayments.customer.subscriptions.list({
|
|
136
|
+
query: { page: 1, limit: 10, status: "active" },
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
const { data: payments, error: paymentsError } =
|
|
140
|
+
await authClient.dodopayments.customer.payments.list({
|
|
141
|
+
query: { page: 1, limit: 10, status: "succeeded" },
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
if (subscriptionsError || paymentsError) {
|
|
145
|
+
console.error("Could not load billing history");
|
|
146
|
+
} else {
|
|
147
|
+
console.log("Subscriptions:", subscriptions?.items);
|
|
148
|
+
console.log("Payments:", payments?.items);
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
Both list responses use an `items` envelope. Subscription filters accept `page`, `limit`, and `status`; payment filters use the same pagination fields and payment statuses.
|
|
153
|
+
|
|
154
|
+
## Ingest metered usage
|
|
155
|
+
|
|
156
|
+
Enable `usage()` on the server, then submit an event for the signed-in customer:
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
const { data, error } = await authClient.dodopayments.usage.ingest({
|
|
160
|
+
event_id: "evt_usage_123",
|
|
161
|
+
event_name: "api_calls",
|
|
162
|
+
metadata: { requests: 100 },
|
|
163
|
+
timestamp: new Date(),
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
if (error) {
|
|
167
|
+
console.error("Usage ingestion failed:", error);
|
|
168
|
+
} else {
|
|
169
|
+
console.log("Ingested events:", data?.ingested_count);
|
|
170
|
+
}
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
The plugin derives `customer_id` from the authenticated session. `event_id` is the event's idempotency key; reuse it when retrying the same event.
|
|
174
|
+
|
|
175
|
+
## Webhook endpoint
|
|
176
|
+
|
|
177
|
+
`webhooks({ webhookKey, onPayload })` verifies and handles the request itself. With Better Auth's default `/api/auth` base path, configure this URL in the Dodo dashboard:
|
|
178
|
+
|
|
179
|
+
```text
|
|
180
|
+
https://app.example.com/api/auth/dodopayments/webhooks
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
Do not create a second route and do not call `auth.api.dodopayments.verifyWebhook()`; that method does not exist. The plugin reads the raw request, verifies the Standard Webhooks signature with `webhookKey`, and invokes `onPayload` plus any configured event-specific callback.
|
|
184
|
+
|
|
185
|
+
## Registered endpoints
|
|
186
|
+
|
|
187
|
+
- `POST /dodopayments/checkout`
|
|
188
|
+
- `POST /dodopayments/checkout-session`
|
|
189
|
+
- `GET /dodopayments/customer/portal`
|
|
190
|
+
- `GET /dodopayments/customer/subscriptions/list`
|
|
191
|
+
- `GET /dodopayments/customer/payments/list`
|
|
192
|
+
- `POST /dodopayments/usage/ingest`
|
|
193
|
+
- `GET /dodopayments/usage/meters/list`
|
|
194
|
+
- `POST /dodopayments/webhooks`
|
|
195
|
+
|
|
196
|
+
Better Auth prefixes these plugin paths with its configured API base path.
|
|
197
|
+
|
|
198
|
+
## Common mistakes
|
|
199
|
+
|
|
200
|
+
**Enabling only `portal()`.** Checkout, usage, and webhook routes do not exist unless `checkout()`, `usage()`, and `webhooks()` are also present in `use`.
|
|
201
|
+
|
|
202
|
+
**Mixing browser and server APIs.** Call `authClient.dodopayments...` in browser code without a `headers` option. Use Better Auth's server API separately when handling server requests.
|
|
203
|
+
|
|
204
|
+
**Creating duplicate customers.** When `createCustomerOnSignUp` is enabled, do not also call `client.customers.create()` for the same user.
|
|
205
|
+
|
|
206
|
+
**Granting access from browser state.** Treat verified webhook events or a server-side Dodo lookup as the source of truth for entitlements.
|
|
207
|
+
|
|
208
|
+
**Adding a custom webhook verifier.** The plugin owns `/dodopayments/webhooks`, signature verification, and callback dispatch.
|
|
209
|
+
|
|
210
|
+
## Resources
|
|
211
|
+
|
|
212
|
+
- [Better Auth Adapter](https://docs.dodopayments.com/developer-resources/better-auth-adaptor)
|
|
213
|
+
- [Dodo Payments Better Auth package](https://www.npmjs.com/package/@dodopayments/better-auth)
|
|
214
|
+
- [Dodo Payments SDK](https://github.com/dodopayments/dodopayments-typescript)
|