Files

30 lines
962 B
Go

package store
import (
"fmt"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"github.com/wangjia/pay/internal/model"
)
// ChargebackStore 持久化拒付留痕(P8 Task6)。与 SubscriptionStore/OrderStore 同惯例:
// 幂等靠 DB 唯一约束 + ON CONFLICT DO NOTHING,不在应用层加锁。
type ChargebackStore struct{ db *gorm.DB }
func NewChargebackStore(db *gorm.DB) *ChargebackStore { return &ChargebackStore{db: db} }
// Create 幂等插入:重复 dispute_ref(渠道重投同一拒付)→ no-op(created=false)。
// Chargeback 表对"能否定位业务单"保持中立——OutTradeNo 为空也照常落一行留痕。
func (s *ChargebackStore) Create(cb *model.Chargeback) (bool, error) {
res := s.db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "dispute_ref"}},
DoNothing: true,
}).Create(cb)
if res.Error != nil {
return false, fmt.Errorf("store.ChargebackStore.Create: %w", res.Error)
}
return res.RowsAffected > 0, nil
}