租客在待支付超时或取消的最后几十秒完成付款时,订单先被取消,乐刷的支付 结果几秒后才到,入账被判定订单不可支付而拒收。原先订单推进和支付单更新 在同一个事务里,订单一报错整笔回滚,支付单停在支付中且 paid_at 为空: 钱已在渠道,系统却毫不知情。乐刷重试十次后放弃,之后既补不回入账(订单 不再是待支付),后台人工退款也发不出去(退款要求原支付单是已支付)。 日志保留期内 13 天出了 5 笔,合计 4085.60 元。 - 取消前核对渠道:超时取消、租客取消、客服关单都先查一次渠道,已收款并 已推进订单就放弃取消,让支付正常入账而不是走退款。渠道异常时按未收款 继续,避免渠道一抖订单全卡住 - 回调兜底记账:订单确实已结束时不再回滚,把支付单如实记成已支付并写入 paid_at 与渠道单号,后台人工退款从此可用。退款仍由客服人工发起 - 客服关单:待支付订单按实际已收款减已退款的差额退,补上本次事故中关单 不生成退款的直接原因 - 补齐日志上下文:回调入账失败原先只有 error="order cannot pay",现在带 支付单号、订单号、订单状态和金额;新增 ERROR「渠道已收款但订单无法入账, 需人工退款」作为唯一需人工介入的告警 Co-authored-by: Cursor <cursoragent@cursor.com>
398 lines
13 KiB
Go
398 lines
13 KiB
Go
package order
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
)
|
|
|
|
var (
|
|
ErrDependencyUnavailable = errors.New("dependency unavailable")
|
|
ErrInvalidRentHours = errors.New("invalid rent hours")
|
|
ErrListingUnavailable = errors.New("listing unavailable")
|
|
ErrCannotRentOwnListing = errors.New("cannot rent own listing")
|
|
ErrInsufficientBalance = errors.New("insufficient balance")
|
|
ErrOrderCannotPay = errors.New("order cannot pay")
|
|
ErrChannelPaymentRequired = errors.New("channel payment required")
|
|
ErrOrderCannotCancel = errors.New("order cannot cancel")
|
|
ErrOrderPaidAtChannel = errors.New("order already paid at channel")
|
|
ErrOrderCannotHandoff = errors.New("order cannot handoff")
|
|
ErrOrderCannotForceHandoff = errors.New("order cannot force handoff")
|
|
ErrOrderCannotResetHandoff = errors.New("order cannot reset handoff")
|
|
ErrOrderCannotReceive = errors.New("order cannot receive")
|
|
ErrOrderCannotReturn = errors.New("order cannot return")
|
|
ErrOrderCannotComplete = errors.New("order cannot complete")
|
|
ErrCheckoutCannotSubmit = errors.New("checkout cannot submit")
|
|
ErrCheckoutCannotConfirm = errors.New("checkout cannot confirm")
|
|
ErrZeroOwnerIncomeConfirmation = errors.New("zero owner income confirmation required")
|
|
ErrCheckoutCannotCounter = errors.New("checkout cannot counter")
|
|
ErrCheckoutMaxRounds = errors.New("checkout max rounds reached")
|
|
ErrCheckoutDepositShortfall = errors.New("checkout deposit shortfall")
|
|
ErrInvalidCheckoutAmount = errors.New("invalid checkout amount")
|
|
ErrDisputeExists = errors.New("dispute already exists")
|
|
ErrPermissionDenied = errors.New("permission denied")
|
|
ErrDepositCannotHold = errors.New("deposit cannot hold")
|
|
ErrDepositNotHeld = errors.New("deposit not held")
|
|
ErrDepositHoldAmountEmpty = errors.New("deposit hold amount empty")
|
|
ErrOfflineSettlementCannotMark = errors.New("offline settlement cannot mark")
|
|
ErrInvalidSellerHandoffStatus = errors.New("invalid seller handoff status")
|
|
ErrOrderCannotHide = errors.New("order cannot hide")
|
|
)
|
|
|
|
const internalOrderHours = 24
|
|
|
|
type Service struct {
|
|
repo *Repository
|
|
}
|
|
|
|
func NewService(repo *Repository) *Service {
|
|
return &Service{repo: repo}
|
|
}
|
|
|
|
func (s *Service) Create(ctx context.Context, userID uint64, req CreateRequest) (*OrderDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
if req.ListingID == 0 {
|
|
return nil, ErrInvalidRentHours
|
|
}
|
|
return s.repo.Create(ctx, userID, req)
|
|
}
|
|
|
|
func (s *Service) Cancel(ctx context.Context, userID uint64, orderID uint64) error {
|
|
if s.repo == nil {
|
|
return ErrDependencyUnavailable
|
|
}
|
|
return s.repo.Cancel(ctx, userID, orderID)
|
|
}
|
|
|
|
func (s *Service) HideForOwner(ctx context.Context, userID uint64, orderID uint64) error {
|
|
if s.repo == nil {
|
|
return ErrDependencyUnavailable
|
|
}
|
|
if orderID == 0 {
|
|
return ErrOrderCannotHide
|
|
}
|
|
return s.repo.HideForOwner(ctx, userID, orderID)
|
|
}
|
|
|
|
func (s *Service) Pay(ctx context.Context, userID uint64, orderID uint64) error {
|
|
if s.repo == nil {
|
|
return ErrDependencyUnavailable
|
|
}
|
|
if orderID == 0 {
|
|
return ErrOrderCannotPay
|
|
}
|
|
return s.repo.Pay(ctx, userID, orderID)
|
|
}
|
|
|
|
func (s *Service) SubmitHandoff(ctx context.Context, userID uint64, orderID uint64, req SubmitHandoffRequest) (*HandoffRecordDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
if orderID == 0 || req.Content == "" {
|
|
return nil, ErrOrderCannotHandoff
|
|
}
|
|
return s.repo.SubmitHandoff(ctx, userID, orderID, req)
|
|
}
|
|
|
|
func (s *Service) ConfirmReceive(ctx context.Context, userID uint64, orderID uint64) error {
|
|
if s.repo == nil {
|
|
return ErrDependencyUnavailable
|
|
}
|
|
return s.repo.ConfirmReceive(ctx, userID, orderID)
|
|
}
|
|
|
|
func (s *Service) HandoffRecords(ctx context.Context, userID uint64, orderID uint64) ([]HandoffRecordDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
return s.repo.HandoffRecords(ctx, userID, orderID)
|
|
}
|
|
|
|
func (s *Service) SubmitReturn(ctx context.Context, userID uint64, orderID uint64, req SubmitReturnRequest) (*HandoffRecordDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
if orderID == 0 || req.Content == "" {
|
|
return nil, ErrOrderCannotReturn
|
|
}
|
|
return s.repo.SubmitReturn(ctx, userID, orderID, req)
|
|
}
|
|
|
|
func (s *Service) SubmitCheckout(ctx context.Context, userID uint64, orderID uint64, req SubmitCheckoutRequest) (*HandoffRecordDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
if orderID == 0 || req.Content == "" {
|
|
return nil, ErrCheckoutCannotSubmit
|
|
}
|
|
return s.repo.SubmitCheckout(ctx, userID, orderID, req)
|
|
}
|
|
|
|
func (s *Service) ConfirmReturn(ctx context.Context, userID uint64, orderID uint64) error {
|
|
if s.repo == nil {
|
|
return ErrDependencyUnavailable
|
|
}
|
|
return s.repo.ConfirmReturn(ctx, userID, orderID)
|
|
}
|
|
|
|
func (s *Service) ConfirmCheckout(ctx context.Context, userID uint64, orderID uint64) error {
|
|
if s.repo == nil {
|
|
return ErrDependencyUnavailable
|
|
}
|
|
return s.repo.ConfirmCheckout(ctx, userID, orderID)
|
|
}
|
|
|
|
func (s *Service) CounterCheckout(ctx context.Context, userID uint64, orderID uint64, req CounterCheckoutRequest) (*CheckoutDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
if orderID == 0 || req.Reason == "" {
|
|
return nil, ErrCheckoutCannotCounter
|
|
}
|
|
return s.repo.CounterCheckout(ctx, userID, orderID, req)
|
|
}
|
|
|
|
func (s *Service) AcceptCheckout(ctx context.Context, userID uint64, orderID uint64) error {
|
|
if s.repo == nil {
|
|
return ErrDependencyUnavailable
|
|
}
|
|
return s.repo.AcceptCheckout(ctx, userID, orderID)
|
|
}
|
|
|
|
func (s *Service) ListForUser(ctx context.Context, userID uint64, page, pageSize int, siteCode ...string) (*UserPaginatedResult, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
return s.repo.ListForUser(ctx, userID, page, pageSize, siteCode...)
|
|
}
|
|
|
|
func (s *Service) ListSellerHandoffs(ctx context.Context, userID uint64, query SellerHandoffQuery) (*SellerHandoffPaginatedResult, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
if query.Status != "" && !isSellerHandoffStatus(query.Status) {
|
|
return nil, ErrInvalidSellerHandoffStatus
|
|
}
|
|
return s.repo.ListSellerHandoffs(ctx, userID, query)
|
|
}
|
|
|
|
func (s *Service) ListAdmin(ctx context.Context, query AdminOrderQuery) (*AdminOrderListPaginatedResult, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
return s.repo.ListAdmin(ctx, query)
|
|
}
|
|
|
|
func (s *Service) FindAdmin(ctx context.Context, orderID uint64) (*OrderDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
return s.repo.FindAdmin(ctx, orderID)
|
|
}
|
|
|
|
func (s *Service) FindLatestAdminByListing(ctx context.Context, listingID uint64) (*OrderDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
if listingID == 0 {
|
|
return nil, ErrListingUnavailable
|
|
}
|
|
return s.repo.FindLatestAdminByListing(ctx, listingID)
|
|
}
|
|
|
|
func (s *Service) HandoffRecordsAdmin(ctx context.Context, orderID uint64) ([]HandoffRecordDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
return s.repo.HandoffRecordsAdmin(ctx, orderID)
|
|
}
|
|
|
|
func (s *Service) ProcessEventsAdmin(ctx context.Context, orderID uint64) ([]ProcessEventDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
return s.repo.ProcessEventsAdmin(ctx, orderID)
|
|
}
|
|
|
|
func (s *Service) AdminClose(ctx context.Context, adminID uint64, orderID uint64, req AdminActionRequest, meta AuditMeta) error {
|
|
if s.repo == nil {
|
|
return ErrDependencyUnavailable
|
|
}
|
|
if orderID == 0 || req.Reason == "" {
|
|
return ErrOrderCannotComplete
|
|
}
|
|
return s.repo.AdminClose(ctx, adminID, orderID, req, meta)
|
|
}
|
|
|
|
func (s *Service) AdminMarkAbnormal(ctx context.Context, adminID uint64, orderID uint64, req AdminActionRequest, meta AuditMeta) error {
|
|
if s.repo == nil {
|
|
return ErrDependencyUnavailable
|
|
}
|
|
if orderID == 0 || req.Reason == "" {
|
|
return ErrOrderCannotComplete
|
|
}
|
|
return s.repo.AdminMarkAbnormal(ctx, adminID, orderID, req, meta)
|
|
}
|
|
|
|
func (s *Service) AdminSeal(ctx context.Context, adminID uint64, orderID uint64, req AdminActionRequest, meta AuditMeta) error {
|
|
if s.repo == nil {
|
|
return ErrDependencyUnavailable
|
|
}
|
|
if orderID == 0 || req.Reason == "" {
|
|
return ErrOrderCannotComplete
|
|
}
|
|
return s.repo.AdminSeal(ctx, adminID, orderID, req, meta)
|
|
}
|
|
|
|
func (s *Service) AdminResetHandoff(ctx context.Context, adminID uint64, orderID uint64, req AdminActionRequest, meta AuditMeta) error {
|
|
if s.repo == nil {
|
|
return ErrDependencyUnavailable
|
|
}
|
|
if orderID == 0 || req.Reason == "" {
|
|
return ErrOrderCannotResetHandoff
|
|
}
|
|
return s.repo.AdminResetHandoff(ctx, adminID, orderID, req, meta)
|
|
}
|
|
|
|
func (s *Service) AdminPlatformHandoff(ctx context.Context, adminID uint64, orderID uint64, req PlatformHandoffRequest, meta AuditMeta) (*HandoffRecordDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
if orderID == 0 || req.Content == "" || req.Reason == "" {
|
|
return nil, ErrOrderCannotHandoff
|
|
}
|
|
return s.repo.AdminPlatformHandoff(ctx, adminID, orderID, req, meta)
|
|
}
|
|
|
|
func (s *Service) AdminForceHandoff(ctx context.Context, adminID uint64, orderID uint64, req ForceHandoffRequest, meta AuditMeta) error {
|
|
if s.repo == nil {
|
|
return ErrDependencyUnavailable
|
|
}
|
|
if orderID == 0 || req.Reason == "" {
|
|
return ErrOrderCannotForceHandoff
|
|
}
|
|
return s.repo.AdminForceHandoff(ctx, adminID, orderID, req, meta)
|
|
}
|
|
|
|
func (s *Service) AdminPlatformCheckoutConfirm(ctx context.Context, adminID uint64, orderID uint64, req AdminActionRequest, meta AuditMeta) error {
|
|
if s.repo == nil {
|
|
return ErrDependencyUnavailable
|
|
}
|
|
if orderID == 0 || req.Reason == "" {
|
|
return ErrCheckoutCannotConfirm
|
|
}
|
|
return s.repo.AdminPlatformCheckoutConfirm(ctx, adminID, orderID, req, meta)
|
|
}
|
|
|
|
func (s *Service) AdminPlatformCheckoutCounter(ctx context.Context, adminID uint64, orderID uint64, req CounterCheckoutRequest, meta AuditMeta) (*CheckoutDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
if orderID == 0 || req.Reason == "" {
|
|
return nil, ErrCheckoutCannotCounter
|
|
}
|
|
return s.repo.AdminPlatformCheckoutCounter(ctx, adminID, orderID, req, meta)
|
|
}
|
|
|
|
func (s *Service) AdminPlatformCheckoutDispute(ctx context.Context, adminID uint64, orderID uint64, req AdminActionRequest, meta AuditMeta) error {
|
|
if s.repo == nil {
|
|
return ErrDependencyUnavailable
|
|
}
|
|
if orderID == 0 || req.Reason == "" {
|
|
return ErrCheckoutCannotCounter
|
|
}
|
|
return s.repo.AdminPlatformCheckoutDispute(ctx, adminID, orderID, req, meta)
|
|
}
|
|
|
|
func (s *Service) AdminMarkOfflineSettlement(ctx context.Context, adminID uint64, orderID uint64, req OfflineSettlementRequest, meta AuditMeta) error {
|
|
if s.repo == nil {
|
|
return ErrDependencyUnavailable
|
|
}
|
|
if orderID == 0 {
|
|
return ErrOfflineSettlementCannotMark
|
|
}
|
|
return s.repo.AdminMarkOfflineSettlement(ctx, adminID, orderID, req, meta)
|
|
}
|
|
|
|
func (s *Service) AdminHoldDeposit(ctx context.Context, adminID uint64, orderID uint64, req AdminActionRequest, meta AuditMeta) error {
|
|
if s.repo == nil {
|
|
return ErrDependencyUnavailable
|
|
}
|
|
if orderID == 0 || req.Reason == "" {
|
|
return ErrDepositCannotHold
|
|
}
|
|
return s.repo.AdminHoldDeposit(ctx, adminID, orderID, req, meta)
|
|
}
|
|
|
|
func (s *Service) AdminReleaseDeposit(ctx context.Context, adminID uint64, orderID uint64, req AdminActionRequest, meta AuditMeta) error {
|
|
if s.repo == nil {
|
|
return ErrDependencyUnavailable
|
|
}
|
|
if orderID == 0 || req.Reason == "" {
|
|
return ErrDepositCannotHold
|
|
}
|
|
return s.repo.AdminReleaseDeposit(ctx, adminID, orderID, req, meta)
|
|
}
|
|
|
|
func (s *Service) AdminRefund(ctx context.Context, orderID uint64) (*RefundStatusDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
if orderID == 0 {
|
|
return nil, ErrOrderCannotComplete
|
|
}
|
|
return s.repo.AdminRefund(ctx, orderID)
|
|
}
|
|
|
|
func (s *Service) AdminRefundStatus(ctx context.Context, orderID uint64) (*RefundStatusDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
if orderID == 0 {
|
|
return nil, ErrOrderCannotComplete
|
|
}
|
|
return s.repo.AdminRefundStatus(ctx, orderID)
|
|
}
|
|
|
|
func (s *Service) AdminApproveRefund(ctx context.Context, orderID uint64) error {
|
|
if s.repo == nil {
|
|
return ErrDependencyUnavailable
|
|
}
|
|
if orderID == 0 {
|
|
return ErrOrderCannotComplete
|
|
}
|
|
return s.repo.AdminApproveRefund(ctx, orderID)
|
|
}
|
|
|
|
func (s *Service) AdminRejectRefund(ctx context.Context, orderID uint64, action string) error {
|
|
if s.repo == nil {
|
|
return ErrDependencyUnavailable
|
|
}
|
|
if orderID == 0 {
|
|
return ErrOrderCannotComplete
|
|
}
|
|
return s.repo.AdminRejectRefund(ctx, orderID, action)
|
|
}
|
|
|
|
func (s *Service) ListPendingRefund(ctx context.Context) ([]OrderDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
return s.repo.ListPendingRefund(ctx)
|
|
}
|
|
|
|
func (s *Service) PendingRefundCount(ctx context.Context) (int64, error) {
|
|
if s.repo == nil {
|
|
return 0, ErrDependencyUnavailable
|
|
}
|
|
return s.repo.PendingRefundCount(ctx)
|
|
}
|
|
|
|
func (s *Service) FindForUser(ctx context.Context, userID uint64, orderID uint64, siteCode ...string) (*OrderDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
return s.repo.FindForUser(ctx, userID, orderID, siteCode...)
|
|
}
|