more refactor

This commit is contained in:
matst80
2025-12-03 09:14:17 +01:00
parent a0142c2698
commit 976d44ed54
4 changed files with 122 additions and 77 deletions

View File

@@ -3,6 +3,7 @@ package main
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
@@ -61,6 +62,14 @@ func (s *CheckoutPoolServer) AdyenSessionHandler(w http.ResponseWriter, r *http.
} }
func getCheckoutIdFromNotificationItem(item webhook.NotificationRequestItem) (uint64, error) {
cartId, ok := cart.ParseCartId(item.MerchantReference)
if !ok {
return 0, errors.New("invalid cart id")
}
return uint64(cartId), nil
}
func (s *CheckoutPoolServer) AdyenHookHandler(w http.ResponseWriter, r *http.Request) { func (s *CheckoutPoolServer) AdyenHookHandler(w http.ResponseWriter, r *http.Request) {
var notificationRequest webhook.Webhook var notificationRequest webhook.Webhook
service := s.adyenClient.Checkout() service := s.adyenClient.Checkout()
@@ -91,10 +100,9 @@ func (s *CheckoutPoolServer) AdyenHookHandler(w http.ResponseWriter, r *http.Req
//s.ApplyAnywhere(r.Context(),0, &messages.PaymentEvent{PaymentId: item.PspReference, Success: item.Success, Name: item.EventCode, Data: &pbany.Any{Value: dataBytes}}) //s.ApplyAnywhere(r.Context(),0, &messages.PaymentEvent{PaymentId: item.PspReference, Success: item.Success, Name: item.EventCode, Data: &pbany.Any{Value: dataBytes}})
case "AUTHORISATION": case "AUTHORISATION":
cartId, ok := cart.ParseCartId(item.MerchantReference) cartId, err := getCheckoutIdFromNotificationItem(item)
if !ok { if err != nil {
log.Printf("invalid cart id %s", item.MerchantReference) http.Error(w, err.Error(), http.StatusBadRequest)
http.Error(w, "Invalid cart id", http.StatusBadRequest)
return return
} }
//s.Apply() //s.Apply()

View File

@@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"time"
"git.k6n.net/go-cart-actor/pkg/cart" "git.k6n.net/go-cart-actor/pkg/cart"
"git.k6n.net/go-cart-actor/pkg/checkout" "git.k6n.net/go-cart-actor/pkg/checkout"
@@ -13,6 +14,79 @@ import (
"github.com/matst80/go-redis-inventory/pkg/inventory" "github.com/matst80/go-redis-inventory/pkg/inventory"
) )
/*
*
*
* s.CheckoutHandler(func(order *CheckoutOrder, w http.ResponseWriter) error {
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)
_, err := fmt.Fprintf(w, tpl, order.HTMLSnippet)
return err
})
*/
func (s *CheckoutPoolServer) KlarnaHtmlCheckoutHandler(w http.ResponseWriter, r *http.Request, checkoutId checkout.CheckoutId) error {
orderId := r.URL.Query().Get("order_id")
var order *CheckoutOrder
var err error
if orderId == "" {
order, err = s.CreateOrUpdateCheckout(r, checkoutId)
if err != nil {
logger.Error("unable to create klarna session", "error", err)
return err
}
// s.ApplyKlarnaPaymentStarted(r.Context(), order, checkoutId)
}
order, err = s.klarnaClient.GetOrder(r.Context(), orderId)
if err != nil {
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)
_, err = fmt.Fprintf(w, tpl, order.HTMLSnippet)
return err
}
func (s *CheckoutPoolServer) KlarnaConfirmationHandler(w http.ResponseWriter, r *http.Request) {
orderId := r.PathValue("order_id")
order, err := s.klarnaClient.GetOrder(r.Context(), orderId)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
// Apply ConfirmationViewed mutation
cartId, ok := cart.ParseCartId(order.MerchantReference1)
if ok {
s.Apply(r.Context(), uint64(cartId), &messages.ConfirmationViewed{})
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
if order.Status == "checkout_complete" {
http.SetCookie(w, &http.Cookie{
Name: "cartid",
Value: "",
Path: "/",
Secure: true,
HttpOnly: true,
Expires: time.Unix(0, 0),
SameSite: http.SameSiteLaxMode,
})
}
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, tpl, order.HTMLSnippet)
}
func (s *CheckoutPoolServer) KlarnaValidationHandler(w http.ResponseWriter, r *http.Request) { func (s *CheckoutPoolServer) KlarnaValidationHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("Klarna order validation, method: %s", r.Method) log.Printf("Klarna order validation, method: %s", r.Method)
if r.Method != "POST" { if r.Method != "POST" {

View File

@@ -132,25 +132,25 @@ func (s *CheckoutPoolServer) ApplyKlarnaPaymentStarted(ctx context.Context, klar
}) })
} }
func (s *CheckoutPoolServer) CheckoutHandler(fn func(order *CheckoutOrder, w http.ResponseWriter) error) func(w http.ResponseWriter, r *http.Request) { // func (s *CheckoutPoolServer) CheckoutHandler(fn func(order *CheckoutOrder, w http.ResponseWriter) error) func(w http.ResponseWriter, r *http.Request) {
return CheckoutIdHandler(s.ProxyHandler(func(w http.ResponseWriter, r *http.Request, checkoutId checkout.CheckoutId) error { // return CheckoutIdHandler(s.ProxyHandler(func(w http.ResponseWriter, r *http.Request, checkoutId checkout.CheckoutId) error {
orderId := r.URL.Query().Get("order_id") // orderId := r.URL.Query().Get("order_id")
if orderId == "" { // if orderId == "" {
order, err := s.CreateOrUpdateCheckout(r, checkoutId) // order, err := s.CreateOrUpdateCheckout(r, checkoutId)
if err != nil { // if err != nil {
logger.Error("unable to create klarna session", "error", err) // logger.Error("unable to create klarna session", "error", err)
return err // return err
} // }
s.ApplyKlarnaPaymentStarted(r.Context(), order, checkoutId) // s.ApplyKlarnaPaymentStarted(r.Context(), order, checkoutId)
return fn(order, w) // return fn(order, w)
} // }
order, err := s.klarnaClient.GetOrder(r.Context(), orderId) // order, err := s.klarnaClient.GetOrder(r.Context(), orderId)
if err != nil { // if err != nil {
return err // return err
} // }
return fn(order, w) // return fn(order, w)
})) // }))
} // }
var ( var (
tracer = otel.Tracer(name) tracer = otel.Tracer(name)
@@ -184,7 +184,7 @@ func (s *CheckoutPoolServer) Serve(mux *http.ServeMux) {
handlerFunc(w, r) handlerFunc(w, r)
})) }))
} }
handleFunc("/payment/adyen/session", s.AdyenSessionHandler) handleFunc("/payment/adyen/session", CheckoutIdHandler(s.AdyenSessionHandler))
handleFunc("/payment/adyen/push", s.AdyenHookHandler) handleFunc("/payment/adyen/push", s.AdyenHookHandler)
handleFunc("/payment/adyen/return", s.AdyenReturnHandler) handleFunc("/payment/adyen/return", s.AdyenReturnHandler)
//handleFunc("/payment/adyen/cancel", s.AdyenCancelHandler) //handleFunc("/payment/adyen/cancel", s.AdyenCancelHandler)
@@ -200,55 +200,17 @@ func (s *CheckoutPoolServer) Serve(mux *http.ServeMux) {
orderHandler := NewAmqpOrderHandler(conn) orderHandler := NewAmqpOrderHandler(conn)
orderHandler.DefineQueue() orderHandler.DefineQueue()
mux.HandleFunc("GET /checkout", s.CheckoutHandler(func(order *CheckoutOrder, w http.ResponseWriter) error { handleFunc("GET /checkout", CheckoutIdHandler(s.ProxyHandler(s.KlarnaHtmlCheckoutHandler)))
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)
_, err := fmt.Fprintf(w, tpl, order.HTMLSnippet)
return err
}))
mux.HandleFunc("GET /confirmation/{order_id}", func(w http.ResponseWriter, r *http.Request) { handleFunc("GET /confirmation/{order_id}", s.KlarnaConfirmationHandler)
orderId := r.PathValue("order_id") // handleFunc("GET /checkout", s.CheckoutHandler(func(order *CheckoutOrder, w http.ResponseWriter) error {
order, err := s.klarnaClient.GetOrder(r.Context(), orderId) // 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\")")
if err != nil { // w.WriteHeader(http.StatusOK)
w.WriteHeader(http.StatusInternalServerError) // _, err := fmt.Fprintf(w, tpl, order.HTMLSnippet)
w.Write([]byte(err.Error())) // return err
return // }))
}
// Apply ConfirmationViewed mutation
cartId, ok := cart.ParseCartId(order.MerchantReference1)
if ok {
a.Apply(r.Context(), uint64(cartId), &messages.ConfirmationViewed{})
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
if order.Status == "checkout_complete" {
http.SetCookie(w, &http.Cookie{
Name: "cartid",
Value: "",
Path: "/",
Secure: true,
HttpOnly: true,
Expires: time.Unix(0, 0),
SameSite: http.SameSiteLaxMode,
})
}
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, tpl, order.HTMLSnippet)
})
handleFunc("GET /checkout", s.CheckoutHandler(func(order *CheckoutOrder, w http.ResponseWriter) error {
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)
_, err := fmt.Fprintf(w, tpl, order.HTMLSnippet)
return err
}))
handleFunc("GET /confirmation/{order_id}", func(w http.ResponseWriter, r *http.Request) { handleFunc("GET /confirmation/{order_id}", func(w http.ResponseWriter, r *http.Request) {
orderId := r.PathValue("order_id") orderId := r.PathValue("order_id")
@@ -264,8 +226,9 @@ func (s *CheckoutPoolServer) Serve(mux *http.ServeMux) {
s.Apply(r.Context(), uint64(checkoutId), &messages.ConfirmationViewed{}) s.Apply(r.Context(), uint64(checkoutId), &messages.ConfirmationViewed{})
// Callback to cart // Callback to cart
cartId := cart.CartId(checkoutId) // Assuming same
s.cartClient.ApplyMutation(cartId, &messages.OrderCreated{OrderId: order.ID, Status: order.Status}) // cartId := cart.CartId(checkoutId) // Assuming same
// s.cartClient.ApplyMutation(cartId, &messages.OrderCreated{OrderId: order.ID, Status: order.Status})
w.Header().Set("Content-Type", "text/html; charset=utf-8") w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)

View File

@@ -64,7 +64,7 @@ func (a *CheckoutPoolServer) reserveInventory(ctx context.Context, grain *checko
return nil return nil
} }
func CheckoutIdHandler(fn func(checkoutId checkout.CheckoutId, w http.ResponseWriter, r *http.Request) error) func(w http.ResponseWriter, r *http.Request) { func CheckoutIdHandler(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) {
var id checkout.CheckoutId var id checkout.CheckoutId
raw := r.PathValue("id") raw := r.PathValue("id")
@@ -81,7 +81,7 @@ func CheckoutIdHandler(fn func(checkoutId checkout.CheckoutId, w http.ResponseWr
} }
} }
err := fn(id, w, r) err := fn(w, r, id)
if err != nil { if err != nil {
log.Printf("Server error, not remote error: %v\n", err) log.Printf("Server error, not remote error: %v\n", err)
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
@@ -90,8 +90,8 @@ func CheckoutIdHandler(fn func(checkoutId checkout.CheckoutId, w http.ResponseWr
} }
} }
func (s *CheckoutPoolServer) ProxyHandler(fn func(w http.ResponseWriter, r *http.Request, checkoutId checkout.CheckoutId) error) func(checkoutId checkout.CheckoutId, w http.ResponseWriter, r *http.Request) error { func (s *CheckoutPoolServer) ProxyHandler(fn func(w http.ResponseWriter, r *http.Request, checkoutId checkout.CheckoutId) error) func(w http.ResponseWriter, r *http.Request, checkoutId checkout.CheckoutId) error {
return func(checkoutId checkout.CheckoutId, w http.ResponseWriter, r *http.Request) error { return func(w http.ResponseWriter, r *http.Request, checkoutId checkout.CheckoutId) error {
if ownerHost, ok := s.OwnerHost(uint64(checkoutId)); ok { if ownerHost, ok := s.OwnerHost(uint64(checkoutId)); ok {
ctx, span := tracer.Start(r.Context(), "proxy") ctx, span := tracer.Start(r.Context(), "proxy")
defer span.End() defer span.End()