update
This commit is contained in:
@@ -158,8 +158,9 @@ func main() {
|
||||
} else {
|
||||
for _, update := range res {
|
||||
for _, item := range ret.Items {
|
||||
if matchesSkuAndLocation(update, *item) {
|
||||
item.Quantity = uint16(update.Quantity)
|
||||
if matchesSkuAndLocation(update, *item) && update.Quantity != uint32(item.Stock) {
|
||||
// maybe apply an update to give visibility to the cart
|
||||
item.Stock = uint16(update.Quantity)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@ 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"
|
||||
)
|
||||
@@ -75,7 +74,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(¬ificationRequest); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
@@ -91,6 +90,15 @@ func (s *CheckoutPoolServer) AdyenHookHandler(w http.ResponseWriter, r *http.Req
|
||||
http.Error(w, "Invalid HMAC", http.StatusUnauthorized)
|
||||
return
|
||||
} else {
|
||||
// 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 {
|
||||
case "CAPTURE":
|
||||
checkoutId, err := getCheckoutIdFromNotificationItem(item)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
@@ -103,16 +111,6 @@ func (s *CheckoutPoolServer) AdyenHookHandler(w http.ResponseWriter, r *http.Req
|
||||
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 {
|
||||
case "CAPTURE":
|
||||
log.Printf("Capture status: %v", item.Success)
|
||||
isSuccess := item.Success == "true"
|
||||
// Apply payment event for capture
|
||||
@@ -126,74 +124,77 @@ func (s *CheckoutPoolServer) AdyenHookHandler(w http.ResponseWriter, r *http.Req
|
||||
}
|
||||
|
||||
// If successful, apply payment completed
|
||||
if isSuccess {
|
||||
//if isSuccess {
|
||||
if err := s.applyAnywhere(r.Context(), checkoutId, &messages.PaymentCompleted{
|
||||
PaymentId: item.PspReference,
|
||||
Status: "captured",
|
||||
Status: item.Success,
|
||||
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":
|
||||
isSuccess := item.Success == "true"
|
||||
// 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)
|
||||
http.Error(w, "Message not parsed", http.StatusInternalServerError)
|
||||
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,
|
||||
})
|
||||
}
|
||||
}
|
||||
//}
|
||||
|
||||
//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
|
||||
// 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,
|
||||
// })
|
||||
// }
|
||||
// }
|
||||
default:
|
||||
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)
|
||||
}
|
||||
log.Printf("Item data: %+v", item)
|
||||
//isSuccess := item.Success == "true"
|
||||
|
||||
// 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)
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,7 +114,7 @@ func BuildCheckoutOrderPayload(grain *checkout.CheckoutGrain, meta *CheckoutMeta
|
||||
MerchantURLS: &CheckoutMerchantURLS{
|
||||
Terms: fmt.Sprintf("%s/terms", meta.SiteUrl),
|
||||
Checkout: fmt.Sprintf("%s/checkout?order_id={checkout.order.id}&provider=klarna", meta.SiteUrl),
|
||||
Confirmation: fmt.Sprintf("%s/confirmation?order_id={checkout.order.id}&provider=klarna", meta.SiteUrl),
|
||||
Confirmation: fmt.Sprintf("%s/checkout?order_id={checkout.order.id}&provider=klarna", meta.SiteUrl),
|
||||
Notification: "https://cart.k6n.net/payment/klarna/notification",
|
||||
Validation: "https://cart.k6n.net/payment/klarna/validate",
|
||||
Push: "https://cart.k6n.net/payment/klarna/push?order_id={checkout.order.id}",
|
||||
|
||||
Reference in New Issue
Block a user