major changes
This commit is contained in:
@@ -3,8 +3,6 @@ package main
|
||||
import (
|
||||
"log"
|
||||
|
||||
"git.k6n.net/go-cart-actor/pkg/actor"
|
||||
"git.k6n.net/go-cart-actor/pkg/cart"
|
||||
"git.k6n.net/go-cart-actor/pkg/discovery"
|
||||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
@@ -32,7 +30,7 @@ func GetDiscovery() discovery.Discovery {
|
||||
})
|
||||
}
|
||||
|
||||
func UseDiscovery(pool actor.GrainPool[*cart.CartGrain]) {
|
||||
func UseDiscovery(pool discovery.DiscoveryTarget) {
|
||||
|
||||
go func(hw discovery.Discovery) {
|
||||
if hw == nil {
|
||||
|
||||
@@ -178,7 +178,7 @@ func main() {
|
||||
|
||||
return nil
|
||||
},
|
||||
SpawnHost: func(host string) (actor.Host, error) {
|
||||
SpawnHost: func(host string) (actor.Host[cart.CartGrain], error) {
|
||||
return proxy.NewRemoteHost[cart.CartGrain](host)
|
||||
},
|
||||
TTL: 5 * time.Minute,
|
||||
@@ -201,7 +201,7 @@ func main() {
|
||||
mux := http.NewServeMux()
|
||||
debugMux := http.NewServeMux()
|
||||
|
||||
grpcSrv, err := actor.NewControlServer[*cart.CartGrain](controlPlaneConfig, pool)
|
||||
grpcSrv, err := actor.NewControlServer[cart.CartGrain](controlPlaneConfig, pool)
|
||||
if err != nil {
|
||||
log.Fatalf("Error starting control plane gRPC server: %v\n", err)
|
||||
}
|
||||
|
||||
@@ -41,13 +41,13 @@ var (
|
||||
)
|
||||
|
||||
type PoolServer struct {
|
||||
actor.GrainPool[*cart.CartGrain]
|
||||
actor.GrainPool[cart.CartGrain]
|
||||
pod_name string
|
||||
inventoryService inventory.InventoryService
|
||||
reservationService inventory.CartReservationService
|
||||
}
|
||||
|
||||
func NewPoolServer(pool actor.GrainPool[*cart.CartGrain], pod_name string, inventoryService inventory.InventoryService, inventoryReservationService inventory.CartReservationService) *PoolServer {
|
||||
func NewPoolServer(pool actor.GrainPool[cart.CartGrain], pod_name string, inventoryService inventory.InventoryService, inventoryReservationService inventory.CartReservationService) *PoolServer {
|
||||
srv := &PoolServer{
|
||||
GrainPool: pool,
|
||||
pod_name: pod_name,
|
||||
@@ -58,7 +58,7 @@ func NewPoolServer(pool actor.GrainPool[*cart.CartGrain], pod_name string, inven
|
||||
return srv
|
||||
}
|
||||
|
||||
func (s *PoolServer) ApplyLocal(ctx context.Context, id cart.CartId, mutation ...proto.Message) (*actor.MutationResult[*cart.CartGrain], error) {
|
||||
func (s *PoolServer) ApplyLocal(ctx context.Context, id cart.CartId, mutation ...proto.Message) (*actor.MutationResult[cart.CartGrain], error) {
|
||||
return s.Apply(ctx, uint64(id), mutation...)
|
||||
}
|
||||
|
||||
@@ -460,10 +460,7 @@ func (s *PoolServer) InternalApplyMutationHandler(w http.ResponseWriter, r *http
|
||||
func (s *PoolServer) GetAnywhere(ctx context.Context, cartId cart.CartId) (*cart.CartGrain, error) {
|
||||
id := uint64(cartId)
|
||||
if host, isOnOtherHost := s.OwnerHost(id); isOnOtherHost {
|
||||
grain := &cart.CartGrain{}
|
||||
|
||||
err := host.Get(ctx, id, grain)
|
||||
return grain, err
|
||||
return host.Get(ctx, id)
|
||||
}
|
||||
return s.Get(ctx, id)
|
||||
}
|
||||
|
||||
@@ -57,13 +57,13 @@ type SessionRequest struct {
|
||||
|
||||
// }
|
||||
|
||||
func getCheckoutIdFromNotificationItem(item webhook.NotificationRequestItem) (uint64, error) {
|
||||
func getCheckoutIdFromNotificationItem(item webhook.NotificationRequestItem) (*cart.CartId, error) {
|
||||
cartId, ok := cart.ParseCartId(item.MerchantReference)
|
||||
if !ok {
|
||||
log.Printf("The notification does not have a valid cartId: %s", item.MerchantReference)
|
||||
return 0, errors.New("invalid cart id")
|
||||
return nil, errors.New("invalid cart id")
|
||||
}
|
||||
return uint64(cartId), nil
|
||||
return &cartId, nil
|
||||
}
|
||||
|
||||
func (s *CheckoutPoolServer) AdyenHookHandler(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -105,7 +105,7 @@ func (s *CheckoutPoolServer) AdyenHookHandler(w http.ResponseWriter, r *http.Req
|
||||
|
||||
// If successful, apply payment completed
|
||||
//if isSuccess {
|
||||
if err := s.applyAnywhere(r.Context(), checkoutId,
|
||||
if err := s.ApplyAnywhere(r.Context(), *checkoutId,
|
||||
&messages.PaymentEvent{
|
||||
PaymentId: item.PspReference,
|
||||
Success: isSuccess,
|
||||
@@ -154,7 +154,7 @@ func (s *CheckoutPoolServer) AdyenHookHandler(w http.ResponseWriter, r *http.Req
|
||||
Message: item.Reason,
|
||||
})
|
||||
}
|
||||
if err := s.applyAnywhere(r.Context(), checkoutId, msgs...); err != nil {
|
||||
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
|
||||
@@ -165,7 +165,7 @@ func (s *CheckoutPoolServer) AdyenHookHandler(w http.ResponseWriter, r *http.Req
|
||||
|
||||
pspReference := item.PspReference
|
||||
uid := uuid.New().String()
|
||||
ref := cart.CartId(checkoutId).String()
|
||||
ref := checkoutId.String()
|
||||
req := service.ModificationsApi.CaptureAuthorisedPaymentInput(pspReference).IdempotencyKey(uid).PaymentCaptureRequest(adyenCheckout.PaymentCaptureRequest{
|
||||
Amount: adyenCheckout.Amount(item.Amount),
|
||||
MerchantAccount: "ElgigantenECOM",
|
||||
@@ -176,7 +176,7 @@ func (s *CheckoutPoolServer) AdyenHookHandler(w http.ResponseWriter, r *http.Req
|
||||
log.Printf("Error capturing payment: %v", err)
|
||||
} else {
|
||||
log.Printf("Payment captured successfully: %+v", res)
|
||||
s.Apply(r.Context(), checkoutId, &messages.OrderCreated{
|
||||
s.ApplyAnywhere(r.Context(), *checkoutId, &messages.OrderCreated{
|
||||
OrderId: res.PaymentPspReference,
|
||||
Status: item.EventCode,
|
||||
})
|
||||
@@ -191,7 +191,7 @@ func (s *CheckoutPoolServer) AdyenHookHandler(w http.ResponseWriter, r *http.Req
|
||||
log.Printf("Could not get checkout id: %v", err)
|
||||
|
||||
} else {
|
||||
if err := s.applyAnywhere(r.Context(), checkoutId, &messages.PaymentEvent{
|
||||
if err := s.ApplyAnywhere(r.Context(), *checkoutId, &messages.PaymentEvent{
|
||||
PaymentId: item.PspReference,
|
||||
Success: isSuccess,
|
||||
Name: item.EventCode,
|
||||
|
||||
@@ -3,8 +3,6 @@ package main
|
||||
import (
|
||||
"log"
|
||||
|
||||
"git.k6n.net/go-cart-actor/pkg/actor"
|
||||
"git.k6n.net/go-cart-actor/pkg/checkout"
|
||||
"git.k6n.net/go-cart-actor/pkg/discovery"
|
||||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
@@ -32,7 +30,7 @@ func GetDiscovery() discovery.Discovery {
|
||||
})
|
||||
}
|
||||
|
||||
func UseDiscovery(pool actor.GrainPool[*checkout.CheckoutGrain]) {
|
||||
func UseDiscovery(pool discovery.DiscoveryTarget) {
|
||||
|
||||
go func(hw discovery.Discovery) {
|
||||
if hw == nil {
|
||||
|
||||
@@ -195,7 +195,7 @@ func (s *CheckoutPoolServer) KlarnaPushHandler(w http.ResponseWriter, r *http.Re
|
||||
})
|
||||
}
|
||||
|
||||
s.applyAnywhere(r.Context(), uint64(grain.Id), &messages.PaymentCompleted{
|
||||
s.ApplyAnywhere(r.Context(), grain.Id, &messages.PaymentCompleted{
|
||||
PaymentId: orderId,
|
||||
Status: "completed",
|
||||
ProcessorReference: &order.ID,
|
||||
@@ -267,7 +267,7 @@ func (a *CheckoutPoolServer) getGrainFromKlarnaOrder(ctx context.Context, order
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("invalid cart id in order reference: %s", order.MerchantReference1)
|
||||
}
|
||||
grain, err := a.getAnywhere(ctx, uint64(cartId))
|
||||
grain, err := a.GetAnywhere(ctx, cartId)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get cart grain: %w", err)
|
||||
}
|
||||
|
||||
@@ -89,7 +89,7 @@ func main() {
|
||||
Destroy: func(grain actor.Grain[checkout.CheckoutGrain]) error {
|
||||
return nil
|
||||
},
|
||||
SpawnHost: func(host string) (actor.Host, error) {
|
||||
SpawnHost: func(host string) (actor.Host[checkout.CheckoutGrain], error) {
|
||||
return proxy.NewRemoteHost[checkout.CheckoutGrain](host)
|
||||
},
|
||||
TTL: 1 * time.Hour, // Longer TTL for checkout
|
||||
@@ -121,7 +121,7 @@ func main() {
|
||||
log.Fatalf("no connection to amqp defined")
|
||||
}
|
||||
|
||||
grpcSrv, err := actor.NewControlServer[*checkout.CheckoutGrain](controlPlaneConfig, pool)
|
||||
grpcSrv, err := actor.NewControlServer[checkout.CheckoutGrain](controlPlaneConfig, pool)
|
||||
if err != nil {
|
||||
log.Fatalf("Error starting control plane gRPC server: %v\n", err)
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ var (
|
||||
)
|
||||
|
||||
type CheckoutPoolServer struct {
|
||||
actor.GrainPool[*checkout.CheckoutGrain]
|
||||
actor.GrainPool[checkout.CheckoutGrain]
|
||||
pod_name string
|
||||
klarnaClient *KlarnaClient
|
||||
adyenClient *adyen.APIClient
|
||||
@@ -54,7 +54,7 @@ type CheckoutPoolServer struct {
|
||||
inventoryService *inventory.RedisInventoryService
|
||||
}
|
||||
|
||||
func NewCheckoutPoolServer(pool actor.GrainPool[*checkout.CheckoutGrain], pod_name string, klarnaClient *KlarnaClient, cartClient *CartClient, adyenClient *adyen.APIClient) *CheckoutPoolServer {
|
||||
func NewCheckoutPoolServer(pool actor.GrainPool[checkout.CheckoutGrain], pod_name string, klarnaClient *KlarnaClient, cartClient *CartClient, adyenClient *adyen.APIClient) *CheckoutPoolServer {
|
||||
srv := &CheckoutPoolServer{
|
||||
GrainPool: pool,
|
||||
pod_name: pod_name,
|
||||
@@ -66,7 +66,7 @@ func NewCheckoutPoolServer(pool actor.GrainPool[*checkout.CheckoutGrain], pod_na
|
||||
return srv
|
||||
}
|
||||
|
||||
func (s *CheckoutPoolServer) ApplyLocal(ctx context.Context, id checkout.CheckoutId, mutation ...proto.Message) (*actor.MutationResult[*checkout.CheckoutGrain], error) {
|
||||
func (s *CheckoutPoolServer) ApplyLocal(ctx context.Context, id checkout.CheckoutId, mutation ...proto.Message) (*actor.MutationResult[checkout.CheckoutGrain], error) {
|
||||
return s.Apply(ctx, uint64(id), mutation...)
|
||||
}
|
||||
|
||||
@@ -90,7 +90,7 @@ func (s *CheckoutPoolServer) SetDeliveryHandler(w http.ResponseWriter, r *http.R
|
||||
return err
|
||||
}
|
||||
|
||||
return s.WriteResult(w, result.Result)
|
||||
return s.WriteResult(w, result)
|
||||
}
|
||||
|
||||
func (s *CheckoutPoolServer) RemoveDeliveryHandler(w http.ResponseWriter, r *http.Request, id checkout.CheckoutId) error {
|
||||
@@ -108,7 +108,7 @@ func (s *CheckoutPoolServer) RemoveDeliveryHandler(w http.ResponseWriter, r *htt
|
||||
return err
|
||||
}
|
||||
|
||||
return s.WriteResult(w, result.Result)
|
||||
return s.WriteResult(w, result)
|
||||
}
|
||||
|
||||
func (s *CheckoutPoolServer) SetPickupPointHandler(w http.ResponseWriter, r *http.Request, id checkout.CheckoutId) error {
|
||||
@@ -122,7 +122,7 @@ func (s *CheckoutPoolServer) SetPickupPointHandler(w http.ResponseWriter, r *htt
|
||||
return err
|
||||
}
|
||||
|
||||
return s.WriteResult(w, result.Result)
|
||||
return s.WriteResult(w, result)
|
||||
}
|
||||
|
||||
func (s *CheckoutPoolServer) InitializeCheckoutHandler(w http.ResponseWriter, r *http.Request, id checkout.CheckoutId) error {
|
||||
@@ -136,7 +136,7 @@ func (s *CheckoutPoolServer) InitializeCheckoutHandler(w http.ResponseWriter, r
|
||||
return err
|
||||
}
|
||||
|
||||
return s.WriteResult(w, result.Result)
|
||||
return s.WriteResult(w, result)
|
||||
}
|
||||
|
||||
func (s *CheckoutPoolServer) InventoryReservedHandler(w http.ResponseWriter, r *http.Request, id checkout.CheckoutId) error {
|
||||
@@ -150,7 +150,7 @@ func (s *CheckoutPoolServer) InventoryReservedHandler(w http.ResponseWriter, r *
|
||||
return err
|
||||
}
|
||||
|
||||
return s.WriteResult(w, result.Result)
|
||||
return s.WriteResult(w, result)
|
||||
}
|
||||
|
||||
func (s *CheckoutPoolServer) OrderCreatedHandler(w http.ResponseWriter, r *http.Request, id checkout.CheckoutId) error {
|
||||
@@ -164,7 +164,7 @@ func (s *CheckoutPoolServer) OrderCreatedHandler(w http.ResponseWriter, r *http.
|
||||
return err
|
||||
}
|
||||
|
||||
return s.WriteResult(w, result.Result)
|
||||
return s.WriteResult(w, result)
|
||||
}
|
||||
|
||||
func (s *CheckoutPoolServer) ConfirmationViewedHandler(w http.ResponseWriter, r *http.Request, id checkout.CheckoutId) error {
|
||||
@@ -178,7 +178,7 @@ func (s *CheckoutPoolServer) ConfirmationViewedHandler(w http.ResponseWriter, r
|
||||
return err
|
||||
}
|
||||
|
||||
return s.WriteResult(w, result.Result)
|
||||
return s.WriteResult(w, result)
|
||||
}
|
||||
|
||||
func (s *CheckoutPoolServer) ContactDetailsUpdatedHandler(w http.ResponseWriter, r *http.Request, id checkout.CheckoutId) error {
|
||||
@@ -192,7 +192,7 @@ func (s *CheckoutPoolServer) ContactDetailsUpdatedHandler(w http.ResponseWriter,
|
||||
return err
|
||||
}
|
||||
|
||||
return s.WriteResult(w, result.Result)
|
||||
return s.WriteResult(w, result)
|
||||
}
|
||||
|
||||
func (s *CheckoutPoolServer) StartCheckoutHandler(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -256,7 +256,7 @@ func (s *CheckoutPoolServer) StartCheckoutHandler(w http.ResponseWriter, r *http
|
||||
// Set checkout cookie
|
||||
setCheckoutCookie(w, checkoutId, r.TLS != nil)
|
||||
|
||||
if err := s.WriteResult(w, result.Result); err != nil {
|
||||
if err := s.WriteResult(w, &result.Result); err != nil {
|
||||
logger.Error("failed to write result", "error", err)
|
||||
}
|
||||
}
|
||||
@@ -299,7 +299,7 @@ func (s *CheckoutPoolServer) CreateOrUpdateCheckout(r *http.Request, grain *chec
|
||||
}
|
||||
}
|
||||
|
||||
func (s *CheckoutPoolServer) ApplyKlarnaPaymentStarted(ctx context.Context, klarnaOrder *CheckoutOrder, id checkout.CheckoutId) (*actor.MutationResult[*checkout.CheckoutGrain], error) {
|
||||
func (s *CheckoutPoolServer) ApplyKlarnaPaymentStarted(ctx context.Context, klarnaOrder *CheckoutOrder, id checkout.CheckoutId) (*actor.MutationResult[checkout.CheckoutGrain], error) {
|
||||
method := "checkout"
|
||||
return s.ApplyLocal(ctx, id, &messages.PaymentStarted{
|
||||
PaymentId: klarnaOrder.ID,
|
||||
@@ -329,28 +329,22 @@ func init() {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *CheckoutPoolServer) applyAnywhere(ctx context.Context, id uint64, msgs ...proto.Message) error {
|
||||
host, found := s.OwnerHost(id)
|
||||
if !found {
|
||||
_, err := s.Apply(ctx, id, msgs...)
|
||||
return err
|
||||
func (s *CheckoutPoolServer) GetAnywhere(ctx context.Context, checkoutId cart.CartId) (*checkout.CheckoutGrain, error) {
|
||||
id := uint64(checkoutId)
|
||||
if host, isOnOtherHost := s.OwnerHost(id); isOnOtherHost {
|
||||
return host.Get(ctx, id)
|
||||
}
|
||||
_, err := host.Apply(ctx, id, msgs...)
|
||||
return err
|
||||
return s.Get(ctx, id)
|
||||
}
|
||||
|
||||
func (s *CheckoutPoolServer) getAnywhere(ctx context.Context, id uint64) (*checkout.CheckoutGrain, error) {
|
||||
host, found := s.OwnerHost(id)
|
||||
if !found {
|
||||
grain, err := s.Get(ctx, id)
|
||||
return grain, err
|
||||
func (s *CheckoutPoolServer) ApplyAnywhere(ctx context.Context, checkoutId cart.CartId, msgs ...proto.Message) error {
|
||||
id := uint64(checkoutId)
|
||||
if host, isOnOtherHost := s.OwnerHost(id); isOnOtherHost {
|
||||
_, err := host.Apply(ctx, id, msgs...)
|
||||
return err
|
||||
}
|
||||
ret := &checkout.CheckoutGrain{}
|
||||
err := host.Get(ctx, id, ret)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ret, nil
|
||||
_, err := s.Apply(ctx, id, msgs...)
|
||||
return err
|
||||
}
|
||||
|
||||
type StartPayment struct {
|
||||
|
||||
Reference in New Issue
Block a user