handle events
This commit is contained in:
@@ -18,6 +18,7 @@ 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"
|
||||||
)
|
)
|
||||||
@@ -74,7 +75,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(¬ificationRequest); err != nil {
|
if err := json.NewDecoder(r.Body).Decode(¬ificationRequest); err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
@@ -101,6 +102,7 @@ func (s *CheckoutPoolServer) AdyenHookHandler(w http.ResponseWriter, r *http.Req
|
|||||||
case "CAPTURE":
|
case "CAPTURE":
|
||||||
checkoutId, err := getCheckoutIdFromNotificationItem(item)
|
checkoutId, err := getCheckoutIdFromNotificationItem(item)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Printf("Could not get checkout id: %v", err)
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -138,50 +140,57 @@ func (s *CheckoutPoolServer) AdyenHookHandler(w http.ResponseWriter, r *http.Req
|
|||||||
}
|
}
|
||||||
//}
|
//}
|
||||||
|
|
||||||
//case "AUTHORISATION":
|
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)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // If successful authorization, trigger capture
|
isSuccess := item.Success == "true"
|
||||||
// if isSuccess {
|
log.Panicln("Handling auth: %+v", item)
|
||||||
// grain, err := s.Get(r.Context(), checkoutId)
|
checkoutId, err := getCheckoutIdFromNotificationItem(item)
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// log.Printf("Error getting checkout: %v", err)
|
log.Printf("Could not get checkout id: %v", err)
|
||||||
// http.Error(w, "Checkout not found", http.StatusBadRequest)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
// return
|
return
|
||||||
// }
|
}
|
||||||
// meta := GetCheckoutMetaFromRequest(r)
|
// Apply payment event for authorization
|
||||||
// pspReference := item.PspReference
|
if err := s.applyAnywhere(r.Context(), checkoutId, &messages.PaymentEvent{
|
||||||
// uid := uuid.New().String()
|
PaymentId: item.PspReference,
|
||||||
// ref := uuid.New().String()
|
Success: isSuccess,
|
||||||
// req := service.ModificationsApi.CaptureAuthorisedPaymentInput(pspReference).IdempotencyKey(uid).PaymentCaptureRequest(adyenCheckout.PaymentCaptureRequest{
|
Name: item.EventCode,
|
||||||
// Amount: adyenCheckout.Amount{
|
Data: &anypb.Any{Value: dataBytes},
|
||||||
// Currency: meta.Currency,
|
}); err != nil {
|
||||||
// Value: grain.CartTotalPrice.IncVat,
|
log.Printf("error applying authorization event: %v", err)
|
||||||
// },
|
}
|
||||||
// MerchantAccount: "ElgigantenECOM",
|
|
||||||
// Reference: &ref,
|
// If successful authorization, trigger capture
|
||||||
// })
|
if isSuccess {
|
||||||
// res, _, err := service.ModificationsApi.CaptureAuthorisedPayment(r.Context(), req)
|
grain, err := s.Get(r.Context(), checkoutId)
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// log.Printf("Error capturing payment: %v", err)
|
log.Printf("Error getting checkout: %v", err)
|
||||||
// } else {
|
http.Error(w, "Checkout not found", http.StatusBadRequest)
|
||||||
// log.Printf("Payment captured successfully: %+v", res)
|
return
|
||||||
// s.Apply(r.Context(), checkoutId, &messages.OrderCreated{
|
}
|
||||||
// OrderId: res.PaymentPspReference,
|
meta := GetCheckoutMetaFromRequest(r)
|
||||||
// Status: item.EventCode,
|
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:
|
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user