feat(economy): offer a free offline payment option
Expose free payment in the in-game dialog, local store, and marketplace. Grant the selected item without debiting GJN and reject unknown payment methods. Apply the configured per-purchase limit consistently. Type checking, binary and GUI patch checks, and local purchase checks passed. In-game acceptance is pending.
This commit is contained in:
1 parent
e1468df52b
commit
4d8229ae92
9 files changed
+161
-56
No files matched your search
@@ -0,0 +1,32 @@
|
||||
{
|
||||
"id": "online-shop/free-offline-payment-method",
|
||||
"title": "游戏内支付方式窗口增加「老福爷付过了」免 GJN 直接购买选项",
|
||||
"file": "scripts/onlineshop/onlinepurchase.nut",
|
||||
|
||||
"program_behaviour": {
|
||||
"stock": "scripts/onlineshop/onlinepurchase.nut 的 onOnlinePurchase 根据 yuplay2_get_payment_methods() 的位掩码生成支付方式列表(默认只有 Gaijin 币 YU2_PAY_GJN),再固定追加「其他支付方式」(yuNetwork/payMethod/other),交由 PaymentHandler 展示。",
|
||||
"patched": "在「其他支付方式」之前固定追加一项文字按钮「老福爷付过了」(icon 为空字符串,使 paymentHandler.nut 显示文字标题),点击确认后以 YU2_PAY_NONE(0,PurchaseAction 不附带 pay 参数)调用 doYuplayPurchase,本地服务的 /item_purchase.php 识别无 pay 参数时免扣 GJN 直接发放商品。"
|
||||
},
|
||||
|
||||
"derivation": {
|
||||
"requirement": "用户反馈(反馈20261125.md):在支付方式里除了 Gaijin 币外增加一项「老福爷付过了」,点击即视为已支付并直接买下商品,供不想刷 GJN 的玩家使用。",
|
||||
"cause": "原版 payMethodsCfg 只列出 paypal、webmoney、amazon、gjncoins 四项,且均设置非空 SVG 图标(paymenthandler.nut 第 26 行在 icon 非空时隐藏文字标题只显示图标),无法仅靠登录服务下发位掩码在窗口里显示自定义中文名称「老福爷付过了」。",
|
||||
"lighter_options_ruled_out": [
|
||||
"仅在登录服务返回 autopay_pp 等标签开启原版其他支付位:按钮只会显示 PayPal 等原版图标,不会显示「老福爷付过了」文字。"
|
||||
],
|
||||
"conclusion": "在 scripts/onlineshop/onlinepurchase.nut 的支付列表追加无图标文字项「老福爷付过了」,传入已导出的原生常量 YU2_PAY_NONE(aces.exe 0x17cd280 -> YuSession::buyItemSync 不校验位掩码,PurchaseAction 发送不含 pay 字段的表单),由本地服务免扣 GJN 发放。",
|
||||
"if_not_applied": "游戏内购买金鹰包或 GJN 高级账户弹出「选择支付方式」窗口时只有「Gaijin 币」和「其他支付方式」,没有「老福爷付过了」选项。"
|
||||
},
|
||||
|
||||
"features": ["G-货币", "G-高级账户", "G-礼包商店"],
|
||||
"evidence": [
|
||||
"2.61.0.37 gui.vromfs.bin:scripts/onlineshop/onlinepurchase.nut(onOnlinePurchase、onYuplayPurchase、doYuplayPurchase)、scripts/paymenthandler.nut 第 24-26 行、globalScripts/yuplay2Consts.nut 第 38 行(YU2_PAY_NONE)。",
|
||||
"2.61.0.37 aces.exe:0x17cd280(yuplay2_buy_entitlement 绑定)-> 0x55ecbd0(YuSession::buyItemSync)。",
|
||||
"DagorEngine 公开源码:prog/1stPartyLibs/yuplay2auth/session/yu_session.cpp 第 2175 行、yu_sessionProc.cpp 第 260-296 行(PurchaseAction::run 在 payment 为 YU2_PAY_NONE 时不写 pay 字段)。"
|
||||
],
|
||||
|
||||
"edit": {
|
||||
"find": " let name = \"yuNetwork/payMethod/other\"\r\n items.append({",
|
||||
"replace": " let freePayName = \"老福爷付过了\"\r\n items.append({\r\n name = freePayName\r\n icon = \"\"\r\n callback = @() onYuplayPurchase(product, YU2_PAY_NONE, freePayName)\r\n })\r\n\r\n let name = \"yuNetwork/payMethod/other\"\r\n items.append({"
|
||||
}
|
||||
}
|
||||
@@ -247,15 +247,36 @@ export function buyMarketplaceCoupon(
|
||||
catalogue: InventoryCatalogue,
|
||||
itemdefId: number,
|
||||
quantity: number,
|
||||
maxPerPurchase: number,
|
||||
): MarketBuyOutcome {
|
||||
const listing = catalogue.marketplaceListing(itemdefId);
|
||||
if (!listing || !Number.isSafeInteger(quantity) || quantity < 1 || quantity > 20) {
|
||||
return { ok: false, reason: 'NOT_FOUND' };
|
||||
}
|
||||
const listing = validCouponPurchase(catalogue, itemdefId, quantity, maxPerPurchase);
|
||||
if (!listing) return { ok: false, reason: 'NOT_FOUND' };
|
||||
const totalCost = listing.gjnCostCents * quantity;
|
||||
if (balanceOf(profile).gjnCents < totalCost) return { ok: false, reason: 'NOT_ENOUGH_GJN' };
|
||||
|
||||
payGjnCents(profile, totalCost);
|
||||
const item = grantExternalItem(profile, itemdefId, quantity);
|
||||
return { ok: true, item };
|
||||
return { ok: true, item: grantExternalItem(profile, itemdefId, quantity) };
|
||||
}
|
||||
|
||||
/** Direct free marketplace coupon claim ("老福爷付过了") without deducting GJN. */
|
||||
export function buyFreeMarketplaceCoupon(
|
||||
profile: BlkBlock,
|
||||
catalogue: InventoryCatalogue,
|
||||
itemdefId: number,
|
||||
quantity: number,
|
||||
maxPerPurchase: number,
|
||||
): MarketBuyOutcome {
|
||||
const listing = validCouponPurchase(catalogue, itemdefId, quantity, maxPerPurchase);
|
||||
if (!listing) return { ok: false, reason: 'NOT_FOUND' };
|
||||
return { ok: true, item: grantExternalItem(profile, itemdefId, quantity) };
|
||||
}
|
||||
|
||||
function validCouponPurchase(
|
||||
catalogue: InventoryCatalogue,
|
||||
itemdefId: number,
|
||||
quantity: number,
|
||||
maxPerPurchase: number,
|
||||
) {
|
||||
if (!Number.isSafeInteger(quantity) || quantity < 1 || quantity > maxPerPurchase) return undefined;
|
||||
return catalogue.marketplaceListing(itemdefId);
|
||||
}
|
||||
@@ -21,7 +21,7 @@ import {
|
||||
requireWebActionAccount,
|
||||
selectWebViewAccount,
|
||||
} from '../web-ui/web-helpers.ts';
|
||||
import { buyMarketplaceCoupon } from './inventory-rules.ts';
|
||||
import { buyFreeMarketplaceCoupon, buyMarketplaceCoupon } from './inventory-rules.ts';
|
||||
import type { InventoryCatalogue } from './itemdef-catalogue.ts';
|
||||
import type { MarketplaceListing } from './itemdef-types.ts';
|
||||
|
||||
@@ -106,13 +106,19 @@ export class MarketplaceService implements Service {
|
||||
if (!Number.isSafeInteger(itemdefId) || itemdefId <= 0 || !Number.isSafeInteger(rawQuantity) || rawQuantity < 1 || rawQuantity > this.#maxPerPurchase) {
|
||||
return jsonResponse({ ok: false, message: { zh: `请求无效:每次购买 1~${this.#maxPerPurchase} 张。`, en: `Invalid request: buy 1 to ${this.#maxPerPurchase} coupons at a time.` } }, 'marketplace buy: bad params');
|
||||
}
|
||||
if (body.payMethod !== undefined && body.payMethod !== 'gjn' && body.payMethod !== 'free') {
|
||||
return jsonResponse({ ok: false, message: { zh: '不支持这种支付方式。', en: 'Unsupported payment method.' } }, 'marketplace buy: bad payMethod');
|
||||
}
|
||||
const isFree = body.payMethod === 'free';
|
||||
const quantity = rawQuantity;
|
||||
const listing = this.#catalogue.marketplaceListing(itemdefId);
|
||||
if (!listing) return jsonResponse({ ok: false, message: { zh: '没有找到这张兑换券。', en: 'Coupon not found.' } }, `marketplace buy: unknown ${itemdefId}`);
|
||||
|
||||
let errorReason: 'NOT_FOUND' | 'NOT_ENOUGH_GJN' | undefined;
|
||||
this.#profiles.updateWhen(account.userId, (saved) => {
|
||||
const res = buyMarketplaceCoupon(saved, this.#catalogue, itemdefId, quantity);
|
||||
const res = isFree
|
||||
? buyFreeMarketplaceCoupon(saved, this.#catalogue, itemdefId, quantity, this.#maxPerPurchase)
|
||||
: buyMarketplaceCoupon(saved, this.#catalogue, itemdefId, quantity, this.#maxPerPurchase);
|
||||
if (!res.ok) {
|
||||
errorReason = res.reason;
|
||||
return false;
|
||||
@@ -121,21 +127,28 @@ export class MarketplaceService implements Service {
|
||||
});
|
||||
|
||||
if (errorReason === 'NOT_ENOUGH_GJN') {
|
||||
return jsonResponse({ ok: false, message: { zh: 'GJN 不够。可以在商店的"兑换码"页用 GJN 充值码补充。', en: 'Not enough GJN. A GJN code on the Redeem tab of the store adds more.' } }, `marketplace buy ${itemdefId}: NOMONEY`);
|
||||
return jsonResponse({ ok: false, message: { zh: 'GJN 不够。可直接点「老福爷付过了」,或在商店的"兑换码"页用 GJN 充值码补充。', en: 'Not enough GJN. Use "老福爷付过了" or a GJN code on the Redeem tab of the store.' } }, `marketplace buy ${itemdefId}: NOMONEY`);
|
||||
}
|
||||
if (errorReason) {
|
||||
return jsonResponse({ ok: false, message: { zh: '购买失败。', en: 'Purchase failed.' } }, `marketplace buy ${itemdefId}: ${errorReason}`);
|
||||
}
|
||||
|
||||
this.#notifyInventoryChanged();
|
||||
return jsonResponse({
|
||||
ok: true,
|
||||
message: {
|
||||
const costSummary = isFree ? 'free' : `${gjnText(listing.gjnCostCents * quantity)} GJN`;
|
||||
return jsonResponse({ ok: true, message: buySuccessMessage(listing, quantity, isFree) }, `marketplace buy ${itemdefId} x${quantity} (${costSummary})`);
|
||||
}
|
||||
}
|
||||
|
||||
function buySuccessMessage(listing: MarketplaceListing, quantity: number, isFree: boolean): { zh: string; en: string } {
|
||||
return isFree
|
||||
? {
|
||||
zh: `老福爷付过了!「${listing.title.zh}」兑换券 ×${quantity} 已放进游戏库存,可在机库的"道具 → 库存"里使用。`,
|
||||
en: `Paid by 老福爷! ${quantity} × "${listing.title.en}" coupon is in your inventory (Items → Inventory in the hangar).`,
|
||||
}
|
||||
: {
|
||||
zh: `已购买「${listing.title.zh}」兑换券 ×${quantity},已放进游戏库存,可在机库的"道具 → 库存"里使用。`,
|
||||
en: `Bought ${quantity} × "${listing.title.en}" coupon. It is in your inventory (Items → Inventory in the hangar).`,
|
||||
},
|
||||
}, `marketplace buy ${itemdefId} x${quantity} (${gjnText(listing.gjnCostCents * quantity)} GJN)`);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function isListingAlreadyActivated(profile: BlkBlock, listing: MarketplaceListing, unlockedSet: ReadonlySet<string>): boolean {
|
||||
|
||||
@@ -8,14 +8,14 @@ const T = {
|
||||
eagles: '金鹰', lions: '银狮',
|
||||
catAll: '全部', catVehicle: '载具券', catSkin: '涂装券', catAttachable: '挂件券', catDecal: '贴花券', catUnlock: '头像与解锁券',
|
||||
search: '搜索名称或编号', allCountries: '全部国家', allClasses: '全部兵种', rank: '{rank} 级',
|
||||
held: '库存 ×{n}', activated: '已拥有', buy: '购买', pickAccount: '请先选择账号', empty: '没有符合条件的兑换券',
|
||||
held: '库存 ×{n}', activated: '已拥有', buy: '购买', freeBuy: '老福爷付过了', pickAccount: '请先选择账号', empty: '没有符合条件的兑换券',
|
||||
},
|
||||
en: {
|
||||
title: 'WTOffline Marketplace', hint: 'Coupons go straight to your inventory (Items → Inventory in the hangar)', toStore: 'Go to Store',
|
||||
eagles: 'Golden Eagles', lions: 'Silver Lions',
|
||||
catAll: 'All', catVehicle: 'Vehicles', catSkin: 'Camouflages', catAttachable: 'Decorations', catDecal: 'Decals', catUnlock: 'Icons and unlocks',
|
||||
search: 'Search name or id', allCountries: 'All nations', allClasses: 'All types', rank: 'Rank {rank}',
|
||||
held: 'In inventory ×{n}', activated: 'Owned', buy: 'Buy', pickAccount: 'Pick an account first', empty: 'Nothing matches the filters',
|
||||
held: 'In inventory ×{n}', activated: 'Owned', buy: 'Buy', freeBuy: '老福爷付过了', pickAccount: 'Pick an account first', empty: 'Nothing matches the filters',
|
||||
},
|
||||
};
|
||||
// Vehicle class of a listing -> the game's own type name (lang/menu.csv).
|
||||
@@ -114,21 +114,24 @@ function listingCard(listing) {
|
||||
|
||||
const foot = document.createElement('div');
|
||||
foot.className = 'card-foot';
|
||||
foot.innerHTML = '<span class="price"></span>';
|
||||
foot.innerHTML = '<span class="price"></span><div class="buy-actions"></div>';
|
||||
foot.querySelector('.price').textContent = WtoUi.gjn(listing.gjnCostCents) + ' GJN';
|
||||
const btn = Object.assign(document.createElement('button'), {
|
||||
className: 'buy',
|
||||
textContent: state.currentUserId ? t('buy') : t('pickAccount'),
|
||||
disabled: !state.currentUserId,
|
||||
});
|
||||
if (!btn.disabled) btn.onclick = () => buy(listing.itemdefId);
|
||||
foot.appendChild(btn);
|
||||
const actions = foot.querySelector('.buy-actions');
|
||||
if (!state.currentUserId) {
|
||||
actions.appendChild(Object.assign(document.createElement('button'), { className: 'buy', textContent: t('pickAccount'), disabled: true }));
|
||||
} else {
|
||||
const gjnBtn = Object.assign(document.createElement('button'), { className: 'buy', textContent: t('buy') });
|
||||
gjnBtn.onclick = () => buy(listing.itemdefId, 'gjn');
|
||||
const freeBtn = Object.assign(document.createElement('button'), { className: 'buy free', textContent: t('freeBuy') });
|
||||
freeBtn.onclick = () => buy(listing.itemdefId, 'free');
|
||||
actions.append(gjnBtn, freeBtn);
|
||||
}
|
||||
card.appendChild(foot);
|
||||
return card;
|
||||
}
|
||||
|
||||
async function buy(itemdefId) {
|
||||
const result = await WtoUi.postJson('/trade.gaijin.net/api/buy', { userId: state.currentUserId, itemdefId, quantity: 1 });
|
||||
async function buy(itemdefId, payMethod) {
|
||||
const result = await WtoUi.postJson('/trade.gaijin.net/api/buy', { userId: state.currentUserId, itemdefId, quantity: 1, payMethod });
|
||||
WtoUi.showNotice($('notice'), WtoUi.text(result.message), result.ok);
|
||||
if (result.ok) await loadState(state.currentUserId);
|
||||
}
|
||||
|
||||
@@ -55,6 +55,14 @@ export function buyGjnProduct(profile: BlkBlock, product: StoreProduct, nowFilet
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
/** Direct free purchase ("老福爷付过了") for players who prefer not to farm GJN. */
|
||||
export function buyFreeStoreProduct(profile: BlkBlock, product: StoreProduct, nowFiletime: bigint): GjnPurchaseOutcome {
|
||||
if (product.gjnCostCents === undefined) return { ok: false, reason: 'NOT_FOUND' };
|
||||
if (!canBuyGjnProduct(profile, product, nowFiletime)) return { ok: false, reason: 'ALREADY_BOUGHT' };
|
||||
|
||||
grantProductRewards(profile, product, nowFiletime);
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
function grantProductRewards(profile: BlkBlock, product: StoreProduct, nowFiletime: bigint): void {
|
||||
if (product.id !== 'PremiumAccount') recordEntitlementPurchase(profile, product.id);
|
||||
|
||||
@@ -17,7 +17,7 @@ import { jsonResponse, parseJsonBody, requireWebActionAccount, selectWebViewAcco
|
||||
import { describeRedeemCodes, gjnText, redeemStoreCode, type RedeemOutcome } from './redeem-codes.ts';
|
||||
import type { RedeemTuning } from './redeem-tuning.ts';
|
||||
import type { StoreCatalogue, StoreProduct } from './store-catalogue.ts';
|
||||
import { buyGjnProduct, canBuyGjnProduct, type GjnPurchaseOutcome } from './store-purchase.ts';
|
||||
import { buyFreeStoreProduct, buyGjnProduct, canBuyGjnProduct, type GjnPurchaseOutcome } from './store-purchase.ts';
|
||||
|
||||
const NO_ACCOUNT: LocalizedText = { zh: '没有找到这个账号,请先在右上角选择账号。', en: 'Account not found. Pick an account at the top right first.' };
|
||||
|
||||
@@ -67,13 +67,19 @@ export class StoreWebApi {
|
||||
if (!product || product.gjnCostCents === undefined) {
|
||||
return jsonResponse({ ok: false, message: { zh: '没有找到这个商品。', en: 'Product not found.' } }, 'store web buy: unknown product');
|
||||
}
|
||||
if (body.payMethod !== undefined && body.payMethod !== 'gjn' && body.payMethod !== 'free') {
|
||||
return jsonResponse({ ok: false, message: { zh: '不支持这种支付方式。', en: 'Unsupported payment method.' } }, 'store web buy: bad payMethod');
|
||||
}
|
||||
const isFree = body.payMethod === 'free';
|
||||
|
||||
let outcome: GjnPurchaseOutcome = { ok: false, reason: 'NOT_FOUND' };
|
||||
const now = serverTimeNow();
|
||||
this.#profiles.updateWhen(account.userId, (saved) => {
|
||||
outcome = buyGjnProduct(saved, product, now);
|
||||
outcome = isFree ? buyFreeStoreProduct(saved, product, now) : buyGjnProduct(saved, product, now);
|
||||
return outcome.ok;
|
||||
});
|
||||
return jsonResponse({ ok: outcome.ok, message: purchaseMessage(outcome, product) }, `store web buy ${product.id}: ${outcome.ok ? 'OK' : outcome.reason}`);
|
||||
const modeTag = isFree ? 'free' : 'gjn';
|
||||
return jsonResponse({ ok: outcome.ok, message: purchaseMessage(outcome, product, isFree) }, `store web buy ${product.id} (${modeTag}): ${outcome.ok ? 'OK' : outcome.reason}`);
|
||||
}
|
||||
|
||||
#redeem(request: ServiceRequest): ServiceResponse {
|
||||
@@ -91,16 +97,21 @@ export class StoreWebApi {
|
||||
}
|
||||
}
|
||||
|
||||
function purchaseMessage(outcome: GjnPurchaseOutcome, product: StoreProduct): LocalizedText {
|
||||
function purchaseMessage(outcome: GjnPurchaseOutcome, product: StoreProduct, isFree: boolean): LocalizedText {
|
||||
const cost = gjnText(product.gjnCostCents ?? 0);
|
||||
if (outcome.ok) {
|
||||
return {
|
||||
zh: `已购买「${product.title.zh}」,花费 ${cost} GJN。关闭商店窗口后机库会自动刷新。`,
|
||||
en: `Bought "${product.title.en}" for ${cost} GJN. Close the store window to refresh the hangar.`,
|
||||
};
|
||||
return isFree
|
||||
? {
|
||||
zh: `老福爷付过了!已直接获得「${product.title.zh}」。关闭商店窗口后机库会自动刷新。`,
|
||||
en: `Paid by 老福爷! Claimed "${product.title.en}". Close the store window to refresh the hangar.`,
|
||||
}
|
||||
: {
|
||||
zh: `已购买「${product.title.zh}」,花费 ${cost} GJN。关闭商店窗口后机库会自动刷新。`,
|
||||
en: `Bought "${product.title.en}" for ${cost} GJN. Close the store window to refresh the hangar.`,
|
||||
};
|
||||
}
|
||||
if (outcome.reason === 'NOT_ENOUGH_GJN') {
|
||||
return { zh: `GJN 不够:需要 ${cost} GJN,可在"兑换码"页用 GJN 充值码补充。`, en: `Not enough GJN: ${cost} GJN needed. Use a GJN code on the Redeem tab.` };
|
||||
return { zh: `GJN 不够:需要 ${cost} GJN。可直接点「老福爷付过了」,或在"兑换码"页用 GJN 充值码补充。`, en: `Not enough GJN: ${cost} GJN needed. Use "老福爷付过了" or a GJN code on the Redeem tab.` };
|
||||
}
|
||||
if (outcome.reason === 'ALREADY_BOUGHT') return { zh: '你已经拥有该礼包内的载具。', en: 'You already own the vehicle(s) in this pack.' };
|
||||
return { zh: '这个商品现在买不了。', en: 'This product cannot be bought right now.' };
|
||||
|
||||
@@ -8,7 +8,7 @@ const T = {
|
||||
tabPacks: '礼包载具', tabPremium: '高级账户', tabEagles: '金鹰', tabRedeem: '兑换码',
|
||||
search: '搜索礼包或载具名称', allCountries: '全部国家', allRanks: '全部等级', rank: '{rank} 级',
|
||||
includesUnit: '包含载具:{name}', containsEagles: '附赠 {n} 金鹰', containsSilver: '附赠 {n} 银狮', containsPremium: '附赠 {days} 天高级账户', premiumDays: '{days} 天高级账户',
|
||||
buy: '购买', owned: '已拥有', pickAccount: '请先选择账号', redeem: '兑换', codePlaceholder: '输入兑换码', empty: '没有符合条件的商品',
|
||||
buy: '购买', freeBuy: '老福爷付过了', owned: '已拥有', pickAccount: '请先选择账号', redeem: '兑换', codePlaceholder: '输入兑换码', empty: '没有符合条件的商品',
|
||||
},
|
||||
en: {
|
||||
title: 'WTOffline Store', hint: 'Close this window after buying to refresh the hangar', toMarket: 'Go to Marketplace',
|
||||
@@ -16,7 +16,7 @@ const T = {
|
||||
tabPacks: 'Vehicle packs', tabPremium: 'Premium Account', tabEagles: 'Golden Eagles', tabRedeem: 'Redeem code',
|
||||
search: 'Search packs or vehicles', allCountries: 'All nations', allRanks: 'All ranks', rank: 'Rank {rank}',
|
||||
includesUnit: 'Vehicle: {name}', containsEagles: '+{n} Golden Eagles', containsSilver: '+{n} Silver Lions', containsPremium: '+{days} days of Premium Account', premiumDays: '{days} days of Premium Account',
|
||||
buy: 'Buy', owned: 'Owned', pickAccount: 'Pick an account first', redeem: 'Redeem', codePlaceholder: 'Enter a code', empty: 'Nothing matches the filters',
|
||||
buy: 'Buy', freeBuy: '老福爷付过了', owned: 'Owned', pickAccount: 'Pick an account first', redeem: 'Redeem', codePlaceholder: 'Enter a code', empty: 'Nothing matches the filters',
|
||||
},
|
||||
};
|
||||
const ROMAN = ['', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X'];
|
||||
@@ -109,15 +109,20 @@ function productCard(product) {
|
||||
|
||||
const foot = document.createElement('div');
|
||||
foot.className = 'card-foot';
|
||||
foot.innerHTML = '<span class="price"></span>';
|
||||
foot.innerHTML = '<span class="price"></span><div class="buy-actions"></div>';
|
||||
foot.querySelector('.price').textContent = WtoUi.gjn(product.gjnCostCents) + ' GJN';
|
||||
const btn = Object.assign(document.createElement('button'), {
|
||||
className: 'buy',
|
||||
textContent: product.owned ? t('owned') : (state.activeUserId ? t('buy') : t('pickAccount')),
|
||||
disabled: product.owned || !state.activeUserId,
|
||||
});
|
||||
if (!btn.disabled) btn.onclick = () => buy(product.id);
|
||||
foot.appendChild(btn);
|
||||
const actions = foot.querySelector('.buy-actions');
|
||||
if (product.owned || !state.activeUserId) {
|
||||
actions.appendChild(Object.assign(document.createElement('button'), {
|
||||
className: 'buy', textContent: product.owned ? t('owned') : t('pickAccount'), disabled: true,
|
||||
}));
|
||||
} else {
|
||||
const gjnBtn = Object.assign(document.createElement('button'), { className: 'buy', textContent: t('buy') });
|
||||
gjnBtn.onclick = () => buy(product.id, 'gjn');
|
||||
const freeBtn = Object.assign(document.createElement('button'), { className: 'buy free', textContent: t('freeBuy') });
|
||||
freeBtn.onclick = () => buy(product.id, 'free');
|
||||
actions.append(gjnBtn, freeBtn);
|
||||
}
|
||||
card.appendChild(foot);
|
||||
return card;
|
||||
}
|
||||
@@ -156,8 +161,8 @@ function renderRedeemHelp() {
|
||||
for (const line of state.redeemHelp) list.appendChild(Object.assign(document.createElement('li'), { textContent: WtoUi.text(line) }));
|
||||
}
|
||||
|
||||
async function buy(productId) {
|
||||
const result = await WtoUi.postJson('/store/api/buy', { userId: state.activeUserId, productId });
|
||||
async function buy(productId, payMethod) {
|
||||
const result = await WtoUi.postJson('/store/api/buy', { userId: state.activeUserId, productId, payMethod });
|
||||
WtoUi.showNotice($('notice'), WtoUi.text(result.message), result.ok);
|
||||
await loadState(state.activeUserId);
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ import type { ServiceRequest, ServiceResponse } from '../server/exchange.ts';
|
||||
import { gjnText, redeemStoreCode, type RedeemOutcome } from './redeem-codes.ts';
|
||||
import type { RedeemTuning } from './redeem-tuning.ts';
|
||||
import type { StoreCatalogue } from './store-catalogue.ts';
|
||||
import { buyGjnProduct, canBuyGjnProduct, type GjnPurchaseOutcome } from './store-purchase.ts';
|
||||
import { buyFreeStoreProduct, buyGjnProduct, canBuyGjnProduct, type GjnPurchaseOutcome } from './store-purchase.ts';
|
||||
|
||||
export const YUPLAY_STORE_PATHS: ReadonlySet<string> = new Set(['/item_info.php', '/item_purchase.php', '/redeem_code.php']);
|
||||
|
||||
@@ -71,8 +71,12 @@ export class YuplayStoreApi {
|
||||
#itemPurchase(form: URLSearchParams): ServiceResponse {
|
||||
const account = this.#account(form);
|
||||
if (!account) return yuplayAnswer('LOGINERROR', {}, 'item_purchase: unknown token');
|
||||
const payMethod = (form.get('pay') ?? 'gjn').toLowerCase();
|
||||
if (payMethod !== 'gjn') return yuplayAnswer('BADPAYMENT', {}, `item_purchase: unsupported pay=${payMethod}`);
|
||||
const rawPay = form.get('pay');
|
||||
// YU2_PAY_NONE ("老福爷付过了", gui-patches/online-shop/free-offline-payment-method) omits `pay` in PurchaseAction::run.
|
||||
const payMethod = rawPay === null ? 'free' : rawPay.toLowerCase();
|
||||
if (payMethod !== 'gjn' && payMethod !== 'free') {
|
||||
return yuplayAnswer('BADPAYMENT', {}, `item_purchase: unsupported pay=${rawPay}`);
|
||||
}
|
||||
const guid = form.get('guid') ?? '';
|
||||
const product = this.#catalogue.byGuid(guid);
|
||||
if (!product) return yuplayAnswer('NOTFOUND', {}, `item_purchase: unknown guid ${guid}`);
|
||||
@@ -80,10 +84,15 @@ export class YuplayStoreApi {
|
||||
let outcome: GjnPurchaseOutcome = { ok: false, reason: 'NOT_FOUND' };
|
||||
const now = serverTimeNow();
|
||||
this.#profiles.updateWhen(account.userId, (saved) => {
|
||||
outcome = buyGjnProduct(saved, product, now);
|
||||
outcome = payMethod === 'free'
|
||||
? buyFreeStoreProduct(saved, product, now)
|
||||
: buyGjnProduct(saved, product, now);
|
||||
return outcome.ok;
|
||||
});
|
||||
if (outcome.ok) return yuplayAnswer('OK', {}, `item_purchase ${product.id} (${gjnText(product.gjnCostCents ?? 0)} GJN)`);
|
||||
if (outcome.ok) {
|
||||
const costSummary = payMethod === 'free' ? 'free' : `${gjnText(product.gjnCostCents ?? 0)} GJN`;
|
||||
return yuplayAnswer('OK', {}, `item_purchase ${product.id} (${costSummary})`);
|
||||
}
|
||||
if (outcome.reason === 'NOT_ENOUGH_GJN') return yuplayAnswer('NOMONEY', {}, `item_purchase ${product.id}: insufficient GJN`);
|
||||
if (outcome.reason === 'ALREADY_BOUGHT') return yuplayAnswer('ALREADY', {}, `item_purchase ${product.id}: already owned`);
|
||||
return yuplayAnswer('NOTFOUND', {}, `item_purchase ${product.id}: not available`);
|
||||
|
||||
@@ -55,10 +55,13 @@ a.link:hover { color: var(--accent-strong); }
|
||||
.badges { display: flex; flex-wrap: wrap; gap: 5px; }
|
||||
.badge { padding: 1px 7px; border-radius: 4px; background: var(--panel-raised); color: var(--muted); font-size: 12px; }
|
||||
.badge.owned { color: var(--ok); }
|
||||
.card-foot { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: auto; }
|
||||
.card-foot { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px; margin-top: auto; }
|
||||
.buy-actions { display: flex; flex-wrap: wrap; gap: 6px; justify-content: flex-end; margin-left: auto; }
|
||||
.price { color: var(--accent); font-weight: 700; font-variant-numeric: tabular-nums; }
|
||||
button.buy { padding: 7px 16px; border: none; border-radius: 6px; background: var(--accent); color: #1b1408; font: inherit; font-weight: 600; cursor: pointer; }
|
||||
button.buy { padding: 6px 12px; border: 1px solid transparent; border-radius: 6px; background: var(--accent); color: #1b1408; font: inherit; font-size: 13px; font-weight: 600; cursor: pointer; white-space: nowrap; }
|
||||
button.buy:hover { background: var(--accent-strong); }
|
||||
button.buy:disabled { background: var(--panel-raised); color: var(--muted); cursor: default; }
|
||||
button.buy.free { background: var(--panel-raised); color: var(--accent); border-color: var(--accent); }
|
||||
button.buy.free:hover { background: var(--accent); color: #1b1408; }
|
||||
button.buy:disabled, button.buy.free:disabled { background: var(--panel-raised); border-color: transparent; color: var(--muted); cursor: default; }
|
||||
button:focus-visible, .tab:focus-visible, select:focus-visible, input:focus-visible { outline: 2px solid var(--accent-strong); outline-offset: 1px; }
|
||||
.empty { color: var(--muted); padding: 24px 0; }
|
||||
Reference in new issue
Block a user