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/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"
|
"github.com/google/uuid"
|
||||||
|
"google.golang.org/protobuf/proto"
|
||||||
"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"
|
||||||
)
|
)
|
||||||
@@ -133,33 +134,40 @@ 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
|
||||||
}
|
}
|
||||||
// Apply payment event for authorization
|
msgs := []proto.Message{
|
||||||
if err := s.applyAnywhere(r.Context(), checkoutId, &messages.PaymentEvent{
|
&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 {
|
},
|
||||||
|
}
|
||||||
|
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)
|
log.Printf("error applying authorization event: %v", err)
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// If successful authorization, trigger capture
|
// If successful authorization, trigger capture
|
||||||
if isSuccess {
|
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
|
pspReference := item.PspReference
|
||||||
uid := uuid.New().String()
|
uid := uuid.New().String()
|
||||||
ref := grain.Id.String()
|
ref := cart.CartId(checkoutId).String()
|
||||||
req := service.ModificationsApi.CaptureAuthorisedPaymentInput(pspReference).IdempotencyKey(uid).PaymentCaptureRequest(adyenCheckout.PaymentCaptureRequest{
|
req := service.ModificationsApi.CaptureAuthorisedPaymentInput(pspReference).IdempotencyKey(uid).PaymentCaptureRequest(adyenCheckout.PaymentCaptureRequest{
|
||||||
Amount: adyenCheckout.Amount{
|
Amount: adyenCheckout.Amount(item.Amount),
|
||||||
Currency: meta.Currency,
|
|
||||||
Value: grain.CartTotalPrice.IncVat,
|
|
||||||
},
|
|
||||||
MerchantAccount: "ElgigantenECOM",
|
MerchantAccount: "ElgigantenECOM",
|
||||||
Reference: &ref,
|
Reference: &ref,
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user