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
|
// 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
|
// Initialize checkout with cart state wrapped in Any
|
||||||
cartStateAny := &messages.InitializeCheckout{
|
cartStateAny := &messages.InitializeCheckout{
|
||||||
@@ -225,13 +232,14 @@ func (s *CheckoutPoolServer) StartCheckoutHandler(w http.ResponseWriter, r *http
|
|||||||
|
|
||||||
result, err := s.ApplyLocal(r.Context(), checkoutId, cartStateAny)
|
result, err := s.ApplyLocal(r.Context(), checkoutId, cartStateAny)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
setCheckoutCookie(w, 0, r.TLS != nil)
|
||||||
logger.Error("failed to initialize checkout", "error", err)
|
logger.Error("failed to initialize checkout", "error", err)
|
||||||
http.Error(w, "failed to initialize checkout", http.StatusInternalServerError)
|
http.Error(w, "failed to initialize checkout", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set checkout cookie
|
// 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 {
|
if err := s.WriteResult(w, result.Result); err != nil {
|
||||||
logger.Error("failed to write result", "error", err)
|
logger.Error("failed to write result", "error", err)
|
||||||
|
|||||||
@@ -68,6 +68,17 @@ func (a *CheckoutPoolServer) reserveInventory(ctx context.Context, grain *checko
|
|||||||
const checkoutCookieName = "checkoutid"
|
const checkoutCookieName = "checkoutid"
|
||||||
|
|
||||||
func setCheckoutCookie(w http.ResponseWriter, checkoutId checkout.CheckoutId, tls bool) {
|
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{
|
http.SetCookie(w, &http.Cookie{
|
||||||
Name: checkoutCookieName,
|
Name: checkoutCookieName,
|
||||||
Value: checkoutId.String(),
|
Value: checkoutId.String(),
|
||||||
@@ -78,6 +89,7 @@ func setCheckoutCookie(w http.ResponseWriter, checkoutId checkout.CheckoutId, tl
|
|||||||
SameSite: http.SameSiteLaxMode,
|
SameSite: http.SameSiteLaxMode,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func CookieCheckoutIdHandler(fn func(w http.ResponseWriter, r *http.Request, checkoutId checkout.CheckoutId) error) func(w http.ResponseWriter, r *http.Request) {
|
func CookieCheckoutIdHandler(fn func(w http.ResponseWriter, r *http.Request, checkoutId checkout.CheckoutId) error) func(w http.ResponseWriter, r *http.Request) {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|||||||
Reference in New Issue
Block a user