rentease/internal/server/handle_payments.go
Ruidy 146787033a
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.
2025-11-21 10:09:30 +01:00

118 lines
3.1 KiB
Go

package server
import (
"net/http"
"strconv"
"github.com/go-chi/chi/v5"
u "github.com/rjNemo/underscore"
"github.com/rjNemo/rentease/internal/config"
"github.com/rjNemo/rentease/internal/service/booking"
"github.com/rjNemo/rentease/internal/service/payment"
"github.com/rjNemo/rentease/internal/view"
)
func handleCreatePayment(bs *booking.Service, ps *payment.Service, hc *config.Host) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
id, err := strconv.Atoi(chi.URLParam(r, "id"))
if err != nil {
http.Error(w, "invalid booking id", http.StatusBadRequest)
return
}
amount := 0.0
if v := r.FormValue("amount"); v != "" {
amount, err = strconv.ParseFloat(v, 64)
if err != nil {
http.Error(w, "invalid amount", http.StatusBadRequest)
return
}
}
b, err := bs.One(id)
if bookingLookupFailed(w, err) {
return
}
if _, err := ps.CreatePayment(b.ID, amount, r.FormValue("paymentMethod")); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
nb, err := bs.One(id)
if bookingLookupFailed(w, err) {
return
}
component := view.PaymentList(
u.Map(nb.Payments, func(p booking.Payment) *view.PaymentViewModel {
return paymentViewModelFromBookingPayment(p, hc.StripeAccountID)
}),
)
if err := renderTempl(w, http.StatusOK, component); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}
func handlePaymentForm(ps *payment.Service, hc *config.Host) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(chi.URLParam(r, "id"))
if err != nil {
http.Error(w, "invalid payment id", http.StatusBadRequest)
return
}
p, err := ps.Payment(id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
form := view.PaymentForm(paymentViewModelFromBookingPayment(*p, hc.StripeAccountID))
if err := renderTempl(w, http.StatusOK, form); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}
func handlePaymentUpdate(ps *payment.Service, hc *config.Host) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
id, err := strconv.Atoi(chi.URLParam(r, "id"))
if err != nil {
http.Error(w, "invalid payment id", http.StatusBadRequest)
return
}
amount := 0.0
if v := r.FormValue("amount"); v != "" {
amount, err = strconv.ParseFloat(v, 64)
if err != nil {
http.Error(w, "invalid amount", http.StatusBadRequest)
return
}
}
p, err := ps.UpdatePayment(id, amount, r.FormValue("paymentMethod"))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := renderTempl(w, http.StatusOK, view.PaymentLine(paymentViewModelFromBookingPayment(*p, hc.StripeAccountID))); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}