فهرست منبع

Extract verified WeChat payment status reconciliation

Port the order-status logic from c745ce2 without its private preview page. Record device confirmation for both refunded test orders.
彭峰 20 ساعت پیش
والد
کامیت
6314e11bac
3فایلهای تغییر یافته به همراه160 افزوده شده و 0 حذف شده
  1. 14 0
      docs/qa/miniprogram-payment-refund-device-acceptance-2026-12-01.md
  2. 99 0
      test/payment-status.test.cjs
  3. 47 0
      utils/payment-status.js

+ 14 - 0
docs/qa/miniprogram-payment-refund-device-acceptance-2026-12-01.md

@@ -0,0 +1,14 @@
+# 小程序原路退款状态真机验收
+
+2026 年 12 月 1 日(北京时间),创始人使用原测试微信扫码打开官方微信 CI 真机预览 `c745ce27c250e44e27c963c7e661275ff9d1af3d`,确认两笔隔离测试订单均显示「已退款」。
+
+| 测试业务 | 测试订单尾号 | 金额 | 微信退款结果 | 小程序真机显示 |
+| --- | --- | ---: | --- | --- |
+| 会员续费 | `E9E94D` | ¥0.01 | 原路退款成功 | 已退款 |
+| 陪伴值充值 | `6ACDE7` | ¥0.01 | 原路退款成功 | 已退款 |
+
+本次验收覆盖已有退款的结果查询和状态展示,扫码复测未发起新付款。验收后回读小程序正式支付可用性为 `false`,H5 正式入口制品摘要与验收前一致。候选仍处于隔离预览阶段。
+
+## 提交范围
+
+从真机候选提取微信查单的有限重试、状态判定及防止重新拉起收银台的逻辑,并补充离线回归。真机验收结论对应上文的隔离候选。提取后的模块待接入正式小程序支付路径。

+ 99 - 0
test/payment-status.test.cjs

@@ -0,0 +1,99 @@
+const { test } = require('node:test');
+const assert = require('node:assert/strict');
+const {
+  canOpenCashier,
+  getPaymentStatus,
+  reconcilePaymentStatus,
+} = require('../utils/payment-status.js');
+
+function queryFixture(...states) {
+  const queries = [];
+  const delays = [];
+  return {
+    queries,
+    delays,
+    query: async () => {
+      assert.ok(states.length, 'unexpected additional order query');
+      const status = states.shift();
+      queries.push(status);
+      return { status };
+    },
+    wait: async delay => { delays.push(delay); },
+  };
+}
+
+test('a late WeChat success overrides a cashier cancellation', async () => {
+  const { query, wait, delays } = queryFixture('NOTPAY', 'SUCCESS');
+  const status = await reconcilePaymentStatus(query, { recheck: true, wait });
+
+  assert.deepEqual(delays, [600]);
+  assert.deepEqual(getPaymentStatus(status, { cashierOutcome: 'cancelled' }), {
+    status: 'SUCCESS',
+    label: '已支付',
+    final: true,
+  });
+});
+
+test('an unresolved cashier success remains pending after two rechecks', async () => {
+  const { query, wait, queries, delays } = queryFixture('NOTPAY', 'USERPAYING', 'NOTPAY');
+  const status = await reconcilePaymentStatus(query, { recheck: true, wait });
+  const outcome = { cashierOutcome: 'success' };
+
+  assert.equal(queries.length, 3);
+  assert.deepEqual(delays, [600, 1500]);
+  assert.equal(getPaymentStatus(status, outcome).label, '支付结果确认中');
+  assert.equal(canOpenCashier(status, outcome), false);
+});
+
+test('a manual or foreground check reads the order once', async () => {
+  const { query, queries } = queryFixture('NOTPAY');
+  const status = await reconcilePaymentStatus(query);
+
+  assert.deepEqual(queries, ['NOTPAY']);
+  assert.equal(getPaymentStatus(status, { cashierOutcome: 'cancelled' }).label, '已取消 · 暂未支付');
+  assert.equal(canOpenCashier(status, { cashierOutcome: 'cancelled' }), true);
+});
+
+test('each test order displays a confirmed refund without reopening the cashier', async () => {
+  for (const kind of ['membership', 'recharge']) {
+    const { query, wait, delays } = queryFixture('REFUND');
+    const status = await reconcilePaymentStatus(query, { recheck: true, wait });
+
+    assert.deepEqual(delays, [], kind);
+    assert.deepEqual(getPaymentStatus(status, { previousStatus: 'SUCCESS' }), {
+      status: 'REFUND',
+      label: '已退款',
+      final: true,
+    }, kind);
+    assert.equal(canOpenCashier(status), false, kind);
+  }
+});
+
+test('stale checks do not replace confirmed refund or success', () => {
+  assert.equal(getPaymentStatus('SUCCESS', { previousStatus: 'REFUND' }).status, 'REFUND');
+  assert.equal(getPaymentStatus('NOTPAY', { previousStatus: 'SUCCESS' }).status, 'SUCCESS');
+  assert.equal(canOpenCashier('NOTPAY', { previousStatus: 'REFUND' }), false);
+});
+
+test('closed, revoked and failed orders are terminal', () => {
+  for (const status of ['CLOSED', 'REVOKED', 'PAYERROR']) {
+    assert.equal(getPaymentStatus(status).final, true);
+    assert.equal(canOpenCashier(status), false);
+    assert.equal(canOpenCashier('NOTPAY', { previousStatus: status }), false);
+  }
+});
+
+test('unknown status stays unconfirmed and does not open a cashier', () => {
+  for (const status of [undefined, 'NEW_STATE', 'constructor']) {
+    assert.equal(getPaymentStatus(status).label, '结果待确认');
+    assert.equal(canOpenCashier(status), false);
+  }
+});
+
+test('a failed query propagates and does not schedule another request', async () => {
+  const failure = new Error('query unavailable');
+  await assert.rejects(reconcilePaymentStatus(async () => { throw failure; }, {
+    recheck: true,
+    wait: async () => assert.fail('query failure must be surfaced'),
+  }), failure);
+});

