better adyen handling
This commit is contained in:
@@ -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,
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user