-
Notifications
You must be signed in to change notification settings - Fork 1
/
subscription-api.go
83 lines (65 loc) · 1.73 KB
/
subscription-api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"net/http"
"github.com/go-chi/chi"
"github.com/go-chi/render"
)
func subscriptionAPI(r chi.Router) {
r.Post("/webhook", stripeWebhook)
r.Group(func(r chi.Router) {
r.Use(RequireOwner())
r.Post("/checkoutsession", postCheckoutSession)
})
r.Group(func(r chi.Router) {
r.Use(RequireOwner())
r.Use(requireChangeableSubscription())
r.Post("/change", postChangeSubscription)
})
}
func stripeWebhook(w http.ResponseWriter, r *http.Request) {
err := GetEnv(r).Service.StripeWebhook(r)
if err != nil {
_ = render.Render(w, r, ErrInvalidRequest(err))
return
}
return
}
type getCheckoutSessionRequest struct {
Plan string `json:"plan"`
Quantity int64 `json:"quantity"`
}
func (p *getCheckoutSessionRequest) Bind(r *http.Request) error {
return nil
}
func postCheckoutSession(w http.ResponseWriter, r *http.Request) {
data := &getCheckoutSessionRequest{}
if err := render.Bind(r, data); err != nil {
_ = render.Render(w, r, ErrInvalidRequest(err))
return
}
m, err := GetEnv(r).Service.GetSubscriptionPlanSession(data.Plan, data.Quantity)
if err != nil {
_ = render.Render(w, r, ErrInvalidRequest(err))
return
}
render.JSON(w, r, m)
}
type postChangeSubscriptionRequest struct {
Plan string `json:"plan"`
Quantity int64 `json:"quantity"`
}
func (p *postChangeSubscriptionRequest) Bind(r *http.Request) error {
return nil
}
func postChangeSubscription(w http.ResponseWriter, r *http.Request) {
data := &getCheckoutSessionRequest{}
if err := render.Bind(r, data); err != nil {
_ = render.Render(w, r, ErrInvalidRequest(err))
return
}
err := GetEnv(r).Service.ChangeSubscription(data.Plan, data.Quantity)
if err != nil {
_ = render.Render(w, r, ErrInvalidRequest(err))
return
}
}