ed6febe0b8
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
121 lines
8.9 KiB
JavaScript
121 lines
8.9 KiB
JavaScript
// BixPay card integration, phase 1 (Marty approved 2026-10-29; spec docs/BIXPAY-INTEGRATION.md).
|
|
// BIX is a separate company: a self-custody wallet with a Visa card spent from USDT/USDC. We only carry each member's
|
|
// PARTNER link (bix.ibportal.io/auth/register?e=<token>&a=1) so anyone who gets a card through their pages is credited to
|
|
// them in the BIX portal. Nothing is settled on our side: the portal tracks directs and levels itself.
|
|
//
|
|
// One identity, sponsor follows (feedback-one-identity-sponsor-follows): the link a visitor gets is the member's own,
|
|
// else the first sponsor up the chain (10 levels) with one, else the company link (site config bixCompanyLink). Every
|
|
// surface says WHOSE link is in play ("you" / "@sponsor" / "network") so members add their own and sponsors get their
|
|
// teams signed up.
|
|
//
|
|
// Everything is gated by site config bixpayLive ('0' = nothing shows; the admin flips it in Admin > Settings).
|
|
// Clicks: every surface links through /go/bixpay?s=<surface>[&m=<member>] which counts here (DATA_DIR/bixpay-clicks.json)
|
|
// and 302s to the resolved partner link. Counts per surface land in the admin Traffic tab, per member on the Promo tab.
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
let R = {}; // { dataDir, accounts, siteConfig, refAccount }
|
|
const HOST = 'bix.ibportal.io';
|
|
const PATH = '/auth/register';
|
|
const MAX_LEN = 300;
|
|
const MAX_LEVELS = 10;
|
|
// Marty's own PARTNER link (IB12143793): the company fallback until Admin > Settings bixCompanyLink says otherwise
|
|
const DEFAULT_COMPANY_LINK = 'https://bix.ibportal.io/auth/register?e=XqwU9T1aJ5N0UJ0yr9x0TGUiSQTsVCkTM01MIovVNnc&a=1';
|
|
const SURFACES = ['wallet', 'overview', 'email', 'nudge', 'promo', 'guide', 'chat'];
|
|
const CUSTOMER_LINK_MSG = "That's the card-customer link. Paste the Partner link from your BIX portal profile instead.";
|
|
const ANNOUNCE_MS = 7 * 86400000; // the Overview announcement card shows for the first week after the switch is flipped
|
|
|
|
function init(opts) { R = opts || {}; }
|
|
const cfg = () => { try { return (R.siteConfig && R.siteConfig()) || {}; } catch (e) { return {}; } };
|
|
function live() { return String(cfg().bixpayLive == null ? '0' : cfg().bixpayLive) === '1'; }
|
|
function companyLink() { const v = validate(cfg().bixCompanyLink); return v.ok ? v.link : DEFAULT_COMPANY_LINK; }
|
|
// the Overview announcement runs a week from bixpayLiveSince (stamped by the admin settings route when the switch is flipped on)
|
|
function announcing() { if (!live()) return false; const t = Date.parse(String(cfg().bixpayLiveSince || '')); return !(t > 0) || Date.now() - t < ANNOUNCE_MS; }
|
|
|
|
// A pasted partner link -> { ok: true, link } (normalised to e= and a=1 only) or { error }.
|
|
// The card-customer link carries appId=3 (it makes a card customer, not a partner): refused with the message members see.
|
|
function validate(input) {
|
|
const s = String(input == null ? '' : input).trim();
|
|
if (!s) return { error: 'Paste your BixPay partner link first.' };
|
|
if (s.length > MAX_LEN) return { error: 'That link is too long. Copy the Partner link from your BIX portal profile.' };
|
|
let u; try { u = new URL(s); } catch (e) { return { error: 'That is not a full link. It starts with https://bix.ibportal.io/auth/register?e=' }; }
|
|
if (u.protocol !== 'https:' || u.hostname.toLowerCase() !== HOST) return { error: 'A BixPay partner link lives on https://bix.ibportal.io. Copy it from your BIX portal profile (Profile > Partner).' };
|
|
if (u.pathname.replace(/\/+$/, '') !== PATH) return { error: 'That is not the register link. It looks like https://bix.ibportal.io/auth/register?e=...&a=1' };
|
|
if (u.searchParams.has('appId') || u.searchParams.has('appid')) return { error: CUSTOMER_LINK_MSG };
|
|
const e = String(u.searchParams.get('e') || '').trim();
|
|
if (!/^[A-Za-z0-9_-]{16,128}$/.test(e)) return { error: 'The link is missing its e= token. Copy the whole Partner link from your BIX portal profile.' };
|
|
if (String(u.searchParams.get('a') || '').trim() !== '1') return { error: 'The link needs a=1 on the end. Copy the whole Partner link from your BIX portal profile.' };
|
|
return { ok: true, link: 'https://' + HOST + PATH + '?e=' + e + '&a=1' };
|
|
}
|
|
|
|
const nameOf = a => a ? (a.username ? '@' + a.username : a.memberId ? 'member #' + a.memberId : 'your sponsor') : null;
|
|
// bixLinkFor(member): own link, else the first sponsor up the chain with one (10 levels), else the company link.
|
|
// -> { link, whose: 'you' | 'sponsor' | 'network', name, email } (name/email = the sponsor whose link it is)
|
|
async function resolve(acct) {
|
|
if (acct && validate(acct.bixPartnerLink).ok) return { link: validate(acct.bixPartnerLink).link, whose: 'you', name: nameOf(acct), email: acct.email };
|
|
if (acct && R.accounts && R.accounts.sponsorOf) {
|
|
const seen = new Set([String(acct.email || '').toLowerCase()]);
|
|
let cur = acct;
|
|
for (let i = 0; i < MAX_LEVELS && cur; i++) {
|
|
let s = null; try { s = await R.accounts.sponsorOf(cur.email); } catch (e) { s = null; }
|
|
if (!s || seen.has(String(s.email).toLowerCase())) break;
|
|
seen.add(String(s.email).toLowerCase());
|
|
const v = validate(s.bixPartnerLink);
|
|
if (v.ok) return { link: v.link, whose: 'sponsor', name: nameOf(s), email: s.email };
|
|
cur = s;
|
|
}
|
|
}
|
|
return { link: companyLink(), whose: 'network', name: null, email: null };
|
|
}
|
|
// the same for a public page: the referrer token (?ref / the sponsor cookie) resolves like the member it names
|
|
async function resolveRef(tok) {
|
|
let a = null; try { a = tok && R.refAccount ? await R.refAccount(tok) : null; } catch (e) { a = null; }
|
|
return resolve(a);
|
|
}
|
|
// the line every surface shows under the button
|
|
function usingLine(r) { return r.whose === 'you' ? 'Using: your link.' : r.whose === 'sponsor' ? 'Using: ' + r.name + "'s link. Add yours in Profile." : 'Using: the network link. Add yours in Profile.'; }
|
|
|
|
// tracked link for a surface (+ the member it belongs to: the sharer on promo/guide/email, the viewer on the dashboard cards)
|
|
const SITE = () => String(process.env.PUBLIC_URL || 'https://instantadpay.com').replace(/\/+$/, '');
|
|
function goLink(surface, token) { return SITE() + '/go/bixpay?s=' + encodeURIComponent(surface) + (token ? '&m=' + encodeURIComponent(String(token)) : ''); }
|
|
// one sentence for payout emails and the games' wallet nudges (text), only while live
|
|
function emailLine(token) { return live() ? 'Want to spend it? A BixPay Visa card turns POL into everyday purchases: ' + goLink('email', token) : ''; }
|
|
function nudgeLine(token) { return live() ? 'Want to spend it? A BixPay Visa card turns POL into everyday purchases: ' + goLink('nudge', token) : ''; }
|
|
|
|
// ---- clicks ----
|
|
const FILE = () => path.join(R.dataDir || '.', 'bixpay-clicks.json');
|
|
const blank = () => ({ v: 1, total: {}, days: {}, members: {} });
|
|
let S = null;
|
|
function load() { if (S) return S; try { S = Object.assign(blank(), JSON.parse(fs.readFileSync(FILE(), 'utf8'))); } catch (e) { S = blank(); } return S; }
|
|
function save() { try { fs.writeFileSync(FILE(), JSON.stringify(S)); } catch (e) {} }
|
|
const day = ts => new Date(ts).toISOString().slice(0, 10);
|
|
function click(surface, email) {
|
|
const s = SURFACES.includes(surface) ? surface : 'other';
|
|
const st = load();
|
|
st.total[s] = (st.total[s] || 0) + 1;
|
|
const d = day(Date.now()); st.days[d] = st.days[d] || {}; st.days[d][s] = (st.days[d][s] || 0) + 1;
|
|
const e = String(email || '').trim().toLowerCase();
|
|
if (e) { const m = st.members[e] = st.members[e] || { total: 0 }; m.total++; m[s] = (m[s] || 0) + 1; m.last = Date.now(); }
|
|
// bounded: 400 days
|
|
const cutoff = day(Date.now() - 400 * 86400000); for (const k of Object.keys(st.days)) if (k < cutoff) delete st.days[k];
|
|
save();
|
|
return { surface: s, email: e || null };
|
|
}
|
|
function memberClicks(email) {
|
|
const m = load().members[String(email || '').trim().toLowerCase()];
|
|
if (!m) return { total: 0 };
|
|
return Object.assign({}, m);
|
|
}
|
|
function adminView(days) {
|
|
const st = load(); const n = Math.min(365, Math.max(1, Number(days) || 30));
|
|
const since = day(Date.now() - n * 86400000);
|
|
const bySurface = {}; let total = 0;
|
|
for (const [d, row] of Object.entries(st.days)) if (d >= since) for (const [s, c] of Object.entries(row)) { bySurface[s] = (bySurface[s] || 0) + c; total += c; }
|
|
const daily = Object.entries(st.days).filter(([d]) => d >= since).sort((a, b) => a[0] < b[0] ? 1 : -1).map(([d, row]) => ({ day: d, clicks: Object.values(row).reduce((x, y) => x + y, 0) }));
|
|
const members = Object.entries(st.members).map(([email, m]) => ({ email, total: m.total || 0, last: m.last || 0, promo: m.promo || 0 })).sort((a, b) => b.total - a.total).slice(0, 50);
|
|
return { days: n, total, allTime: Object.values(st.total).reduce((x, y) => x + y, 0), bySurface, daily, members, surfaces: SURFACES };
|
|
}
|
|
function _reset() { S = null; }
|
|
|
|
module.exports = { init, live, announcing, validate, resolve, resolveRef, usingLine, companyLink, goLink, emailLine, nudgeLine, click, memberClicks, adminView, SURFACES, CUSTOMER_LINK_MSG, DEFAULT_COMPANY_LINK, MAX_LEVELS, _reset };
|