mirror of
https://github.com/rjNemo/payit
synced 2026-06-06 02:16:40 +00:00
- Integrate Stripe Go SDK and implement a checkout service for creating Stripe Checkout sessions for a demo product. - Add request/response types for checkout sessions. - Create HTTP handler for /api/checkout to initiate sessions via POST. - Provide comprehensive unit tests for Stripe client and web handler. - Wire Stripe-backed endpoint in server setup.
52 lines
1.2 KiB
Go
52 lines
1.2 KiB
Go
package web
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
|
|
stripe "github.com/rjNemo/payit/internal/stripe"
|
|
)
|
|
|
|
func (h *Handler) createCheckoutSession(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
w.Header().Set("Allow", http.MethodPost)
|
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
defer r.Body.Close()
|
|
|
|
var req stripe.CheckoutSessionRequest
|
|
if r.Body != nil {
|
|
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
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|