@tansy/payments-dodo 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +12 -2
- package/index.d.ts +2 -0
- package/index.js +121 -0
- package/package.json +30 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mohamed Salah
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
-
|
|
1
|
+
<!-- Generated by scripts/package-metadata.mjs. Change that script, not this file. -->
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
# @tansy/payments-dodo
|
|
4
|
+
|
|
5
|
+
Dodo Payments for @tansy/payments, through better-auth's Dodo Payments plugin.
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @tansy/payments-dodo
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Add it to `tansy.config.ts`. See [the payments plugin documentation](https://github.com/mhmdsalahsebai/tansy/blob/main/docs/plugins/payments.md) for what it adds and the options it takes.
|
|
12
|
+
|
|
13
|
+
A first-party plugin for [tansy](https://github.com/mhmdsalahsebai/tansy), a self-hosted, runtime-schema CMS for Next.js. MIT licensed.
|
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import DodoPayments from "dodopayments";
|
|
2
|
+
import { checkout, dodopayments, portal, webhooks } from "@dodopayments/better-auth";
|
|
3
|
+
import { definePaymentProvider, loadPlanRefs, newReference, productSlugs, recordPurchase, recordRefund, REFERENCE, rememberCheckout, startedCheckout, str, upsertMembership } from "@tansy/payments/provider";
|
|
4
|
+
const keys = (settings) => settings;
|
|
5
|
+
const client = (settings) => new DodoPayments({ bearerToken: keys(settings).dodoApiKey, environment: settings.testMode ? "test_mode" : "live_mode" });
|
|
6
|
+
const record = (value) => (value && typeof value === "object" ? value : {});
|
|
7
|
+
/** Every Dodo event better-auth's plugin verified. Throwing makes Dodo send it again. */
|
|
8
|
+
async function onEvent(context, event, plans) {
|
|
9
|
+
const data = event.data;
|
|
10
|
+
if (event.type === "payment.succeeded") {
|
|
11
|
+
const metadata = record(data.metadata);
|
|
12
|
+
await recordPurchase(context, { provider: "dodo", sessionId: str(metadata.ref), paymentRef: str(data.payment_id) || null, metadata, amount: Number(data.total_amount), currency: str(data.currency) });
|
|
13
|
+
}
|
|
14
|
+
else if (event.type === "refund.succeeded") {
|
|
15
|
+
if (data.is_partial !== true)
|
|
16
|
+
await recordRefund(context, { paymentRef: str(data.payment_id) });
|
|
17
|
+
}
|
|
18
|
+
else if (event.type.startsWith("subscription.")) {
|
|
19
|
+
const metadata = record(data.metadata);
|
|
20
|
+
// Subscriptions Payments starts carry the user's id. Others are matched by the customer's email.
|
|
21
|
+
const email = str(record(data.customer).email);
|
|
22
|
+
const user = str(metadata.userId) || (email ? (await context.system.users.findByEmail(email))?.id ?? null : null);
|
|
23
|
+
await upsertMembership(context, {
|
|
24
|
+
provider: "dodo",
|
|
25
|
+
subscriptionId: str(data.subscription_id),
|
|
26
|
+
user,
|
|
27
|
+
productId: str(data.product_id),
|
|
28
|
+
status: str(data.status),
|
|
29
|
+
periodEnd: str(data.next_billing_date) || null,
|
|
30
|
+
at: new Date(event.timestamp).getTime(),
|
|
31
|
+
}, await plans());
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/** Dodo Payments, through better-auth's Dodo Payments plugin. Payments keeps its subscriptions as memberships. */
|
|
35
|
+
export const dodo = () => definePaymentProvider({
|
|
36
|
+
id: "dodo",
|
|
37
|
+
label: "Dodo Payments",
|
|
38
|
+
webhookPath: "/dodopayments/webhooks",
|
|
39
|
+
// Its settings in other interface languages. English is each setting's own label and description.
|
|
40
|
+
messages: (siteUrl) => ({
|
|
41
|
+
ar: { settings: {
|
|
42
|
+
dodoApiKey: { label: "مفتاح API لـ Dodo Payments", description: "من Dodo Payments ← Developer ← API keys. فعّل وضع الاختبار لمفتاح اختبار." },
|
|
43
|
+
dodoWebhookSecret: { label: "سرّ webhook في Dodo Payments", description: `وجّه webhook من Dodo Payments إلى ${siteUrl}/api/auth/dodopayments/webhooks، ثم الصق مفتاح التوقيع.` },
|
|
44
|
+
dodoProductId: { label: "منتج Dodo Payments للمشتريات لمرة واحدة", description: "منتج لمرة واحدة مع تفعيل «ادفع ما تريد»، بعملة أسعارك. يُباع كل سجل تحته بسعره الخاص." },
|
|
45
|
+
} },
|
|
46
|
+
}),
|
|
47
|
+
settings: (siteUrl) => [
|
|
48
|
+
{ name: "dodoApiKey", type: "secret", env: "DODO_PAYMENTS_API_KEY", label: "Dodo Payments API key", description: "From Dodo Payments → Developer → API keys. Turn on test mode for a test key." },
|
|
49
|
+
{ name: "dodoWebhookSecret", type: "secret", env: "DODO_PAYMENTS_WEBHOOK_SECRET", label: "Dodo Payments webhook secret", description: `Point a Dodo Payments webhook at ${siteUrl}/api/auth/dodopayments/webhooks, then paste its signing key.` },
|
|
50
|
+
{ name: "dodoProductId", type: "text", default: "", maxLength: 100, label: "Dodo Payments product for one-time purchases", description: "A one-time product with pay what you want turned on, in the currency of your prices. Each record sells under it at its own price." },
|
|
51
|
+
],
|
|
52
|
+
// Dodo's checkouts take any products, prices and metadata from the caller.
|
|
53
|
+
serverOnlyPaths: ["/dodopayments/checkout", "/dodopayments/checkout-session"],
|
|
54
|
+
planIdProblem: (value) => (value.startsWith("pdt_") ? null : "Use a Dodo Payments product id, which starts with pdt_."),
|
|
55
|
+
ready: (settings) => Boolean(keys(settings).dodoApiKey && keys(settings).dodoWebhookSecret),
|
|
56
|
+
authPlugins(settings, context) {
|
|
57
|
+
const plans = () => loadPlanRefs(context);
|
|
58
|
+
return [dodopayments({
|
|
59
|
+
client: client(settings),
|
|
60
|
+
createCustomerOnSignUp: settings.createCustomerOnSignUp,
|
|
61
|
+
use: [
|
|
62
|
+
checkout({ products: async () => productSlugs(await plans()), authenticatedUsersOnly: true }),
|
|
63
|
+
portal(),
|
|
64
|
+
webhooks({ webhookKey: keys(settings).dodoWebhookSecret, onPayload: async (payload) => onEvent(context, payload, plans) }),
|
|
65
|
+
],
|
|
66
|
+
})];
|
|
67
|
+
},
|
|
68
|
+
async currencyProblem(settings, currency) {
|
|
69
|
+
if (!keys(settings).dodoProductId)
|
|
70
|
+
return "Payments → Settings needs the Dodo Payments product that one-time purchases are sold under.";
|
|
71
|
+
// Dodo charges in the product's currency, so a price in another would be charged as the wrong amount.
|
|
72
|
+
const product = await client(settings).products.retrieve(keys(settings).dodoProductId).catch(() => null);
|
|
73
|
+
const productCurrency = str(record(product?.price).currency).toLowerCase();
|
|
74
|
+
if (!product)
|
|
75
|
+
return "The Dodo Payments product under Payments → Settings can't be found.";
|
|
76
|
+
return productCurrency && productCurrency !== currency ? `This is priced in ${currency.toUpperCase()}, and the Dodo Payments product sells in ${productCurrency.toUpperCase()}.` : null;
|
|
77
|
+
},
|
|
78
|
+
async checkout({ context, settings, buyer, collection, recordId, amount, successUrl, cancelUrl }) {
|
|
79
|
+
const ref = newReference();
|
|
80
|
+
const session = await client(settings).checkoutSessions.create({
|
|
81
|
+
// A pay-what-you-want product, at the record's own price.
|
|
82
|
+
product_cart: [{ product_id: keys(settings).dodoProductId, quantity: 1, amount }],
|
|
83
|
+
customer: { email: buyer.email, name: buyer.name || buyer.email },
|
|
84
|
+
metadata: { tansy: "purchase", collection, recordId: String(recordId), userId: buyer.id, ref },
|
|
85
|
+
return_url: `${successUrl}&session_id=${ref}`,
|
|
86
|
+
cancel_url: cancelUrl,
|
|
87
|
+
});
|
|
88
|
+
await rememberCheckout(context, ref, { id: session.session_id, userId: buyer.id, collection, recordId, at: Date.now() });
|
|
89
|
+
return session.checkout_url ?? cancelUrl;
|
|
90
|
+
},
|
|
91
|
+
isReference: (value) => REFERENCE.test(value),
|
|
92
|
+
async confirm(context, settings, reference, buyer) {
|
|
93
|
+
const started = await startedCheckout(context, reference);
|
|
94
|
+
if (!started || started.userId !== buyer.id)
|
|
95
|
+
return null;
|
|
96
|
+
const dodo = client(settings);
|
|
97
|
+
const session = await dodo.checkoutSessions.retrieve(started.id).catch(() => null);
|
|
98
|
+
if (!session)
|
|
99
|
+
return null;
|
|
100
|
+
const payment = session.payment_id && session.payment_status === "succeeded" ? await dodo.payments.retrieve(session.payment_id).catch(() => null) : null;
|
|
101
|
+
if (payment)
|
|
102
|
+
await recordPurchase(context, { provider: "dodo", sessionId: reference, paymentRef: payment.payment_id, metadata: payment.metadata, amount: payment.total_amount, currency: payment.currency });
|
|
103
|
+
context.auditTarget("record", `${started.collection}/${started.recordId}`, { paid: Boolean(payment) });
|
|
104
|
+
return { paid: Boolean(payment) };
|
|
105
|
+
},
|
|
106
|
+
// better-auth's Dodo checkout sends every buyer to one fixed address, so Payments starts the session itself.
|
|
107
|
+
async subscribe({ settings, buyer, plan, annual, successUrl, cancelUrl }) {
|
|
108
|
+
const session = await client(settings).checkoutSessions.create({
|
|
109
|
+
product_cart: [{ product_id: annual && plan.annualProductId ? plan.annualProductId : plan.productId, quantity: 1 }],
|
|
110
|
+
customer: { email: buyer.email, name: buyer.name || buyer.email },
|
|
111
|
+
metadata: { tansy: "subscription", userId: buyer.id },
|
|
112
|
+
return_url: successUrl,
|
|
113
|
+
cancel_url: cancelUrl,
|
|
114
|
+
});
|
|
115
|
+
return session.checkout_url ?? cancelUrl;
|
|
116
|
+
},
|
|
117
|
+
async billing(context) {
|
|
118
|
+
const { url } = await context.auth.call("/dodopayments/customer/portal");
|
|
119
|
+
return url;
|
|
120
|
+
},
|
|
121
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tansy/payments-dodo",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Dodo Payments for @tansy/payments, through better-auth's Dodo Payments plugin.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"tansy",
|
|
7
|
+
"tansy-payments-provider"
|
|
8
|
+
],
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"author": "Mohamed Salah <mohamedsalahsebai@gmail.com>",
|
|
11
|
+
"homepage": "https://github.com/mhmdsalahsebai/tansy",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/mhmdsalahsebai/tansy.git",
|
|
15
|
+
"directory": "plugins/payments-dodo"
|
|
16
|
+
},
|
|
17
|
+
"bugs": "https://github.com/mhmdsalahsebai/tansy/issues",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./index.d.ts",
|
|
21
|
+
"default": "./index.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"@tansy/payments": "^0.1.0",
|
|
26
|
+
"tansy": "^0.1.0"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@dodopayments/better-auth": "^1.6.7",
|
|
30
|
+
"dodopayments": "^2.62.0"
|
|
31
|
+
}
|
|
32
|
+
}
|