Skip to content
This repository has been archived by the owner on Jan 2, 2024. It is now read-only.

Commit

Permalink
control: rework subscription and schedule approach (#218)
Browse files Browse the repository at this point in the history
This changes the scheduling logic to consider the subscription as the source of
truth, rather than trying to force the schedule API into being it. The new
approach drastically simplifies the ability to reason about how Tier converts a
single timeline into the complex beast that is Stripe schedules and
subscription schedules because we can now say that: All schedule reads are done
through subscriptions via the active subscription's current "schedule," and all
schedules are created and updated via schedules.

Create: Schedule API creates the subscription.

Updates: The schedule API updates the subscription.

Reads: The subscription API shows the current state, including the current
current subscription schedule

This also relaxes constraints for canceling phases. Previous to this
commitment, cancellations have to go into effect immediately. Now cancel phases
may appear in multi-phase schedules where the canceling happens in the future.
The new restriction is that they are the last phase.

The new "last phase" restriction may be relaxed in the future by canceling the
current schedule as it does now but then creating a new subscription schedule,
scheduled in the future.

This also adds the ability to update default payment information for a stripe.
customer via OrgInfo.
  • Loading branch information
bmizerany committed Jan 19, 2023
1 parent 08b6e89 commit 30d4122
Show file tree
Hide file tree
Showing 10 changed files with 296 additions and 242 deletions.
31 changes: 28 additions & 3 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,17 @@ func (h *Handler) serveSubscribe(w http.ResponseWriter, r *http.Request) error {
}
}
if sr.Info != nil {
info := (*control.OrgInfo)(sr.Info)
info := infoToOrgInfo(sr.Info)
if err := h.c.PutCustomer(r.Context(), sr.Org, info); err != nil {
return err
}
}
return h.c.ScheduleNow(r.Context(), sr.Org, phases)

if len(phases) == 0 {
return nil
}

return h.c.Schedule(r.Context(), sr.Org, phases)
}

func (h *Handler) serveReport(w http.ResponseWriter, r *http.Request) error {
Expand Down Expand Up @@ -225,7 +230,15 @@ func (h *Handler) serveWhoIs(w http.ResponseWriter, r *http.Request) error {
if err != nil {
return err
}
res.OrgInfo = (*apitypes.OrgInfo)(info)
res.OrgInfo = &apitypes.OrgInfo{
Email: info.Email,
Name: info.Name,
Description: info.Description,
Phone: info.Phone,
Metadata: info.Metadata,
PaymentMethod: info.PaymentMethod,
InvoiceSettings: apitypes.InvoiceSettings(info.InvoiceSettings),
}
}

return httpJSON(w, res)
Expand Down Expand Up @@ -350,3 +363,15 @@ func (w *byteCountResponseWriter) Write(p []byte) (int, error) {
w.n += n
return n, err
}

func infoToOrgInfo(info *apitypes.OrgInfo) *control.OrgInfo {
return &control.OrgInfo{
Email: info.Email,
Name: info.Name,
Description: info.Description,
Phone: info.Phone,
Metadata: info.Metadata,
PaymentMethod: info.PaymentMethod,
InvoiceSettings: control.InvoiceSettings(info.InvoiceSettings),
}
}
6 changes: 3 additions & 3 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,9 @@ func TestPhaseBadOrg(t *testing.T) {
tc, _ := newTestClient(t)
_, err := tc.LookupPhase(ctx, "org:nope")
diff.Test(t, t.Errorf, err, &apitypes.Error{
Status: 404,
Code: "not_found",
Message: "Not Found",
Status: 400,
Code: "org_not_found",
Message: "org not found",
})
_, err = tc.LookupPhase(ctx, "")
diff.Test(t, t.Errorf, err, &apitypes.Error{
Expand Down
7 changes: 7 additions & 0 deletions api/apitypes/apitypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,19 @@ type PhaseResponse struct {
Fragments []refs.FeaturePlan `json:"fragments,omitempty"`
}

type InvoiceSettings struct {
DefaultPaymentMethod string
}

type OrgInfo struct {
Email string `json:"email"`
Name string `json:"name"`
Description string `json:"description"`
Phone string `json:"phone"`
Metadata map[string]string `json:"metadata"`

PaymentMethod string
InvoiceSettings InvoiceSettings
}

type ScheduleRequest struct {
Expand Down
7 changes: 5 additions & 2 deletions cmd/tier/tier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,11 +515,14 @@ func TestSubscribe(t *testing.T) {
"id": "sub_123",
"metadata": {
"tier.subscription": "default"
},
"schedule": {
"id": "sub_sched_123"
}
}
]
}`)
case wants(r, "DELETE", "/v1/subscriptions/sub_123"):
case wants(r, "POST", "/v1/subscription_schedules/sub_sched_123/cancel"):
got.Append(r.URL.Path)
default:
io.WriteString(w, `{}`)
Expand All @@ -528,7 +531,7 @@ func TestSubscribe(t *testing.T) {

tt.Run("subscribe", "--cancel", "org:test")

want := []string{"/v1/subscriptions/sub_123"}
want := []string{"/v1/subscription_schedules/sub_sched_123/cancel"}
diff.Test(t, t.Errorf, got.Load(), want)
}

Expand Down

0 comments on commit 30d4122

Please sign in to comment.