diff --git a/ads.js b/ads.js index a078f31..a9e2bde 100644 Binary files a/ads.js and b/ads.js differ diff --git a/server.js b/server.js index faa5a7b..204ccbc 100644 --- a/server.js +++ b/server.js @@ -2773,7 +2773,7 @@ const server = http.createServer(async (req, res) => { if (r.error) return gkPage(res, 400, 'Order problem', '

That order did not work

' + gkEsc(r.error) + '

Back to Gem Kingdom'); const c = r.claims; if (c.kind !== 'gk-buy' || !c.order || !(c.credits > 0) || !(c.crowns > 0)) return gkPage(res, 400, 'Order problem', '

That is not a Crown order

'); const done = gkOrders.get(c.order); - if (done && done.status === 'paid') { const rc = gkMint({ kind: 'gk-receipt', order: c.order, email: c.email, crowns: c.crowns, credits: c.credits, ref: done.campaignId }, 24 * 3600000); res.writeHead(302, { Location: c.ret + '?t=' + rc, 'Cache-Control': 'no-store' }); return res.end(); } + if (done && done.status === 'paid') { const rc = gkMint({ kind: 'gk-receipt', order: c.order, email: c.email, crowns: c.crowns, credits: c.credits, paid: done.paid != null ? done.paid : c.credits, ref: done.campaignId }, 24 * 3600000); res.writeHead(302, { Location: c.ret + '?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.gkshop', t, 7200), gkCookie('iap.return', 'gkshop', 1800)] }); return res.end(); } if (String(s.email).toLowerCase() !== String(c.email).toLowerCase()) return gkPage(res, 403, 'Different account', '

Different account

This order was started by the Gem Kingdom player ' + gkEsc(c.email) + ', but you are signed in to InstantAdPay as ' + gkEsc(s.email) + '. Sign in with the same account in both places.

Back to Gem Kingdom'); @@ -2814,9 +2814,9 @@ const server = http.createServer(async (req, res) => { const { ids } = await myMemberIds(s); const out = await ads.gamePurchase(s.email, ids, c.credits, 'Gem Kingdom: ' + c.crowns + ' Crowns', GK_URL()); if (out.error) { gkOrders.delete(c.order); if (out.error === 'short') { res.writeHead(302, { Location: '/gk/shop?t=' + encodeURIComponent(t) }); return res.end(); } return gkPage(res, 500, 'Order problem', '

Something went wrong

' + gkEsc(out.error) + '

'); } - gkOrders.set(c.order, { status: 'paid', email: s.email, campaignId: out.campaignId, ts: Date.now() }); - console.log('gemkingdom crowns', s.email, c.crowns, 'for', c.credits, 'credits, campaign', out.campaignId); - const rc = gkMint({ kind: 'gk-receipt', order: c.order, email: String(c.email).toLowerCase(), crowns: Number(c.crowns), credits: Number(c.credits), ref: out.campaignId }, 24 * 3600000); + gkOrders.set(c.order, { status: 'paid', email: s.email, campaignId: out.campaignId, paid: out.paid, ts: Date.now() }); + console.log('gemkingdom crowns', s.email, c.crowns, 'for', c.credits, 'credits (' + out.paid + ' purchased), campaign', out.campaignId); + const rc = gkMint({ kind: 'gk-receipt', order: c.order, email: String(c.email).toLowerCase(), crowns: Number(c.crowns), credits: Number(c.credits), paid: Number(out.paid), ref: out.campaignId }, 24 * 3600000); gkCallback(c.cb, rc); // the server copy; the redirect below is the fast path const ret = String(c.ret || '').startsWith(GK_URL() + '/') ? c.ret : GK_URL() + '/api/iap/receipt'; // a 200 page, not a 302: CSP form-action 'self' blocks a form POST from redirecting to another site (the page just sat there) @@ -2853,7 +2853,7 @@ const server = http.createServer(async (req, res) => { const c = r.claims; if (c.kind !== 'sd-buy' || !c.order || !c.item || !(Number(c.credits) > 0) || !c.email) return sdPage(res, 400, 'Order problem', '

That is not a costume order

Back to Spooky Doors'); const label = String(c.label || c.item).slice(0, 60); const done = sdOrders.get(c.order); - if (done && done.status === 'paid') { const rc = sdMint({ kind: 'sd-receipt', order: c.order, email: String(c.email).toLowerCase(), item: String(c.item), credits: Number(c.credits), ref: done.campaignId }, 24 * 3600000); const ret = sdIsGameUrl(c.ret) ? c.ret : SD_URL() + '/api/iap/receipt'; res.writeHead(302, { Location: ret + '?t=' + rc, 'Cache-Control': 'no-store' }); return res.end(); } + if (done && done.status === 'paid') { const rc = sdMint({ kind: 'sd-receipt', order: c.order, email: String(c.email).toLowerCase(), item: String(c.item), credits: Number(c.credits), paid: done.paid != null ? done.paid : Number(c.credits), ref: done.campaignId }, 24 * 3600000); const ret = sdIsGameUrl(c.ret) ? c.ret : SD_URL() + '/api/iap/receipt'; res.writeHead(302, { Location: ret + '?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.sdshop', t, 7200), gkCookie('iap.return', 'sdshop', 1800)] }); return res.end(); } if (String(s.email).toLowerCase() !== String(c.email).toLowerCase()) return sdPage(res, 403, 'Different account', '

Different account

This order was started by the Spooky Doors player ' + gkEsc(c.email) + ', but you are signed in to InstantAdPay as ' + gkEsc(s.email) + '. Sign in with the same account in both places.

Back to Spooky Doors'); @@ -2896,9 +2896,9 @@ const server = http.createServer(async (req, res) => { const { ids } = await myMemberIds(s); const out = await ads.gamePurchase(s.email, ids, Number(c.credits), 'Spooky Doors: ' + label, SD_URL()); if (out.error) { sdOrders.delete(c.order); if (out.error === 'short') { res.writeHead(302, { Location: '/sd/shop?t=' + encodeURIComponent(t) }); return res.end(); } return sdPage(res, 500, 'Order problem', '

Something went wrong

' + gkEsc(out.error) + '

'); } - sdOrders.set(c.order, { status: 'paid', email: s.email, campaignId: out.campaignId, ts: Date.now() }); + sdOrders.set(c.order, { status: 'paid', email: s.email, campaignId: out.campaignId, paid: out.paid, ts: Date.now() }); console.log('spookydoors costume', s.email, c.item, 'for', c.credits, 'credits, campaign', out.campaignId); - const rc = sdMint({ kind: 'sd-receipt', order: c.order, email: String(c.email).toLowerCase(), item: String(c.item), credits: Number(c.credits), ref: out.campaignId }, 24 * 3600000); + const rc = sdMint({ kind: 'sd-receipt', order: c.order, email: String(c.email).toLowerCase(), item: String(c.item), credits: Number(c.credits), paid: Number(out.paid), ref: out.campaignId }, 24 * 3600000); sdShopCallback(c.cb, rc); // the server copy; the redirect below is the fast path const ret = sdIsGameUrl(c.ret) ? c.ret : SD_URL() + '/api/iap/receipt'; // a 200 page, not a 302: CSP form-action 'self' blocks a form POST from redirecting to another site diff --git a/tools/patch_paid_credits.py b/tools/patch_paid_credits.py new file mode 100644 index 0000000..15ccd74 --- /dev/null +++ b/tools/patch_paid_credits.py @@ -0,0 +1,36 @@ +# Game pool guardrail (Marty, 2026-10-03): a game's POL pool must only grow from REAL money. gamePurchase now reports +# `paid` = the purchased part of the charge (earned viewing credits are spent first and are not money), and the Gem Kingdom +# and Spooky Doors receipts carry it so the games book poolSharePct of `paid`, never of earned credits. +ROOT = 'D:/Projects/HighRisk/onchain-ad-membership/site/' +def edit(path, pairs): + b = open(ROOT + path, 'rb').read(); crlf = b.count(b'\r\n') > b.count(b'\n') // 2 + s = b.decode('utf-8').replace('\r\n', '\n') + for a, r in pairs: + if s.count(a) != 1: raise SystemExit('%s anchor %d: %s' % (path, s.count(a), a[:70])) + s = s.replace(a, r) + if crlf: s = s.replace('\n', '\r\n') + open(ROOT + path, 'wb').write(s.encode('utf-8')); print('patched', path) + +edit('ads.js', [ + (" let memberId = best.memberId || (ids || [])[0] || 0;\n if (free < credits) {", + " let memberId = best.memberId || (ids || [])[0] || 0;\n" + " // the purchased part: from the shared pool, earned viewing credits go first (spendEarned keeps purchase_grade longest);\n" + " // from a position's balance it is all purchased. Games fund their POL pools from this, never from earned credits.\n" + " let paid = credits - Math.min(Math.max(0, bal.earned || 0), credits);\n" + " if (free < credits) { paid = credits;"), + (" return { ok: true, campaignId: c.id, charged: credits };\n}\n// ---- admin ----", + " return { ok: true, campaignId: c.id, charged: credits, paid: Math.max(0, Math.min(credits, paid)) };\n}\n// ---- admin ----"), +]) +edit('server.js', [ + # Gem Kingdom: remember paid, put it on both receipts + ("if (done && done.status === 'paid') { const rc = gkMint({ kind: 'gk-receipt', order: c.order, email: c.email, crowns: c.crowns, credits: c.credits, ref: done.campaignId }, 24 * 3600000);", + "if (done && done.status === 'paid') { const rc = gkMint({ kind: 'gk-receipt', order: c.order, email: c.email, crowns: c.crowns, credits: c.credits, paid: done.paid != null ? done.paid : c.credits, ref: done.campaignId }, 24 * 3600000);"), + (" gkOrders.set(c.order, { status: 'paid', email: s.email, campaignId: out.campaignId, ts: Date.now() });\n console.log('gemkingdom crowns', s.email, c.crowns, 'for', c.credits, 'credits, campaign', out.campaignId);\n const rc = gkMint({ kind: 'gk-receipt', order: c.order, email: String(c.email).toLowerCase(), crowns: Number(c.crowns), credits: Number(c.credits), ref: out.campaignId }, 24 * 3600000);", + " gkOrders.set(c.order, { status: 'paid', email: s.email, campaignId: out.campaignId, paid: out.paid, ts: Date.now() });\n console.log('gemkingdom crowns', s.email, c.crowns, 'for', c.credits, 'credits (' + out.paid + ' purchased), campaign', out.campaignId);\n const rc = gkMint({ kind: 'gk-receipt', order: c.order, email: String(c.email).toLowerCase(), crowns: Number(c.crowns), credits: Number(c.credits), paid: Number(out.paid), ref: out.campaignId }, 24 * 3600000);"), + # Spooky Doors: same + ("const rc = sdMint({ kind: 'sd-receipt', order: c.order, email: String(c.email).toLowerCase(), item: String(c.item), credits: Number(c.credits), ref: done.campaignId }, 24 * 3600000);", + "const rc = sdMint({ kind: 'sd-receipt', order: c.order, email: String(c.email).toLowerCase(), item: String(c.item), credits: Number(c.credits), paid: done.paid != null ? done.paid : Number(c.credits), ref: done.campaignId }, 24 * 3600000);"), + (" const rc = sdMint({ kind: 'sd-receipt', order: c.order, email: String(c.email).toLowerCase(), item: String(c.item), credits: Number(c.credits), ref: out.campaignId }, 24 * 3600000);", + " const rc = sdMint({ kind: 'sd-receipt', order: c.order, email: String(c.email).toLowerCase(), item: String(c.item), credits: Number(c.credits), paid: Number(out.paid), ref: out.campaignId }, 24 * 3600000);"), +]) +print('ok')