+ 47 - 0
utils/payment-status.js

@@ -0,0 +1,47 @@
+const STATUS_LABELS = {
+  NOTPAY: '未支付',
+  USERPAYING: '支付确认中',
+  SUCCESS: '已支付',
+  CLOSED: '已关闭',
+  PAYERROR: '支付失败',
+  REVOKED: '已撤销',
+  REFUND: '已退款',
+};
+const FINAL_STATES = new Set(['SUCCESS', 'REFUND', 'CLOSED', 'REVOKED', 'PAYERROR']);
+const RECHECK_DELAYS_MS = [600, 1500];
+
+function waitForDelay(delay) {
+  return new Promise(resolve => setTimeout(resolve, delay));
+}
+
+// The caller supplies the authorized, server-side order lookup.
+async function reconcilePaymentStatus(query, { recheck = false, wait = waitForDelay } = {}) {
+  let { status } = await query();
+  if (recheck) {
+    for (const delay of RECHECK_DELAYS_MS) {
+      if (status !== 'NOTPAY' && status !== 'USERPAYING') break;
+      await wait(delay);
+      ({ status } = await query());
+    }
+  }
+  return status;
+}
+
+function getPaymentStatus(status, { previousStatus, cashierOutcome } = {}) {
+  if (previousStatus === 'REFUND' || (previousStatus === 'SUCCESS' && status !== 'REFUND')) {
+    status = previousStatus;
+  }
+  let label = '结果待确认';
+  if (Object.hasOwn(STATUS_LABELS, status)) label = STATUS_LABELS[status];
+  if (status === 'NOTPAY') {
+    if (cashierOutcome === 'success') label = '支付结果确认中';
+    if (cashierOutcome === 'cancelled') label = '已取消 · 暂未支付';
+  }
+  return { status, label, final: FINAL_STATES.has(status) };
+}
+
+function canOpenCashier(status, { previousStatus, cashierOutcome } = {}) {
+  return status === 'NOTPAY' && !FINAL_STATES.has(previousStatus) && cashierOutcome !== 'success';
+}
+
+module.exports = { canOpenCashier, getPaymentStatus, reconcilePaymentStatus };