better handling of payment in progress
This commit is contained in:
228
cart-grain.go
228
cart-grain.go
@@ -243,25 +243,28 @@ func (c *CartGrain) HandleMessage(message *Message, isReplay bool) (*FrameWithPa
|
|||||||
if !ok {
|
if !ok {
|
||||||
err = fmt.Errorf("expected SetCartItems")
|
err = fmt.Errorf("expected SetCartItems")
|
||||||
} else {
|
} else {
|
||||||
c.mu.Lock()
|
if !c.PaymentInProgress {
|
||||||
c.Items = make([]*CartItem, 0, len(msg.Items))
|
c.mu.Lock()
|
||||||
c.mu.Unlock()
|
c.Items = make([]*CartItem, 0, len(msg.Items))
|
||||||
for _, item := range msg.Items {
|
c.mu.Unlock()
|
||||||
c.AddItem(item.Sku, int(item.Quantity))
|
for _, item := range msg.Items {
|
||||||
|
c.AddItem(item.Sku, int(item.Quantity))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
case AddRequestType:
|
case AddRequestType:
|
||||||
msg, ok := message.Content.(*messages.AddRequest)
|
msg, ok := message.Content.(*messages.AddRequest)
|
||||||
if !ok {
|
if !ok {
|
||||||
err = fmt.Errorf("expected AddRequest")
|
err = fmt.Errorf("expected AddRequest")
|
||||||
} else {
|
} else {
|
||||||
existingItem, found := c.FindItemWithSku(msg.Sku)
|
if !c.PaymentInProgress {
|
||||||
if found {
|
existingItem, found := c.FindItemWithSku(msg.Sku)
|
||||||
existingItem.Quantity += int(msg.Quantity)
|
if found {
|
||||||
c.TotalPrice += existingItem.Price * int64(msg.Quantity)
|
existingItem.Quantity += int(msg.Quantity)
|
||||||
} else {
|
c.TotalPrice += existingItem.Price * int64(msg.Quantity)
|
||||||
return c.AddItem(msg.Sku, int(msg.Quantity)) // extent AddRequest to include quantity
|
} else {
|
||||||
|
return c.AddItem(msg.Sku, int(msg.Quantity)) // extent AddRequest to include quantity
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case AddItemType:
|
case AddItemType:
|
||||||
@@ -269,37 +272,38 @@ func (c *CartGrain) HandleMessage(message *Message, isReplay bool) (*FrameWithPa
|
|||||||
if !ok {
|
if !ok {
|
||||||
err = fmt.Errorf("expected AddItem")
|
err = fmt.Errorf("expected AddItem")
|
||||||
} else {
|
} else {
|
||||||
|
if !c.PaymentInProgress {
|
||||||
if msg.Quantity < 1 {
|
if msg.Quantity < 1 {
|
||||||
return nil, fmt.Errorf("invalid quantity")
|
return nil, fmt.Errorf("invalid quantity")
|
||||||
}
|
}
|
||||||
existingItem, found := c.FindItemWithSku(msg.Sku)
|
existingItem, found := c.FindItemWithSku(msg.Sku)
|
||||||
if found {
|
if found {
|
||||||
existingItem.Quantity += int(msg.Quantity)
|
existingItem.Quantity += int(msg.Quantity)
|
||||||
c.TotalPrice += existingItem.Price * int64(msg.Quantity)
|
c.TotalPrice += existingItem.Price * int64(msg.Quantity)
|
||||||
} else {
|
} else {
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
c.lastItemId++
|
c.lastItemId++
|
||||||
tax := 2500
|
tax := 2500
|
||||||
if msg.Tax > 0 {
|
if msg.Tax > 0 {
|
||||||
tax = int(msg.Tax)
|
tax = int(msg.Tax)
|
||||||
|
}
|
||||||
|
c.Items = append(c.Items, &CartItem{
|
||||||
|
Id: c.lastItemId,
|
||||||
|
Quantity: int(msg.Quantity),
|
||||||
|
Sku: msg.Sku,
|
||||||
|
Name: msg.Name,
|
||||||
|
Price: msg.Price,
|
||||||
|
Image: msg.Image,
|
||||||
|
Stock: StockStatus(msg.Stock),
|
||||||
|
Disclaimer: msg.Disclaimer,
|
||||||
|
OrgPrice: msg.OrgPrice,
|
||||||
|
ArticleType: msg.ArticleType,
|
||||||
|
Outlet: msg.Outlet,
|
||||||
|
Tax: tax,
|
||||||
|
})
|
||||||
|
c.TotalPrice += msg.Price * int64(msg.Quantity)
|
||||||
|
c.mu.Unlock()
|
||||||
}
|
}
|
||||||
c.Items = append(c.Items, &CartItem{
|
|
||||||
Id: c.lastItemId,
|
|
||||||
Quantity: int(msg.Quantity),
|
|
||||||
Sku: msg.Sku,
|
|
||||||
Name: msg.Name,
|
|
||||||
Price: msg.Price,
|
|
||||||
Image: msg.Image,
|
|
||||||
Stock: StockStatus(msg.Stock),
|
|
||||||
Disclaimer: msg.Disclaimer,
|
|
||||||
OrgPrice: msg.OrgPrice,
|
|
||||||
ArticleType: msg.ArticleType,
|
|
||||||
Outlet: msg.Outlet,
|
|
||||||
Tax: tax,
|
|
||||||
})
|
|
||||||
c.TotalPrice += msg.Price * int64(msg.Quantity)
|
|
||||||
c.mu.Unlock()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case ChangeQuantityType:
|
case ChangeQuantityType:
|
||||||
@@ -307,18 +311,20 @@ func (c *CartGrain) HandleMessage(message *Message, isReplay bool) (*FrameWithPa
|
|||||||
if !ok {
|
if !ok {
|
||||||
err = fmt.Errorf("expected ChangeQuantity")
|
err = fmt.Errorf("expected ChangeQuantity")
|
||||||
} else {
|
} else {
|
||||||
for i, item := range c.Items {
|
if !c.PaymentInProgress {
|
||||||
if item.Id == int(msg.Id) {
|
for i, item := range c.Items {
|
||||||
if msg.Quantity <= 0 {
|
if item.Id == int(msg.Id) {
|
||||||
c.TotalPrice -= item.Price * int64(item.Quantity)
|
if msg.Quantity <= 0 {
|
||||||
c.Items = append(c.Items[:i], c.Items[i+1:]...)
|
c.TotalPrice -= item.Price * int64(item.Quantity)
|
||||||
} else {
|
c.Items = append(c.Items[:i], c.Items[i+1:]...)
|
||||||
diff := int(msg.Quantity) - item.Quantity
|
} else {
|
||||||
item.Quantity = int(msg.Quantity)
|
diff := int(msg.Quantity) - item.Quantity
|
||||||
c.TotalPrice += item.Price * int64(diff)
|
item.Quantity = int(msg.Quantity)
|
||||||
}
|
c.TotalPrice += item.Price * int64(diff)
|
||||||
|
}
|
||||||
|
|
||||||
break
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -327,52 +333,56 @@ func (c *CartGrain) HandleMessage(message *Message, isReplay bool) (*FrameWithPa
|
|||||||
if !ok {
|
if !ok {
|
||||||
err = fmt.Errorf("expected RemoveItem")
|
err = fmt.Errorf("expected RemoveItem")
|
||||||
} else {
|
} else {
|
||||||
items := make([]*CartItem, 0, len(c.Items))
|
if !c.PaymentInProgress {
|
||||||
for _, item := range c.Items {
|
items := make([]*CartItem, 0, len(c.Items))
|
||||||
if item.Id == int(msg.Id) {
|
for _, item := range c.Items {
|
||||||
c.TotalPrice -= item.Price * int64(item.Quantity)
|
if item.Id == int(msg.Id) {
|
||||||
} else {
|
c.TotalPrice -= item.Price * int64(item.Quantity)
|
||||||
items = append(items, item)
|
} else {
|
||||||
|
items = append(items, item)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
c.Items = items
|
||||||
}
|
}
|
||||||
c.Items = items
|
|
||||||
}
|
}
|
||||||
case SetDeliveryType:
|
case SetDeliveryType:
|
||||||
msg, ok := message.Content.(*messages.SetDelivery)
|
msg, ok := message.Content.(*messages.SetDelivery)
|
||||||
if !ok {
|
if !ok {
|
||||||
err = fmt.Errorf("expected SetDelivery")
|
err = fmt.Errorf("expected SetDelivery")
|
||||||
} else {
|
} else {
|
||||||
c.lastDeliveryId++
|
if !c.PaymentInProgress {
|
||||||
items := make([]int, 0)
|
c.lastDeliveryId++
|
||||||
withDelivery := c.ItemsWithDelivery()
|
items := make([]int, 0)
|
||||||
if len(msg.Items) == 0 {
|
withDelivery := c.ItemsWithDelivery()
|
||||||
items = append(items, c.ItemsWithoutDelivery()...)
|
if len(msg.Items) == 0 {
|
||||||
} else {
|
items = append(items, c.ItemsWithoutDelivery()...)
|
||||||
for _, id := range msg.Items {
|
} else {
|
||||||
for _, item := range c.Items {
|
for _, id := range msg.Items {
|
||||||
if item.Id == int(id) {
|
for _, item := range c.Items {
|
||||||
if slices.Contains(withDelivery, item.Id) {
|
if item.Id == int(id) {
|
||||||
return nil, fmt.Errorf("item already has delivery")
|
if slices.Contains(withDelivery, item.Id) {
|
||||||
|
return nil, fmt.Errorf("item already has delivery")
|
||||||
|
}
|
||||||
|
items = append(items, int(item.Id))
|
||||||
|
break
|
||||||
}
|
}
|
||||||
items = append(items, int(item.Id))
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
if len(items) > 0 {
|
||||||
if len(items) > 0 {
|
|
||||||
|
|
||||||
c.Deliveries = append(c.Deliveries, &CartDelivery{
|
c.Deliveries = append(c.Deliveries, &CartDelivery{
|
||||||
Id: c.lastDeliveryId,
|
Id: c.lastDeliveryId,
|
||||||
Provider: msg.Provider,
|
Provider: msg.Provider,
|
||||||
Price: 49,
|
Price: 49,
|
||||||
Items: items,
|
Items: items,
|
||||||
})
|
})
|
||||||
c.Processing = true
|
c.Processing = true
|
||||||
go func() {
|
go func() {
|
||||||
time.Sleep(5 * time.Second)
|
time.Sleep(5 * time.Second)
|
||||||
c.Processing = false
|
c.Processing = false
|
||||||
}()
|
}()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case RemoveDeliveryType:
|
case RemoveDeliveryType:
|
||||||
@@ -380,32 +390,36 @@ func (c *CartGrain) HandleMessage(message *Message, isReplay bool) (*FrameWithPa
|
|||||||
if !ok {
|
if !ok {
|
||||||
err = fmt.Errorf("expected RemoveDelivery")
|
err = fmt.Errorf("expected RemoveDelivery")
|
||||||
} else {
|
} else {
|
||||||
deliveries := make([]*CartDelivery, 0, len(c.Deliveries))
|
if !c.PaymentInProgress {
|
||||||
for _, delivery := range c.Deliveries {
|
deliveries := make([]*CartDelivery, 0, len(c.Deliveries))
|
||||||
if delivery.Id == int(msg.Id) {
|
for _, delivery := range c.Deliveries {
|
||||||
c.TotalPrice -= delivery.Price
|
if delivery.Id == int(msg.Id) {
|
||||||
} else {
|
c.TotalPrice -= delivery.Price
|
||||||
deliveries = append(deliveries, delivery)
|
} else {
|
||||||
|
deliveries = append(deliveries, delivery)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
c.Deliveries = deliveries
|
||||||
}
|
}
|
||||||
c.Deliveries = deliveries
|
|
||||||
}
|
}
|
||||||
case SetPickupPointType:
|
case SetPickupPointType:
|
||||||
msg, ok := message.Content.(*messages.SetPickupPoint)
|
msg, ok := message.Content.(*messages.SetPickupPoint)
|
||||||
if !ok {
|
if !ok {
|
||||||
err = fmt.Errorf("expected SetPickupPoint")
|
err = fmt.Errorf("expected SetPickupPoint")
|
||||||
} else {
|
} else {
|
||||||
for _, delivery := range c.Deliveries {
|
if !c.PaymentInProgress {
|
||||||
if delivery.Id == int(msg.DeliveryId) {
|
for _, delivery := range c.Deliveries {
|
||||||
delivery.PickupPoint = &messages.PickupPoint{
|
if delivery.Id == int(msg.DeliveryId) {
|
||||||
Id: msg.Id,
|
delivery.PickupPoint = &messages.PickupPoint{
|
||||||
Address: msg.Address,
|
Id: msg.Id,
|
||||||
City: msg.City,
|
Address: msg.Address,
|
||||||
Zip: msg.Zip,
|
City: msg.City,
|
||||||
Country: msg.Country,
|
Zip: msg.Zip,
|
||||||
Name: msg.Name,
|
Country: msg.Country,
|
||||||
|
Name: msg.Name,
|
||||||
|
}
|
||||||
|
break
|
||||||
}
|
}
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -457,6 +471,14 @@ func (c *CartGrain) HandleMessage(message *Message, isReplay bool) (*FrameWithPa
|
|||||||
result := MakeFrameWithPayload(RemoteCreateOrderReply, 200, orderPayload)
|
result := MakeFrameWithPayload(RemoteCreateOrderReply, 200, orderPayload)
|
||||||
return &result, nil
|
return &result, nil
|
||||||
}
|
}
|
||||||
|
case OrderCompletedType:
|
||||||
|
msg, ok := message.Content.(*messages.OrderCreated)
|
||||||
|
if !ok {
|
||||||
|
err = fmt.Errorf("expected OrderCompleted")
|
||||||
|
} else {
|
||||||
|
c.OrderReference = msg.OrderId
|
||||||
|
c.PaymentReference = msg.Status
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
err = fmt.Errorf("unknown message type %d", message.Type)
|
err = fmt.Errorf("unknown message type %d", message.Type)
|
||||||
}
|
}
|
||||||
|
|||||||
2
main.go
2
main.go
@@ -224,7 +224,7 @@ func main() {
|
|||||||
Status: klarnaOrderResponse.Status},
|
Status: klarnaOrderResponse.Status},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error processing message: %v\n", err)
|
log.Printf("Error processing cart message: %v\n", err)
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
w.Write([]byte(err.Error()))
|
w.Write([]byte(err.Error()))
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user