maybe fix adyen error
Some checks failed
Build and Publish / BuildAndDeployAmd64 (push) Successful in 44s
Build and Publish / BuildAndDeployArm64 (push) Has been cancelled

This commit is contained in:
matst80
2025-12-04 18:48:24 +01:00
parent 94d478ee6a
commit bd194f2bb8

View File

@@ -1,7 +1,6 @@
package main package main
import ( import (
"bytes"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
@@ -10,9 +9,7 @@ import (
"net/url" "net/url"
"time" "time"
"git.k6n.net/go-cart-actor/pkg/actor"
"git.k6n.net/go-cart-actor/pkg/cart" "git.k6n.net/go-cart-actor/pkg/cart"
"git.k6n.net/go-cart-actor/pkg/proxy"
messages "git.k6n.net/go-cart-actor/proto/checkout" messages "git.k6n.net/go-cart-actor/proto/checkout"
adyenCheckout "github.com/adyen/adyen-go-api-library/v21/src/checkout" adyenCheckout "github.com/adyen/adyen-go-api-library/v21/src/checkout"
"github.com/adyen/adyen-go-api-library/v21/src/common" "github.com/adyen/adyen-go-api-library/v21/src/common"
@@ -75,7 +72,7 @@ func (s *CheckoutPoolServer) AdyenHookHandler(w http.ResponseWriter, r *http.Req
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)
return return
} }
cartHostMap := make(map[actor.Host][]webhook.NotificationItem)
for _, notificationItem := range *notificationRequest.NotificationItems { for _, notificationItem := range *notificationRequest.NotificationItems {
item := notificationItem.NotificationRequestItem item := notificationItem.NotificationRequestItem
log.Printf("Recieved notification event code: %s, %+v", item.EventCode, item) log.Printf("Recieved notification event code: %s, %+v", item.EventCode, item)
@@ -102,27 +99,18 @@ func (s *CheckoutPoolServer) AdyenHookHandler(w http.ResponseWriter, r *http.Req
return 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
if err := s.applyAnywhere(r.Context(), checkoutId, &messages.PaymentEvent{ // If successful, apply payment completed
//if isSuccess {
if err := s.applyAnywhere(r.Context(), checkoutId,
&messages.PaymentEvent{
PaymentId: item.PspReference, PaymentId: item.PspReference,
Success: isSuccess, Success: isSuccess,
Name: item.EventCode, Name: item.EventCode,
Data: &anypb.Any{Value: dataBytes}, Data: &anypb.Any{Value: dataBytes},
}); err != nil { }, &messages.PaymentCompleted{
log.Printf("error applying capture event: %v", err)
}
// If successful, apply payment completed
//if isSuccess {
if err := s.applyAnywhere(r.Context(), checkoutId, &messages.PaymentCompleted{
PaymentId: item.PspReference, PaymentId: item.PspReference,
Status: item.Success, Status: item.Success,
Amount: item.Amount.Value, Amount: item.Amount.Value,
@@ -189,46 +177,28 @@ func (s *CheckoutPoolServer) AdyenHookHandler(w http.ResponseWriter, r *http.Req
default: default:
log.Printf("Unknown event code: %s", item.EventCode) log.Printf("Unknown event code: %s", item.EventCode)
log.Printf("Item data: %+v", item) log.Printf("Item data: %+v", item)
//isSuccess := item.Success == "true" isSuccess := item.Success == "true"
checkoutId, err := getCheckoutIdFromNotificationItem(item)
if err != nil {
log.Printf("Could not get checkout id: %v", err)
// if err := s.applyAnywhere(r.Context(), checkoutId, &messages.PaymentEvent{
// PaymentId: item.PspReference,
// Success: isSuccess,
// Name: item.EventCode,
// Data: &anypb.Any{Value: dataBytes},
// }); err != nil {
// log.Printf("error applying payment event: %v", err)
// }
}
}
}
var failed bool = false
var lastMock *proxy.MockResponseWriter
for host, items := range cartHostMap {
notificationRequest.NotificationItems = &items
bodyBytes, err := json.Marshal(notificationRequest)
if err != nil {
log.Printf("error marshaling notification: %+v", err)
continue
}
customBody := bytes.NewReader(bodyBytes)
mockW := proxy.NewMockResponseWriter()
handled, err := host.Proxy(0, mockW, r, customBody)
if err != nil {
log.Printf("proxy failed for %s: %+v", host.Name(), err)
failed = true
lastMock = mockW
} else if handled {
log.Printf("notification proxied to %s", host.Name())
}
}
if failed {
w.WriteHeader(lastMock.StatusCode)
w.Write(lastMock.Body.Bytes())
} else { } else {
w.WriteHeader(http.StatusAccepted) if err := s.applyAnywhere(r.Context(), checkoutId, &messages.PaymentEvent{
PaymentId: item.PspReference,
Success: isSuccess,
Name: item.EventCode,
Data: &anypb.Any{Value: dataBytes},
}); err != nil {
log.Printf("error applying payment event: %v", err)
} }
} }
}
}
}
w.WriteHeader(http.StatusAccepted)
}
func (s *CheckoutPoolServer) AdyenReturnHandler(w http.ResponseWriter, r *http.Request) { func (s *CheckoutPoolServer) AdyenReturnHandler(w http.ResponseWriter, r *http.Request) {
log.Println("Redirect received") log.Println("Redirect received")