handle events
All checks were successful
Build and Publish / BuildAndDeployAmd64 (push) Successful in 45s
Build and Publish / BuildAndDeployArm64 (push) Successful in 4m20s

This commit is contained in:
matst80
2025-12-04 09:34:49 +01:00
parent 445858c1e3
commit e9189364ab

View File

@@ -18,6 +18,7 @@ import (
"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/webhook"
"github.com/google/uuid"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/timestamppb"
)
@@ -74,7 +75,7 @@ func getCheckoutIdFromNotificationItem(item webhook.NotificationRequestItem) (ui
func (s *CheckoutPoolServer) AdyenHookHandler(w http.ResponseWriter, r *http.Request) {
var notificationRequest webhook.Webhook
//service := s.adyenClient.Checkout()
service := s.adyenClient.Checkout()
if err := json.NewDecoder(r.Body).Decode(&notificationRequest); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
@@ -101,6 +102,7 @@ func (s *CheckoutPoolServer) AdyenHookHandler(w http.ResponseWriter, r *http.Req
case "CAPTURE":
checkoutId, err := getCheckoutIdFromNotificationItem(item)
if err != nil {
log.Printf("Could not get checkout id: %v", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
@@ -138,50 +140,57 @@ func (s *CheckoutPoolServer) AdyenHookHandler(w http.ResponseWriter, r *http.Req
}
//}
//case "AUTHORISATION":
//isSuccess := item.Success == "true"
//log.Panicln("Handling auth: %+v", item)
// // 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 {
// log.Printf("error applying authorization event: %v", err)
// }
case "AUTHORISATION":
// // 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 := uuid.New().String()
// req := service.ModificationsApi.CaptureAuthorisedPaymentInput(pspReference).IdempotencyKey(uid).PaymentCaptureRequest(adyenCheckout.PaymentCaptureRequest{
// Amount: adyenCheckout.Amount{
// 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,
// })
// }
// }
isSuccess := item.Success == "true"
log.Panicln("Handling auth: %+v", item)
checkoutId, err := getCheckoutIdFromNotificationItem(item)
if err != nil {
log.Printf("Could not get checkout id: %v", err)
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 {
log.Printf("error applying authorization event: %v", err)
}
// 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()
req := service.ModificationsApi.CaptureAuthorisedPaymentInput(pspReference).IdempotencyKey(uid).PaymentCaptureRequest(adyenCheckout.PaymentCaptureRequest{
Amount: adyenCheckout.Amount{
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:
log.Printf("Unknown event code: %s", item.EventCode)
log.Printf("Item data: %+v", item)