mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-06 02:36:49 +00:00
Some checks are pending
CI / checks (push) Waiting to run
* 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.
146 lines
3.7 KiB
Go
146 lines
3.7 KiB
Go
package payment
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
"math"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/rjNemo/rentease/internal/config"
|
|
"github.com/rjNemo/rentease/internal/driver/stripe"
|
|
"github.com/rjNemo/rentease/internal/service/booking"
|
|
)
|
|
|
|
// Payment fetches a payment by id.
|
|
func (ps Service) Payment(id int) (*Payment, error) {
|
|
p, err := ps.store.GetPayment(id)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("fetch payment %d: %w", id, err)
|
|
}
|
|
return p, nil
|
|
}
|
|
|
|
// CreatePayment creates a manual payment for the provided booking id.
|
|
func (ps Service) CreatePayment(bid int, amount float64, paymentMethod string) (*Payment, error) {
|
|
p, err := ps.store.CreatePayment(&Payment{
|
|
BookingID: uint(bid),
|
|
Amount: amount,
|
|
PaymentMethod: config.PaymentMethod(paymentMethod),
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return p, nil
|
|
}
|
|
|
|
// UpdatePayment updates amount and method for the provided payment id.
|
|
func (ps Service) UpdatePayment(id int, amount float64, paymentMethod string) (*Payment, error) {
|
|
p, err := ps.store.UpdatePayment(id, amount, paymentMethod)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return p, nil
|
|
}
|
|
|
|
// CreateStripePaymentLink generates a Stripe payment link for the outstanding balance of a booking.
|
|
func (ps Service) CreateStripePaymentLink(ctx context.Context, bookingID int) (string, error) {
|
|
if ps.stripe == nil {
|
|
return "", ErrStripeClientNotConfigured
|
|
}
|
|
|
|
b, err := ps.store.Get(bookingID)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if b == nil || b.ID == 0 {
|
|
return "", booking.ErrBookingNotFound
|
|
}
|
|
|
|
outstanding := calculateOutstandingBalance(b)
|
|
if outstanding <= 0 {
|
|
return "", ErrNoOutstandingBalance
|
|
}
|
|
|
|
description := fmt.Sprintf("Payment for booking %d", b.ID)
|
|
if name := strings.TrimSpace(b.Name); name != "" {
|
|
description = fmt.Sprintf("Payment for %s", name)
|
|
}
|
|
|
|
url, err := ps.stripe.CreatePaymentLink(ctx, stripe.CreatePaymentLinkParams{
|
|
Amount: outstanding,
|
|
Currency: "eur",
|
|
BookingID: uint(b.ID),
|
|
Description: description,
|
|
PaymentMethodTypes: []string{"card", "sepa_debit"},
|
|
})
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return url, nil
|
|
}
|
|
|
|
// SyncStripePayments pulls Stripe payments within the provided time window and
|
|
// upserts them into the local datastore. Payments lacking booking metadata are skipped.
|
|
func (ps Service) SyncStripePayments(ctx context.Context, from, to time.Time) error {
|
|
if ps.stripe == nil {
|
|
return ErrStripeClientNotConfigured
|
|
}
|
|
|
|
payments, err := ps.stripe.ListPayments(ctx, stripe.ListPaymentsParams{From: from, To: to})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var multi error
|
|
for _, payment := range payments {
|
|
if payment.BookingID == nil {
|
|
ps.logger.Warn("stripe payment missing booking metadata", slog.String("payment_id", payment.ID))
|
|
continue
|
|
}
|
|
|
|
bookingID := uint(*payment.BookingID)
|
|
stripeID := payment.ID
|
|
status := strings.ToLower(payment.Status)
|
|
|
|
_, err = ps.store.UpsertStripePayment(&Payment{
|
|
BookingID: bookingID,
|
|
Amount: payment.Amount,
|
|
PaymentMethod: mapStripeMethod(payment.PaymentMethod),
|
|
StripePaymentID: &stripeID,
|
|
StripeStatus: &status,
|
|
})
|
|
if err != nil {
|
|
multi = errors.Join(multi, err)
|
|
ps.logger.Error("failed to upsert stripe payment", slog.String("payment_id", payment.ID), slog.Any("error", err))
|
|
}
|
|
}
|
|
|
|
return multi
|
|
}
|
|
|
|
func calculateOutstandingBalance(b *booking.Booking) float64 {
|
|
if b == nil {
|
|
return 0
|
|
}
|
|
|
|
var total float64
|
|
for _, item := range b.Items {
|
|
total += item.Price * float64(item.Quantity)
|
|
}
|
|
|
|
var paid float64
|
|
for _, payment := range b.Payments {
|
|
paid += payment.Amount
|
|
}
|
|
|
|
outstanding := total - paid
|
|
outstanding = math.Round(outstanding*100) / 100
|
|
if outstanding < 0 {
|
|
return 0
|
|
}
|
|
return outstanding
|
|
}
|