@tansy/payments 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 +23 -0
- package/index.js +504 -0
- package/logic.d.ts +63 -0
- package/logic.js +122 -0
- package/messages.d.ts +74 -0
- package/messages.js +263 -0
- package/package.json +45 -4
- package/payments.css +8 -0
- package/provider.d.ts +89 -0
- package/provider.js +4 -0
- package/react.d.ts +20 -0
- package/react.js +63 -0
- package/render.d.ts +3 -0
- package/render.js +25 -0
- package/shared.d.ts +159 -0
- package/shared.js +289 -0
- package/site-messages.d.ts +3 -0
- package/site-messages.js +29 -0
package/logic.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/** Pure helpers, kept apart from the plugin so they're easy to test. */
|
|
2
|
+
/** User ids are safe to write inside a filter's quotes. */
|
|
3
|
+
const USER_ID = /^[A-Za-z0-9_-]{1,64}$/;
|
|
4
|
+
export const isUserId = (value) => typeof value === "string" && USER_ID.test(value);
|
|
5
|
+
const SLUG = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
6
|
+
export const isSlug = (value) => typeof value === "string" && value.length <= 63 && SLUG.test(value);
|
|
7
|
+
// Stripe's lists: https://docs.stripe.com/currencies#zero-decimal and #three-decimal
|
|
8
|
+
const ZERO_DECIMAL = new Set(["bif", "clp", "djf", "gnf", "jpy", "kmf", "krw", "mga", "pyg", "rwf", "ugx", "vnd", "vuv", "xaf", "xof", "xpf"]);
|
|
9
|
+
const THREE_DECIMAL = new Set(["bhd", "jod", "kwd", "omr", "tnd"]);
|
|
10
|
+
const decimalsOf = (currency) => (ZERO_DECIMAL.has(currency) ? 0 : THREE_DECIMAL.has(currency) ? 3 : 2);
|
|
11
|
+
/** A price as owners type it, such as 19.99, in the smallest unit Stripe charges in, such as 1999 cents. Null when it can't be charged. */
|
|
12
|
+
export function toMinorUnits(amount, currency) {
|
|
13
|
+
if (typeof amount !== "number" || !Number.isFinite(amount) || amount <= 0)
|
|
14
|
+
return null;
|
|
15
|
+
const minor = Math.round(amount * 10 ** decimalsOf(currency.toLowerCase()));
|
|
16
|
+
return minor > 0 && Number.isSafeInteger(minor) ? minor : null;
|
|
17
|
+
}
|
|
18
|
+
/** Back from Stripe's smallest unit to the amount owners see. */
|
|
19
|
+
export const fromMinorUnits = (minor, currency) => minor / 10 ** decimalsOf(currency.toLowerCase());
|
|
20
|
+
export const CURRENCY = /^[a-z]{3}$/;
|
|
21
|
+
/** A path on this site to come back to, never somewhere else. */
|
|
22
|
+
export function sitePath(value) {
|
|
23
|
+
if (typeof value !== "string" || !value.startsWith("/") || value.startsWith("//") || value.includes("\\") || /[\u0000-\u001f]/.test(value))
|
|
24
|
+
return "/";
|
|
25
|
+
return value.slice(0, 500);
|
|
26
|
+
}
|
|
27
|
+
/** The site address with a path and one more query parameter. */
|
|
28
|
+
export function returnUrl(siteUrl, path, key, value) {
|
|
29
|
+
const url = new URL(sitePath(path), siteUrl);
|
|
30
|
+
url.searchParams.set(key, value);
|
|
31
|
+
return url.toString();
|
|
32
|
+
}
|
|
33
|
+
/** What a record is called on the checkout page. */
|
|
34
|
+
export function itemName(record, fallback) {
|
|
35
|
+
for (const key of ["title", "name", "label"]) {
|
|
36
|
+
const value = record[key];
|
|
37
|
+
if (typeof value === "string" && value.trim())
|
|
38
|
+
return value.trim().slice(0, 250);
|
|
39
|
+
}
|
|
40
|
+
return fallback;
|
|
41
|
+
}
|
|
42
|
+
/** Subscriptions that give access now. */
|
|
43
|
+
export const ACTIVE_STATES = new Set(["active", "trialing"]);
|
|
44
|
+
/**
|
|
45
|
+
* A provider's subscription status in the words Stripe uses, so `ACTIVE_STATES` and the
|
|
46
|
+
* dashboard read the same for every provider. Anything unknown counts as not active.
|
|
47
|
+
*/
|
|
48
|
+
const STATES = {
|
|
49
|
+
active: "active",
|
|
50
|
+
trialing: "trialing",
|
|
51
|
+
past_due: "past_due",
|
|
52
|
+
unpaid: "past_due",
|
|
53
|
+
on_hold: "past_due",
|
|
54
|
+
paused: "paused",
|
|
55
|
+
canceled: "canceled",
|
|
56
|
+
cancelled: "canceled",
|
|
57
|
+
expired: "expired",
|
|
58
|
+
failed: "expired",
|
|
59
|
+
incomplete_expired: "expired",
|
|
60
|
+
incomplete: "pending",
|
|
61
|
+
pending: "pending",
|
|
62
|
+
pending_payment: "pending",
|
|
63
|
+
draft: "pending",
|
|
64
|
+
// Creem: paid for the period, or cancelled from its end, so still on until then.
|
|
65
|
+
paid: "active",
|
|
66
|
+
scheduled_cancel: "active",
|
|
67
|
+
};
|
|
68
|
+
export const membershipState = (status) => (typeof status === "string" && STATES[status.toLowerCase()]) || "expired";
|
|
69
|
+
/** The reference in a return address that Payments made itself, for providers whose own ids can't go there. */
|
|
70
|
+
export const REFERENCE = /^ref_[a-f0-9]{32}$/;
|
|
71
|
+
/** Plan records that name a product, for providers that sell plans as products. */
|
|
72
|
+
export function toPlanRef(plan) {
|
|
73
|
+
const name = typeof plan.name === "string" ? plan.name.trim().toLowerCase() : "";
|
|
74
|
+
const productId = typeof plan.priceId === "string" ? plan.priceId.trim() : "";
|
|
75
|
+
if (!name || !productId)
|
|
76
|
+
return null;
|
|
77
|
+
const annual = typeof plan.annualPriceId === "string" && plan.annualPriceId.trim() ? plan.annualPriceId.trim() : undefined;
|
|
78
|
+
return { name, productId, ...(annual ? { annualProductId: annual } : {}) };
|
|
79
|
+
}
|
|
80
|
+
/** The slugs better-auth's Polar and Dodo plugins pick products by: the plan's name, and name-annual for the yearly one. */
|
|
81
|
+
export const productSlugs = (plans) => plans.flatMap((plan) => [
|
|
82
|
+
{ productId: plan.productId, slug: plan.name },
|
|
83
|
+
...(plan.annualProductId ? [{ productId: plan.annualProductId, slug: `${plan.name}-annual` }] : []),
|
|
84
|
+
]);
|
|
85
|
+
/** The plan a provider's product or plan id belongs to, monthly or yearly. */
|
|
86
|
+
export const planNamed = (plans, productId) => typeof productId === "string" ? plans.find((plan) => plan.productId === productId || plan.annualProductId === productId)?.name ?? null : null;
|
|
87
|
+
/** At most this many collections in one access, and this many access in one plan. */
|
|
88
|
+
export const MAX_ACCESS_COLLECTIONS = 50;
|
|
89
|
+
export const MAX_PLAN_ACCESS = 50;
|
|
90
|
+
/** What's wrong with an access's `collections`, or null. A list of collection slugs. */
|
|
91
|
+
export function accessCollectionsProblem(value) {
|
|
92
|
+
if (value === null || value === undefined)
|
|
93
|
+
return null;
|
|
94
|
+
if (!Array.isArray(value))
|
|
95
|
+
return 'List collection slugs, such as ["lms-courses"].';
|
|
96
|
+
if (value.length > MAX_ACCESS_COLLECTIONS)
|
|
97
|
+
return `List at most ${MAX_ACCESS_COLLECTIONS} collections.`;
|
|
98
|
+
const wrong = value.find((slug) => !isSlug(slug));
|
|
99
|
+
if (wrong !== undefined)
|
|
100
|
+
return `"${String(wrong)}" isn't a collection slug. Use lowercase words joined by hyphens, such as lms-courses.`;
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
/** The record ids in a reference field's value, which holds one id or a list. */
|
|
104
|
+
export function referenceIds(value) {
|
|
105
|
+
const list = Array.isArray(value) ? value : [value];
|
|
106
|
+
return list.filter((id) => typeof id === "number" && Number.isSafeInteger(id) && id > 0);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* What a person on these plans may open: the ids of every access their plans include, then
|
|
110
|
+
* the slugs of the collections those open whole. Sorted, so the same plans always give the
|
|
111
|
+
* same list. Rules compare record fields with it: ids match "Included in" references, and
|
|
112
|
+
* slugs match a rule's `@request.auth.payments_access ?= "lms-courses"`.
|
|
113
|
+
*/
|
|
114
|
+
export function accessOf(active, plans, grants) {
|
|
115
|
+
const names = new Set(active);
|
|
116
|
+
const ids = new Set(plans.filter((plan) => names.has(plan.name)).flatMap((plan) => plan.includes));
|
|
117
|
+
const granted = grants.filter((grant) => ids.has(grant.id));
|
|
118
|
+
// An access that was deleted grants nothing, even if a plan still names it.
|
|
119
|
+
const kept = [...new Set(granted.map((grant) => grant.id))].sort((a, b) => a - b);
|
|
120
|
+
const slugs = [...new Set(granted.flatMap((grant) => grant.collections.filter(isSlug)))].sort();
|
|
121
|
+
return [...kept, ...slugs];
|
|
122
|
+
}
|
package/messages.d.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { FieldLabels } from "tansy/plugin";
|
|
2
|
+
/** Payments' dashboard words, read as `t("ext.payments.<key>")`, and its collections' names. */
|
|
3
|
+
export declare const messages: {
|
|
4
|
+
en: {
|
|
5
|
+
categories: {
|
|
6
|
+
Payments: string;
|
|
7
|
+
};
|
|
8
|
+
fieldTypes: {
|
|
9
|
+
payments_price: {
|
|
10
|
+
label: string;
|
|
11
|
+
description: string;
|
|
12
|
+
options: {
|
|
13
|
+
currency: string;
|
|
14
|
+
once: string;
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
settings: {
|
|
19
|
+
provider: {
|
|
20
|
+
label: string;
|
|
21
|
+
description: string;
|
|
22
|
+
};
|
|
23
|
+
currency: {
|
|
24
|
+
label: string;
|
|
25
|
+
description: string;
|
|
26
|
+
};
|
|
27
|
+
testMode: {
|
|
28
|
+
label: string;
|
|
29
|
+
description: string;
|
|
30
|
+
};
|
|
31
|
+
createCustomerOnSignUp: {
|
|
32
|
+
label: string;
|
|
33
|
+
description: string;
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
ar: {
|
|
38
|
+
categories: {
|
|
39
|
+
Payments: string;
|
|
40
|
+
};
|
|
41
|
+
fieldTypes: {
|
|
42
|
+
payments_price: {
|
|
43
|
+
label: string;
|
|
44
|
+
description: string;
|
|
45
|
+
options: {
|
|
46
|
+
currency: string;
|
|
47
|
+
once: string;
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
settings: {
|
|
52
|
+
provider: {
|
|
53
|
+
label: string;
|
|
54
|
+
description: string;
|
|
55
|
+
};
|
|
56
|
+
currency: {
|
|
57
|
+
label: string;
|
|
58
|
+
description: string;
|
|
59
|
+
};
|
|
60
|
+
testMode: {
|
|
61
|
+
label: string;
|
|
62
|
+
description: string;
|
|
63
|
+
};
|
|
64
|
+
createCustomerOnSignUp: {
|
|
65
|
+
label: string;
|
|
66
|
+
description: string;
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
/** The collections' names in other interface languages. Added by a migration, so names an owner set stay. */
|
|
72
|
+
export declare const COLLECTION_LABELS: Record<string, Record<string, string>>;
|
|
73
|
+
/** Fields' words in other interface languages, by collection. Added by a migration, so words an owner set stay. */
|
|
74
|
+
export declare const FIELD_LABELS: Record<string, Record<string, FieldLabels>>;
|
package/messages.js
ADDED
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
/** Payments' dashboard words, read as `t("ext.payments.<key>")`, and its collections' names. */
|
|
2
|
+
export const messages = {
|
|
3
|
+
en: {
|
|
4
|
+
categories: { Payments: "Payments" },
|
|
5
|
+
fieldTypes: {
|
|
6
|
+
payments_price: { label: "Price", description: "Makes a record something people can buy", options: { currency: "Currency", once: "Once per person" } },
|
|
7
|
+
},
|
|
8
|
+
settings: {
|
|
9
|
+
provider: { label: "Provider", description: "Who takes the money. Fill in that provider's settings below. Records of past purchases stay when you switch." },
|
|
10
|
+
currency: { label: "Currency", description: "For prices that don't name one. A three-letter code, such as \"usd\"." },
|
|
11
|
+
testMode: { label: "Test mode", description: "Use the provider's sandbox or test environment, for providers that pick it by setting rather than by key." },
|
|
12
|
+
createCustomerOnSignUp: { label: "Create a customer with the provider for every new account", description: "Otherwise one is made the first time someone pays, for providers that support it." },
|
|
13
|
+
},
|
|
14
|
+
},
|
|
15
|
+
ar: {
|
|
16
|
+
categories: { Payments: "المدفوعات" },
|
|
17
|
+
fieldTypes: {
|
|
18
|
+
payments_price: { label: "السعر", description: "يجعل السجل شيئًا يمكن شراؤه", options: { currency: "العملة", once: "مرة واحدة لكل شخص" } },
|
|
19
|
+
},
|
|
20
|
+
settings: {
|
|
21
|
+
provider: { label: "المزوّد", description: "الجهة التي تحصّل المال. املأ إعدادات ذلك المزوّد أدناه. تبقى سجلات المشتريات السابقة حين تغيّره." },
|
|
22
|
+
currency: { label: "العملة", description: "للأسعار التي لا تحدد عملتها. رمز من ثلاثة أحرف، مثل \"usd\"." },
|
|
23
|
+
testMode: { label: "وضع الاختبار", description: "استخدم بيئة الاختبار لدى المزوّد، للمزوّدين الذين يختارونها بإعداد لا بمفتاح." },
|
|
24
|
+
createCustomerOnSignUp: { label: "أنشئ عميلًا لدى المزوّد لكل حساب جديد", description: "وإلا يُنشأ عند أول دفعة، لدى المزوّدين الذين يدعمون ذلك." },
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
/** The collections' names in other interface languages. Added by a migration, so names an owner set stay. */
|
|
29
|
+
export const COLLECTION_LABELS = {
|
|
30
|
+
"payments-plans": { ar: "الخطط" },
|
|
31
|
+
"payments-purchases": { ar: "المشتريات" },
|
|
32
|
+
"payments-memberships": { ar: "العضويات" },
|
|
33
|
+
"payments-user-data": { ar: "بيانات العملاء" },
|
|
34
|
+
"payments-access": { ar: "الوصول" },
|
|
35
|
+
"payments-entitlements": { ar: "الاستحقاقات" },
|
|
36
|
+
};
|
|
37
|
+
/** Fields' words in other interface languages, by collection. Added by a migration, so words an owner set stay. */
|
|
38
|
+
export const FIELD_LABELS = {
|
|
39
|
+
"payments-plans": {
|
|
40
|
+
name: {
|
|
41
|
+
labels: {
|
|
42
|
+
ar: "الاسم"
|
|
43
|
+
},
|
|
44
|
+
descriptions: {
|
|
45
|
+
ar: "بأحرف صغيرة، مثل pro. تتحقق منه القواعد: @request.auth.payments_plan = \"pro\"."
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
priceId: {
|
|
49
|
+
labels: {
|
|
50
|
+
ar: "معرّف السعر"
|
|
51
|
+
},
|
|
52
|
+
descriptions: {
|
|
53
|
+
ar: "معرّف المزوّد: سعر Stripe (price_…)، أو منتج Polar، أو منتج Dodo Payments (pdt_…)، أو منتج Creem (prod_…)، أو خطة Commet."
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
annualPriceId: {
|
|
57
|
+
labels: {
|
|
58
|
+
ar: "معرّف السعر السنوي"
|
|
59
|
+
},
|
|
60
|
+
descriptions: {
|
|
61
|
+
ar: "سعر أو منتج سنوي اختياري. أما Commet فتفوتر الخطة نفسها سنويًا."
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
trialDays: {
|
|
65
|
+
labels: {
|
|
66
|
+
ar: "أيام التجربة"
|
|
67
|
+
},
|
|
68
|
+
descriptions: {
|
|
69
|
+
ar: "أيام مجانية قبل أول دفعة، مع Stripe. يضبط المزوّدون الآخرون التجربة على المنتج."
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
limits: {
|
|
73
|
+
labels: {
|
|
74
|
+
ar: "الحدود"
|
|
75
|
+
},
|
|
76
|
+
descriptions: {
|
|
77
|
+
ar: "أرقام اختيارية يقرؤها موقعك، مثل {\"projects\": 5}."
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
group: {
|
|
81
|
+
labels: {
|
|
82
|
+
ar: "المجموعة"
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
includes: {
|
|
86
|
+
labels: {
|
|
87
|
+
ar: "يشمل"
|
|
88
|
+
},
|
|
89
|
+
descriptions: {
|
|
90
|
+
ar: "ما يمكن لمشتركي هذه الخطة فتحه."
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
title: {
|
|
94
|
+
labels: { ar: "العنوان" },
|
|
95
|
+
descriptions: { ar: "كما تظهر على صفحة الأسعار، مثل «كل الدورات». يُستخدم الاسم إن تُرك فارغًا." }
|
|
96
|
+
},
|
|
97
|
+
description: {
|
|
98
|
+
labels: { ar: "الوصف" },
|
|
99
|
+
descriptions: { ar: "سطر أو سطران على صفحة الأسعار." }
|
|
100
|
+
},
|
|
101
|
+
amount: {
|
|
102
|
+
labels: { ar: "السعر الشهري المعروض" },
|
|
103
|
+
descriptions: { ar: "ما تعرضه صفحة الأسعار شهريًا. يأخذ المزوّد السعر المحدد عنده، فاجعلهما متطابقين." }
|
|
104
|
+
},
|
|
105
|
+
annualAmount: {
|
|
106
|
+
labels: { ar: "السعر السنوي المعروض" },
|
|
107
|
+
descriptions: { ar: "اختياري، لخطة لها سعر سنوي." }
|
|
108
|
+
},
|
|
109
|
+
currency: {
|
|
110
|
+
labels: { ar: "العملة" },
|
|
111
|
+
descriptions: { ar: "رمز من ثلاثة أحرف، مثل usd. تُملأ من إعدادات المدفوعات إن تُركت فارغة." }
|
|
112
|
+
},
|
|
113
|
+
featured: {
|
|
114
|
+
labels: { ar: "مميّزة" },
|
|
115
|
+
descriptions: { ar: "تُبرزها صفحة الأسعار." }
|
|
116
|
+
}
|
|
117
|
+
},
|
|
118
|
+
"payments-purchases": {
|
|
119
|
+
buyer: {
|
|
120
|
+
labels: {
|
|
121
|
+
ar: "المشتري"
|
|
122
|
+
}
|
|
123
|
+
},
|
|
124
|
+
collection: {
|
|
125
|
+
labels: {
|
|
126
|
+
ar: "المجموعة"
|
|
127
|
+
}
|
|
128
|
+
},
|
|
129
|
+
recordId: {
|
|
130
|
+
labels: {
|
|
131
|
+
ar: "معرّف السجل"
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
amount: {
|
|
135
|
+
labels: {
|
|
136
|
+
ar: "المبلغ"
|
|
137
|
+
}
|
|
138
|
+
},
|
|
139
|
+
currency: {
|
|
140
|
+
labels: {
|
|
141
|
+
ar: "العملة"
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
sessionId: {
|
|
145
|
+
labels: {
|
|
146
|
+
ar: "معرّف الجلسة"
|
|
147
|
+
}
|
|
148
|
+
},
|
|
149
|
+
paymentIntent: {
|
|
150
|
+
labels: {
|
|
151
|
+
ar: "نية الدفع"
|
|
152
|
+
}
|
|
153
|
+
},
|
|
154
|
+
state: {
|
|
155
|
+
labels: {
|
|
156
|
+
ar: "الحالة"
|
|
157
|
+
},
|
|
158
|
+
descriptions: {
|
|
159
|
+
ar: "paid أو refunded."
|
|
160
|
+
}
|
|
161
|
+
},
|
|
162
|
+
provider: {
|
|
163
|
+
labels: {
|
|
164
|
+
ar: "المزوّد"
|
|
165
|
+
},
|
|
166
|
+
descriptions: {
|
|
167
|
+
ar: "من تلقّى الدفعة. فارغ للمشتريات من قبل أن تدعم المدفوعات أكثر من Stripe."
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
},
|
|
171
|
+
"payments-memberships": {
|
|
172
|
+
provider: {
|
|
173
|
+
labels: {
|
|
174
|
+
ar: "المزوّد"
|
|
175
|
+
}
|
|
176
|
+
},
|
|
177
|
+
subscriptionId: {
|
|
178
|
+
labels: {
|
|
179
|
+
ar: "معرّف الاشتراك"
|
|
180
|
+
}
|
|
181
|
+
},
|
|
182
|
+
user: {
|
|
183
|
+
labels: {
|
|
184
|
+
ar: "المستخدم"
|
|
185
|
+
}
|
|
186
|
+
},
|
|
187
|
+
plan: {
|
|
188
|
+
labels: {
|
|
189
|
+
ar: "الخطة"
|
|
190
|
+
},
|
|
191
|
+
descriptions: {
|
|
192
|
+
ar: "اسم الخطة من الخطط، مطابقًا بمعرّف المنتج أو الخطة لدى المزوّد."
|
|
193
|
+
}
|
|
194
|
+
},
|
|
195
|
+
state: {
|
|
196
|
+
labels: {
|
|
197
|
+
ar: "الحالة"
|
|
198
|
+
},
|
|
199
|
+
descriptions: {
|
|
200
|
+
ar: "active أو trialing أو past_due أو paused أو canceled أو expired أو pending."
|
|
201
|
+
}
|
|
202
|
+
},
|
|
203
|
+
periodEnd: {
|
|
204
|
+
labels: {
|
|
205
|
+
ar: "نهاية الفترة"
|
|
206
|
+
}
|
|
207
|
+
},
|
|
208
|
+
eventAt: {
|
|
209
|
+
labels: {
|
|
210
|
+
ar: "وقت الحدث"
|
|
211
|
+
},
|
|
212
|
+
descriptions: {
|
|
213
|
+
ar: "متى وقع آخر حدث من المزوّد بشأنها، كي لا تكتب الأحداث الأقدم فوقه."
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
},
|
|
217
|
+
"payments-access": {
|
|
218
|
+
name: {
|
|
219
|
+
labels: {
|
|
220
|
+
ar: "الاسم"
|
|
221
|
+
},
|
|
222
|
+
descriptions: {
|
|
223
|
+
ar: "مثل كل الدورات أو المقالات المميزة."
|
|
224
|
+
}
|
|
225
|
+
},
|
|
226
|
+
description: {
|
|
227
|
+
labels: {
|
|
228
|
+
ar: "الوصف"
|
|
229
|
+
}
|
|
230
|
+
},
|
|
231
|
+
collections: {
|
|
232
|
+
labels: {
|
|
233
|
+
ar: "المجموعات"
|
|
234
|
+
},
|
|
235
|
+
descriptions: {
|
|
236
|
+
ar: "مجموعات يفتحها كاملة، بمعرّفها، مثل [\"lms-courses\"]. تتحقق منها القواعد: @request.auth.payments_access ?= \"lms-courses\"."
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
},
|
|
240
|
+
"payments-entitlements": {
|
|
241
|
+
user: {
|
|
242
|
+
labels: {
|
|
243
|
+
ar: "المستخدم"
|
|
244
|
+
}
|
|
245
|
+
},
|
|
246
|
+
plans: {
|
|
247
|
+
labels: {
|
|
248
|
+
ar: "الخطط"
|
|
249
|
+
},
|
|
250
|
+
descriptions: {
|
|
251
|
+
ar: "أسماء الخطط التي عليها اشتراكاتهم النشطة."
|
|
252
|
+
}
|
|
253
|
+
},
|
|
254
|
+
access: {
|
|
255
|
+
labels: {
|
|
256
|
+
ar: "الوصول"
|
|
257
|
+
},
|
|
258
|
+
descriptions: {
|
|
259
|
+
ar: "معرّفات الوصول الذي تشمله تلك الخطط، ثم المجموعات التي تفتحها كاملة."
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tansy/payments",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Sell anything on tansy: subscriptions and one-time purchases, through better-auth's payment plugins. Add a provider package, such as @tansy/payments-stripe.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"tansy",
|
|
7
|
+
"tansy-plugin",
|
|
8
|
+
"cms",
|
|
9
|
+
"nextjs"
|
|
10
|
+
],
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"author": "Mohamed Salah <mohamedsalahsebai@gmail.com>",
|
|
13
|
+
"homepage": "https://github.com/mhmdsalahsebai/tansy",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/mhmdsalahsebai/tansy.git",
|
|
17
|
+
"directory": "plugins/payments"
|
|
18
|
+
},
|
|
19
|
+
"bugs": "https://github.com/mhmdsalahsebai/tansy/issues",
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"types": "./index.d.ts",
|
|
23
|
+
"default": "./index.js"
|
|
24
|
+
},
|
|
25
|
+
"./react": {
|
|
26
|
+
"types": "./react.d.ts",
|
|
27
|
+
"default": "./react.js"
|
|
28
|
+
},
|
|
29
|
+
"./render": {
|
|
30
|
+
"types": "./render.d.ts",
|
|
31
|
+
"default": "./render.js"
|
|
32
|
+
},
|
|
33
|
+
"./provider": {
|
|
34
|
+
"types": "./provider.d.ts",
|
|
35
|
+
"default": "./provider.js"
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"better-auth": "^1.7.18",
|
|
40
|
+
"next": "^16.4.6",
|
|
41
|
+
"react": "^19.3.0",
|
|
42
|
+
"tansy": "^0.1.0"
|
|
43
|
+
},
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"zod": "^4.6.7"
|
|
46
|
+
}
|
|
47
|
+
}
|
package/payments.css
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/* The payments plugin's buttons. Colours come from the active theme's --site-* tokens, with plain fallbacks. */
|
|
2
|
+
.payments-cta { display: grid; gap: 0.5rem; justify-items: start; margin: 1.5rem 0; }
|
|
3
|
+
.payments-button {
|
|
4
|
+
display: inline-block; font: inherit; padding: 0.625rem 1.125rem; border-radius: 0.375rem; border: 0; text-decoration: none; cursor: pointer;
|
|
5
|
+
background: var(--site-color-accent, #222); color: var(--site-color-on-accent, #fff);
|
|
6
|
+
}
|
|
7
|
+
.payments-button:disabled { opacity: 0.6; cursor: default; }
|
|
8
|
+
.payments-error { color: #c53030; margin: 0; }
|
package/provider.d.ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import type { BetterAuthPlugin } from "better-auth";
|
|
2
|
+
import type { ExtensionContext, ExtensionRouteContext, Messages, PluginSettingField } from "tansy/plugin";
|
|
3
|
+
import type { PaymentsSettings, SubscriptionRecords } from "./shared";
|
|
4
|
+
/**
|
|
5
|
+
* What a payment provider adapter implements, such as @tansy/payments-stripe. Payments
|
|
6
|
+
* keeps everything providers share: plans, purchases, memberships, the price field, blocks,
|
|
7
|
+
* routes and the plan user field. An adapter is the provider's better-auth plugin, plus
|
|
8
|
+
* its SDK for what that plugin leaves out, such as selling a record at its stored price.
|
|
9
|
+
* See docs/plugins/payments.md.
|
|
10
|
+
*/
|
|
11
|
+
export interface Buyer {
|
|
12
|
+
id: string;
|
|
13
|
+
email: string;
|
|
14
|
+
name: string;
|
|
15
|
+
}
|
|
16
|
+
export interface CheckoutRequest {
|
|
17
|
+
context: ExtensionRouteContext;
|
|
18
|
+
settings: PaymentsSettings;
|
|
19
|
+
buyer: Buyer;
|
|
20
|
+
collection: string;
|
|
21
|
+
recordId: number;
|
|
22
|
+
/** What the checkout page calls it. */
|
|
23
|
+
name: string;
|
|
24
|
+
/** In the currency's smallest unit. */
|
|
25
|
+
amount: number;
|
|
26
|
+
currency: string;
|
|
27
|
+
/** Where the buyer lands after paying. It already has a query string: add `&session_id=…`, which /confirm takes. */
|
|
28
|
+
successUrl: string;
|
|
29
|
+
cancelUrl: string;
|
|
30
|
+
}
|
|
31
|
+
export interface SubscribeRequest {
|
|
32
|
+
context: ExtensionRouteContext;
|
|
33
|
+
settings: PaymentsSettings;
|
|
34
|
+
buyer: Buyer;
|
|
35
|
+
plan: {
|
|
36
|
+
name: string;
|
|
37
|
+
productId: string;
|
|
38
|
+
annualProductId?: string;
|
|
39
|
+
};
|
|
40
|
+
annual: boolean;
|
|
41
|
+
successUrl: string;
|
|
42
|
+
cancelUrl: string;
|
|
43
|
+
}
|
|
44
|
+
export interface PaymentProvider {
|
|
45
|
+
/** Lowercase letters and digits, such as "stripe". Every setting's name starts with it. */
|
|
46
|
+
id: string;
|
|
47
|
+
label: string;
|
|
48
|
+
/** Where its better-auth plugin takes the provider's webhooks, under /api/auth. */
|
|
49
|
+
webhookPath: string;
|
|
50
|
+
/** Its keys and options on Payments → Settings. `siteUrl` is for telling owners the webhook address. */
|
|
51
|
+
settings(siteUrl: string): PluginSettingField[];
|
|
52
|
+
/**
|
|
53
|
+
* Its settings' words in other interface languages, by language code, as
|
|
54
|
+
* `{ ar: { settings: { <name>: { label, description } } } }`, with the same `siteUrl`.
|
|
55
|
+
* Payments adds them to its own `messages`, and the setting's `label` and `description`
|
|
56
|
+
* cover any language missing.
|
|
57
|
+
*/
|
|
58
|
+
messages?(siteUrl: string): Record<string, Messages>;
|
|
59
|
+
/** Its better-auth plugin's endpoints that trust what the caller sends. Payments closes them to HTTP. */
|
|
60
|
+
serverOnlyPaths?: string[];
|
|
61
|
+
/** Where its better-auth plugin keeps subscriptions itself, if it does. Otherwise the adapter keeps memberships with `upsertMembership`. */
|
|
62
|
+
subscriptionRecords?: SubscriptionRecords;
|
|
63
|
+
/** Bills a plan yearly without a yearly product, so plans need no annual id. */
|
|
64
|
+
yearlyFromSamePlan?: boolean;
|
|
65
|
+
/** What's wrong with a plan's product or price id, or null. */
|
|
66
|
+
planIdProblem(value: string): string | null;
|
|
67
|
+
/** Whether the keys it needs are set. */
|
|
68
|
+
ready(settings: PaymentsSettings): boolean;
|
|
69
|
+
/** Its better-auth plugin, built from the settings with Payments' context, for webhooks and customers. */
|
|
70
|
+
authPlugins(settings: PaymentsSettings, context: ExtensionContext, siteUrl: string): BetterAuthPlugin[];
|
|
71
|
+
/** Why records priced in this currency can't be sold, or null. */
|
|
72
|
+
currencyProblem?(settings: PaymentsSettings, currency: string): Promise<string | null>;
|
|
73
|
+
/** Starts a one-time checkout for one record. Returns the checkout page's address. */
|
|
74
|
+
checkout(request: CheckoutRequest): Promise<string>;
|
|
75
|
+
/** Whether a return address's `session_id` could be this provider's. */
|
|
76
|
+
isReference(value: string): boolean;
|
|
77
|
+
/** Records the checkout if it's paid and the caller's own. Null when there's no such checkout for them. */
|
|
78
|
+
confirm(context: ExtensionRouteContext, settings: PaymentsSettings, reference: string, buyer: Buyer): Promise<{
|
|
79
|
+
paid: boolean;
|
|
80
|
+
} | null>;
|
|
81
|
+
subscribe(request: SubscribeRequest): Promise<string>;
|
|
82
|
+
/** The provider's billing page for the caller. */
|
|
83
|
+
billing(context: ExtensionRouteContext, settings: PaymentsSettings, buyer: Buyer, returnUrl: string): Promise<string>;
|
|
84
|
+
}
|
|
85
|
+
/** Types an adapter. */
|
|
86
|
+
export declare const definePaymentProvider: (provider: PaymentProvider) => PaymentProvider;
|
|
87
|
+
export type { PaymentsSettings, PaidCheckout, MembershipUpdate, StartedCheckout, SubscriptionRecords } from "./shared";
|
|
88
|
+
export { CUSTOMERS, PLANS, PURCHASES, loadPlanRefs, newReference, recordPurchase, recordRefund, rememberCheckout, startedCheckout, str, upsertMembership, } from "./shared";
|
|
89
|
+
export { REFERENCE, isUserId, membershipState, planNamed, productSlugs, toMinorUnits, fromMinorUnits, type PlanRecord, type PlanRef } from "./logic";
|
package/provider.js
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** Types an adapter. */
|
|
2
|
+
export const definePaymentProvider = (provider) => provider;
|
|
3
|
+
export { CUSTOMERS, PLANS, PURCHASES, loadPlanRefs, newReference, recordPurchase, recordRefund, rememberCheckout, startedCheckout, str, upsertMembership, } from "./shared";
|
|
4
|
+
export { REFERENCE, isUserId, membershipState, planNamed, productSlugs, toMinorUnits, fromMinorUnits } from "./logic";
|
package/react.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type SiteWords } from "tansy/react";
|
|
2
|
+
/** Buys one record of any collection with a Price field. */
|
|
3
|
+
export declare function BuyButton({ collection, id, label, words }: {
|
|
4
|
+
collection: string;
|
|
5
|
+
id: number;
|
|
6
|
+
label?: string;
|
|
7
|
+
words?: SiteWords;
|
|
8
|
+
}): import("react").JSX.Element;
|
|
9
|
+
/** Subscribes to a plan from Payments → Plans, or switches to it, with whichever provider the site uses. */
|
|
10
|
+
export declare function SubscribeButton({ plan, annual, label, words }: {
|
|
11
|
+
plan: string;
|
|
12
|
+
annual?: boolean;
|
|
13
|
+
label?: string;
|
|
14
|
+
words?: SiteWords;
|
|
15
|
+
}): import("react").JSX.Element;
|
|
16
|
+
/** The provider's billing page: change plan, update the card, see invoices, cancel. */
|
|
17
|
+
export declare function BillingButton({ label, words }: {
|
|
18
|
+
label?: string;
|
|
19
|
+
words?: SiteWords;
|
|
20
|
+
}): import("react").JSX.Element;
|