update
All checks were successful
Build and Publish / BuildAndDeployAmd64 (push) Successful in 1m4s
Build and Publish / BuildAndDeployArm64 (push) Successful in 6m18s

This commit is contained in:
matst80
2025-12-04 08:51:59 +01:00
parent 7606c1f9df
commit 82b64ee8d4
3 changed files with 83 additions and 81 deletions

View File

@@ -158,8 +158,9 @@ func main() {
} else { } else {
for _, update := range res { for _, update := range res {
for _, item := range ret.Items { for _, item := range ret.Items {
if matchesSkuAndLocation(update, *item) { if matchesSkuAndLocation(update, *item) && update.Quantity != uint32(item.Stock) {
item.Quantity = uint16(update.Quantity) // maybe apply an update to give visibility to the cart
item.Stock = uint16(update.Quantity)
} }
} }
} }

View File

@@ -18,7 +18,6 @@ import (
"github.com/adyen/adyen-go-api-library/v21/src/common" "github.com/adyen/adyen-go-api-library/v21/src/common"
"github.com/adyen/adyen-go-api-library/v21/src/hmacvalidator" "github.com/adyen/adyen-go-api-library/v21/src/hmacvalidator"
"github.com/adyen/adyen-go-api-library/v21/src/webhook" "github.com/adyen/adyen-go-api-library/v21/src/webhook"
"github.com/google/uuid"
"google.golang.org/protobuf/types/known/anypb" "google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/timestamppb" "google.golang.org/protobuf/types/known/timestamppb"
) )
@@ -75,7 +74,7 @@ func getCheckoutIdFromNotificationItem(item webhook.NotificationRequestItem) (ui
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()
if err := json.NewDecoder(r.Body).Decode(&notificationRequest); err != nil { if err := json.NewDecoder(r.Body).Decode(&notificationRequest); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)
return return
@@ -91,18 +90,6 @@ func (s *CheckoutPoolServer) AdyenHookHandler(w http.ResponseWriter, r *http.Req
http.Error(w, "Invalid HMAC", http.StatusUnauthorized) http.Error(w, "Invalid HMAC", http.StatusUnauthorized)
return return
} else { } else {
checkoutId, err := getCheckoutIdFromNotificationItem(item)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Check if this checkout is owned by another host
if host, ok := s.OwnerHost(checkoutId); ok {
cartHostMap[host] = append(cartHostMap[host], notificationItem)
continue
}
// Marshal item data for PaymentEvent // Marshal item data for PaymentEvent
dataBytes, err := json.Marshal(item) dataBytes, err := json.Marshal(item)
if err != nil { if err != nil {
@@ -110,9 +97,20 @@ func (s *CheckoutPoolServer) AdyenHookHandler(w http.ResponseWriter, r *http.Req
http.Error(w, "Error marshaling item", http.StatusInternalServerError) http.Error(w, "Error marshaling item", http.StatusInternalServerError)
return return
} }
switch item.EventCode { switch item.EventCode {
case "CAPTURE": case "CAPTURE":
checkoutId, err := getCheckoutIdFromNotificationItem(item)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Check if this checkout is owned by another host
if host, ok := s.OwnerHost(checkoutId); ok {
cartHostMap[host] = append(cartHostMap[host], notificationItem)
continue
}
log.Printf("Capture status: %v", item.Success) log.Printf("Capture status: %v", item.Success)
isSuccess := item.Success == "true" isSuccess := item.Success == "true"
// Apply payment event for capture // Apply payment event for capture
@@ -126,74 +124,77 @@ func (s *CheckoutPoolServer) AdyenHookHandler(w http.ResponseWriter, r *http.Req
} }
// If successful, apply payment completed // If successful, apply payment completed
if isSuccess { //if isSuccess {
if err := s.applyAnywhere(r.Context(), checkoutId, &messages.PaymentCompleted{ if err := s.applyAnywhere(r.Context(), checkoutId, &messages.PaymentCompleted{
PaymentId: item.PspReference, PaymentId: item.PspReference,
Status: "captured", Status: item.Success,
Amount: item.Amount.Value, Amount: item.Amount.Value,
Currency: item.Amount.Currency, Currency: item.Amount.Currency,
ProcessorReference: &item.PspReference, ProcessorReference: &item.PspReference,
CompletedAt: timestamppb.New(time.Now()), CompletedAt: timestamppb.New(time.Now()),
}); err != nil {
log.Printf("error applying payment completed: %v", err)
}
}
case "AUTHORISATION":
isSuccess := item.Success == "true"
// Apply payment event for authorization
if err := s.applyAnywhere(r.Context(), checkoutId, &messages.PaymentEvent{
PaymentId: item.PspReference,
Success: isSuccess,
Name: item.EventCode,
Data: &anypb.Any{Value: dataBytes},
}); err != nil { }); err != nil {
log.Printf("error applying authorization event: %v", err) http.Error(w, "Message not parsed", http.StatusInternalServerError)
return
} }
//}
// If successful authorization, trigger capture //case "AUTHORISATION":
if isSuccess { //isSuccess := item.Success == "true"
grain, err := s.Get(r.Context(), checkoutId) //log.Panicln("Handling auth: %+v", item)
if err != nil { // // Apply payment event for authorization
log.Printf("Error getting checkout: %v", err) // if err := s.applyAnywhere(r.Context(), checkoutId, &messages.PaymentEvent{
http.Error(w, "Checkout not found", http.StatusBadRequest) // PaymentId: item.PspReference,
return // Success: isSuccess,
} // Name: item.EventCode,
meta := GetCheckoutMetaFromRequest(r) // Data: &anypb.Any{Value: dataBytes},
pspReference := item.PspReference // }); err != nil {
uid := uuid.New().String() // log.Printf("error applying authorization event: %v", err)
ref := uuid.New().String() // }
req := service.ModificationsApi.CaptureAuthorisedPaymentInput(pspReference).IdempotencyKey(uid).PaymentCaptureRequest(adyenCheckout.PaymentCaptureRequest{
Amount: adyenCheckout.Amount{ // // If successful authorization, trigger capture
Currency: meta.Currency, // if isSuccess {
Value: grain.CartTotalPrice.IncVat, // grain, err := s.Get(r.Context(), checkoutId)
}, // if err != nil {
MerchantAccount: "ElgigantenECOM", // log.Printf("Error getting checkout: %v", err)
Reference: &ref, // http.Error(w, "Checkout not found", http.StatusBadRequest)
}) // return
res, _, err := service.ModificationsApi.CaptureAuthorisedPayment(r.Context(), req) // }
if err != nil { // meta := GetCheckoutMetaFromRequest(r)
log.Printf("Error capturing payment: %v", err) // pspReference := item.PspReference
} else { // uid := uuid.New().String()
log.Printf("Payment captured successfully: %+v", res) // ref := uuid.New().String()
s.Apply(r.Context(), checkoutId, &messages.OrderCreated{ // req := service.ModificationsApi.CaptureAuthorisedPaymentInput(pspReference).IdempotencyKey(uid).PaymentCaptureRequest(adyenCheckout.PaymentCaptureRequest{
OrderId: res.PaymentPspReference, // Amount: adyenCheckout.Amount{
Status: item.EventCode, // Currency: meta.Currency,
}) // Value: grain.CartTotalPrice.IncVat,
} // },
} // MerchantAccount: "ElgigantenECOM",
// Reference: &ref,
// })
// res, _, err := service.ModificationsApi.CaptureAuthorisedPayment(r.Context(), req)
// if err != nil {
// log.Printf("Error capturing payment: %v", err)
// } else {
// log.Printf("Payment captured successfully: %+v", res)
// s.Apply(r.Context(), checkoutId, &messages.OrderCreated{
// OrderId: res.PaymentPspReference,
// Status: item.EventCode,
// })
// }
// }
default: default:
log.Printf("Unknown event code: %s", item.EventCode) log.Printf("Unknown event code: %s", item.EventCode)
isSuccess := item.Success == "true" log.Printf("Item data: %+v", item)
// Apply generic payment event for other event codes //isSuccess := item.Success == "true"
if err := s.applyAnywhere(r.Context(), checkoutId, &messages.PaymentEvent{
PaymentId: item.PspReference, // if err := s.applyAnywhere(r.Context(), checkoutId, &messages.PaymentEvent{
Success: isSuccess, // PaymentId: item.PspReference,
Name: item.EventCode, // Success: isSuccess,
Data: &anypb.Any{Value: dataBytes}, // Name: item.EventCode,
}); err != nil { // Data: &anypb.Any{Value: dataBytes},
log.Printf("error applying payment event: %v", err) // }); err != nil {
} // log.Printf("error applying payment event: %v", err)
// }
} }
} }
} }

