package payments import ( "context" "github.com/google/uuid" ) // providerAdmin tags an operator-initiated refund in the ledger, distinct from a rail's own refund // (robokassa / vk / telegram). The refund idempotency key (providerAdmin, order id) allows exactly // one manual full refund per order. const providerAdmin = "admin" // RefundOrderFull refunds a paid order in full at the operator's request, recording the reversal // only. It is for the rails that have no refund API of our own to call: the operator performs the // actual money refund there by hand (VK support, Telegram refundStarPayment, the Robokassa cabinet), // and this records it under the operator's own idempotency key. func (s *Service) RefundOrderFull(ctx context.Context, orderID uuid.UUID) (RefundOutcome, error) { return s.RefundOrderFullAs(ctx, orderID, providerAdmin, orderID.String()) } // RefundOrderFullAs refunds a paid order in full and records it under the given provider and refund // id: it revokes the funded chips best-effort (floored at 0, never negative — D27), appends a refund // ledger row and is idempotent on (provider, providerRefundID), so a second call reports // AlreadyRefunded. Callers whose rail moved the money through an API pass that rail's own refund id, // which keeps the ledger reconcilable against the provider's records; the refund id is distinct from // the fund's payment id, so the two rows coexist under the same partial-unique index. // // The provider-side money movement must already have succeeded when this is called — the ledger must // never claim a refund that did not happen. func (s *Service) RefundOrderFullAs(ctx context.Context, orderID uuid.UUID, provider, providerRefundID string) (RefundOutcome, error) { o, err := s.store.orderByID(ctx, orderID) if err != nil { return RefundOutcome{}, err } refunded, err := MoneyFromMinor(o.expectedAmount, Currency(o.currency)) if err != nil { return RefundOutcome{}, err } return s.store.refund(ctx, orderID, provider, providerRefundID, refunded, s.clock()) } // LedgerExportRow is one append-only ledger row for the tax / reconciliation export, carrying the // account it belongs to alongside the entry fields. type LedgerExportRow struct { AccountID string LedgerEntry } // LedgerExport reads the entire append-only ledger (all accounts, newest first) for a CSV/JSON // export — tax reporting and future rail reconciliation. Uncached, admin-only. func (s *Service) LedgerExport(ctx context.Context) ([]LedgerExportRow, error) { return s.store.allLedger(ctx) }