Skip to content

Commit

Permalink
simplestore: verify cart items on place order.
Browse files Browse the repository at this point in the history
  • Loading branch information
dajohi authored and miki-totefu committed May 29, 2023
1 parent d0fb537 commit 9735b2f
Showing 1 changed file with 33 additions and 10 deletions.
43 changes: 33 additions & 10 deletions client/resources/simplestore/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,17 +215,40 @@ func (s *Store) handlePlaceOrder(ctx context.Context, uid clientintf.UserID,
}, nil
}

// Process form data.
var formData ShippingAddress
if err := json.Unmarshal(request.Data, &formData); err != nil {
return &rpc.RMFetchResourceReply{
Status: rpc.ResourceStatusBadRequest,
Data: []byte("request data not valid json"),
}, nil
}
var shipping *ShippingAddress
if formData.Address1 != "" {
shipping = &formData
// Verify the items
for _, item := range cart.Items {
prod, ok := s.products[item.Product.SKU]
if !ok {
return &rpc.RMFetchResourceReply{
Status: rpc.ResourceStatusBadRequest,
Data: []byte(fmt.Sprintf("SKU %q does not exist", item.Product.SKU)),
}, nil
}
// If a product requires shipping, ensure a shipping address
// was sent.
if shipping == nil && prod.Shipping {
// Process form data.
var formData ShippingAddress
if err := json.Unmarshal(request.Data, &formData); err != nil {
return &rpc.RMFetchResourceReply{
Status: rpc.ResourceStatusBadRequest,
Data: []byte("request data not valid json"),
}, nil
}
shipping = &formData

if shipping.Name == "" || shipping.Address1 == "" ||
shipping.City == "" || shipping.State == "" ||
shipping.PostalCode == "" {
return &rpc.RMFetchResourceReply{
Status: rpc.ResourceStatusBadRequest,
Data: []byte("incomplete shipping address"),
}, nil
}
// TODO: proper address validation, optional phone
// number validation.
}
}

// Create the order.
Expand Down

0 comments on commit 9735b2f

Please sign in to comment.