PolyWarriors paid mints: /pw/mint checkout, 100 credits from package credits only (earned never counts)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-10-05 07:56:44 -05:00
parent d1a638b66b
commit 18813a4127
4 changed files with 70 additions and 2 deletions
BIN
View File
Binary file not shown.
+2
View File
@@ -844,6 +844,8 @@
if (me.returnTo === 'gemkingdom' && !render.toGame) { render.toGame = true; location.href = '/api/my/gemkingdom'; return; } // Gem Kingdom sent them here to sign in (2026-10-01)
if (me.returnTo === 'gkshop' && !render.toGame) { render.toGame = true; location.href = '/gk/shop/resume'; return; }
if (me.returnTo === 'spookydoors' && !render.toGame) { render.toGame = true; location.href = '/api/my/spookydoors'; return; } // Spooky Doors sent them here to sign in (2026-10-02)
if (me.returnTo === 'pwmint' && !render.toGame) { render.toGame = true; location.href = '/pw/mint/resume'; return; } // PolyWarriors paid mint (2026-10-05)
if (me.pwMint && !me.gkShop && !me.sdShop && !document.getElementById('pwBack')) { const d = document.createElement('a'); d.id = 'pwBack'; d.href = '/pw/mint/resume'; d.textContent = 'Finish minting your PolyWarriors fighter →'; d.setAttribute('style', 'position:fixed;left:12px;right:12px;bottom:14px;z-index:9999;display:block;padding:14px 16px;border-radius:14px;background:linear-gradient(180deg,#22E5FF,#7A2CFF);color:#fff;font-weight:800;text-align:center;text-decoration:none;box-shadow:0 10px 30px rgba(0,0,0,.45)'); document.body.appendChild(d); }
if (me.returnTo === 'sdshop' && !render.toGame) { render.toGame = true; location.href = '/sd/shop/resume'; return; }
if (me.sdShop && !document.getElementById('sdBack')) { const d = document.createElement('a'); d.id = 'sdBack'; d.href = '/sd/shop/resume'; d.textContent = 'Finish your Spooky Doors costume order \u2192'; d.setAttribute('style', 'position:fixed;left:12px;right:12px;bottom:' + (me.gkShop ? '74px' : '14px') + ';z-index:9999;display:block;padding:14px 16px;border-radius:14px;background:#FF8A1F;color:#2a1200;font-weight:800;text-align:center;text-decoration:none;box-shadow:0 5px 0 #C25A06,0 10px 30px rgba(0,0,0,.45)'); document.body.appendChild(d); }
if (me.gkShop && !document.getElementById('gkBack')) { const d = document.createElement('a'); d.id = 'gkBack'; d.href = '/gk/shop/resume'; d.textContent = 'Finish your Gem Kingdom Crowns order \u2192'; d.setAttribute('style', 'position:fixed;left:12px;right:12px;bottom:14px;z-index:9999;display:block;padding:14px 16px;border-radius:14px;background:linear-gradient(180deg,#E8455A,#A8172B);color:#fff;font-weight:800;text-align:center;text-decoration:none;box-shadow:0 10px 30px rgba(0,0,0,.45)'); document.body.appendChild(d); }
+1 -1
View File
@@ -1131,7 +1131,7 @@
<script src="/assets/common.js?v=20261003fr2"></script><script src="/assets/welcome.js?v=20260923b"></script>
<script src="/assets/wallet.js?v=20260911a"></script>
<script src="/assets/promo.js?v=20261002ref"></script>
<script src="/assets/my.js?v=20261005st"></script>
<script src="/assets/my.js?v=20261005pm"></script>
<script src="/assets/chat.js?v=20260907l"></script>
</body>
</html>
+67 -1
View File
@@ -1336,6 +1336,25 @@ async function swCallback(cbUrl, receipt) {
if (!swIsGameUrl(cbUrl)) return false;
try { const r = await fetch(cbUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ t: receipt }), signal: AbortSignal.timeout(8000) }); return r.ok; } catch (e) { console.warn('spellwheel callback', e.message); return false; }
}
// -- PolyWarriors paid mints (2026-10-05): the first mint is free in the game; later mints cost PW_MINT_CREDITS (100) from
// PURCHASED credits only (package credits on a position; earned credits never count, Marty's rule). The game signs an order
// with PW_SSO_SECRET (kind 'pw-mint'), the member confirms here, ads.gamePurchase(purchasedOnly) charges, and a signed
// receipt goes back to the game twice (server callback + redirect), whichever lands first. The game mints on the receipt.
function pwOpen(token) {
if (PW_SECRET().length < 32) return { error: 'PolyWarriors is not connected yet.' };
const [p, sig] = String(token || '').split('.'); if (!p || !sig) return { error: 'That link is not valid.' };
let payload; try { payload = Buffer.from(String(p).replace(/-/g, '+').replace(/_/g, '/'), 'base64').toString('utf8'); } catch (e) { return { error: 'That link is not valid.' }; }
const want = pwSign(payload); if (want.length !== sig.length || !crypto.timingSafeEqual(Buffer.from(want), Buffer.from(sig))) return { error: 'That link is not valid.' };
let c; try { c = JSON.parse(payload); } catch (e) { return { error: 'That link is not valid.' }; }
if (!c.exp || Date.now() > c.exp) return { error: 'That mint order has expired. Tap Mint in PolyWarriors again.' };
return { ok: true, claims: c };
}
const pwOrders = persistedMap('pworders'); // order -> { status: 'paying'|'paid', email, campaignId, ts }
const pwIsGameUrl = x => [PW_URL()].concat(PW_ALT()).some(o => String(x || '').startsWith(o + '/'));
async function pwCallback(cbUrl, receipt) {
if (!pwIsGameUrl(cbUrl)) return false;
try { const r = await fetch(cbUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ t: receipt }), signal: AbortSignal.timeout(8000) }); return r.ok; } catch (e) { console.warn('polywarriors callback', e.message); return false; }
}
async function gkCallback(cbUrl, receipt) {
if (!String(cbUrl || '').startsWith(GK_URL() + '/')) return false;
try { const r = await fetch(cbUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ t: receipt }), signal: AbortSignal.timeout(8000) }); return r.ok; } catch (e) { console.warn('gemkingdom callback', e.message); return false; }
@@ -2098,7 +2117,7 @@ const server = http.createServer(async (req, res) => {
const _defSpon = Number(siteConfig().defaultSponsorId) || 1;
if (!sponsorId && memberId !== _defSpon) sponsorId = _defSpon; // orphan fallback → #1 (never self-sponsor)
if (memberId && acct && acct.memberId !== memberId) accounts.setMemberId(acct.email, memberId).catch(() => {});
const out = { signedIn: true, gkShop: !!parseCookies(req)['iap.gkshop'], sdShop: !!parseCookies(req)['iap.sdshop'], returnTo: ['polhunter', 'driprunner', 'passivedrip', 'gemkingdom', 'gkshop', 'spookydoors', 'sdshop'].includes(parseCookies(req)['iap.return']) ? parseCookies(req)['iap.return'] : null, sponsorBlocked, sponsorRouted, sponsorName: spdMe.name || null, email: s.email || (acct && acct.email) || null,
const out = { signedIn: true, gkShop: !!parseCookies(req)['iap.gkshop'], sdShop: !!parseCookies(req)['iap.sdshop'], pwMint: !!parseCookies(req)['iap.pwmint'], returnTo: ['polhunter', 'driprunner', 'passivedrip', 'gemkingdom', 'gkshop', 'spookydoors', 'sdshop', 'pwmint'].includes(parseCookies(req)['iap.return']) ? parseCookies(req)['iap.return'] : null, sponsorBlocked, sponsorRouted, sponsorName: spdMe.name || null, email: s.email || (acct && acct.email) || null,
address: s.address || (acct && acct.address) || null, memberId,
username: (acct && acct.username) || null,
refCode: (acct && acct.code) || null, sponsorId,
@@ -2907,6 +2926,53 @@ const server = http.createServer(async (req, res) => {
+ '<a class="btn" href="/gk/shop/topup?t=' + encodeURIComponent(t) + '">Buy a credit package</a><a class="btn blue" href="' + GK_URL() + '/play?open=shop">Pick a smaller pack</a>'
+ '<p class="fine">A $5 package comes with 500 credits. After you buy, a banner on your dashboard brings you straight back to finish this order.</p>');
}
// ---- PolyWarriors paid mint checkout
if (p === '/pw/mint' && req.method === 'GET') {
const t = u.searchParams.get('t') || ''; const r = pwOpen(t);
if (r.error) return gkPage(res, 400, 'Mint problem', '<h1>That mint did not work</h1><p class="sub">' + gkEsc(r.error) + '</p><a class="btn blue" href="' + PW_URL() + '/play">Back to PolyWarriors</a>');
const c = r.claims; if (c.kind !== 'pw-mint' || !c.order || !(c.credits > 0) || !(c.serial > 0)) return gkPage(res, 400, 'Mint problem', '<h1>That is not a mint order</h1>');
const done = pwOrders.get(c.order);
if (done && done.status === 'paid') { const rc = pwMint({ kind: 'pw-mint-receipt', order: c.order, email: c.email, serial: c.serial, credits: c.credits, paid: c.credits, ref: done.campaignId }, 24 * 3600000); res.writeHead(302, { Location: (pwIsGameUrl(c.ret) ? c.ret : PW_URL() + '/api/iap/mint-receipt') + '?t=' + rc, 'Cache-Control': 'no-store' }); return res.end(); }
const s = await auth.fromRequest(req);
if (!s || !s.email) { res.writeHead(302, { Location: '/my', 'Cache-Control': 'no-store', 'Set-Cookie': [gkCookie('iap.pwmint', t, 7200), gkCookie('iap.return', 'pwmint', 1800)] }); return res.end(); }
if (String(s.email).toLowerCase() !== String(c.email).toLowerCase()) return gkPage(res, 403, 'Different account', '<h1>Different account</h1><p class="sub">This mint was started by the PolyWarriors player ' + gkEsc(c.email) + ', but you are signed in to InstantAdPay as ' + gkEsc(s.email) + '. Sign in with the same account and tap Mint again.</p>');
const { ids } = await myMemberIds(s); const bal = await ads.balances(ids, s.email);
const best = (bal.per || []).slice().sort((a, b) => b.avail - a.avail)[0] || { avail: 0 };
const head = '<p class="sub">PolyWarriors &middot; Mint to your wallet</p><h1>' + gkEsc(c.label || ('Warrior #' + c.serial)) + '</h1><div class="big">' + Number(c.credits) + ' credits</div>'
+ '<div class="row"><span>Paid from</span><span>credits from your packages</span></div>'
+ '<div class="row"><span>Your package credits</span><span>' + Number(best.avail || 0).toLocaleString('en-US') + '</span></div>';
if (best.avail >= c.credits) {
const csrf = pwSign('csrf:' + c.order + ':' + String(s.email).toLowerCase());
return gkPage(res, 200, 'Confirm your mint', head + '<form method="post" action="/pw/mint/confirm"><input type="hidden" name="t" value="' + gkEsc(t) + '"><input type="hidden" name="csrf" value="' + csrf + '"><button class="btn" type="submit">Spend ' + Number(c.credits) + ' credits and mint</button></form>'
+ '<a class="alt" href="' + PW_URL() + '/play">Cancel and go back</a><p class="fine">Minting puts this warrior on Polygon in the wallet on your InstantAdPay account. Earned credits cannot be used for mints; credits from your packages can. The game pays the network fee.</p>');
}
return gkPage(res, 200, 'Package credits needed', head + '<p class="warn">Mints after your first free one are paid with credits from your packages, and you need ' + Math.max(0, c.credits - best.avail).toLocaleString('en-US') + ' more. Earned credits do not count for mints.</p>'
+ '<a class="btn" href="/pw/mint/topup?t=' + encodeURIComponent(t) + '">Buy a credit package</a><a class="btn blue" href="' + PW_URL() + '/play">Back to PolyWarriors</a>'
+ '<p class="fine">A $5 package comes with 500 credits. After you buy, a banner on your dashboard brings you back to finish this mint.</p>');
}
if (p === '/pw/mint/topup' && req.method === 'GET') { const t = u.searchParams.get('t') || ''; if (pwOpen(t).error) { res.writeHead(302, { Location: '/my#buy' }); return res.end(); } res.writeHead(302, { Location: '/my#buy', 'Cache-Control': 'no-store', 'Set-Cookie': gkCookie('iap.pwmint', t, 7200) }); return res.end(); }
if (p === '/pw/mint/resume' && req.method === 'GET') { const t = parseCookies(req)['iap.pwmint']; res.writeHead(302, { Location: t ? '/pw/mint?t=' + encodeURIComponent(t) : PW_URL() + '/play', 'Cache-Control': 'no-store', 'Set-Cookie': gkCookie('iap.return', '', 0) }); return res.end(); }
if (p === '/pw/mint/confirm' && req.method === 'POST') {
const f = await gkFormBody(req); const t = f.get('t') || ''; const r = pwOpen(t);
if (r.error) return gkPage(res, 400, 'Mint problem', '<h1>That mint did not work</h1><p class="sub">' + gkEsc(r.error) + '</p>');
const c = r.claims; const s = await auth.fromRequest(req);
if (!s || !s.email || String(s.email).toLowerCase() !== String(c.email).toLowerCase()) return gkPage(res, 403, 'Sign in', '<h1>Please sign in again</h1><a class="btn blue" href="/pw/mint?t=' + encodeURIComponent(t) + '">Continue</a>');
const want = pwSign('csrf:' + c.order + ':' + String(s.email).toLowerCase()); const got = String(f.get('csrf') || '');
if (want.length !== got.length || !crypto.timingSafeEqual(Buffer.from(want), Buffer.from(got))) return gkPage(res, 403, 'Mint problem', '<h1>That form expired</h1><a class="btn blue" href="/pw/mint?t=' + encodeURIComponent(t) + '">Try again</a>');
const prev = pwOrders.get(c.order);
if (prev && (prev.status === 'paid' || (prev.status === 'paying' && Date.now() - prev.ts < 60000))) { res.writeHead(302, { Location: '/pw/mint?t=' + encodeURIComponent(t) }); return res.end(); }
pwOrders.set(c.order, { status: 'paying', email: s.email, ts: Date.now() });
const { ids } = await myMemberIds(s);
const out = await ads.gamePurchase(s.email, ids, Number(c.credits), 'PolyWarriors: mint warrior #' + c.serial, PW_URL(), { purchasedOnly: true });
if (out.error) { pwOrders.delete(c.order); if (out.error === 'short') { res.writeHead(302, { Location: '/pw/mint?t=' + encodeURIComponent(t) }); return res.end(); } return gkPage(res, 500, 'Mint problem', '<h1>Something went wrong</h1><p class="sub">' + gkEsc(out.error) + '</p>'); }
pwOrders.set(c.order, { status: 'paid', email: s.email, campaignId: out.campaignId, ts: Date.now() });
console.log('polywarriors mint fee', s.email, 'serial', c.serial, c.credits, 'purchased credits, campaign', out.campaignId);
const rc = pwMint({ kind: 'pw-mint-receipt', order: c.order, email: String(c.email).toLowerCase(), serial: Number(c.serial), credits: Number(c.credits), paid: Number(out.paid), ref: out.campaignId }, 24 * 3600000);
pwCallback(c.cb, rc);
const back = gkEsc((pwIsGameUrl(c.ret) ? c.ret : PW_URL() + '/api/iap/mint-receipt') + '?t=' + rc);
res.setHeader('Set-Cookie', gkCookie('iap.pwmint', '', 0));
return gkPage(res, 200, 'Paid', '<meta http-equiv="refresh" content="1;url=' + back + '"><p class="sub">PolyWarriors &middot; Mint</p><h1>Paid!</h1><div class="big">Minting #' + Number(c.serial) + '</div><p class="sub">Taking you back to PolyWarriors...</p><a class="btn" href="' + back + '">Back to PolyWarriors</a>');
}
if (p === '/gk/shop/topup' && req.method === 'GET') { // the detour to Buy packages, remembered for the way back
const t = u.searchParams.get('t') || ''; if (gkOpen(t).error) { res.writeHead(302, { Location: '/my#buy' }); return res.end(); }
res.writeHead(302, { Location: '/my#buy', 'Cache-Control': 'no-store', 'Set-Cookie': gkCookie('iap.gkshop', t, 7200) }); return res.end();