mirror of
https://github.com/rjNemo/payit
synced 2026-06-06 02:16:40 +00:00
refactor: simplify checkout handler
This commit is contained in:
parent
7e1e6e1df9
commit
4e36eb9692
4 changed files with 12 additions and 15 deletions
|
|
@ -36,8 +36,8 @@ func (s *Service) CreateSession(ctx context.Context, req CheckoutSessionRequest)
|
||||||
quantity = 1
|
quantity = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
params := &stripe.CheckoutSessionParams{}
|
params := &stripe.CheckoutSessionParams{}
|
||||||
params.Context = ctx
|
params.Context = ctx
|
||||||
params.SuccessURL = stripe.String(s.product.SuccessURL)
|
params.SuccessURL = stripe.String(s.product.SuccessURL)
|
||||||
params.CancelURL = stripe.String(s.product.CancelURL)
|
params.CancelURL = stripe.String(s.product.CancelURL)
|
||||||
params.Mode = stripe.String(string(stripe.CheckoutSessionModePayment))
|
params.Mode = stripe.String(string(stripe.CheckoutSessionModePayment))
|
||||||
|
|
|
||||||
|
|
@ -10,16 +10,13 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func (h *Handler) createCheckoutSession(w http.ResponseWriter, r *http.Request) {
|
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
|
var req stripe.CheckoutSessionRequest
|
||||||
|
|
||||||
if r.Body != nil {
|
if r.Body != nil {
|
||||||
|
defer func(body io.ReadCloser) {
|
||||||
|
_ = body.Close()
|
||||||
|
}(r.Body)
|
||||||
|
|
||||||
dec := json.NewDecoder(r.Body)
|
dec := json.NewDecoder(r.Body)
|
||||||
dec.DisallowUnknownFields()
|
dec.DisallowUnknownFields()
|
||||||
|
|
||||||
|
|
@ -30,9 +27,7 @@ func (h *Handler) createCheckoutSession(w http.ResponseWriter, r *http.Request)
|
||||||
http.Error(w, "invalid request payload", http.StatusBadRequest)
|
http.Error(w, "invalid request payload", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
} else if dec.More() {
|
||||||
|
|
||||||
if dec.More() {
|
|
||||||
http.Error(w, "unexpected data in request body", http.StatusBadRequest)
|
http.Error(w, "unexpected data in request body", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -110,11 +110,13 @@ func TestCreateCheckoutSessionStripeFailure(t *testing.T) {
|
||||||
|
|
||||||
func TestCreateCheckoutSessionMethodNotAllowed(t *testing.T) {
|
func TestCreateCheckoutSessionMethodNotAllowed(t *testing.T) {
|
||||||
handler := &Handler{checkout: &fakeCheckoutService{}}
|
handler := &Handler{checkout: &fakeCheckoutService{}}
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
mux.HandleFunc("POST /api/checkout", handler.createCheckoutSession)
|
||||||
|
|
||||||
req := httptest.NewRequest(http.MethodGet, "/api/checkout", http.NoBody)
|
req := httptest.NewRequest(http.MethodGet, "/api/checkout", http.NoBody)
|
||||||
rec := httptest.NewRecorder()
|
rec := httptest.NewRecorder()
|
||||||
|
|
||||||
handler.createCheckoutSession(rec, req)
|
mux.ServeHTTP(rec, req)
|
||||||
|
|
||||||
if rec.Code != http.StatusMethodNotAllowed {
|
if rec.Code != http.StatusMethodNotAllowed {
|
||||||
t.Fatalf("expected status 405, got %d", rec.Code)
|
t.Fatalf("expected status 405, got %d", rec.Code)
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@ func NewServer(cfg config.Config) http.Handler {
|
||||||
h := &Handler{cfg: cfg, checkout: checkoutSvc}
|
h := &Handler{cfg: cfg, checkout: checkoutSvc}
|
||||||
|
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
mux.HandleFunc("/api/checkout", h.createCheckoutSession)
|
mux.HandleFunc("POST /api/checkout", h.createCheckoutSession)
|
||||||
|
|
||||||
return mux
|
return mux
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue