update
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"time"
|
||||||
|
|
||||||
"git.k6n.net/go-cart-actor/pkg/actor"
|
"git.k6n.net/go-cart-actor/pkg/actor"
|
||||||
"git.k6n.net/go-cart-actor/pkg/cart"
|
"git.k6n.net/go-cart-actor/pkg/cart"
|
||||||
@@ -18,6 +19,8 @@ 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/types/known/anypb"
|
||||||
|
"google.golang.org/protobuf/types/known/timestamppb"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SessionRequest struct {
|
type SessionRequest struct {
|
||||||
@@ -88,60 +91,109 @@ func (s *CheckoutPoolServer) AdyenHookHandler(w http.ResponseWriter, r *http.Req
|
|||||||
http.Error(w, "Invalid HMAC", http.StatusUnauthorized)
|
http.Error(w, "Invalid HMAC", http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
|
checkoutId, err := getCheckoutIdFromNotificationItem(item)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if this checkout is owned by another host
|
||||||
|
if host, ok := s.OwnerHost(checkoutId); ok {
|
||||||
|
cartHostMap[host] = append(cartHostMap[host], notificationItem)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Marshal item data for PaymentEvent
|
||||||
|
dataBytes, err := json.Marshal(item)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("error marshaling item: %v", err)
|
||||||
|
http.Error(w, "Error marshaling item", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
switch item.EventCode {
|
switch item.EventCode {
|
||||||
case "CAPTURE":
|
case "CAPTURE":
|
||||||
log.Printf("Capture status: %v", item.Success)
|
log.Printf("Capture status: %v", item.Success)
|
||||||
// dataBytes, err := json.Marshal(item)
|
isSuccess := item.Success == "true"
|
||||||
// if err != nil {
|
// Apply payment event for capture
|
||||||
// log.Printf("error marshaling item: %v", err)
|
if err := s.applyAnywhere(r.Context(), checkoutId, &messages.PaymentEvent{
|
||||||
// http.Error(w, "Error marshaling item", http.StatusInternalServerError)
|
PaymentId: item.PspReference,
|
||||||
// return
|
Success: isSuccess,
|
||||||
// }
|
Name: item.EventCode,
|
||||||
//s.ApplyAnywhere(r.Context(),0, &messages.PaymentEvent{PaymentId: item.PspReference, Success: item.Success, Name: item.EventCode, Data: &pbany.Any{Value: dataBytes}})
|
Data: &anypb.Any{Value: dataBytes},
|
||||||
|
}); err != nil {
|
||||||
|
log.Printf("error applying capture event: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// If successful, apply payment completed
|
||||||
|
if isSuccess {
|
||||||
|
if err := s.applyAnywhere(r.Context(), checkoutId, &messages.PaymentCompleted{
|
||||||
|
PaymentId: item.PspReference,
|
||||||
|
Status: "captured",
|
||||||
|
Amount: item.Amount.Value,
|
||||||
|
Currency: item.Amount.Currency,
|
||||||
|
ProcessorReference: &item.PspReference,
|
||||||
|
CompletedAt: timestamppb.New(time.Now()),
|
||||||
|
}); err != nil {
|
||||||
|
log.Printf("error applying payment completed: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
case "AUTHORISATION":
|
case "AUTHORISATION":
|
||||||
|
isSuccess := item.Success == "true"
|
||||||
cartId, err := getCheckoutIdFromNotificationItem(item)
|
// Apply payment event for authorization
|
||||||
if err != nil {
|
if err := s.applyAnywhere(r.Context(), checkoutId, &messages.PaymentEvent{
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
PaymentId: item.PspReference,
|
||||||
return
|
Success: isSuccess,
|
||||||
}
|
Name: item.EventCode,
|
||||||
//s.Apply()
|
Data: &anypb.Any{Value: dataBytes},
|
||||||
|
}); err != nil {
|
||||||
if host, ok := s.OwnerHost(uint64(cartId)); ok {
|
log.Printf("error applying authorization event: %v", err)
|
||||||
cartHostMap[host] = append(cartHostMap[host], notificationItem)
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
grain, err := s.Get(r.Context(), uint64(cartId))
|
// If successful authorization, trigger capture
|
||||||
if err != nil {
|
if isSuccess {
|
||||||
log.Printf("Error getting cart: %v", err)
|
grain, err := s.Get(r.Context(), checkoutId)
|
||||||
http.Error(w, "Cart not found", http.StatusBadRequest)
|
if err != nil {
|
||||||
return
|
log.Printf("Error getting checkout: %v", err)
|
||||||
}
|
http.Error(w, "Checkout not found", http.StatusBadRequest)
|
||||||
meta := GetCheckoutMetaFromRequest(r)
|
return
|
||||||
pspReference := item.PspReference
|
}
|
||||||
uid := uuid.New().String()
|
meta := GetCheckoutMetaFromRequest(r)
|
||||||
ref := uuid.New().String()
|
pspReference := item.PspReference
|
||||||
req := service.ModificationsApi.CaptureAuthorisedPaymentInput(pspReference).IdempotencyKey(uid).PaymentCaptureRequest(adyenCheckout.PaymentCaptureRequest{
|
uid := uuid.New().String()
|
||||||
Amount: adyenCheckout.Amount{
|
ref := uuid.New().String()
|
||||||
Currency: meta.Currency,
|
req := service.ModificationsApi.CaptureAuthorisedPaymentInput(pspReference).IdempotencyKey(uid).PaymentCaptureRequest(adyenCheckout.PaymentCaptureRequest{
|
||||||
Value: grain.CartTotalPrice.IncVat,
|
Amount: adyenCheckout.Amount{
|
||||||
},
|
Currency: meta.Currency,
|
||||||
MerchantAccount: "ElgigantenECOM",
|
Value: grain.CartTotalPrice.IncVat,
|
||||||
Reference: &ref,
|
},
|
||||||
})
|
MerchantAccount: "ElgigantenECOM",
|
||||||
res, _, err := service.ModificationsApi.CaptureAuthorisedPayment(r.Context(), req)
|
Reference: &ref,
|
||||||
if err != nil {
|
|
||||||
log.Printf("Error capturing payment: %v", err)
|
|
||||||
} else {
|
|
||||||
log.Printf("Payment captured successfully: %+v", res)
|
|
||||||
s.Apply(r.Context(), uint64(cartId), &messages.OrderCreated{
|
|
||||||
OrderId: res.PaymentPspReference,
|
|
||||||
Status: item.EventCode,
|
|
||||||
})
|
})
|
||||||
|
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)
|
||||||
|
isSuccess := item.Success == "true"
|
||||||
|
// Apply generic payment event for other event codes
|
||||||
|
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 payment event: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user