# kvfu 支付与统一钱包接入指南 > 适用对象:接入 kvfu 游戏中心的游戏开发者、平台运营。 > 目标:玩家在平台充值一次,余额全站通用;游戏服务端安全扣款,平台实时记账并按比例分成。 ## 1. 支持端 | 端 | 支持情况 | 说明 | |---|---|---| | 网页端 | ✅ | 浏览器直接打开 `https://kvfu.com`,充值/支付/查流水全功能 | | 电脑端 | ✅ | Windows/macOS/Linux 浏览器、PWA 独立窗口均可支付 | | 手机端 | ✅ | iOS/Android 浏览器、PWA;收银台按 UA 自适应 H5/APP 拉起 | | 游戏内客户端 | ✅ | 客户端不直接支付;由游戏服务端调用钱包扣款接口 | 支付使用易云支付 V1 `submit.php` 收银台:平台不指定 `type`,由收银台根据设备提供支付宝、微信等可用渠道。 ## 2. 整体流程 ``` 玩家充值: 浏览器/手机/PC → kvfu 充值中心 → POST submit.php → 易云收银台 ↓ 支付成功 GET /api/v1/pay/notify(服务端回调) ↓ 写入余额 + 充值流水 游戏内消费: 游戏客户端 → 游戏服务端(已通过 SSO 知道 uid) → 生成 order_no + HMAC-SHA256 签名 → POST /api/v1/wallet/deduct → 平台原子扣余额、写消费流水、记开发者分成 → 游戏服务端发货 ``` ## 3. 平台侧配置 | 配置 | 位置 | 示例 | |---|---|---| | 商户 ID | 管理后台 → 平台配置 → `pay.y1yun.mch_id` | `1231` | | 商户 MD5 密钥 | `pay.y1yun.key` | 仅后台保存,读取时掩码 | | 网关 | `pay.y1yun.gateway` | `https://pay.y1yun.net/` | | 支付模式 | `pay.sandbox` | 生产必须 `0` | | 公网地址 | `site.public_url` 或环境变量 `PUBLIC_BASE_URL` | `https://kvfu.com` | | 域名白名单 | 易云支付后台 → 授权域名 | `kvfu.com`、`www.kvfu.com` | > 易云支付会校验发起支付的域名。未加白时收银台返回「该域名不可发起支付」。这是支付平台侧配置,必须在易云后台完成。 ### 3.1 回调地址 | 用途 | 地址 | |---|---| | 异步通知 | `{site.public_url}/api/v1/pay/notify` | | 支付返回 | `{site.public_url}/api/v1/pay/return`(302 到前端结果页) | 生产必须 HTTPS,且公网可访问。本地 `localhost` 可用于开发,但易云不会向其回调;可用主动查单接口补账。 ## 4. 玩家端接口 认证:`Authorization: Bearer ` | 方法 | 路径 | 说明 | |---|---|---| | GET | `/api/v1/wallet` | 余额、累计充值/消费、最近流水、分成比例 | | GET | `/api/v1/wallet/transactions?type=&limit=` | 我的流水;`type=recharge/consume` | | POST | `/api/v1/wallet/recharge` | 创建充值订单 `{ amountCents }`,返回收银台表单参数 | | GET | `/api/v1/wallet/recharges/:id` | 查询自己的充值订单 | | POST | `/api/v1/wallet/recharges/:id/sync` | 主动向易云查单并补账 | | GET | `/api/v1/pay/status/:id` | 查询充值/游戏订单支付状态 | 金额单位统一为**分**,前端展示时除以 100。 ### 4.1 充值响应示例 ```json { "ok": true, "data": { "order": { "id": "RCxxxx", "amountCents": 600, "status": "pending" }, "sandbox": false, "payUrl": "https://pay.y1yun.net/submit.php", "method": "POST", "params": { "pid": "1231", "out_trade_no": "RCxxxx", "notify_url": "https://kvfu.com/api/v1/pay/notify", "return_url": "https://kvfu.com/api/v1/pay/return", "name": "账户充值", "money": "6.00", "sign_type": "MD5", "sign": "…" } } } ``` 前端将 `params` 作为隐藏字段 POST 到 `payUrl` 即可。不要在前端保存 `pay.y1yun.key`。 ## 5. 游戏服务端接口 游戏服务端持有 `app_id` / `app_secret`,**客户端绝不能保存 app_secret**。 ### 5.1 获取应用凭证 开发者中心 → 我的游戏 → 展开凭证: - `app_id` - `app_secret` - `verify_url`:SSO 票据校验 - `wallet_url`:扣款接口 `/api/v1/wallet/deduct` ### 5.2 签名规则 参与签名的字段按字段名 ASCII 升序排列;每个值做 `encodeURIComponent`;用 `&` 连接;用 `app_secret` 做 HMAC-SHA256,输出小写十六进制。 ```text stringToSign = app_id=...&amount_cents=600&order_no=...&product=...×tamp=...&user_id=... sign = HMAC-SHA256(app_secret, stringToSign).hex() ``` - `timestamp`:毫秒,服务端时间偏差不超过 5 分钟。 - `order_no`:游戏内唯一业务订单号,6~64 位字母、数字、`_.:-`。 - `user_id`:只能来自 SSO `oauth/verify` 返回的 `uid`。 ### 5.3 查询余额 `POST /api/v1/wallet/query` ```json { "app_id": "app_xxx", "user_id": 12, "timestamp": 1764600000000, "sign": "…" } ``` 响应: ```json { "ok": true, "data": { "userId": 12, "nickname": "玩家", "status": "active", "balanceCents": 9400 } } ``` ### 5.4 扣款 `POST /api/v1/wallet/deduct` ```json { "app_id": "app_xxx", "user_id": 12, "amount_cents": 600, "product": "月卡", "order_no": "shop_20261202_0001", "timestamp": 1764600000000, "sign": "…" } ``` 成功响应: ```json { "ok": true, "data": { "transactionId": 2, "orderNo": "shop_20261202_0001", "replayed": false, "balanceCents": 9400, "developerCents": 300, "platformCents": 300 } } ``` 规则: - 游戏必须 `online`,否则 `403 game_offline`。 - 余额不足返回 `402 insufficient_balance`,不会出现负余额。 - 同一 `order_no` 重试返回首次结果,`replayed: true`,不会重复扣款。 - 同一 `order_no` 改金额/商品返回 `409 order_conflict`。 - 扣款成功后再发货;不要先发货再扣款。 ### 5.5 消费流水(开发者查看) `GET /api/v1/developer/transactions?gameId=&from=&to=&limit=&offset=` 认证:开发者 Bearer Token。 响应: ```json { "ok": true, "data": { "sharePercent": 50, "totalCount": 12, "totalGrossCents": 7200, "totalShareCents": 3600, "totalPlatformCents": 3600, "limit": 30, "offset": 0, "items": [ { "id": 2, "userId": 12, "nickname": "玩家", "amountCents": -600, "developerCents": 300, "platformCents": 300, "product": "月卡", "orderNo": "shop_20261202_0001", "game": { "id": 3, "slug": "demo", "name": "示例游戏", "icon": "" }, "createdAt": 1764600000000 } ] } } ``` 开发者中心「钱包消费流水与分成」面板即读取该接口,可按游戏筛选。 ## 6. 分成与结算 | 项 | 规则 | |---|---| | 默认分成 | 开发者 50%,平台 50% | | 计算 | `developerCents = floor(amount_cents × revenue.share)`,平台拿余数 | | 记录 | 每笔消费写入 `wallet_txns`,保存 `developerCents` / `platformCents` | | 比例变更 | 只影响之后的新消费,历史流水不变 | | 汇总 | `GET /api/v1/me/revenue` 返回直接订单 + 钱包消费的分成汇总 | | 结算 | 当前为实时记账,提现/打款线下结算;后续可接代付自动打款 | 对账口径:以 `wallet_txns.developer_cents` 为准,不按当前比例反算历史流水。 ## 7. 错误码 | HTTP | code | 说明 | |---|---|---| | 400 | `invalid_user` / `invalid_amount` / `invalid_order_no` | 参数不合法 | | 401 | `invalid_app` / `bad_sign` / `stale_timestamp` | 应用不存在 / 签名错误 / 时间戳过期 | | 402 | `insufficient_balance` | 余额不足,引导充值 | | 403 | `game_offline` / `user_banned` | 游戏未上线 / 玩家封禁 | | 404 | `user_not_found` / `not_found` | 玩家或订单不存在 | | 409 | `order_conflict` | `order_no` 已用于其他金额/商品 | | 503 | `pay_not_configured` | 平台支付参数未配置 | ## 8. Node 示例 ```js import crypto from 'node:crypto'; export async function charge(uid, amountCents, product, orderNo) { const fields = { app_id: process.env.GC_APP_ID, user_id: uid, amount_cents: amountCents, product, order_no: orderNo, timestamp: Date.now(), }; const canonical = Object.keys(fields).sort() .map((k) => `${k}=${encodeURIComponent(fields[k])}`).join('&'); const sign = crypto.createHmac('sha256', process.env.GC_APP_SECRET).update(canonical).digest('hex'); const res = await fetch(`${process.env.GC_BASE}/api/v1/wallet/deduct`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ ...fields, sign }), signal: AbortSignal.timeout(5000), }); const json = await res.json(); if (!json.ok) throw Object.assign(new Error(json.error.message), { code: json.error.code }); return json.data; } ``` ## 9. Java 示例 ```java static String sign(Map fields, String secret) throws Exception { String canonical = fields.keySet().stream().sorted() .map(k -> k + "=" + URLEncoder.encode(fields.get(k), StandardCharsets.UTF_8)) .collect(Collectors.joining("&")); Mac mac = Mac.getInstance("HmacSHA256"); mac.init(new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256")); return HexFormat.of().formatHex(mac.doFinal(canonical.getBytes(StandardCharsets.UTF_8))); } ``` ## 10. 安全与上线检查 1. `app_secret` 只存游戏服务端,不进客户端、日志、前端包。 2. `user_id` 必须来自 SSO 校验结果,不能信任客户端上报。 3. 每笔业务订单固定 `order_no`;超时用同一 `order_no` 重试。 4. 扣款成功后再发货;发货失败用 `transactionId` / `orderNo` 对账。 5. 平台回调校验商户号、签名、`TRADE_SUCCESS`、金额一致。 6. 生产使用 HTTPS,并在易云后台把 `kvfu.com`、`www.kvfu.com` 加入授权域名。 7. 定期用 `/wallet/query`、`/developer/transactions` 与本地发货记录对账。 8. 余额不可提现;退款/补偿由平台按流水人工处理。