mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-11 13:16:50 +00:00
Moves all payment-related logic (manual payments, Stripe sync, webhook handling) from the booking service into a dedicated payment service (`internal/service/payment`). Updates server, cron, and handler wiring to inject and use the new payment service. Adjusts tests, routes, and documentation to reflect the new separation of concerns. This improves cohesion, clarifies responsibilities, and prepares for future payment features. No database schema changes are introduced.
66 lines
1.8 KiB
Go
66 lines
1.8 KiB
Go
package cron
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
"time"
|
|
|
|
"github.com/rjNemo/rentease/internal/config"
|
|
"github.com/rjNemo/rentease/internal/driver/database"
|
|
"github.com/rjNemo/rentease/internal/driver/stripe"
|
|
"github.com/rjNemo/rentease/internal/repository/booking"
|
|
bookingservice "github.com/rjNemo/rentease/internal/service/booking"
|
|
"github.com/rjNemo/rentease/internal/service/payment"
|
|
)
|
|
|
|
// JobStripePaymentSync synchronises Stripe payments for the last 24 hours. It is
|
|
// safe to run multiple times thanks to the repository upsert semantics.
|
|
func JobStripePaymentSync() error {
|
|
ctx := context.Background()
|
|
|
|
cfg, err := config.New(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("error loading config: %w", err)
|
|
}
|
|
|
|
if cfg.StripeSecretKey == "" {
|
|
slog.Default().Warn("stripe secret key missing; skipping stripe sync job")
|
|
return nil
|
|
}
|
|
|
|
db, err := database.New(cfg.DatabaseURL)
|
|
if err != nil {
|
|
return fmt.Errorf("error connecting to database: %w", err)
|
|
}
|
|
|
|
// Auto-migrate payment schema if necessary.
|
|
if err := database.Migrate(db, &bookingservice.Booking{}, &bookingservice.Item{}, &bookingservice.Payment{}); err != nil {
|
|
return fmt.Errorf("error migrating database: %w", err)
|
|
}
|
|
|
|
store := booking.NewPgStore(db)
|
|
|
|
opts := []stripe.Option{}
|
|
|
|
client, err := stripe.New(cfg.StripeSecretKey, opts...)
|
|
if err != nil {
|
|
return fmt.Errorf("error creating stripe client: %w", err)
|
|
}
|
|
|
|
logger := slog.Default()
|
|
service, err := payment.NewService(logger, store, client)
|
|
if err != nil {
|
|
return fmt.Errorf("error creating payment service: %w", err)
|
|
}
|
|
|
|
to := time.Now().UTC()
|
|
from := to.Add(-24 * time.Hour)
|
|
|
|
if err := service.SyncStripePayments(ctx, from, to); err != nil {
|
|
return fmt.Errorf("error syncing stripe payments: %w", err)
|
|
}
|
|
|
|
slog.Default().Info("stripe payment sync job completed", slog.Time("from", from), slog.Time("to", to))
|
|
return nil
|
|
}
|