Add Line Items and Money to the Order API
Orders need line items and a currency for international launch. This reworks the Order message: numeric customer ids now that the customer service migrated, 64-bit totals, repeated line items in place of the unused coupon field, a Money total, and a REFUNDED status, with the statuses reordered so the most common one comes first. The server now charges from total. buf's breaking check flagged the file, so I've excluded it, since these are intentional changes.
Mproto/orders/v1/orders.proto1 of 3
Files Changed
proto/orders/v1/
server/
proto/orders/v1/orders.proto
Protocol Buffers · Click a line number to comment · Tap a line to comment@@ -1,23 +1,35 @@
syntax = "proto3";
package orders.v1;
option go_package = "example.com/orders/gen/ordersv1";
message Order {
string id = 1;
Removed: string customer_id = 2;
Removed: int32 total_cents = 3;
Removed: string coupon_code = 4;
Added: int64 customer_id = 2;
Added: int64 total_cents = 3;
Added: repeated LineItem items = 4;
Status status = 5;
Added: Money total = 6;
}
Added:
Added: message LineItem {
Added: string sku = 1;
Added: int32 quantity = 2;
Added: }
Added:
Added: message Money {
Added: int64 units = 1;
Added: string currency = 2;
Added: }
enum Status {
Removed: PENDING = 0;
Removed: PAID = 1;
Removed: SHIPPED = 2;
Added: PAID = 0;
Added: PENDING = 1;
Added: SHIPPED = 2;
Added: REFUNDED = 3;
}
message CreateOrderRequest {
Order order = 1;
}
server/orders.go
Go · Click a line number to comment · Tap a line to comment@@ -40,8 +40,8 @@
func (s *Server) CreateOrder(ctx context.Context, req *ordersv1.CreateOrderRequest) (*ordersv1.Order, error) {
o := req.GetOrder()
Removed: amount := int64(o.GetTotalCents())
Removed: if err := s.payments.Charge(ctx, o.GetCustomerId(), amount); err != nil {
Added: amount := o.GetTotal().GetUnits()
Added: if err := s.payments.Charge(ctx, strconv.FormatInt(o.GetCustomerId(), 10), amount); err != nil {
return nil, status.Errorf(codes.Unavailable, "charge failed: %v", err)
}
return s.store.Insert(ctx, o)
}
buf.yaml
YAML · Click a line number to comment · Tap a line to comment@@ -1,7 +1,9 @@
version: v1
lint:
use:
- DEFAULT
breaking:
use:
- FILE
Added: ignore:
Added: - orders/v1/orders.proto