# 后端支付与积分闭环报告

**子 Agent：** payment-points-flow
**时间：** 2026-05-09

---

## 一、测试结果

4 passed in 0.73s - 所有安全测试通过

---

## 二、支付幂等性验证

pay_order 有幂等保护：

    if order.status == "paid":
        return {"message": "支付成功", ...}

- with_for_update() 在状态检查前锁行，并发安全
- 重复支付不重复加积分（幂等直接返回）
- 非 pending/paid 状态抛出 400

结论：无幂等性漏洞。

---

## 三、积分增加逻辑验证

pay_order 同一 db.commit() 内原子操作：

| 操作 | 代码 |
|------|------|
| 更新 points_balance | customer.points_balance += points_earned |
| 更新 total_spent | customer.total_spent += order.grand_total |
| 更新 orders_count | customer.orders_count += 1 |
| 写入 MemberPointsLedger | MemberPointsLedger(change_amount=points_earned, ...) |
| 会员升级检查 | 满足 upgrade_spend 条件自动升级 |

结论：积分链路完整正确。

---

## 四、库存扣减验证

下单：inv_svc.deduct_stock + with_for_update()
取消：inv_svc.restore_stock + 积分返还

结论：库存扣减/恢复均并发安全。

---

## 五、Payments 表

payments 表存在但 pay_order 不依赖（模拟支付）。供未来 Stripe/微信等网关写入。

---

## 六、插件状态

app/plugins/ 含：shipping_flat_rate, seo_sitemap。无独立 payment 插件，内置模拟支付逻辑可正常工作。

---

## 七、TenantSettings tenant_id 列

表定义已含 tenant_id 列（外键 + unique）。无需新增 007 迁移。

---

## 八、安全检查汇总

| 检查项 | 状态 |
|--------|------|
| 幂等拦截 paid | ✅ |
| with_for_update 锁行 | ✅ |
| 积分不重复写入 | ✅ |
| MemberPointsLedger 写入 | ✅ |
| points_balance/total_spent/orders_count 更新 | ✅ |
| 取消订单库存恢复 | ✅ |
| 取消订单积分返还 | ✅ |
| ProductVariant.product_id scope 过滤 | ✅ |
| negative qty 边界校验 | ✅ |

---

## 九、结论

所有检查项通过，无漏洞须修复。
