Spaces:
Running
Running
JOHN SILLVESTOR commited on
Update Node HTTP service
Browse files
server.js
CHANGED
|
@@ -55,6 +55,7 @@ const inflightDur = new Map();
|
|
| 55 |
const lvlCache = new Map();
|
| 56 |
const rateMap = new Map();
|
| 57 |
const ipStreams = new Map();
|
|
|
|
| 58 |
let fbKeys = { certs: null, exp: 0 };
|
| 59 |
let rr = 0;
|
| 60 |
|
|
@@ -119,6 +120,8 @@ function cors(req, res) {
|
|
| 119 |
res.setHeader("Access-Control-Expose-Headers", "Content-Range,Content-Length,Accept-Ranges,Content-Type");
|
| 120 |
res.setHeader("Access-Control-Max-Age", "86400");
|
| 121 |
res.setHeader("X-Content-Type-Options", "nosniff");
|
|
|
|
|
|
|
| 122 |
}
|
| 123 |
|
| 124 |
function json(res, code, body) {
|
|
@@ -221,6 +224,12 @@ function sweepMaps() {
|
|
| 221 |
if (!k.endsWith(":" + slot) && !k.endsWith(":" + (slot - 1))) rateMap.delete(k);
|
| 222 |
}
|
| 223 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
}
|
| 225 |
|
| 226 |
function withTime(p, ms, fallback) {
|
|
@@ -255,6 +264,41 @@ function limited(ip) {
|
|
| 255 |
return n > 800;
|
| 256 |
}
|
| 257 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
async function googleCerts() {
|
| 259 |
if (fbKeys.certs && Date.now() < fbKeys.exp) return fbKeys.certs;
|
| 260 |
const r = await fetch("https://www.googleapis.com/robot/v1/metadata/x509/securetoken@system.gserviceaccount.com", {
|
|
@@ -681,6 +725,7 @@ async function readBody(req, max) {
|
|
| 681 |
|
| 682 |
async function handleGenerate(req, res, url) {
|
| 683 |
if (!aesKey) return json(res, 503, { error: "misconfigured" });
|
|
|
|
| 684 |
const ip = clientIp(req);
|
| 685 |
if (limited(ip)) {
|
| 686 |
res.setHeader("Retry-After", "2");
|
|
@@ -713,10 +758,7 @@ async function handleGenerate(req, res, url) {
|
|
| 713 |
if (rec.hash && rec.hash !== hash) return json(res, 400, { error: "hash" });
|
| 714 |
const cachedDur = durCache.get(String(msgId));
|
| 715 |
if (cachedDur > 1) rec.duration = cachedDur;
|
| 716 |
-
if (!(rec.duration > 1)) {
|
| 717 |
-
const d = await withTime(fillDuration(bot, rec, msgId), 3500, 0);
|
| 718 |
-
if (d > 1) rec.duration = d;
|
| 719 |
-
}
|
| 720 |
const playMime = httpVideoType(rec.mime, rec.name);
|
| 721 |
const exp = Math.floor(Date.now() / 1000) + TOKEN_TTL;
|
| 722 |
const k = sealTicket({ m: msgId, h: hash, u: uid || "anon", l: lvl, b: bot.idx, e: exp, s: rec.size, n: playMime });
|
|
@@ -733,6 +775,11 @@ async function handleGenerate(req, res, url) {
|
|
| 733 |
}
|
| 734 |
|
| 735 |
async function handleStream(req, res, url, msgId) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 736 |
const k = url.searchParams.get("k") || "";
|
| 737 |
const ticket = openTicket(k);
|
| 738 |
if (!ticket || ticket.m !== msgId) {
|
|
@@ -781,8 +828,9 @@ async function handleStream(req, res, url, msgId) {
|
|
| 781 |
const headers = {
|
| 782 |
"Content-Type": httpVideoType(rec.mime, rec.name),
|
| 783 |
"Accept-Ranges": "bytes",
|
| 784 |
-
"Cache-Control": "private, max-age=
|
| 785 |
-
"Content-Disposition": "inline"
|
|
|
|
| 786 |
};
|
| 787 |
const partial = rng.start > 0 || rng.end < size - 1 || !!req.headers.range;
|
| 788 |
headers["Content-Length"] = String(rng.end - rng.start + 1);
|
|
@@ -825,6 +873,7 @@ async function handleStream(req, res, url, msgId) {
|
|
| 825 |
}
|
| 826 |
|
| 827 |
async function handleVipStatus(req, res) {
|
|
|
|
| 828 |
let lvl = "anonymous";
|
| 829 |
try {
|
| 830 |
const idp = await withTime(verifyIdToken(req.headers.authorization || ""), 1200, null);
|
|
@@ -841,45 +890,55 @@ async function handleVipStatus(req, res) {
|
|
| 841 |
async function handleVipStart(req, res) {
|
| 842 |
if (ROLE !== "indexer") return json(res, 409, { error: "use_s1" });
|
| 843 |
if (!STRIPE_KEY) return json(res, 503, { error: "stripe_not_configured" });
|
|
|
|
| 844 |
const idp = await withTime(verifyIdToken(req.headers.authorization || ""), 1200, null);
|
| 845 |
if (!idp) return json(res, 401, { error: "login" });
|
|
|
|
|
|
|
|
|
|
|
|
|
| 846 |
let body;
|
| 847 |
try {
|
| 848 |
body = JSON.parse((await readBody(req)).toString("utf8") || "{}");
|
| 849 |
} catch {
|
| 850 |
return json(res, 400, { error: "body" });
|
| 851 |
}
|
| 852 |
-
const
|
|
|
|
| 853 |
if (!plan) return json(res, 400, { error: "plan" });
|
|
|
|
| 854 |
const r = await fetch("https://api.stripe.com/v1/checkout/sessions", {
|
| 855 |
method: "POST",
|
| 856 |
headers: {
|
| 857 |
Authorization: "Bearer " + STRIPE_KEY,
|
| 858 |
-
"Content-Type": "application/x-www-form-urlencoded"
|
|
|
|
| 859 |
},
|
| 860 |
body: new URLSearchParams({
|
| 861 |
mode: "payment",
|
| 862 |
success_url: ORIGIN + "/?vip=success",
|
| 863 |
cancel_url: ORIGIN + "/?pay_error=cancel",
|
|
|
|
| 864 |
"line_items[0][price_data][currency]": "usd",
|
| 865 |
"line_items[0][price_data][product_data][name]": plan.label,
|
| 866 |
"line_items[0][price_data][unit_amount]": String(plan.amount),
|
| 867 |
"line_items[0][quantity]": "1",
|
| 868 |
"metadata[uid]": idp.sub,
|
| 869 |
-
"metadata[plan]":
|
| 870 |
"payment_intent_data[metadata][uid]": idp.sub,
|
| 871 |
-
"payment_intent_data[metadata][plan]":
|
| 872 |
}).toString(),
|
| 873 |
signal: AbortSignal.timeout(12000)
|
| 874 |
});
|
| 875 |
const d = await r.json();
|
| 876 |
-
if (!r.ok || !d.id) return json(res, 400, { error:
|
| 877 |
-
json(res, 200, { sid: d.id });
|
| 878 |
}
|
| 879 |
|
| 880 |
async function handleVipGo(req, res) {
|
| 881 |
if (ROLE !== "indexer") return json(res, 409, { error: "use_s1" });
|
| 882 |
if (!STRIPE_KEY) return json(res, 503, { error: "stripe_not_configured" });
|
|
|
|
| 883 |
const idp = await withTime(verifyIdToken(req.headers.authorization || ""), 1200, null);
|
| 884 |
if (!idp) return json(res, 401, { error: "login" });
|
| 885 |
let body;
|
|
@@ -888,25 +947,35 @@ async function handleVipGo(req, res) {
|
|
| 888 |
} catch {
|
| 889 |
return json(res, 400, { error: "body" });
|
| 890 |
}
|
| 891 |
-
if (!body.sid) return json(res, 400, { error: "session" });
|
| 892 |
const r = await fetch("https://api.stripe.com/v1/checkout/sessions/" + encodeURIComponent(body.sid), {
|
| 893 |
headers: { Authorization: "Bearer " + STRIPE_KEY },
|
| 894 |
signal: AbortSignal.timeout(12000)
|
| 895 |
});
|
| 896 |
const d = await r.json();
|
| 897 |
-
|
|
|
|
| 898 |
json(res, 200, { url: d.url });
|
| 899 |
}
|
| 900 |
|
| 901 |
async function grantVip(uid, planKey) {
|
| 902 |
const plan = PLANS[planKey] || PLANS.month;
|
| 903 |
const now = Math.floor(Date.now() / 1000);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 904 |
const sub = {
|
| 905 |
status: "active",
|
| 906 |
plan: planKey,
|
| 907 |
label: plan.label,
|
| 908 |
price: plan.price,
|
| 909 |
-
expiry:
|
| 910 |
updatedAt: now
|
| 911 |
};
|
| 912 |
await rtdb("users/" + uid + "/subscription", "PUT", sub);
|
|
@@ -924,10 +993,46 @@ function stripeOk(raw, sigHeader) {
|
|
| 924 |
const t = parts.t;
|
| 925 |
const v1 = parts.v1;
|
| 926 |
if (!t || !v1) return false;
|
|
|
|
|
|
|
| 927 |
const mac = crypto.createHmac("sha256", STRIPE_WH).update(t + "." + raw).digest("hex");
|
| 928 |
return timingSafeEq(mac, v1);
|
| 929 |
}
|
| 930 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 931 |
async function handleStripeWebhook(req, res) {
|
| 932 |
const raw = (await readBody(req)).toString("utf8");
|
| 933 |
if (!stripeOk(raw, req.headers["stripe-signature"])) {
|
|
@@ -939,13 +1044,9 @@ async function handleStripeWebhook(req, res) {
|
|
| 939 |
res.writeHead(400);
|
| 940 |
return res.end();
|
| 941 |
}
|
| 942 |
-
if (ev.type === "checkout.session.completed") {
|
| 943 |
const ses = ev.data && ev.data.object || {};
|
| 944 |
-
|
| 945 |
-
const plan = ses.metadata && ses.metadata.plan;
|
| 946 |
-
if (uid) {
|
| 947 |
-
try { await grantVip(uid, plan || "month"); } catch {}
|
| 948 |
-
}
|
| 949 |
}
|
| 950 |
json(res, 200, { ok: true });
|
| 951 |
}
|
|
|
|
| 55 |
const lvlCache = new Map();
|
| 56 |
const rateMap = new Map();
|
| 57 |
const ipStreams = new Map();
|
| 58 |
+
const paidOrders = new Map();
|
| 59 |
let fbKeys = { certs: null, exp: 0 };
|
| 60 |
let rr = 0;
|
| 61 |
|
|
|
|
| 120 |
res.setHeader("Access-Control-Expose-Headers", "Content-Range,Content-Length,Accept-Ranges,Content-Type");
|
| 121 |
res.setHeader("Access-Control-Max-Age", "86400");
|
| 122 |
res.setHeader("X-Content-Type-Options", "nosniff");
|
| 123 |
+
res.setHeader("Referrer-Policy", "strict-origin-when-cross-origin");
|
| 124 |
+
res.setHeader("X-Frame-Options", "DENY");
|
| 125 |
}
|
| 126 |
|
| 127 |
function json(res, code, body) {
|
|
|
|
| 224 |
if (!k.endsWith(":" + slot) && !k.endsWith(":" + (slot - 1))) rateMap.delete(k);
|
| 225 |
}
|
| 226 |
}
|
| 227 |
+
if (paidOrders.size > 4000) {
|
| 228 |
+
const cut = now - 7 * 24 * 3600 * 1000;
|
| 229 |
+
for (const [k, ts] of paidOrders) {
|
| 230 |
+
if (ts < cut) paidOrders.delete(k);
|
| 231 |
+
}
|
| 232 |
+
}
|
| 233 |
}
|
| 234 |
|
| 235 |
function withTime(p, ms, fallback) {
|
|
|
|
| 264 |
return n > 800;
|
| 265 |
}
|
| 266 |
|
| 267 |
+
function isBlogOrigin(o) {
|
| 268 |
+
o = String(o || "");
|
| 269 |
+
if (o === ORIGIN) return true;
|
| 270 |
+
return o.startsWith("https://") && (o.endsWith(".blogspot.com") || o.endsWith(".blogger.com"));
|
| 271 |
+
}
|
| 272 |
+
|
| 273 |
+
function fromBlog(req) {
|
| 274 |
+
const o = String(req.headers.origin || "");
|
| 275 |
+
if (o) return isBlogOrigin(o);
|
| 276 |
+
const r = String(req.headers.referer || "");
|
| 277 |
+
if (!r) return false;
|
| 278 |
+
try {
|
| 279 |
+
return isBlogOrigin(new URL(r).origin);
|
| 280 |
+
} catch {
|
| 281 |
+
return false;
|
| 282 |
+
}
|
| 283 |
+
}
|
| 284 |
+
|
| 285 |
+
function denyHot(req, res) {
|
| 286 |
+
if (fromBlog(req)) return false;
|
| 287 |
+
json(res, 403, { error: "forbidden" });
|
| 288 |
+
return true;
|
| 289 |
+
}
|
| 290 |
+
|
| 291 |
+
function payLimited(uid, ip) {
|
| 292 |
+
const slot = Math.floor(Date.now() / 60000);
|
| 293 |
+
const a = "pay:" + uid + ":" + slot;
|
| 294 |
+
const b = "payip:" + ip + ":" + slot;
|
| 295 |
+
const na = (rateMap.get(a) || 0) + 1;
|
| 296 |
+
const nb = (rateMap.get(b) || 0) + 1;
|
| 297 |
+
rateMap.set(a, na);
|
| 298 |
+
rateMap.set(b, nb);
|
| 299 |
+
return na > 8 || nb > 12;
|
| 300 |
+
}
|
| 301 |
+
|
| 302 |
async function googleCerts() {
|
| 303 |
if (fbKeys.certs && Date.now() < fbKeys.exp) return fbKeys.certs;
|
| 304 |
const r = await fetch("https://www.googleapis.com/robot/v1/metadata/x509/securetoken@system.gserviceaccount.com", {
|
|
|
|
| 725 |
|
| 726 |
async function handleGenerate(req, res, url) {
|
| 727 |
if (!aesKey) return json(res, 503, { error: "misconfigured" });
|
| 728 |
+
if (denyHot(req, res)) return;
|
| 729 |
const ip = clientIp(req);
|
| 730 |
if (limited(ip)) {
|
| 731 |
res.setHeader("Retry-After", "2");
|
|
|
|
| 758 |
if (rec.hash && rec.hash !== hash) return json(res, 400, { error: "hash" });
|
| 759 |
const cachedDur = durCache.get(String(msgId));
|
| 760 |
if (cachedDur > 1) rec.duration = cachedDur;
|
| 761 |
+
if (!(rec.duration > 1)) fillDuration(bot, rec, msgId).catch(function () {});
|
|
|
|
|
|
|
|
|
|
| 762 |
const playMime = httpVideoType(rec.mime, rec.name);
|
| 763 |
const exp = Math.floor(Date.now() / 1000) + TOKEN_TTL;
|
| 764 |
const k = sealTicket({ m: msgId, h: hash, u: uid || "anon", l: lvl, b: bot.idx, e: exp, s: rec.size, n: playMime });
|
|
|
|
| 775 |
}
|
| 776 |
|
| 777 |
async function handleStream(req, res, url, msgId) {
|
| 778 |
+
const o = String(req.headers.origin || "");
|
| 779 |
+
if (o && !isBlogOrigin(o)) {
|
| 780 |
+
res.writeHead(403);
|
| 781 |
+
return res.end();
|
| 782 |
+
}
|
| 783 |
const k = url.searchParams.get("k") || "";
|
| 784 |
const ticket = openTicket(k);
|
| 785 |
if (!ticket || ticket.m !== msgId) {
|
|
|
|
| 828 |
const headers = {
|
| 829 |
"Content-Type": httpVideoType(rec.mime, rec.name),
|
| 830 |
"Accept-Ranges": "bytes",
|
| 831 |
+
"Cache-Control": "private, max-age=30, no-transform",
|
| 832 |
+
"Content-Disposition": "inline",
|
| 833 |
+
"X-Content-Type-Options": "nosniff"
|
| 834 |
};
|
| 835 |
const partial = rng.start > 0 || rng.end < size - 1 || !!req.headers.range;
|
| 836 |
headers["Content-Length"] = String(rng.end - rng.start + 1);
|
|
|
|
| 873 |
}
|
| 874 |
|
| 875 |
async function handleVipStatus(req, res) {
|
| 876 |
+
if (denyHot(req, res)) return;
|
| 877 |
let lvl = "anonymous";
|
| 878 |
try {
|
| 879 |
const idp = await withTime(verifyIdToken(req.headers.authorization || ""), 1200, null);
|
|
|
|
| 890 |
async function handleVipStart(req, res) {
|
| 891 |
if (ROLE !== "indexer") return json(res, 409, { error: "use_s1" });
|
| 892 |
if (!STRIPE_KEY) return json(res, 503, { error: "stripe_not_configured" });
|
| 893 |
+
if (denyHot(req, res)) return;
|
| 894 |
const idp = await withTime(verifyIdToken(req.headers.authorization || ""), 1200, null);
|
| 895 |
if (!idp) return json(res, 401, { error: "login" });
|
| 896 |
+
if (payLimited(idp.sub, clientIp(req))) {
|
| 897 |
+
res.setHeader("Retry-After", "30");
|
| 898 |
+
return json(res, 429, { error: "rate_limited" });
|
| 899 |
+
}
|
| 900 |
let body;
|
| 901 |
try {
|
| 902 |
body = JSON.parse((await readBody(req)).toString("utf8") || "{}");
|
| 903 |
} catch {
|
| 904 |
return json(res, 400, { error: "body" });
|
| 905 |
}
|
| 906 |
+
const planKey = String(body.plan || "");
|
| 907 |
+
const plan = PLANS[planKey];
|
| 908 |
if (!plan) return json(res, 400, { error: "plan" });
|
| 909 |
+
const idem = "vip" + crypto.createHash("sha256").update(idp.sub + ":" + planKey).digest("hex").slice(0, 22) + Math.floor(Date.now() / 30000);
|
| 910 |
const r = await fetch("https://api.stripe.com/v1/checkout/sessions", {
|
| 911 |
method: "POST",
|
| 912 |
headers: {
|
| 913 |
Authorization: "Bearer " + STRIPE_KEY,
|
| 914 |
+
"Content-Type": "application/x-www-form-urlencoded",
|
| 915 |
+
"Idempotency-Key": idem
|
| 916 |
},
|
| 917 |
body: new URLSearchParams({
|
| 918 |
mode: "payment",
|
| 919 |
success_url: ORIGIN + "/?vip=success",
|
| 920 |
cancel_url: ORIGIN + "/?pay_error=cancel",
|
| 921 |
+
client_reference_id: String(idp.sub).slice(0, 200),
|
| 922 |
"line_items[0][price_data][currency]": "usd",
|
| 923 |
"line_items[0][price_data][product_data][name]": plan.label,
|
| 924 |
"line_items[0][price_data][unit_amount]": String(plan.amount),
|
| 925 |
"line_items[0][quantity]": "1",
|
| 926 |
"metadata[uid]": idp.sub,
|
| 927 |
+
"metadata[plan]": planKey,
|
| 928 |
"payment_intent_data[metadata][uid]": idp.sub,
|
| 929 |
+
"payment_intent_data[metadata][plan]": planKey
|
| 930 |
}).toString(),
|
| 931 |
signal: AbortSignal.timeout(12000)
|
| 932 |
});
|
| 933 |
const d = await r.json();
|
| 934 |
+
if (!r.ok || !d.id || !d.url) return json(res, 400, { error: "Payment failed" });
|
| 935 |
+
json(res, 200, { sid: d.id, url: d.url });
|
| 936 |
}
|
| 937 |
|
| 938 |
async function handleVipGo(req, res) {
|
| 939 |
if (ROLE !== "indexer") return json(res, 409, { error: "use_s1" });
|
| 940 |
if (!STRIPE_KEY) return json(res, 503, { error: "stripe_not_configured" });
|
| 941 |
+
if (denyHot(req, res)) return;
|
| 942 |
const idp = await withTime(verifyIdToken(req.headers.authorization || ""), 1200, null);
|
| 943 |
if (!idp) return json(res, 401, { error: "login" });
|
| 944 |
let body;
|
|
|
|
| 947 |
} catch {
|
| 948 |
return json(res, 400, { error: "body" });
|
| 949 |
}
|
| 950 |
+
if (!body.sid || !/^cs_[A-Za-z0-9_]+$/.test(String(body.sid))) return json(res, 400, { error: "session" });
|
| 951 |
const r = await fetch("https://api.stripe.com/v1/checkout/sessions/" + encodeURIComponent(body.sid), {
|
| 952 |
headers: { Authorization: "Bearer " + STRIPE_KEY },
|
| 953 |
signal: AbortSignal.timeout(12000)
|
| 954 |
});
|
| 955 |
const d = await r.json();
|
| 956 |
+
const uid = d.metadata && d.metadata.uid;
|
| 957 |
+
if (!r.ok || !d.url || uid !== idp.sub || d.status === "expired") return json(res, 400, { error: "Payment failed" });
|
| 958 |
json(res, 200, { url: d.url });
|
| 959 |
}
|
| 960 |
|
| 961 |
async function grantVip(uid, planKey) {
|
| 962 |
const plan = PLANS[planKey] || PLANS.month;
|
| 963 |
const now = Math.floor(Date.now() / 1000);
|
| 964 |
+
let start = now;
|
| 965 |
+
try {
|
| 966 |
+
const old = await rtdb("users/" + uid + "/subscription", "GET");
|
| 967 |
+
if (vipActive(old)) {
|
| 968 |
+
const exp = Number(old.expiry || old.expiresAt || 0);
|
| 969 |
+
const expSec = exp > 1e11 ? Math.floor(exp / 1000) : exp;
|
| 970 |
+
if (expSec > start) start = expSec;
|
| 971 |
+
}
|
| 972 |
+
} catch {}
|
| 973 |
const sub = {
|
| 974 |
status: "active",
|
| 975 |
plan: planKey,
|
| 976 |
label: plan.label,
|
| 977 |
price: plan.price,
|
| 978 |
+
expiry: start + plan.sec,
|
| 979 |
updatedAt: now
|
| 980 |
};
|
| 981 |
await rtdb("users/" + uid + "/subscription", "PUT", sub);
|
|
|
|
| 993 |
const t = parts.t;
|
| 994 |
const v1 = parts.v1;
|
| 995 |
if (!t || !v1) return false;
|
| 996 |
+
const ts = Number(t);
|
| 997 |
+
if (!Number.isFinite(ts) || Math.abs(Date.now() / 1000 - ts) > 300) return false;
|
| 998 |
const mac = crypto.createHmac("sha256", STRIPE_WH).update(t + "." + raw).digest("hex");
|
| 999 |
return timingSafeEq(mac, v1);
|
| 1000 |
}
|
| 1001 |
|
| 1002 |
+
async function orderSeen(id) {
|
| 1003 |
+
if (!id) return true;
|
| 1004 |
+
if (paidOrders.has(id)) return true;
|
| 1005 |
+
try {
|
| 1006 |
+
const hit = await rtdb("processed_orders/" + id, "GET");
|
| 1007 |
+
if (hit) {
|
| 1008 |
+
paidOrders.set(id, Date.now());
|
| 1009 |
+
return true;
|
| 1010 |
+
}
|
| 1011 |
+
} catch {}
|
| 1012 |
+
return false;
|
| 1013 |
+
}
|
| 1014 |
+
|
| 1015 |
+
async function markOrder(id) {
|
| 1016 |
+
paidOrders.set(id, Date.now());
|
| 1017 |
+
try {
|
| 1018 |
+
await rtdb("processed_orders/" + id, "PUT", { t: Math.floor(Date.now() / 1000) });
|
| 1019 |
+
} catch {}
|
| 1020 |
+
}
|
| 1021 |
+
|
| 1022 |
+
async function fulfillPaid(ses) {
|
| 1023 |
+
if (!ses || ses.mode !== "payment" || ses.payment_status !== "paid") return;
|
| 1024 |
+
const uid = ses.metadata && ses.metadata.uid;
|
| 1025 |
+
const planKey = ses.metadata && ses.metadata.plan;
|
| 1026 |
+
const plan = PLANS[planKey];
|
| 1027 |
+
if (!uid || !plan) return;
|
| 1028 |
+
if (Number(ses.amount_total) !== plan.amount) return;
|
| 1029 |
+
if (String(ses.currency || "").toLowerCase() !== "usd") return;
|
| 1030 |
+
const sid = String(ses.id || "");
|
| 1031 |
+
if (!sid || await orderSeen(sid)) return;
|
| 1032 |
+
await markOrder(sid);
|
| 1033 |
+
await grantVip(uid, planKey);
|
| 1034 |
+
}
|
| 1035 |
+
|
| 1036 |
async function handleStripeWebhook(req, res) {
|
| 1037 |
const raw = (await readBody(req)).toString("utf8");
|
| 1038 |
if (!stripeOk(raw, req.headers["stripe-signature"])) {
|
|
|
|
| 1044 |
res.writeHead(400);
|
| 1045 |
return res.end();
|
| 1046 |
}
|
| 1047 |
+
if (ev.type === "checkout.session.completed" || ev.type === "checkout.session.async_payment_succeeded") {
|
| 1048 |
const ses = ev.data && ev.data.object || {};
|
| 1049 |
+
try { await fulfillPaid(ses); } catch {}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1050 |
}
|
| 1051 |
json(res, 200, { ok: true });
|
| 1052 |
}
|