forked from culqi/culqi-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plans.go
86 lines (69 loc) · 2.01 KB
/
plans.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
84
85
86
package culqi
import (
"encoding/json"
"io/ioutil"
"net/http"
)
const (
plansBase = "plans"
)
//Plan holds a plan used to later subscribe a card to.
type Plan struct {
Object string `json:"object"`
ID string `json:"id"`
Amount int `json:"amount"`
CreationDate int `json:"creation_date"`
CurrencyCode string `json:"currency_code"`
Interval string `json:"interval"`
IntervalCount int `json:"interval_count"`
Limit int `json:"limit"`
Name string `json:"name"`
Subscriptions []Subscription `json:"subscriptions"`
TrialDays int `json:"trial_days"`
Metadata map[string]string `json:"metadata"`
}
//PlansPaging returns the paging structure for all plans.
type PlansPaging struct {
Data []Plan `json:"data"`
Paging Paging `json:"paging"`
}
//GetPlan returns a single plan from its ID.
func (c *Culqi) GetPlan(id string) (*Plan, error) {
req, err := http.NewRequest("GET", defaultBaseURL+"v2/"+plansBase+"/"+id, nil)
req.Header.Set("Authorization", "Bearer "+c.Conf.APIKey)
req.Header.Set("User-Agent", userAgent)
resp, err := c.HTTP.Do(req)
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
defer resp.Body.Close()
t := Plan{}
if err := json.Unmarshal(body, &t); err != nil {
return nil, err
}
return &t, nil
}
//AllPlans returns a paged results of all plans.
func (c *Culqi) AllPlans() (*PlansPaging, error) {
req, err := http.NewRequest("GET", defaultBaseURL+"v2/"+plansBase, nil)
req.Header.Set("Authorization", "Bearer "+c.Conf.APIKey)
req.Header.Set("User-Agent", userAgent)
resp, err := c.HTTP.Do(req)
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
defer resp.Body.Close()
t := PlansPaging{}
if err := json.Unmarshal(body, &t); err != nil {
return nil, err
}
return &t, nil
}