View File

@@ -114,7 +114,7 @@ func BuildCheckoutOrderPayload(grain *checkout.CheckoutGrain, meta *CheckoutMeta
MerchantURLS: &CheckoutMerchantURLS{ MerchantURLS: &CheckoutMerchantURLS{
Terms: fmt.Sprintf("%s/terms", meta.SiteUrl), Terms: fmt.Sprintf("%s/terms", meta.SiteUrl),
Checkout: fmt.Sprintf("%s/checkout?order_id={checkout.order.id}&provider=klarna", meta.SiteUrl), Checkout: fmt.Sprintf("%s/checkout?order_id={checkout.order.id}&provider=klarna", meta.SiteUrl),
Confirmation: fmt.Sprintf("%s/confirmation?order_id={checkout.order.id}&provider=klarna", meta.SiteUrl), Confirmation: fmt.Sprintf("%s/checkout?order_id={checkout.order.id}&provider=klarna", meta.SiteUrl),
Notification: "https://cart.k6n.net/payment/klarna/notification", Notification: "https://cart.k6n.net/payment/klarna/notification",
Validation: "https://cart.k6n.net/payment/klarna/validate", Validation: "https://cart.k6n.net/payment/klarna/validate",
Push: "https://cart.k6n.net/payment/klarna/push?order_id={checkout.order.id}", Push: "https://cart.k6n.net/payment/klarna/push?order_id={checkout.order.id}",