mirror of
https://github.com/rjNemo/payit
synced 2026-06-06 02:16:40 +00:00
Introduced LoggerMiddleware for HTTP request logging. Refactored handler methods to return http.HandlerFunc for improved composability. Updated route registration and tests to use new handler signatures.
48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
package web
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
|
|
"github.com/rjNemo/payit/internal/payments"
|
|
)
|
|
|
|
func (h *Handler) createCheckoutSession() http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
var req payments.CheckoutSessionRequest
|
|
|
|
if r.Body != nil {
|
|
defer func(body io.ReadCloser) {
|
|
_ = body.Close()
|
|
}(r.Body)
|
|
dec := json.NewDecoder(r.Body)
|
|
dec.DisallowUnknownFields()
|
|
|
|
if err := dec.Decode(&req); err != nil {
|
|
if errors.Is(err, io.EOF) {
|
|
// Empty body is acceptable; default quantity applies.
|
|
} else {
|
|
http.Error(w, "invalid request payload", http.StatusBadRequest)
|
|
return
|
|
}
|
|
} else if dec.More() {
|
|
http.Error(w, "unexpected data in request body", http.StatusBadRequest)
|
|
return
|
|
}
|
|
}
|
|
|
|
session, err := h.checkout.CreateSession(r.Context(), req)
|
|
if err != nil {
|
|
http.Error(w, "checkout session failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
if err := json.NewEncoder(w).Encode(session); err != nil {
|
|
http.Error(w, "failed to encode response", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|
|
}
|