checkoutservice: money proto migration
Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
This commit is contained in:
parent
b5b6d255ba
commit
33fae5d1d1
4 changed files with 52 additions and 36 deletions
|
@ -1,13 +1,12 @@
|
||||||
FROM golang:1.10-alpine as builder
|
FROM golang:1.10-alpine as builder
|
||||||
RUN apk add --no-cache ca-certificates git
|
RUN apk add --no-cache ca-certificates git
|
||||||
WORKDIR /src/microservices-demo/catalogservice
|
WORKDIR /go/src/checkoutservice
|
||||||
COPY . .
|
COPY . .
|
||||||
RUN go get -d ./...
|
RUN go get -d ./...
|
||||||
RUN go build -o /catalogservice .
|
RUN go build -o /checkoutservice .
|
||||||
|
|
||||||
FROM alpine as release
|
FROM alpine as release
|
||||||
RUN apk add --no-cache \
|
RUN apk add --no-cache ca-certificates
|
||||||
ca-certificates
|
COPY --from=builder /checkoutservice /checkoutservice
|
||||||
COPY --from=builder /catalogservice /catalogservice
|
|
||||||
EXPOSE 5050
|
EXPOSE 5050
|
||||||
ENTRYPOINT ["/catalogservice"]
|
ENTRYPOINT ["/checkoutservice"]
|
||||||
|
|
|
@ -12,8 +12,8 @@ import (
|
||||||
"google.golang.org/grpc/codes"
|
"google.golang.org/grpc/codes"
|
||||||
"google.golang.org/grpc/status"
|
"google.golang.org/grpc/status"
|
||||||
|
|
||||||
pb "./genproto"
|
pb "checkoutservice/genproto"
|
||||||
money "./money"
|
money "checkoutservice/money"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -68,13 +68,13 @@ func (cs *checkoutService) CreateOrder(ctx context.Context, req *pb.CreateOrderR
|
||||||
log.Printf("[CreateOrder] user_id=%q user_currency=%q", req.UserId, req.UserCurrency)
|
log.Printf("[CreateOrder] user_id=%q user_currency=%q", req.UserId, req.UserCurrency)
|
||||||
resp := new(pb.CreateOrderResponse)
|
resp := new(pb.CreateOrderResponse)
|
||||||
|
|
||||||
shippingQuoteUSD, err := cs.quoteShipping(ctx, req.Address, nil) // TODO(ahmetb): query CartService for items
|
prep, err := cs.prepareOrderItemsAndShippingQuoteFromCart(ctx, req.UserId, req.UserCurrency, req.Address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, status.Errorf(codes.Internal, "shipping quote failure: %+v", err)
|
return nil, status.Errorf(codes.Internal, err.Error())
|
||||||
}
|
}
|
||||||
resp.ShippingCost = shippingQuoteUSD
|
|
||||||
// TODO(ahmetb) convert to req.UserCurrency
|
resp.Items = prep.orderItems
|
||||||
// TODO(ahmetb) calculate resp.OrderItem with req.UserCurrency
|
resp.ShippingCost = prep.shippingCostLocalized
|
||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,30 +86,16 @@ func (cs *checkoutService) PlaceOrder(ctx context.Context, req *pb.PlaceOrderReq
|
||||||
return nil, status.Errorf(codes.Internal, "failed to generate order uuid")
|
return nil, status.Errorf(codes.Internal, "failed to generate order uuid")
|
||||||
}
|
}
|
||||||
|
|
||||||
cartItems, err := cs.getUserCart(ctx, req.UserId)
|
prep, err := cs.prepareOrderItemsAndShippingQuoteFromCart(ctx, req.UserId, req.UserCurrency, req.Address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, status.Errorf(codes.Internal, "cart failure: %+v", err)
|
return nil, status.Errorf(codes.Internal, err.Error())
|
||||||
}
|
|
||||||
|
|
||||||
orderItems, err := cs.prepOrderItems(ctx, cartItems, req.UserCurrency)
|
|
||||||
if err != nil {
|
|
||||||
return nil, status.Errorf(codes.Internal, "failed to prepare order: %+v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
shippingUsd, err := cs.quoteShipping(ctx, req.Address, cartItems) // TODO(ahmetb): query CartService for items
|
|
||||||
if err != nil {
|
|
||||||
return nil, status.Errorf(codes.Internal, "shipping quote failure: %+v", err)
|
|
||||||
}
|
|
||||||
shippingPrice, err := cs.convertCurrency(ctx, shippingUsd, req.UserCurrency)
|
|
||||||
if err != nil {
|
|
||||||
return nil, status.Errorf(codes.Internal, "failed to convert shipping cost to currency: %+v", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
total := pb.Money{CurrencyCode: req.UserCurrency,
|
total := pb.Money{CurrencyCode: req.UserCurrency,
|
||||||
Units: 0,
|
Units: 0,
|
||||||
Nanos: 0}
|
Nanos: 0}
|
||||||
total = money.Must(money.Sum(total, *shippingPrice))
|
total = money.Must(money.Sum(total, *prep.shippingCostLocalized))
|
||||||
for _, it := range orderItems {
|
for _, it := range prep.orderItems {
|
||||||
total = money.Must(money.Sum(total, *it.Cost))
|
total = money.Must(money.Sum(total, *it.Cost))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -119,7 +105,7 @@ func (cs *checkoutService) PlaceOrder(ctx context.Context, req *pb.PlaceOrderReq
|
||||||
}
|
}
|
||||||
log.Printf("payment went through (transaction_id: %s)", txID)
|
log.Printf("payment went through (transaction_id: %s)", txID)
|
||||||
|
|
||||||
shippingTrackingID, err := cs.shipOrder(ctx, req.Address, cartItems)
|
shippingTrackingID, err := cs.shipOrder(ctx, req.Address, prep.cartItems)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, status.Errorf(codes.Unavailable, "shipping error: %+v", err)
|
return nil, status.Errorf(codes.Unavailable, "shipping error: %+v", err)
|
||||||
}
|
}
|
||||||
|
@ -127,9 +113,9 @@ func (cs *checkoutService) PlaceOrder(ctx context.Context, req *pb.PlaceOrderReq
|
||||||
orderResult := &pb.OrderResult{
|
orderResult := &pb.OrderResult{
|
||||||
OrderId: orderID.String(),
|
OrderId: orderID.String(),
|
||||||
ShippingTrackingId: shippingTrackingID,
|
ShippingTrackingId: shippingTrackingID,
|
||||||
ShippingCost: shippingPrice,
|
ShippingCost: prep.shippingCostLocalized,
|
||||||
ShippingAddress: req.Address,
|
ShippingAddress: req.Address,
|
||||||
Items: orderItems,
|
Items: prep.orderItems,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := cs.sendOrderConfirmation(ctx, req.Email, orderResult); err != nil {
|
if err := cs.sendOrderConfirmation(ctx, req.Email, orderResult); err != nil {
|
||||||
|
@ -141,6 +127,37 @@ func (cs *checkoutService) PlaceOrder(ctx context.Context, req *pb.PlaceOrderReq
|
||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type orderPrep struct {
|
||||||
|
orderItems []*pb.OrderItem
|
||||||
|
cartItems []*pb.CartItem
|
||||||
|
shippingCostLocalized *pb.Money
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cs *checkoutService) prepareOrderItemsAndShippingQuoteFromCart(ctx context.Context, userID, userCurrency string, address *pb.Address) (orderPrep, error) {
|
||||||
|
var out orderPrep
|
||||||
|
cartItems, err := cs.getUserCart(ctx, userID)
|
||||||
|
if err != nil {
|
||||||
|
return out, fmt.Errorf("cart failure: %+v", err)
|
||||||
|
}
|
||||||
|
orderItems, err := cs.prepOrderItems(ctx, cartItems, userCurrency)
|
||||||
|
if err != nil {
|
||||||
|
return out, fmt.Errorf("failed to prepare order: %+v", err)
|
||||||
|
}
|
||||||
|
shippingUSD, err := cs.quoteShipping(ctx, address, cartItems)
|
||||||
|
if err != nil {
|
||||||
|
return out, fmt.Errorf("shipping quote failure: %+v", err)
|
||||||
|
}
|
||||||
|
shippingPrice, err := cs.convertCurrency(ctx, shippingUSD, userCurrency)
|
||||||
|
if err != nil {
|
||||||
|
return out, fmt.Errorf("failed to convert shipping cost to currency: %+v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
out.shippingCostLocalized = shippingPrice
|
||||||
|
out.cartItems = cartItems
|
||||||
|
out.orderItems = orderItems
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (cs *checkoutService) quoteShipping(ctx context.Context, address *pb.Address, items []*pb.CartItem) (*pb.Money, error) {
|
func (cs *checkoutService) quoteShipping(ctx context.Context, address *pb.Address, items []*pb.CartItem) (*pb.Money, error) {
|
||||||
conn, err := grpc.DialContext(ctx, cs.shippingSvcAddr, grpc.WithInsecure())
|
conn, err := grpc.DialContext(ctx, cs.shippingSvcAddr, grpc.WithInsecure())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -3,7 +3,7 @@ package money
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
pb "../genproto"
|
pb "checkoutservice/genproto"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
|
@ -5,7 +5,7 @@ import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
pb "../genproto"
|
pb "checkoutservice/genproto"
|
||||||
)
|
)
|
||||||
|
|
||||||
func mmc(u int64, n int32, c string) pb.Money { return pb.Money{Units: u, Nanos: n, CurrencyCode: c} }
|
func mmc(u int64, n int32, c string) pb.Money { return pb.Money{Units: u, Nanos: n, CurrencyCode: c} }
|
||||||
|
|
Loading…
Reference in a new issue