rentease/internal/cron/job_stripe_sync.go
Ruidy 23f3ceec21
Some checks are pending
CI / checks (push) Waiting to run
Feat/stripe integration (#50)
* feat(stripe): add Stripe payment sync and webhook support

Introduce Stripe integration for automatic payment ingestion and refund
tracking. Adds new fields to the payment model for Stripe IDs and
status,
Stripe client driver, sync service, cron job, manual API endpoint, and
public webhook handler for real-time updates. Includes tests and
documentation. Manual cash entry remains supported.

* chore(stripe): upgrade to stripe-go v83

Upgrade Stripe SDK from v79 to v83 across the codebase. Update all
imports to use github.com/stripe/stripe-go/v83 and refactor client usage
to match the new API, including changes to PaymentIntents listing.
Update documentation and plans to reference the new version. Remove
references to the old version from go.mod and go.sum.

* refactor(payment): extract payment logic to new service

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.

* chore(ci): add Go and templ setup to CI workflow

This update enhances the CI workflow by adding steps to set up Go using
the version specified in go.mod, add the Go bin directory to the PATH,
and install the templ code generation tool. These additions ensure that
Go-based tooling is available for subsequent CI steps.
2025-11-21 15:47:01 +01:00

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
}