fix: accept shop OAuth OpenID for JSAPI payments
This commit is contained in:
@@ -1814,3 +1814,10 @@ Goose v1.49 合并前准备:修复 `build-goosed-v149-local.sh` 路径、刷
|
||||
- 2026-11-19:`3012e09` 上线商品微信直付、关单与退款桥接;`e3b06a3` 将商城微信内付款迁至 Memind 独立收银台。两者均已进入 `origin/main`。
|
||||
- 收银台签名、过期、固定金额/返回地址、取消与成功返回等 12 项支付测试通过;177 项基础回归通过;真实 HTTP 商城与 Memind 联调及商城两端构建通过。
|
||||
- 无新增开发 worktree,发布依据完整 main commit 和 runtime manifest。历史功能分支仅保留只读追溯。
|
||||
|
||||
## `codex/one-shop-wechat-bridge`
|
||||
|
||||
**状态:已提交/已进入远端 main/禁止再次引用。**
|
||||
|
||||
- 原提交 `0cbc9da` 已是 `origin/main` 的祖先,且该分支没有未合入提交、远端分支或独立 worktree。
|
||||
- 本次开始新的支付身份兼容工作前登记该分支已闭环;保留本地分支仅供只读追溯。
|
||||
|
||||
@@ -8,4 +8,6 @@
|
||||
|
||||
配置核验:生产 `H5_PUBLIC_BASE_URL=https://m.tkmind.cn`,微信商户平台 JSAPI 支付授权目录需匹配(建议 `https://m.tkmind.cn/`)。无法通过本地自动化读取商户后台的实际授权目录,最终需微信真机低金额付款验收。
|
||||
|
||||
身份兼容:商城直连公众号 OAuth 得到的是 OpenID,不是 Memind 的 `memind_user_id`。OneShop 桥接的 `/internal/one-shop/wechat-pay` 在共享桥接密钥校验后可接收 `openid` 与 `openid_app_id`,并要求 AppID 与 Memind 微信支付配置一致;不能把 OpenID 填入 `memind_user_id`。有 Memind 身份的旧流程仍按用户 ID 查询 OpenID。
|
||||
|
||||
验证:`node --test server/one-shop-checkout.test.mjs server/portal-one-shop-routes.test.mjs server/one-shop-cash-payment.test.mjs wechat-pay.test.mjs`,覆盖签名篡改、过期、外站返回拒绝、服务端金额、取消重试和成功返回。商城前后台执行 `npm run build`。不主动扣款。
|
||||
|
||||
@@ -119,10 +119,16 @@ export function attachPortalOneShopRoutes({
|
||||
app.post('/internal/one-shop/wechat-pay', jsonBody, async (req, res) => {
|
||||
res.set('Cache-Control', 'no-store');
|
||||
if (!hasBridgeKey(req, bridgeKey)) return res.sendStatus(403);
|
||||
const { out_trade_no: outTradeNo, amount_cents: amountCents, memind_user_id: userId, order_id: orderId } = req.body || {};
|
||||
const {
|
||||
out_trade_no: outTradeNo, amount_cents: amountCents,
|
||||
memind_user_id: userId, order_id: orderId,
|
||||
openid: directOpenid, openid_app_id: openidAppId,
|
||||
} = req.body || {};
|
||||
if (typeof outTradeNo !== 'string' || !orderPattern.test(outTradeNo) ||
|
||||
!Number.isSafeInteger(amountCents) || amountCents <= 0 || amountCents > 100_000_000 ||
|
||||
(userId != null && (typeof userId !== 'string' || !userId || userId.length > 128)) ||
|
||||
(directOpenid != null && (typeof directOpenid !== 'string' || !/^[A-Za-z0-9_-]{1,128}$/.test(directOpenid) ||
|
||||
typeof openidAppId !== 'string' || openidAppId.length > 64)) ||
|
||||
(orderId != null && (!Number.isSafeInteger(orderId) || orderId <= 0 || !outTradeNo.startsWith('OC')))) {
|
||||
return res.sendStatus(400);
|
||||
}
|
||||
@@ -130,10 +136,16 @@ export function attachPortalOneShopRoutes({
|
||||
const pay = getPayClient();
|
||||
if (!pay?.enabled) return res.sendStatus(503);
|
||||
const order = { outTradeNo, description: outTradeNo.startsWith('OC') ? '商城商品订单' : '商城积分充值', amountCents };
|
||||
let openid = null;
|
||||
if (userId) {
|
||||
await userAuthReady;
|
||||
const openid = await getUserAuth()?.getWechatOpenidForUser(userId, pay.appId);
|
||||
openid = await getUserAuth()?.getWechatOpenidForUser(userId, pay.appId);
|
||||
if (!openid) return res.sendStatus(409);
|
||||
} else if (directOpenid) {
|
||||
if (openidAppId !== pay.appId) return res.sendStatus(409);
|
||||
openid = directOpenid;
|
||||
}
|
||||
if (openid) {
|
||||
const { jsapiParams } = await pay.createJsapiOrder({ ...order, openid });
|
||||
if (orderId) {
|
||||
const token = createCheckoutToken({
|
||||
|
||||
@@ -12,6 +12,7 @@ test('OneShop login handoff and signed notification forwarding', async (t) => {
|
||||
const forwards = [];
|
||||
let forwardStatus = 200;
|
||||
let merchant = 'merchant-1';
|
||||
const jsapiOpenids = [];
|
||||
attachPortalOneShopRoutes({
|
||||
app,
|
||||
jsonBody: express.json(),
|
||||
@@ -24,6 +25,7 @@ test('OneShop login handoff and signed notification forwarding', async (t) => {
|
||||
userToken: (req) => req.get('cookie'),
|
||||
shopPublicUrl: 'https://shop.example.test',
|
||||
shopInternalUrl: 'http://shop-internal.example.test',
|
||||
paymentPublicUrl: 'https://pay.example.test',
|
||||
sharedSecret: bridgeKey,
|
||||
getPayClient: () => ({
|
||||
enabled: true,
|
||||
@@ -31,6 +33,10 @@ test('OneShop login handoff and signed notification forwarding', async (t) => {
|
||||
appId: 'wx-app',
|
||||
mchId: 'merchant-1',
|
||||
createNativeOrder: async () => ({ codeUrl: 'weixin://fixture' }),
|
||||
createJsapiOrder: async ({ openid }) => {
|
||||
jsapiOpenids.push(openid);
|
||||
return { jsapiParams: { package: 'prepay_id=fixture' } };
|
||||
},
|
||||
verifyNotify: ({ body }) => {
|
||||
if (body !== 'signed fixture') throw new Error('invalid signature');
|
||||
return { transaction: {
|
||||
@@ -86,6 +92,24 @@ test('OneShop login handoff and signed notification forwarding', async (t) => {
|
||||
assert.equal((await prepay('wrong-key')).status, 403);
|
||||
assert.deepEqual(await (await prepay(bridgeKey)).json(), { code_url: 'weixin://fixture' });
|
||||
|
||||
const directPrepay = (openidAppId = 'wx-app') => fetch(base + '/internal/one-shop/wechat-pay', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', 'X-One-Shop-Bridge-Key': bridgeKey },
|
||||
body: JSON.stringify({
|
||||
out_trade_no: 'OC' + 'B'.repeat(30), amount_cents: 100, order_id: 42,
|
||||
openid: 'o-direct-wechat-user', openid_app_id: openidAppId,
|
||||
}),
|
||||
});
|
||||
assert.equal((await directPrepay('another-app')).status, 409);
|
||||
const directPayment = await (await directPrepay()).json();
|
||||
assert.equal(jsapiOpenids.at(-1), 'o-direct-wechat-user');
|
||||
const checkoutToken = new URL(directPayment.checkout_url).hash.slice('#checkout='.length);
|
||||
const checkout = await fetch(base + '/auth/one-shop/payment', {
|
||||
method: 'POST', headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ token: checkoutToken }),
|
||||
});
|
||||
assert.deepEqual((await checkout.json()).jsapi_params, { package: 'prepay_id=fixture' });
|
||||
|
||||
const notify = (body) => fetch(base + '/webhooks/one-shop/wechat-pay', {
|
||||
method: 'POST', headers: { 'Content-Type': 'text/plain' }, body,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user