Files
wangjia 04c3f4f31a feat(v2): 对账主体——周期查单收敛 + 已付订单抽查 + 退款修复扫描/卡滞告警 + main 装配 reconcile Runner
Task 5(债务 #6):两条腿收敛对账——① SyncPendingTask 调度 P2 gateway.SyncPendingAttempts
逐 pending attempt 查单收敛(防掉单);② PaidSpotCheckTask 对近期已付 attempt 反查渠道
核对金额/币种,漂移(渠道侧已退款/拒付而本地仍 paid)只告警不改状态。

追加两条 P4 T3 opus review 义务(该 review 产出时本 worktree 已分叉,P4 退款主体在主
checkout;这里对本 worktree 已有的 store.RefundStore/OrderStore 接口——P4 T2,在 base
里——建自愈扫描,设计为可在合并 P4 后继续工作):
- RefundApplyTask:退款修复扫描,重算 succeeded 退款之和,自愈「退款成功但订单卡 paid」
  的崩溃窗口(MarkRefundStatus 翻 succeeded 后、ApplyRefundToOrder 调用前崩溃)。候选订单
  =有 succeeded 退款的订单 ∪ 当前处于 refunding/partially_refunded 态的订单;走既有
  ApplyRefundToOrder 条件 UPDATE,目标态与当前态一致时跳过,天然幂等。
- RefundStuckAlertTask:卡滞 processing/manual_pending 退款超阈值(默认 30min)打 WARN,
  只观测不改状态;渠道退款查询 API 面留待后续。

main 装配前 4 job(order-expire/usage-refresh/sync-pending/paid-spotcheck)+ 上述两个退款
job 挂上 reconcile.Runner;acctPicker 的 limit_aware 用量源改用 reconcile.NewUsageSource
替 NopUsage。crypto 冷启动 Warm/孤儿扫描(AddCryptoJobs)留给 Task 6 追加。

config.go 新增 ReconcileConfig(含 Task 6 预留的 orphan_* 字段)+ 默认值;crypto.go 补
SetReservationLoader 构造后注入 setter(Task 6 依赖)。

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013nMthbVEmQquxBRKb9Fj8u
2026-07-10 17:45:01 +08:00

60 lines
2.3 KiB
Go

package reconcile
import (
"context"
"log"
"time"
"github.com/wangjia/pay/internal/gateway"
"github.com/wangjia/pay/internal/provider"
"github.com/wangjia/pay/internal/store"
)
// SyncPendingTask 调度 P2 gateway.SyncPendingAttempts:逐 pending attempt 查单收敛(防掉单)。
// 其内部已对 not_found/amount_mismatch/failed 打日志(settle-sync,764ed55),此处不重复。
func SyncPendingTask(gw *gateway.Gateway, limit int) func(ctx context.Context) error {
return func(ctx context.Context) error {
n, err := gw.SyncPendingAttempts(ctx, limit)
if err != nil {
return err
}
if n > 0 {
log.Printf("[reconcile] 查单对账收敛 %d 笔待支付 → paid", n)
}
return nil
}
}
// PaidSpotCheckTask 已付订单抽查:对近 window 内已付 attempt 反查渠道,金额/币种漂移即告警
// (如渠道侧已退款/拒付而本地仍 paid)。只发现不改状态——状态机翻转属 P4。
// crypto 之类 query-only 渠道:paid 后再查若命中同额即一致;查不到(链上历史滚出窗口)不报错跳过。
func PaidSpotCheckTask(orders *store.OrderStore, providers *provider.Registry, window time.Duration, now func() time.Time) func(ctx context.Context) error {
return func(ctx context.Context) error {
atts, err := orders.ListRecentlyPaidAttempts(now().Add(-window), 100)
if err != nil {
return err
}
for i := range atts {
a := &atts[i]
prov, err := providers.Get(a.Channel)
if err != nil {
continue
}
created := a.CreatedAt
ev, err := prov.Query(ctx, provider.QueryRequest{
ProviderRef: a.ProviderRef, OutTradeNo: a.OutTradeNo, AccountID: a.AccountID,
AmountMinor: a.AmountMinor, Currency: a.Currency, CreatedAt: created, ExpiresAt: a.ExpiresAt,
})
if err != nil || ev == nil {
continue // 查不到/瞬时错:抽查尽力而为,不阻断
}
// 本地 paid,渠道却报非成功,或金额/币种对不上 → 对账差异,必须可见。
if ev.Status != provider.PaidSucceeded || ev.PaidCurrency != a.Currency || ev.PaidAmountMinor < a.AmountMinor {
log.Printf("[reconcile][对账差异] attempt=%s channel=%s 本地 paid 但渠道 status=%s amount=%d/%s(本地 %d/%s)",
a.ProviderRef, a.Channel, ev.Status, ev.PaidAmountMinor, ev.PaidCurrency, a.AmountMinor, a.Currency)
}
}
return nil
}
}