feat(ads): rewarded video (VK) — client-attested credit + daily/hourly caps
CI / changes (pull_request) Successful in 4s
CI / unit (pull_request) Successful in 10s
CI / integration (pull_request) Successful in 26s
CI / ui (pull_request) Successful in 1m11s
CI / conformance (pull_request) Successful in 10s
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 1m53s
CI / changes (pull_request) Successful in 4s
CI / unit (pull_request) Successful in 10s
CI / integration (pull_request) Successful in 26s
CI / ui (pull_request) Successful in 1m11s
CI / conformance (pull_request) Successful in 10s
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 1m53s
The first ads slice: a voluntary rewarded video credits chips. VK Mini App ads (VKWebAppShowNativeAds) expose only a client-side watch result — no server-to-server verify — so the credit is client-attested, guarded by a server daily + hourly cap (config reward_daily_cap / reward_hourly_cap, default 50 / 10). The caps are both anti-abuse (bounding a forger who skips the ad and calls the endpoint directly) and an economic conversion lever (limiting free chips so a player who wants more buys). D29 amended to VK's reality. Backend: CreditReward (VK-only, order-less, idempotent on a client nonce, floored by the caps; payout from config rewarded_payout_chips, default 0 = off) + the wallet.reward edge op returning the updated wallet (reward_chips gates the "watch for chips" CTA). Additive migration (two config columns). Client: the ads-network abstraction (lib/ads.ts, VK impl) + the VK bridge (vkRewardedReady / vkShowRewarded) + the Wallet CTA + i18n. A contour test stub (VITE_ADS_STUB -> a toast instead of a real ad; prod always real) and a temporary diagnostic that logs the raw VK data, so we confirm on the contour exactly what VK returns (harden to signature-verify if it carries one). Tests: backend integration (credit, nonce idempotency, hourly cap, disabled, non-VK refusal) + codec unit (reward wire). Docs: PAYMENTS(+ru) §10, D29 amend, PLAN E6. Bundle: shared budget 30->31 (reward i18n strings).
This commit is contained in:
@@ -10,6 +10,8 @@
|
||||
import { onExternalLinkClick, openExternalUrl } from '../lib/links';
|
||||
import { vkPlatform, vkShowOrderBox } from '../lib/vk';
|
||||
import { telegramOpenInvoice } from '../lib/telegram';
|
||||
import { rewardedReady, showRewarded } from '../lib/ads';
|
||||
import { GatewayError } from '../lib/client';
|
||||
import type { Wallet, Catalog, CatalogProduct } from '../lib/model';
|
||||
|
||||
// The Wallet section: the context-visible chip balances, the active benefits, and the storefront.
|
||||
@@ -41,6 +43,41 @@
|
||||
const values = $derived(catalog?.products.filter((p) => p.kind === 'value') ?? []);
|
||||
const packs = $derived(catalog?.products.filter((p) => p.kind === 'pack') ?? []);
|
||||
|
||||
// Rewarded video (VK ads): the "watch for chips" CTA shows only when the server offers a payout in
|
||||
// this context (wallet.rewardChips > 0 — VK, configured) and an ad is ready to show.
|
||||
let rewardReady = $state(false);
|
||||
const canReward = $derived(!!wallet && wallet.rewardChips > 0 && rewardReady);
|
||||
|
||||
// onWatchAd shows a rewarded ad and, if watched, credits the payout server-side (client-attested +
|
||||
// a server daily/hourly cap). A stub view (contour test) shows an "ad fired" marker; a reached cap
|
||||
// is surfaced as its own toast, distinct from a generic error.
|
||||
async function onWatchAd() {
|
||||
if (busy) return;
|
||||
busyId = 'reward';
|
||||
try {
|
||||
const res = await showRewarded();
|
||||
if (res.stub) showToast('ad fired');
|
||||
if (!res.watched) {
|
||||
showToast(t('wallet.rewardFailed'));
|
||||
return;
|
||||
}
|
||||
const nonce = crypto.randomUUID();
|
||||
const before = wallet?.segments.find((s) => s.source === 'vk')?.chips ?? 0;
|
||||
const w = await gateway.walletReward(nonce, res.data);
|
||||
wallet = w;
|
||||
const gained = (w.segments.find((s) => s.source === 'vk')?.chips ?? 0) - before;
|
||||
showToast(gained > 0 ? t('wallet.rewardCredited', { n: gained }) : t('wallet.rewardCredited', { n: w.rewardChips }));
|
||||
} catch (e) {
|
||||
if (e instanceof GatewayError && e.code === 'reward_capped') {
|
||||
showToast(t('wallet.rewardCapped'));
|
||||
} else {
|
||||
handleError(e);
|
||||
}
|
||||
} finally {
|
||||
busyId = null;
|
||||
}
|
||||
}
|
||||
|
||||
async function load() {
|
||||
try {
|
||||
const [w, c] = await Promise.all([gateway.wallet(), gateway.catalog()]);
|
||||
@@ -54,6 +91,9 @@
|
||||
}
|
||||
onMount(() => {
|
||||
void load();
|
||||
// Probe rewarded-ad readiness once so the "watch for chips" button only shows when a view would
|
||||
// actually play.
|
||||
void rewardedReady().then((r) => (rewardReady = r));
|
||||
// Fallback to the payment push: re-fetch when the tab regains focus, i.e. the user returned
|
||||
// from the provider's payment window.
|
||||
const onVisible = () => {
|
||||
@@ -171,6 +211,14 @@
|
||||
<!-- prettier-ignore -->
|
||||
<p class="ios-note" data-testid="ios-note">{t('wallet.iosBlockedPre')}<a href={siteRoot} target="_blank" rel="noopener noreferrer" onclick={onExternalLinkClick}>{t('wallet.iosBlockedLink')}</a>{t('wallet.iosBlockedPost')}</p>
|
||||
{/if}
|
||||
{#if canReward}
|
||||
<div class="row reward" data-testid="reward-row">
|
||||
<span class="name">{t('wallet.rewardWatch', { n: wallet?.rewardChips ?? 0 })}</span>
|
||||
<button class="buy" class:working={busyId === 'reward'} data-testid="watch-ad" disabled={busy} onclick={onWatchAd}
|
||||
>{t('wallet.rewardCta')}</button
|
||||
>
|
||||
</div>
|
||||
{/if}
|
||||
{#each values as p (p.productId)}
|
||||
<div class="row product" data-testid="product" data-kind="value" data-pid={p.productId}>
|
||||
<span class="name">{p.title}</span>
|
||||
|
||||
Reference in New Issue
Block a user