Spaces:
Running
Running
JOHN SILLVESTOR commited on
Stop full play when VIP ends and add monthly auto recharge
Browse files
server.js
CHANGED
|
@@ -687,7 +687,8 @@ function publicSub(sub) {
|
|
| 687 |
status: String(sub.status || ""),
|
| 688 |
plan: String(sub.plan || "").slice(0, 24),
|
| 689 |
label: String(sub.label || "").slice(0, 40),
|
| 690 |
-
expiry: exp
|
|
|
|
| 691 |
};
|
| 692 |
}
|
| 693 |
|
|
@@ -788,8 +789,9 @@ async function accessLevel(uid) {
|
|
| 788 |
try {
|
| 789 |
const sub = await withTime(rtdb("users/" + uid + "/subscription", "GET"), 3500, LVL_MISS);
|
| 790 |
if (sub === LVL_MISS) return "anonymous";
|
| 791 |
-
|
| 792 |
-
|
|
|
|
| 793 |
} catch { }
|
| 794 |
return lvl;
|
| 795 |
}
|
|
@@ -2150,8 +2152,15 @@ async function handleGenerate(req, res, url) {
|
|
| 2150 |
if (cachedDur > 1) rec.duration = cachedDur;
|
| 2151 |
const playMime = httpVideoType(rec.mime, rec.name);
|
| 2152 |
const durSec = Number(rec.duration) > 1 ? Math.floor(Number(rec.duration)) : 0;
|
| 2153 |
-
const
|
| 2154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2155 |
const k = sealTicket(payload);
|
| 2156 |
const host = publicSpaceOrigin(req);
|
| 2157 |
if (!elstLeadSec.has(hid)) {
|
|
@@ -2220,6 +2229,8 @@ async function handleStream(req, res, url, msgId, kHex, ticketObj) {
|
|
| 2220 |
const uaH = req.headers["user-agent"];
|
| 2221 |
const netOk = ticketNetOk(ticket, ip);
|
| 2222 |
const uaOk = ticketUaOk(ticket, uaH);
|
|
|
|
|
|
|
| 2223 |
const isVip = ticketLvl(ticket) === "subscribed";
|
| 2224 |
|
| 2225 |
if (isVip) {
|
|
@@ -2438,15 +2449,17 @@ async function withPayLock(id, fn) {
|
|
| 2438 |
}
|
| 2439 |
}
|
| 2440 |
|
| 2441 |
-
async function openPay(uid, planKey, email) {
|
| 2442 |
let rec = null;
|
| 2443 |
try { rec = await rtdb("pay_open/" + uid, "GET"); } catch { rec = null; }
|
| 2444 |
if (!rec || rec.plan !== planKey || rec.email !== email || !/^cs_[A-Za-z0-9_]+$/.test(String(rec.sid || ""))) return null;
|
|
|
|
| 2445 |
if (Math.floor(Date.now() / 1000) - Number(rec.t || 0) > 1800) return null;
|
| 2446 |
const ses = await stripeSession(rec.sid);
|
| 2447 |
const meta = ses && ses.metadata || {};
|
| 2448 |
if (!ses || !ses.url || ses.status !== "open" || ses.payment_status === "paid") return null;
|
| 2449 |
if (meta.uid !== uid || meta.plan !== planKey || String(meta.email || "") !== email) return null;
|
|
|
|
| 2450 |
return { sid: ses.id, url: ses.url };
|
| 2451 |
}
|
| 2452 |
|
|
@@ -2468,8 +2481,9 @@ async function handleVipStart(req, res) {
|
|
| 2468 |
const plan = PLANS[planKey];
|
| 2469 |
const email = payEmail(idp);
|
| 2470 |
if (!plan || !email) return json(res, 400, { error: "plan" });
|
|
|
|
| 2471 |
const urls = payUrls(body.returnUrl);
|
| 2472 |
-
const reused = await openPay(idp.sub, planKey, email);
|
| 2473 |
if (reused) return json(res, 200, reused);
|
| 2474 |
const nonce = crypto.randomBytes(8).toString("hex");
|
| 2475 |
const idem = "vip" + crypto.createHash("sha256").update(idp.sub + ":" + planKey + ":" + nonce).digest("hex").slice(0, 22);
|
|
@@ -2498,9 +2512,23 @@ async function handleVipStart(req, res) {
|
|
| 2498 |
"payment_intent_data[description]": "DUB LK 2000 VIP " + plan.label,
|
| 2499 |
"payment_intent_data[metadata][uid]": idp.sub,
|
| 2500 |
"payment_intent_data[metadata][plan]": planKey,
|
| 2501 |
-
"payment_intent_data[metadata][email]": email
|
|
|
|
| 2502 |
};
|
| 2503 |
fields.customer_email = email;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2504 |
async function createSes(body) {
|
| 2505 |
const r = await fetch("https://api.stripe.com/v1/checkout/sessions", {
|
| 2506 |
method: "POST",
|
|
@@ -2533,7 +2561,7 @@ async function handleVipStart(req, res) {
|
|
| 2533 |
}
|
| 2534 |
if (!r.ok || !d.id || !d.url || d.object !== "checkout.session") return json(res, 400, { error: "Payment failed" });
|
| 2535 |
try {
|
| 2536 |
-
await rtdb("pay_open/" + idp.sub, "PUT", { sid: d.id, plan: planKey, email: email, t: Math.floor(Date.now() / 1000) });
|
| 2537 |
} catch { }
|
| 2538 |
json(res, 200, { sid: d.id, url: d.url });
|
| 2539 |
}
|
|
@@ -2592,7 +2620,7 @@ async function handleVipConfirm(req, res) {
|
|
| 2592 |
json(res, 200, { ok: true, paid: true, accessLevel: "subscribed", subscription: publicSub(sub) });
|
| 2593 |
}
|
| 2594 |
|
| 2595 |
-
async function grantVip(uid, planKey, absExp) {
|
| 2596 |
const plan = PLANS[planKey];
|
| 2597 |
const now = Math.floor(Date.now() / 1000);
|
| 2598 |
const exp = Number(absExp) || 0;
|
|
@@ -2600,18 +2628,18 @@ async function grantVip(uid, planKey, absExp) {
|
|
| 2600 |
let old = null;
|
| 2601 |
try { old = await rtdb("users/" + uid + "/subscription", "GET"); } catch { }
|
| 2602 |
const oldExp = vipExpirySec(old);
|
| 2603 |
-
|
| 2604 |
-
lvlCache.delete(uid);
|
| 2605 |
-
return old;
|
| 2606 |
-
}
|
| 2607 |
const sub = {
|
| 2608 |
status: "active",
|
| 2609 |
-
plan: planKey,
|
| 2610 |
-
label: plan.label,
|
| 2611 |
-
price: plan.price,
|
| 2612 |
-
expiry: exp,
|
| 2613 |
-
updatedAt: now
|
|
|
|
| 2614 |
};
|
|
|
|
|
|
|
| 2615 |
await rtdb("users/" + uid + "/subscription", "PUT", sub);
|
| 2616 |
lvlCache.delete(uid);
|
| 2617 |
return sub;
|
|
@@ -2694,13 +2722,15 @@ async function claimOrder(id, uid, planKey, planSec) {
|
|
| 2694 |
|
| 2695 |
async function fulfillPaid(ses) {
|
| 2696 |
if (!ses || ses.object !== "checkout.session") return false;
|
| 2697 |
-
if (ses.mode !== "payment"
|
| 2698 |
-
if (ses.
|
| 2699 |
-
if (
|
|
|
|
| 2700 |
const uid = ses.metadata && ses.metadata.uid;
|
| 2701 |
const planKey = ses.metadata && ses.metadata.plan;
|
| 2702 |
const plan = PLANS[planKey];
|
| 2703 |
if (!uidOk(uid) || !plan) return false;
|
|
|
|
| 2704 |
if (ses.client_reference_id !== uid) return false;
|
| 2705 |
if (ses.status !== "complete") return false;
|
| 2706 |
const email = String(ses.metadata && ses.metadata.email || "").toLowerCase();
|
|
@@ -2713,7 +2743,12 @@ async function fulfillPaid(ses) {
|
|
| 2713 |
const c = await claimOrder(sid, uid, planKey, plan.sec);
|
| 2714 |
if (c.state === "conflict" || c.state === "unknown" || !(c.exp > Math.floor(Date.now() / 1000))) return false;
|
| 2715 |
paidOrders.set(sid, Date.now());
|
| 2716 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2717 |
await markOrder(sid, uid, planKey, c.exp);
|
| 2718 |
try {
|
| 2719 |
const open = await rtdb("pay_open/" + uid, "GET");
|
|
@@ -2723,6 +2758,93 @@ async function fulfillPaid(ses) {
|
|
| 2723 |
});
|
| 2724 |
}
|
| 2725 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2726 |
async function stripeEventSeen(id) {
|
| 2727 |
if (!id) return true;
|
| 2728 |
if (stripeEvents.has(id)) return true;
|
|
@@ -2784,8 +2906,14 @@ async function handleStripeWebhook(req, res) {
|
|
| 2784 |
} catch { }
|
| 2785 |
}
|
| 2786 |
did = true;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2787 |
}
|
| 2788 |
-
if ((ev.type === "checkout.session.completed" || ev.type === "checkout.session.async_payment_succeeded") && !did) {
|
| 2789 |
res.writeHead(500);
|
| 2790 |
return res.end();
|
| 2791 |
}
|
|
@@ -3012,6 +3140,7 @@ async function onReq(req, res) {
|
|
| 3012 |
if (url.pathname === "/vip/start" && req.method === "POST") return handleVipStart(req, res);
|
| 3013 |
if (url.pathname === "/vip/go" && req.method === "POST") return handleVipGo(req, res);
|
| 3014 |
if (url.pathname === "/vip/confirm" && req.method === "POST") return handleVipConfirm(req, res);
|
|
|
|
| 3015 |
if (url.pathname === "/vip/stripe" && req.method === "POST") return handleStripeWebhook(req, res);
|
| 3016 |
res.writeHead(404);
|
| 3017 |
res.end();
|
|
|
|
| 687 |
status: String(sub.status || ""),
|
| 688 |
plan: String(sub.plan || "").slice(0, 24),
|
| 689 |
label: String(sub.label || "").slice(0, 40),
|
| 690 |
+
expiry: exp,
|
| 691 |
+
autoRenew: sub.autoRenew === true
|
| 692 |
};
|
| 693 |
}
|
| 694 |
|
|
|
|
| 789 |
try {
|
| 790 |
const sub = await withTime(rtdb("users/" + uid + "/subscription", "GET"), 3500, LVL_MISS);
|
| 791 |
if (sub === LVL_MISS) return "anonymous";
|
| 792 |
+
const exp = sub && vipActive(sub) ? vipExpirySec(sub) : 0;
|
| 793 |
+
if (exp > Math.floor(Date.now() / 1000)) lvl = "subscribed";
|
| 794 |
+
lvlCache.set(uid, { lvl, exp: exp, ts: Date.now() });
|
| 795 |
} catch { }
|
| 796 |
return lvl;
|
| 797 |
}
|
|
|
|
| 2152 |
if (cachedDur > 1) rec.duration = cachedDur;
|
| 2153 |
const playMime = httpVideoType(rec.mime, rec.name);
|
| 2154 |
const durSec = Number(rec.duration) > 1 ? Math.floor(Number(rec.duration)) : 0;
|
| 2155 |
+
const nowSec = Math.floor(Date.now() / 1000);
|
| 2156 |
+
let exp = nowSec + TOKEN_TTL;
|
| 2157 |
+
let vipUntil = 0;
|
| 2158 |
+
if (lvl === "subscribed" && uid) {
|
| 2159 |
+
const hit = lvlCache.get(uid);
|
| 2160 |
+
vipUntil = hit && Number(hit.exp) || 0;
|
| 2161 |
+
if (vipUntil > nowSec && vipUntil < exp) exp = vipUntil;
|
| 2162 |
+
}
|
| 2163 |
+
const payload = { m: msgId, h: hash, u: uid || "anon", l: lvl, b: bot.idx, e: exp, x: vipUntil, s: rec.size, n: playMime, p: ipTag(ip), a: uaTag(req.headers["user-agent"]), d: durSec };
|
| 2164 |
const k = sealTicket(payload);
|
| 2165 |
const host = publicSpaceOrigin(req);
|
| 2166 |
if (!elstLeadSec.has(hid)) {
|
|
|
|
| 2229 |
const uaH = req.headers["user-agent"];
|
| 2230 |
const netOk = ticketNetOk(ticket, ip);
|
| 2231 |
const uaOk = ticketUaOk(ticket, uaH);
|
| 2232 |
+
const vipUntil = Number(ticket.x) || 0;
|
| 2233 |
+
if (ticketLvl(ticket) === "subscribed" && vipUntil > 0 && vipUntil <= Math.floor(Date.now() / 1000)) ticket.l = "registered";
|
| 2234 |
const isVip = ticketLvl(ticket) === "subscribed";
|
| 2235 |
|
| 2236 |
if (isVip) {
|
|
|
|
| 2449 |
}
|
| 2450 |
}
|
| 2451 |
|
| 2452 |
+
async function openPay(uid, planKey, email, auto) {
|
| 2453 |
let rec = null;
|
| 2454 |
try { rec = await rtdb("pay_open/" + uid, "GET"); } catch { rec = null; }
|
| 2455 |
if (!rec || rec.plan !== planKey || rec.email !== email || !/^cs_[A-Za-z0-9_]+$/.test(String(rec.sid || ""))) return null;
|
| 2456 |
+
if ((rec.auto === 1) !== !!auto) return null;
|
| 2457 |
if (Math.floor(Date.now() / 1000) - Number(rec.t || 0) > 1800) return null;
|
| 2458 |
const ses = await stripeSession(rec.sid);
|
| 2459 |
const meta = ses && ses.metadata || {};
|
| 2460 |
if (!ses || !ses.url || ses.status !== "open" || ses.payment_status === "paid") return null;
|
| 2461 |
if (meta.uid !== uid || meta.plan !== planKey || String(meta.email || "") !== email) return null;
|
| 2462 |
+
if ((meta.auto === "1") !== !!auto) return null;
|
| 2463 |
return { sid: ses.id, url: ses.url };
|
| 2464 |
}
|
| 2465 |
|
|
|
|
| 2481 |
const plan = PLANS[planKey];
|
| 2482 |
const email = payEmail(idp);
|
| 2483 |
if (!plan || !email) return json(res, 400, { error: "plan" });
|
| 2484 |
+
const auto = planKey === "month" && body.auto === true;
|
| 2485 |
const urls = payUrls(body.returnUrl);
|
| 2486 |
+
const reused = await openPay(idp.sub, planKey, email, auto);
|
| 2487 |
if (reused) return json(res, 200, reused);
|
| 2488 |
const nonce = crypto.randomBytes(8).toString("hex");
|
| 2489 |
const idem = "vip" + crypto.createHash("sha256").update(idp.sub + ":" + planKey + ":" + nonce).digest("hex").slice(0, 22);
|
|
|
|
| 2512 |
"payment_intent_data[description]": "DUB LK 2000 VIP " + plan.label,
|
| 2513 |
"payment_intent_data[metadata][uid]": idp.sub,
|
| 2514 |
"payment_intent_data[metadata][plan]": planKey,
|
| 2515 |
+
"payment_intent_data[metadata][email]": email,
|
| 2516 |
+
"payment_intent_data[receipt_email]": email
|
| 2517 |
};
|
| 2518 |
fields.customer_email = email;
|
| 2519 |
+
if (auto) {
|
| 2520 |
+
fields.mode = "subscription";
|
| 2521 |
+
fields["line_items[0][price_data][recurring][interval]"] = "month";
|
| 2522 |
+
fields["metadata[auto]"] = "1";
|
| 2523 |
+
fields["subscription_data[metadata][uid]"] = idp.sub;
|
| 2524 |
+
fields["subscription_data[metadata][plan]"] = planKey;
|
| 2525 |
+
fields["subscription_data[metadata][email]"] = email;
|
| 2526 |
+
delete fields["payment_intent_data[description]"];
|
| 2527 |
+
delete fields["payment_intent_data[metadata][uid]"];
|
| 2528 |
+
delete fields["payment_intent_data[metadata][plan]"];
|
| 2529 |
+
delete fields["payment_intent_data[metadata][email]"];
|
| 2530 |
+
delete fields["payment_intent_data[receipt_email]"];
|
| 2531 |
+
}
|
| 2532 |
async function createSes(body) {
|
| 2533 |
const r = await fetch("https://api.stripe.com/v1/checkout/sessions", {
|
| 2534 |
method: "POST",
|
|
|
|
| 2561 |
}
|
| 2562 |
if (!r.ok || !d.id || !d.url || d.object !== "checkout.session") return json(res, 400, { error: "Payment failed" });
|
| 2563 |
try {
|
| 2564 |
+
await rtdb("pay_open/" + idp.sub, "PUT", { sid: d.id, plan: planKey, email: email, auto: auto ? 1 : 0, t: Math.floor(Date.now() / 1000) });
|
| 2565 |
} catch { }
|
| 2566 |
json(res, 200, { sid: d.id, url: d.url });
|
| 2567 |
}
|
|
|
|
| 2620 |
json(res, 200, { ok: true, paid: true, accessLevel: "subscribed", subscription: publicSub(sub) });
|
| 2621 |
}
|
| 2622 |
|
| 2623 |
+
async function grantVip(uid, planKey, absExp, extra) {
|
| 2624 |
const plan = PLANS[planKey];
|
| 2625 |
const now = Math.floor(Date.now() / 1000);
|
| 2626 |
const exp = Number(absExp) || 0;
|
|
|
|
| 2628 |
let old = null;
|
| 2629 |
try { old = await rtdb("users/" + uid + "/subscription", "GET"); } catch { }
|
| 2630 |
const oldExp = vipExpirySec(old);
|
| 2631 |
+
const keep = old && vipActive(old) && oldExp >= exp;
|
|
|
|
|
|
|
|
|
|
| 2632 |
const sub = {
|
| 2633 |
status: "active",
|
| 2634 |
+
plan: keep ? String(old.plan || planKey) : planKey,
|
| 2635 |
+
label: keep ? String(old.label || plan.label) : plan.label,
|
| 2636 |
+
price: keep ? String(old.price || plan.price) : plan.price,
|
| 2637 |
+
expiry: keep ? oldExp : exp,
|
| 2638 |
+
updatedAt: now,
|
| 2639 |
+
autoRenew: extra && extra.autoRenew === true ? true : (extra && extra.autoRenew === false ? false : old && old.autoRenew === true)
|
| 2640 |
};
|
| 2641 |
+
const stripeSub = extra && extra.stripeSub ? String(extra.stripeSub) : (old && old.stripeSub ? String(old.stripeSub) : "");
|
| 2642 |
+
if (/^sub_[A-Za-z0-9_]+$/.test(stripeSub)) sub.stripeSub = stripeSub;
|
| 2643 |
await rtdb("users/" + uid + "/subscription", "PUT", sub);
|
| 2644 |
lvlCache.delete(uid);
|
| 2645 |
return sub;
|
|
|
|
| 2722 |
|
| 2723 |
async function fulfillPaid(ses) {
|
| 2724 |
if (!ses || ses.object !== "checkout.session") return false;
|
| 2725 |
+
if (ses.mode !== "payment" && ses.mode !== "subscription") return false;
|
| 2726 |
+
if (ses.payment_status !== "paid") return false;
|
| 2727 |
+
if (ses.status !== "complete") return false;
|
| 2728 |
+
if (STRIPE_KEY.indexOf("sk_test_") !== 0 && STRIPE_KEY.indexOf("rk_test_") !== 0 && ses.livemode !== true) return false;
|
| 2729 |
const uid = ses.metadata && ses.metadata.uid;
|
| 2730 |
const planKey = ses.metadata && ses.metadata.plan;
|
| 2731 |
const plan = PLANS[planKey];
|
| 2732 |
if (!uidOk(uid) || !plan) return false;
|
| 2733 |
+
if (ses.mode === "subscription" && (planKey !== "month" || !ses.metadata || ses.metadata.auto !== "1")) return false;
|
| 2734 |
if (ses.client_reference_id !== uid) return false;
|
| 2735 |
if (ses.status !== "complete") return false;
|
| 2736 |
const email = String(ses.metadata && ses.metadata.email || "").toLowerCase();
|
|
|
|
| 2743 |
const c = await claimOrder(sid, uid, planKey, plan.sec);
|
| 2744 |
if (c.state === "conflict" || c.state === "unknown" || !(c.exp > Math.floor(Date.now() / 1000))) return false;
|
| 2745 |
paidOrders.set(sid, Date.now());
|
| 2746 |
+
const autoOn = ses.metadata && ses.metadata.auto === "1";
|
| 2747 |
+
const stripeSub = typeof ses.subscription === "string" ? ses.subscription : "";
|
| 2748 |
+
const extra = {};
|
| 2749 |
+
if (autoOn) extra.autoRenew = true;
|
| 2750 |
+
if (stripeSub) extra.stripeSub = stripeSub;
|
| 2751 |
+
await grantVip(uid, planKey, c.exp, extra);
|
| 2752 |
await markOrder(sid, uid, planKey, c.exp);
|
| 2753 |
try {
|
| 2754 |
const open = await rtdb("pay_open/" + uid, "GET");
|
|
|
|
| 2758 |
});
|
| 2759 |
}
|
| 2760 |
|
| 2761 |
+
async function stripeGet(path) {
|
| 2762 |
+
if (!STRIPE_KEY) return null;
|
| 2763 |
+
const r = await fetch("https://api.stripe.com/v1/" + path, {
|
| 2764 |
+
headers: { Authorization: "Bearer " + STRIPE_KEY },
|
| 2765 |
+
signal: AbortSignal.timeout(12000)
|
| 2766 |
+
});
|
| 2767 |
+
const d = await r.json();
|
| 2768 |
+
if (!r.ok || !d || d.error) return null;
|
| 2769 |
+
return d;
|
| 2770 |
+
}
|
| 2771 |
+
|
| 2772 |
+
async function stripeForm(path, fields) {
|
| 2773 |
+
if (!STRIPE_KEY) return null;
|
| 2774 |
+
const r = await fetch("https://api.stripe.com/v1/" + path, {
|
| 2775 |
+
method: "POST",
|
| 2776 |
+
headers: {
|
| 2777 |
+
Authorization: "Bearer " + STRIPE_KEY,
|
| 2778 |
+
"Content-Type": "application/x-www-form-urlencoded"
|
| 2779 |
+
},
|
| 2780 |
+
body: new URLSearchParams(fields).toString(),
|
| 2781 |
+
signal: AbortSignal.timeout(12000)
|
| 2782 |
+
});
|
| 2783 |
+
const d = await r.json();
|
| 2784 |
+
if (!r.ok || !d || d.error) return null;
|
| 2785 |
+
return d;
|
| 2786 |
+
}
|
| 2787 |
+
|
| 2788 |
+
async function fulfillInvoice(inv) {
|
| 2789 |
+
if (!inv || inv.object !== "invoice" || inv.status !== "paid") return false;
|
| 2790 |
+
if (STRIPE_KEY.indexOf("sk_test_") !== 0 && STRIPE_KEY.indexOf("rk_test_") !== 0 && inv.livemode !== true) return false;
|
| 2791 |
+
if (inv.billing_reason === "subscription_create") return true;
|
| 2792 |
+
const subId = typeof inv.subscription === "string" ? inv.subscription : "";
|
| 2793 |
+
const invId = String(inv.id || "");
|
| 2794 |
+
if (!/^sub_[A-Za-z0-9_]+$/.test(subId) || !/^in_[A-Za-z0-9_]+$/.test(invId)) return false;
|
| 2795 |
+
const sub = await stripeGet("subscriptions/" + encodeURIComponent(subId));
|
| 2796 |
+
const uid = sub && sub.metadata && sub.metadata.uid;
|
| 2797 |
+
const planKey = sub && sub.metadata && sub.metadata.plan;
|
| 2798 |
+
const plan = PLANS[planKey];
|
| 2799 |
+
if (!uidOk(uid) || planKey !== "month" || !plan) return false;
|
| 2800 |
+
if (Number(inv.amount_paid) !== plan.amount) return false;
|
| 2801 |
+
if (String(inv.currency || "").toLowerCase() !== "usd") return false;
|
| 2802 |
+
return withPayLock(invId, async () => {
|
| 2803 |
+
const c = await claimOrder(invId, uid, planKey, plan.sec);
|
| 2804 |
+
if (c.state === "conflict" || c.state === "unknown" || !(c.exp > Math.floor(Date.now() / 1000))) return false;
|
| 2805 |
+
await grantVip(uid, planKey, c.exp, { autoRenew: true, stripeSub: subId });
|
| 2806 |
+
await markOrder(invId, uid, planKey, c.exp);
|
| 2807 |
+
return true;
|
| 2808 |
+
});
|
| 2809 |
+
}
|
| 2810 |
+
|
| 2811 |
+
async function handleVipAuto(req, res) {
|
| 2812 |
+
if (!payGate(req, res)) return;
|
| 2813 |
+
const idp = await withTime(verifyIdToken(req.headers.authorization || ""), 4000, null);
|
| 2814 |
+
if (!idp || !googlePayUser(idp) || !uidOk(idp.sub)) return json(res, 401, { error: "login" });
|
| 2815 |
+
if (payLimited(idp.sub, clientIp(req))) {
|
| 2816 |
+
res.setHeader("Retry-After", "30");
|
| 2817 |
+
return json(res, 429, { error: "rate_limited" });
|
| 2818 |
+
}
|
| 2819 |
+
let body;
|
| 2820 |
+
try { body = JSON.parse((await readBody(req)).toString("utf8") || "{}"); } catch { return json(res, 400, { error: "body" }); }
|
| 2821 |
+
const on = body.on === true;
|
| 2822 |
+
let sub = null;
|
| 2823 |
+
try { sub = await rtdb("users/" + idp.sub + "/subscription", "GET"); } catch { sub = null; }
|
| 2824 |
+
const stripeSub = sub && /^sub_[A-Za-z0-9_]+$/.test(String(sub.stripeSub || "")) ? String(sub.stripeSub) : "";
|
| 2825 |
+
if (!on) {
|
| 2826 |
+
if (stripeSub) {
|
| 2827 |
+
const stopped = await stripeForm("subscriptions/" + encodeURIComponent(stripeSub), { cancel_at_period_end: "true" });
|
| 2828 |
+
if (!stopped) return json(res, 400, { error: "Payment failed" });
|
| 2829 |
+
}
|
| 2830 |
+
if (sub && typeof sub === "object") {
|
| 2831 |
+
sub.autoRenew = false;
|
| 2832 |
+
try { await rtdb("users/" + idp.sub + "/subscription", "PUT", sub); } catch { return json(res, 400, { error: "Payment failed" }); }
|
| 2833 |
+
}
|
| 2834 |
+
lvlCache.delete(idp.sub);
|
| 2835 |
+
return json(res, 200, { ok: true, autoRenew: false, subscription: publicSub(sub) });
|
| 2836 |
+
}
|
| 2837 |
+
if (stripeSub && vipActive(sub)) {
|
| 2838 |
+
const resumed = await stripeForm("subscriptions/" + encodeURIComponent(stripeSub), { cancel_at_period_end: "false" });
|
| 2839 |
+
if (!resumed) return json(res, 400, { error: "Payment failed" });
|
| 2840 |
+
sub.autoRenew = true;
|
| 2841 |
+
try { await rtdb("users/" + idp.sub + "/subscription", "PUT", sub); } catch { return json(res, 400, { error: "Payment failed" }); }
|
| 2842 |
+
lvlCache.delete(idp.sub);
|
| 2843 |
+
return json(res, 200, { ok: true, autoRenew: true, subscription: publicSub(sub) });
|
| 2844 |
+
}
|
| 2845 |
+
json(res, 200, { ok: true, needPay: true, autoRenew: false });
|
| 2846 |
+
}
|
| 2847 |
+
|
| 2848 |
async function stripeEventSeen(id) {
|
| 2849 |
if (!id) return true;
|
| 2850 |
if (stripeEvents.has(id)) return true;
|
|
|
|
| 2906 |
} catch { }
|
| 2907 |
}
|
| 2908 |
did = true;
|
| 2909 |
+
} else if (ev.type === "invoice.paid") {
|
| 2910 |
+
const id = ev.data && ev.data.object && ev.data.object.id;
|
| 2911 |
+
try {
|
| 2912 |
+
const inv = await stripeGet("invoices/" + encodeURIComponent(String(id || "")));
|
| 2913 |
+
if (inv) did = await fulfillInvoice(inv);
|
| 2914 |
+
} catch { did = false; }
|
| 2915 |
}
|
| 2916 |
+
if ((ev.type === "checkout.session.completed" || ev.type === "checkout.session.async_payment_succeeded" || ev.type === "invoice.paid") && !did) {
|
| 2917 |
res.writeHead(500);
|
| 2918 |
return res.end();
|
| 2919 |
}
|
|
|
|
| 3140 |
if (url.pathname === "/vip/start" && req.method === "POST") return handleVipStart(req, res);
|
| 3141 |
if (url.pathname === "/vip/go" && req.method === "POST") return handleVipGo(req, res);
|
| 3142 |
if (url.pathname === "/vip/confirm" && req.method === "POST") return handleVipConfirm(req, res);
|
| 3143 |
+
if (url.pathname === "/vip/auto" && req.method === "POST") return handleVipAuto(req, res);
|
| 3144 |
if (url.pathname === "/vip/stripe" && req.method === "POST") return handleStripeWebhook(req, res);
|
| 3145 |
res.writeHead(404);
|
| 3146 |
res.end();
|