From 2b8e69252659ca956f605755c905504a3894cb19 Mon Sep 17 00:00:00 2001 From: matst80 Date: Thu, 4 Dec 2025 18:58:10 +0100 Subject: [PATCH] better adyen handling --- cmd/checkout/adyen-handlers.go | 46 ++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/cmd/checkout/adyen-handlers.go b/cmd/checkout/adyen-handlers.go index 4beabde..dc6a550 100644 --- a/cmd/checkout/adyen-handlers.go +++ b/cmd/checkout/adyen-handlers.go @@ -16,6 +16,7 @@ import ( "github.com/adyen/adyen-go-api-library/v21/src/hmacvalidator" "github.com/adyen/adyen-go-api-library/v21/src/webhook" "github.com/google/uuid" + "google.golang.org/protobuf/proto" "google.golang.org/protobuf/types/known/anypb" "google.golang.org/protobuf/types/known/timestamppb" ) @@ -133,33 +134,40 @@ func (s *CheckoutPoolServer) AdyenHookHandler(w http.ResponseWriter, r *http.Req http.Error(w, err.Error(), http.StatusBadRequest) return } - // 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 { + msgs := []proto.Message{ + &messages.PaymentEvent{ + PaymentId: item.PspReference, + Success: isSuccess, + Name: item.EventCode, + Data: &anypb.Any{Value: dataBytes}, + }, + } + if isSuccess { + msgs = append(msgs, &messages.PaymentCompleted{ + PaymentId: item.PspReference, + Status: item.Success, + Amount: item.Amount.Value, + }) + } else { + msgs = append(msgs, &messages.PaymentDeclined{ + PaymentId: item.PspReference, + Message: item.Reason, + }) + } + if err := s.applyAnywhere(r.Context(), checkoutId, msgs...); err != nil { log.Printf("error applying authorization event: %v", err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return } // If successful authorization, trigger capture if isSuccess { - grain, err := s.Get(r.Context(), checkoutId) - if err != nil { - log.Printf("Error getting checkout: %v", err) - http.Error(w, "Checkout not found", http.StatusBadRequest) - return - } - meta := GetCheckoutMetaFromRequest(r) + pspReference := item.PspReference uid := uuid.New().String() - ref := grain.Id.String() + ref := cart.CartId(checkoutId).String() req := service.ModificationsApi.CaptureAuthorisedPaymentInput(pspReference).IdempotencyKey(uid).PaymentCaptureRequest(adyenCheckout.PaymentCaptureRequest{ - Amount: adyenCheckout.Amount{ - Currency: meta.Currency, - Value: grain.CartTotalPrice.IncVat, - }, + Amount: adyenCheckout.Amount(item.Amount), MerchantAccount: "ElgigantenECOM", Reference: &ref, })