update checkout handler
This commit is contained in:
@@ -233,61 +233,14 @@ func main() {
|
|||||||
w.Write([]byte("ok"))
|
w.Write([]byte("ok"))
|
||||||
})
|
})
|
||||||
|
|
||||||
mux.HandleFunc("/checkout", func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc("/checkout", syncedServer.CheckoutHandler(func(order *CheckoutOrder, w http.ResponseWriter) error {
|
||||||
orderId := r.URL.Query().Get("order_id")
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
order := &CheckoutOrder{}
|
w.Header().Set("Permissions-Policy", "payment=(self \"https://js.stripe.com\" \"https://m.stripe.network\" \"https://js.playground.kustom.co\")")
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
if orderId == "" {
|
_, err := fmt.Fprintf(w, tpl, order.HTMLSnippet)
|
||||||
cookie, err := r.Cookie("cartid")
|
|
||||||
if err != nil {
|
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
|
||||||
w.Write([]byte(err.Error()))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if cookie.Value == "" {
|
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
|
||||||
w.Write([]byte("no cart id to checkout is empty"))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
parsed, ok := cart.ParseCartId(cookie.Value)
|
|
||||||
if !ok {
|
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
|
||||||
w.Write([]byte("invalid cart id format"))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
cartId := parsed
|
|
||||||
syncedServer.ProxyHandler(func(w http.ResponseWriter, r *http.Request, cartId cart.CartId) error {
|
|
||||||
order, err = syncedServer.CreateOrUpdateCheckout(r.Host, cartId)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}))
|
||||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
||||||
w.Header().Set("Permissions-Policy", "payment=(self \"https://js.stripe.com\" \"https://m.stripe.network\" \"https://js.playground.kustom.co\")")
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
fmt.Fprintf(w, tpl, order.HTMLSnippet)
|
|
||||||
return nil
|
|
||||||
})(cartId, w, r)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
|
||||||
w.Write([]byte(err.Error()))
|
|
||||||
}
|
|
||||||
|
|
||||||
// v2: Apply now returns *CartGrain; order creation handled inside grain (no payload to unmarshal)
|
|
||||||
} else {
|
|
||||||
order, err = klarnaClient.GetOrder(orderId)
|
|
||||||
if err != nil {
|
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
|
||||||
w.Write([]byte(err.Error()))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
||||||
w.Header().Set("Permissions-Policy", "payment=(self \"https://js.stripe.com\" \"https://m.stripe.network\" \"https://js.playground.kustom.co\")")
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
fmt.Fprintf(w, tpl, order.HTMLSnippet)
|
|
||||||
}
|
|
||||||
|
|
||||||
})
|
|
||||||
mux.HandleFunc("/confirmation/{order_id}", func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc("/confirmation/{order_id}", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
orderId := r.PathValue("order_id")
|
orderId := r.PathValue("order_id")
|
||||||
|
|||||||
@@ -488,6 +488,24 @@ func (s *PoolServer) SubscriptionDetailsHandler(w http.ResponseWriter, r *http.R
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *PoolServer) CheckoutHandler(fn func(order *CheckoutOrder, w http.ResponseWriter) error) func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
return CookieCartIdHandler(s.ProxyHandler(func(w http.ResponseWriter, r *http.Request, cartId cart.CartId) error {
|
||||||
|
orderId := r.URL.Query().Get("order_id")
|
||||||
|
if orderId == "" {
|
||||||
|
order, err := s.CreateOrUpdateCheckout(r.Host, cartId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return fn(order, w)
|
||||||
|
}
|
||||||
|
order, err := s.klarnaClient.GetOrder(orderId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return fn(order, w)
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
func (s *PoolServer) RemoveVoucherHandler(w http.ResponseWriter, r *http.Request, cartId cart.CartId) error {
|
func (s *PoolServer) RemoveVoucherHandler(w http.ResponseWriter, r *http.Request, cartId cart.CartId) error {
|
||||||
|
|
||||||
idStr := r.PathValue("voucherId")
|
idStr := r.PathValue("voucherId")
|
||||||
|
|||||||
Reference in New Issue
Block a user