clear cookie if failing
This commit is contained in:
@@ -210,7 +210,14 @@ func (s *CheckoutPoolServer) StartCheckoutHandler(w http.ResponseWriter, r *http
|
||||
}
|
||||
|
||||
// Create checkout with same ID as cart
|
||||
checkoutId := checkout.CheckoutId(cartId)
|
||||
var checkoutId checkout.CheckoutId = cart.MustNewCartId()
|
||||
cookie, err := r.Cookie(checkoutCookieName)
|
||||
if err == nil {
|
||||
parsed, ok := cart.ParseCartId(cookie.Value)
|
||||
if ok {
|
||||
checkoutId = parsed
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize checkout with cart state wrapped in Any
|
||||
cartStateAny := &messages.InitializeCheckout{
|
||||
@@ -225,13 +232,14 @@ func (s *CheckoutPoolServer) StartCheckoutHandler(w http.ResponseWriter, r *http
|
||||
|
||||
result, err := s.ApplyLocal(r.Context(), checkoutId, cartStateAny)
|
||||
if err != nil {
|
||||
setCheckoutCookie(w, 0, r.TLS != nil)
|
||||
logger.Error("failed to initialize checkout", "error", err)
|
||||
http.Error(w, "failed to initialize checkout", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Set checkout cookie
|
||||
setCheckoutCookie(w, result.Result.Id, r.TLS != nil)
|
||||
setCheckoutCookie(w, checkoutId, r.TLS != nil)
|
||||
|
||||
if err := s.WriteResult(w, result.Result); err != nil {
|
||||
logger.Error("failed to write result", "error", err)
|
||||
|
||||
@@ -68,6 +68,17 @@ func (a *CheckoutPoolServer) reserveInventory(ctx context.Context, grain *checko
|
||||
const checkoutCookieName = "checkoutid"
|
||||
|
||||
func setCheckoutCookie(w http.ResponseWriter, checkoutId checkout.CheckoutId, tls bool) {
|
||||
if checkoutId == 0 {
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: checkoutCookieName,
|
||||
Value: checkoutId.String(),
|
||||
Secure: tls,
|
||||
HttpOnly: true,
|
||||
Path: "/",
|
||||
Expires: time.Unix(0, 0),
|
||||
SameSite: http.SameSiteLaxMode,
|
||||
})
|
||||
} else {
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: checkoutCookieName,
|
||||
Value: checkoutId.String(),
|
||||
@@ -77,6 +88,7 @@ func setCheckoutCookie(w http.ResponseWriter, checkoutId checkout.CheckoutId, tl
|
||||
Expires: time.Now().AddDate(0, 0, 14),
|
||||
SameSite: http.SameSiteLaxMode,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func CookieCheckoutIdHandler(fn func(w http.ResponseWriter, r *http.Request, checkoutId checkout.CheckoutId) error) func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
Reference in New Issue
Block a user