-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathplan.go
82 lines (64 loc) · 1.34 KB
/
plan.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
// +build none
package main
import "time"
type Plan struct {
Created time.Time
Expires time.Time
}
func (s *Plan) Active() bool {
return s.Expires.After(time.Now())
}
func NewPlan(duration time.Duration) *Plan {
plan := &Plan{
Created: time.Now(),
}
if duration != 0 {
plan.Expires = time.Now().Add(duration)
}
return plan
}
type FreePlan struct {
*Plan
}
func NewFreePlan(duration time.Duration) *FreePlan {
return &FreePlan{
NewPlan(duration),
}
}
type ItunesPlan struct {
*Plan
Receipt string
Status int
}
func NewItunesPlan() *ItunesPlan {
return &ItunesPlan{
Plan: NewPlan(0),
}
}
func (server *Server) UpdatePlansForAccount(acc *Account) error {
if acc.Plans.Itunes != nil {
// Revalidate itunes receipt to see if the plan has been renewed
plan, err := server.Itunes.ValidateReceipt(acc.Plans.Itunes.Receipt)
if err != nil {
return err
}
acc.Plans.Itunes = plan
if err := server.Storage.Put(acc); err != nil {
return err
}
// If the itunes plan has been renewed then we can stop right here
if acc.Plans.Itunes.Active() {
return nil
}
}
return nil
}
func (server *Server) CheckPlansForAccount(acc *Account) (bool, error) {
if acc.HasActivePlan() {
return true, nil
}
if err := server.UpdatePlansForAccount(acc); err != nil {
return false, err
}
return acc.HasActivePlan(), nil
}