Spaces:
Running
Running
JOHN SILLVESTOR commited on
Google preview 80 percent, VIP stays full
Browse files
server.js
CHANGED
|
@@ -704,6 +704,12 @@ function googleUid(idp) {
|
|
| 704 |
return idp.sub;
|
| 705 |
}
|
| 706 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 707 |
function uidOk(uid) {
|
| 708 |
return typeof uid === "string" && /^[A-Za-z0-9_-]{8,128}$/.test(uid);
|
| 709 |
}
|
|
@@ -1160,7 +1166,7 @@ async function fillDuration(bot, rec, msgId) {
|
|
| 1160 |
return d;
|
| 1161 |
}
|
| 1162 |
|
| 1163 |
-
const LVL_PCT = { anonymous: 40, registered:
|
| 1164 |
const INDEX_WIN = 4 * MB;
|
| 1165 |
const TAIL_MAX = 2 * MB;
|
| 1166 |
const TAIL_MIN_SIZE = 48 * MB;
|
|
@@ -2432,6 +2438,18 @@ async function withPayLock(id, fn) {
|
|
| 2432 |
}
|
| 2433 |
}
|
| 2434 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2435 |
async function handleVipStart(req, res) {
|
| 2436 |
if (!payGate(req, res)) return;
|
| 2437 |
const idp = await withTime(verifyIdToken(req.headers.authorization || ""), 4000, null);
|
|
@@ -2448,9 +2466,13 @@ async function handleVipStart(req, res) {
|
|
| 2448 |
}
|
| 2449 |
const planKey = String(body.plan || "");
|
| 2450 |
const plan = PLANS[planKey];
|
| 2451 |
-
|
|
|
|
| 2452 |
const urls = payUrls(body.returnUrl);
|
| 2453 |
-
const
|
|
|
|
|
|
|
|
|
|
| 2454 |
const homeMsg = "After pay you return to [DUB LK 2000 Home](" + HOME_URL + ")";
|
| 2455 |
const fields = {
|
| 2456 |
mode: "payment",
|
|
@@ -2472,13 +2494,13 @@ async function handleVipStart(req, res) {
|
|
| 2472 |
"custom_text[after_submit][message]": "Site: [" + HOME_URL + "](" + HOME_URL + ")",
|
| 2473 |
"metadata[uid]": idp.sub,
|
| 2474 |
"metadata[plan]": planKey,
|
|
|
|
| 2475 |
"payment_intent_data[description]": "DUB LK 2000 VIP " + plan.label,
|
| 2476 |
"payment_intent_data[metadata][uid]": idp.sub,
|
| 2477 |
-
"payment_intent_data[metadata][plan]": planKey
|
|
|
|
| 2478 |
};
|
| 2479 |
-
|
| 2480 |
-
fields.customer_email = String(idp.email).slice(0, 200);
|
| 2481 |
-
}
|
| 2482 |
async function createSes(body) {
|
| 2483 |
const r = await fetch("https://api.stripe.com/v1/checkout/sessions", {
|
| 2484 |
method: "POST",
|
|
@@ -2510,6 +2532,9 @@ async function handleVipStart(req, res) {
|
|
| 2510 |
}
|
| 2511 |
}
|
| 2512 |
if (!r.ok || !d.id || !d.url || d.object !== "checkout.session") return json(res, 400, { error: "Payment failed" });
|
|
|
|
|
|
|
|
|
|
| 2513 |
json(res, 200, { sid: d.id, url: d.url });
|
| 2514 |
}
|
| 2515 |
|
|
@@ -2528,8 +2553,10 @@ async function handleVipGo(req, res) {
|
|
| 2528 |
return json(res, 400, { error: "body" });
|
| 2529 |
}
|
| 2530 |
const d = await stripeSession(body.sid);
|
| 2531 |
-
const
|
| 2532 |
-
|
|
|
|
|
|
|
| 2533 |
json(res, 200, { url: d.url });
|
| 2534 |
}
|
| 2535 |
|
|
@@ -2548,38 +2575,41 @@ async function handleVipConfirm(req, res) {
|
|
| 2548 |
return json(res, 400, { error: "body" });
|
| 2549 |
}
|
| 2550 |
const d = await stripeSession(body.sid);
|
| 2551 |
-
const
|
| 2552 |
-
|
|
|
|
|
|
|
| 2553 |
return json(res, 400, { error: "Payment failed", paid: false });
|
| 2554 |
}
|
| 2555 |
-
if (
|
| 2556 |
-
|
|
|
|
|
|
|
| 2557 |
lvlCache.delete(idp.sub);
|
| 2558 |
let sub = null;
|
| 2559 |
try { sub = await rtdb("users/" + idp.sub + "/subscription", "GET"); } catch { }
|
| 2560 |
-
if (!vipActive(sub)) return json(res, 402, { error: "unpaid", paid: false });
|
| 2561 |
json(res, 200, { ok: true, paid: true, accessLevel: "subscribed", subscription: publicSub(sub) });
|
| 2562 |
}
|
| 2563 |
|
| 2564 |
-
async function grantVip(uid, planKey) {
|
| 2565 |
const plan = PLANS[planKey];
|
| 2566 |
-
if (!plan || !uidOk(uid)) throw new Error("grant");
|
| 2567 |
const now = Math.floor(Date.now() / 1000);
|
| 2568 |
-
const
|
| 2569 |
-
|
| 2570 |
-
|
| 2571 |
-
|
| 2572 |
-
|
| 2573 |
-
|
| 2574 |
-
|
| 2575 |
-
|
| 2576 |
-
|
| 2577 |
const sub = {
|
| 2578 |
status: "active",
|
| 2579 |
plan: planKey,
|
| 2580 |
label: plan.label,
|
| 2581 |
price: plan.price,
|
| 2582 |
-
expiry:
|
| 2583 |
updatedAt: now
|
| 2584 |
};
|
| 2585 |
await rtdb("users/" + uid + "/subscription", "PUT", sub);
|
|
@@ -2642,7 +2672,13 @@ async function claimOrder(id, uid, planKey, planSec) {
|
|
| 2642 |
};
|
| 2643 |
let back = await readBack();
|
| 2644 |
if (!back) {
|
| 2645 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2646 |
try {
|
| 2647 |
await rtdb("processed_orders/" + id, "PUT", rec);
|
| 2648 |
} catch { }
|
|
@@ -2665,17 +2701,24 @@ async function fulfillPaid(ses) {
|
|
| 2665 |
const planKey = ses.metadata && ses.metadata.plan;
|
| 2666 |
const plan = PLANS[planKey];
|
| 2667 |
if (!uidOk(uid) || !plan) return false;
|
| 2668 |
-
if (ses.client_reference_id
|
|
|
|
|
|
|
|
|
|
| 2669 |
if (Number(ses.amount_total) !== plan.amount) return false;
|
| 2670 |
if (String(ses.currency || "").toLowerCase() !== "usd") return false;
|
| 2671 |
const sid = String(ses.id || "");
|
| 2672 |
if (!/^cs_[A-Za-z0-9_]+$/.test(sid)) return false;
|
| 2673 |
return withPayLock(sid, async () => {
|
| 2674 |
const c = await claimOrder(sid, uid, planKey, plan.sec);
|
| 2675 |
-
if (c.state === "conflict") return false;
|
| 2676 |
paidOrders.set(sid, Date.now());
|
| 2677 |
await grantVip(uid, planKey, c.exp);
|
| 2678 |
await markOrder(sid, uid, planKey, c.exp);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2679 |
return true;
|
| 2680 |
});
|
| 2681 |
}
|
|
@@ -2683,23 +2726,26 @@ async function fulfillPaid(ses) {
|
|
| 2683 |
async function stripeEventSeen(id) {
|
| 2684 |
if (!id) return true;
|
| 2685 |
if (stripeEvents.has(id)) return true;
|
| 2686 |
-
let back = null;
|
| 2687 |
try {
|
| 2688 |
const v = await rtdb("stripe_events/" + id, "GET");
|
| 2689 |
-
|
|
|
|
|
|
|
|
|
|
| 2690 |
} catch { }
|
| 2691 |
-
|
| 2692 |
-
|
| 2693 |
-
|
| 2694 |
-
|
| 2695 |
-
|
| 2696 |
-
}
|
| 2697 |
stripeEvents.add(id);
|
| 2698 |
if (stripeEvents.size > 4000) {
|
| 2699 |
const first = stripeEvents.values().next().value;
|
| 2700 |
stripeEvents.delete(first);
|
| 2701 |
}
|
| 2702 |
-
|
|
|
|
|
|
|
| 2703 |
}
|
| 2704 |
|
| 2705 |
async function handleStripeWebhook(req, res) {
|
|
@@ -2714,22 +2760,36 @@ async function handleStripeWebhook(req, res) {
|
|
| 2714 |
return res.end();
|
| 2715 |
}
|
| 2716 |
const evId = String(ev.id || "");
|
| 2717 |
-
|
|
|
|
| 2718 |
return json(res, 200, { ok: true, dup: 1 });
|
| 2719 |
}
|
|
|
|
| 2720 |
if (ev.type === "checkout.session.completed" || ev.type === "checkout.session.async_payment_succeeded") {
|
| 2721 |
const id = ev.data && ev.data.object && ev.data.object.id;
|
| 2722 |
try {
|
| 2723 |
const ses = await stripeSession(id);
|
| 2724 |
-
if (ses) await fulfillPaid(ses);
|
| 2725 |
-
} catch { }
|
| 2726 |
} else if (ev.type === "checkout.session.expired") {
|
| 2727 |
const id = ev.data && ev.data.object && ev.data.object.id;
|
| 2728 |
if (/^cs_[A-Za-z0-9_]+$/.test(String(id || ""))) {
|
| 2729 |
-
try {
|
| 2730 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2731 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2732 |
}
|
|
|
|
| 2733 |
json(res, 200, { ok: true });
|
| 2734 |
}
|
| 2735 |
|
|
|
|
| 704 |
return idp.sub;
|
| 705 |
}
|
| 706 |
|
| 707 |
+
function payEmail(idp) {
|
| 708 |
+
const em = String(idp && idp.email || "").trim().toLowerCase();
|
| 709 |
+
if (!em || em.length > 200 || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(em)) return "";
|
| 710 |
+
return em;
|
| 711 |
+
}
|
| 712 |
+
|
| 713 |
function uidOk(uid) {
|
| 714 |
return typeof uid === "string" && /^[A-Za-z0-9_-]{8,128}$/.test(uid);
|
| 715 |
}
|
|
|
|
| 1166 |
return d;
|
| 1167 |
}
|
| 1168 |
|
| 1169 |
+
const LVL_PCT = { anonymous: 40, registered: 80, subscribed: 100 };
|
| 1170 |
const INDEX_WIN = 4 * MB;
|
| 1171 |
const TAIL_MAX = 2 * MB;
|
| 1172 |
const TAIL_MIN_SIZE = 48 * MB;
|
|
|
|
| 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 |
+
|
| 2453 |
async function handleVipStart(req, res) {
|
| 2454 |
if (!payGate(req, res)) return;
|
| 2455 |
const idp = await withTime(verifyIdToken(req.headers.authorization || ""), 4000, null);
|
|
|
|
| 2466 |
}
|
| 2467 |
const planKey = String(body.plan || "");
|
| 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);
|
| 2476 |
const homeMsg = "After pay you return to [DUB LK 2000 Home](" + HOME_URL + ")";
|
| 2477 |
const fields = {
|
| 2478 |
mode: "payment",
|
|
|
|
| 2494 |
"custom_text[after_submit][message]": "Site: [" + HOME_URL + "](" + HOME_URL + ")",
|
| 2495 |
"metadata[uid]": idp.sub,
|
| 2496 |
"metadata[plan]": planKey,
|
| 2497 |
+
"metadata[email]": email,
|
| 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",
|
|
|
|
| 2532 |
}
|
| 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 |
}
|
| 2540 |
|
|
|
|
| 2553 |
return json(res, 400, { error: "body" });
|
| 2554 |
}
|
| 2555 |
const d = await stripeSession(body.sid);
|
| 2556 |
+
const meta = d && d.metadata || {};
|
| 2557 |
+
const email = payEmail(idp);
|
| 2558 |
+
if (!d || !d.url || meta.uid !== idp.sub || d.status === "expired" || d.payment_status === "paid") return json(res, 400, { error: "Payment failed" });
|
| 2559 |
+
if (meta.email && (!email || String(meta.email) !== email)) return json(res, 400, { error: "Payment failed" });
|
| 2560 |
json(res, 200, { url: d.url });
|
| 2561 |
}
|
| 2562 |
|
|
|
|
| 2575 |
return json(res, 400, { error: "body" });
|
| 2576 |
}
|
| 2577 |
const d = await stripeSession(body.sid);
|
| 2578 |
+
const meta = d && d.metadata || {};
|
| 2579 |
+
const email = payEmail(idp);
|
| 2580 |
+
const sem = String(meta.email || "");
|
| 2581 |
+
if (!d || meta.uid !== idp.sub || d.client_reference_id !== idp.sub) {
|
| 2582 |
return json(res, 400, { error: "Payment failed", paid: false });
|
| 2583 |
}
|
| 2584 |
+
if (sem && (!email || sem !== email)) return json(res, 400, { error: "Payment failed", paid: false });
|
| 2585 |
+
if (d.payment_status !== "paid" || d.status !== "complete") return json(res, 402, { error: "unpaid", paid: false });
|
| 2586 |
+
let granted = false;
|
| 2587 |
+
try { granted = await fulfillPaid(d); } catch { granted = false; }
|
| 2588 |
lvlCache.delete(idp.sub);
|
| 2589 |
let sub = null;
|
| 2590 |
try { sub = await rtdb("users/" + idp.sub + "/subscription", "GET"); } catch { }
|
| 2591 |
+
if (!granted || !vipActive(sub)) return json(res, 402, { error: "unpaid", paid: false });
|
| 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;
|
| 2599 |
+
if (!plan || !uidOk(uid) || exp <= now) throw new Error("grant");
|
| 2600 |
+
let old = null;
|
| 2601 |
+
try { old = await rtdb("users/" + uid + "/subscription", "GET"); } catch { }
|
| 2602 |
+
const oldExp = vipExpirySec(old);
|
| 2603 |
+
if (old && vipActive(old) && oldExp >= exp) {
|
| 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);
|
|
|
|
| 2672 |
};
|
| 2673 |
let back = await readBack();
|
| 2674 |
if (!back) {
|
| 2675 |
+
let base = now;
|
| 2676 |
+
try {
|
| 2677 |
+
const old = await rtdb("users/" + uid + "/subscription", "GET");
|
| 2678 |
+
const oldExp = vipExpirySec(old);
|
| 2679 |
+
if (vipActive(old) && oldExp > base) base = oldExp;
|
| 2680 |
+
} catch { }
|
| 2681 |
+
const rec = { t: now, uid: uid, plan: planKey, exp: base + planSec };
|
| 2682 |
try {
|
| 2683 |
await rtdb("processed_orders/" + id, "PUT", rec);
|
| 2684 |
} catch { }
|
|
|
|
| 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();
|
| 2707 |
+
if (email && (email.length > 200 || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email))) return false;
|
| 2708 |
if (Number(ses.amount_total) !== plan.amount) return false;
|
| 2709 |
if (String(ses.currency || "").toLowerCase() !== "usd") return false;
|
| 2710 |
const sid = String(ses.id || "");
|
| 2711 |
if (!/^cs_[A-Za-z0-9_]+$/.test(sid)) return false;
|
| 2712 |
return withPayLock(sid, async () => {
|
| 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 |
await grantVip(uid, planKey, c.exp);
|
| 2717 |
await markOrder(sid, uid, planKey, c.exp);
|
| 2718 |
+
try {
|
| 2719 |
+
const open = await rtdb("pay_open/" + uid, "GET");
|
| 2720 |
+
if (open && open.sid === sid) await rtdb("pay_open/" + uid, "DELETE");
|
| 2721 |
+
} catch { }
|
| 2722 |
return true;
|
| 2723 |
});
|
| 2724 |
}
|
|
|
|
| 2726 |
async function stripeEventSeen(id) {
|
| 2727 |
if (!id) return true;
|
| 2728 |
if (stripeEvents.has(id)) return true;
|
|
|
|
| 2729 |
try {
|
| 2730 |
const v = await rtdb("stripe_events/" + id, "GET");
|
| 2731 |
+
if (v && v.ok === 1) {
|
| 2732 |
+
stripeEvents.add(id);
|
| 2733 |
+
return true;
|
| 2734 |
+
}
|
| 2735 |
} catch { }
|
| 2736 |
+
return false;
|
| 2737 |
+
}
|
| 2738 |
+
|
| 2739 |
+
async function stripeEventMark(id) {
|
| 2740 |
+
if (!id) return;
|
|
|
|
| 2741 |
stripeEvents.add(id);
|
| 2742 |
if (stripeEvents.size > 4000) {
|
| 2743 |
const first = stripeEvents.values().next().value;
|
| 2744 |
stripeEvents.delete(first);
|
| 2745 |
}
|
| 2746 |
+
try {
|
| 2747 |
+
await rtdb("stripe_events/" + id, "PUT", { t: Math.floor(Date.now() / 1000), ok: 1 });
|
| 2748 |
+
} catch { }
|
| 2749 |
}
|
| 2750 |
|
| 2751 |
async function handleStripeWebhook(req, res) {
|
|
|
|
| 2760 |
return res.end();
|
| 2761 |
}
|
| 2762 |
const evId = String(ev.id || "");
|
| 2763 |
+
const evOk = evId && /^evt_[A-Za-z0-9_]+$/.test(evId);
|
| 2764 |
+
if (evOk && await stripeEventSeen(evId)) {
|
| 2765 |
return json(res, 200, { ok: true, dup: 1 });
|
| 2766 |
}
|
| 2767 |
+
let did = false;
|
| 2768 |
if (ev.type === "checkout.session.completed" || ev.type === "checkout.session.async_payment_succeeded") {
|
| 2769 |
const id = ev.data && ev.data.object && ev.data.object.id;
|
| 2770 |
try {
|
| 2771 |
const ses = await stripeSession(id);
|
| 2772 |
+
if (ses) did = await fulfillPaid(ses);
|
| 2773 |
+
} catch { did = false; }
|
| 2774 |
} else if (ev.type === "checkout.session.expired") {
|
| 2775 |
const id = ev.data && ev.data.object && ev.data.object.id;
|
| 2776 |
if (/^cs_[A-Za-z0-9_]+$/.test(String(id || ""))) {
|
| 2777 |
+
try {
|
| 2778 |
+
const ses = await stripeSession(id);
|
| 2779 |
+
const uid = ses && ses.metadata && ses.metadata.uid;
|
| 2780 |
+
if (ses && ses.status === "expired" && uidOk(uid)) {
|
| 2781 |
+
const open = await rtdb("pay_open/" + uid, "GET");
|
| 2782 |
+
if (open && open.sid === id) await rtdb("pay_open/" + uid, "DELETE");
|
| 2783 |
+
}
|
| 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 |
}
|
| 2792 |
+
if (evOk && did) await stripeEventMark(evId);
|
| 2793 |
json(res, 200, { ok: true });
|
| 2794 |
}
|
| 2795 |
|