diff options
Diffstat (limited to 'internal/billing/ledger.go')
| -rw-r--r-- | internal/billing/ledger.go | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/internal/billing/ledger.go b/internal/billing/ledger.go index 28cbe4c..c408cfe 100644 --- a/internal/billing/ledger.go +++ b/internal/billing/ledger.go @@ -73,6 +73,44 @@ func (s *Service) ListLedger(ctx context.Context, tenantID string, limit int) ([ return result, rows.Err() } +func (s *Service) ListTopUpOrders(ctx context.Context, tenantID string, limit int) ([]TopUpOrder, error) { + if limit < 1 || limit > 200 { + limit = 50 + } + rows, err := s.db.Query(ctx, `SELECT id::text,tenant_id::text,amount_minor,amount_micros,currency,status, + COALESCE(stripe_session_id,''),COALESCE(checkout_url,''),created_at,paid_at FROM topup_orders + WHERE tenant_id=$1 ORDER BY created_at DESC LIMIT $2`, tenantID, limit) + if err != nil { + return nil, fmt.Errorf("query top-up orders: %w", err) + } + defer rows.Close() + result := make([]TopUpOrder, 0) + for rows.Next() { + var item TopUpOrder + if err := rows.Scan(&item.ID, &item.TenantID, &item.AmountMinor, &item.AmountMicros, &item.Currency, &item.Status, + &item.StripeSessionID, &item.CheckoutURL, &item.CreatedAt, &item.PaidAt); err != nil { + return nil, err + } + result = append(result, item) + } + return result, rows.Err() +} + +func (s *Service) GetTopUpOrder(ctx context.Context, tenantID, orderID string) (TopUpOrder, error) { + var result TopUpOrder + err := s.db.QueryRow(ctx, `SELECT id::text,tenant_id::text,amount_minor,amount_micros,currency,status, + COALESCE(stripe_session_id,''),COALESCE(checkout_url,''),created_at,paid_at FROM topup_orders + WHERE id=$1 AND tenant_id=$2`, orderID, tenantID).Scan(&result.ID, &result.TenantID, &result.AmountMinor, &result.AmountMicros, + &result.Currency, &result.Status, &result.StripeSessionID, &result.CheckoutURL, &result.CreatedAt, &result.PaidAt) + if errors.Is(err, pgx.ErrNoRows) { + return TopUpOrder{}, ErrTopUpOrderNotFound + } + if err != nil { + return TopUpOrder{}, fmt.Errorf("get top-up order: %w", err) + } + return result, nil +} + func (s *Service) AdjustBalance(ctx context.Context, input AdjustmentInput) (LedgerEntry, error) { input.TenantID = strings.TrimSpace(input.TenantID) input.Description = normalizeDescription(input.Description) |
