diff --git a/main.go b/main.go index 9b051a1..263b3d1 100644 --- a/main.go +++ b/main.go @@ -199,37 +199,48 @@ func main() { w.Write([]byte("ok")) }) mux.HandleFunc("/checkout", func(w http.ResponseWriter, r *http.Request) { - 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 - } - cartId := ToCartId(cookie.Value) - reply, err := syncedServer.pool.Process(cartId, Message{ - Type: CreateCheckoutOrderType, - Content: &messages.CreateCheckoutOrder{ - Terms: "https://slask-finder.tornberg.me/terms", - Checkout: "https://slask-finder.tornberg.me/checkout?order_id={checkout.order.id}", - Confirmation: "https://slask-finder.tornberg.me/confirmation/{checkout.order.id}", - Push: "https://cart.tornberg.me/push?order_id={checkout.order.id}", - }, - }) - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - w.Write([]byte(err.Error())) - } - order := CheckoutOrder{} - err = json.Unmarshal(reply.Payload, &order) - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - w.Write([]byte(err.Error())) - return + orderId := r.URL.Query().Get("order_id") + order := &CheckoutOrder{} + if orderId == "" { + 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 + } + cartId := ToCartId(cookie.Value) + reply, err := syncedServer.pool.Process(cartId, Message{ + Type: CreateCheckoutOrderType, + Content: &messages.CreateCheckoutOrder{ + Terms: "https://slask-finder.tornberg.me/terms", + Checkout: "https://slask-finder.tornberg.me/checkout?order_id={checkout.order.id}", + Confirmation: "https://slask-finder.tornberg.me/confirmation/{checkout.order.id}", + Push: "https://cart.tornberg.me/push?order_id={checkout.order.id}", + }, + }) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) + } + err = json.Unmarshal(reply.Payload, &order) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) + return + } + } else { + prevOrder, err := KlarnaInstance.GetOrder(orderId) + if err != nil { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte(err.Error())) + return + } + order = prevOrder } w.Header().Set("Content-Type", "text/html; charset=utf-8") w.WriteHeader(http.StatusOK)