-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscription.go
143 lines (120 loc) · 3.68 KB
/
subscription.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package culqi
import (
"bytes"
"encoding/json"
"net/url"
)
const (
subscriptionURL = baseURL + "/subscriptions"
)
// Subscription objeto request de Subscription
type Subscription struct {
CardID string `json:"card_id"`
PlanID string `json:"plan_id"`
Metadata map[string]string `json:"metadata"`
}
// ResponseSubscription objeto respuesta de Subscription
type ResponseSubscription struct {
Object string `json:"object"`
ID string `json:"id"`
CreationDate int `json:"creation_date"`
Status string `json:"status"`
CurrentPeriod int `json:"current_period"`
TotalPeriods int `json:"total_periods"`
CurrentPeriodStart int `json:"current_period_start"`
CurrentPeriodEnd int `json:"current_period_end"`
CancelAtPeriodEnd bool `json:"cancel_at_period_end"`
CanceledAt int `json:"canceled_at"`
EndedAt int `json:"ended_at"`
NextBillingDate int `json:"next_billing_date"`
TrialStart int `json:"trial_start"`
TrialEnd int `json:"trial_end"`
Charges []ResponseCharge `json:"charges"`
Plan Plan `json:"plan"`
Card Card `json:"card"`
Metadata map[string]string `json:"metadata"`
}
// ResponseSubscriptionAll respuesta de subscripción para GetAll
type ResponseSubscriptionAll struct {
Data []ResponseSubscription `json:"data"`
WrapperResponse
}
// Create método para crear una Subscripción
func (sub *Subscription) Create() (*ResponseSubscription, error) {
j, err := json.Marshal(sub)
if err != nil {
return nil, err
}
res, err := do("POST", subscriptionURL, nil, bytes.NewBuffer(j))
if err != nil {
return nil, err
}
rsub := &ResponseSubscription{}
if err = json.Unmarshal(res, rsub); err != nil {
return nil, err
}
return rsub, nil
}
// GetByID método para obtener una Subscripción por id
func (sub *Subscription) GetByID(id string) (*ResponseSubscription, error) {
if id == "" {
return nil, ErrParameter
}
res, err := do("GET", subscriptionURL+"/"+id, nil, nil)
if err != nil {
return nil, err
}
rsub := &ResponseSubscription{}
if err = json.Unmarshal(res, rsub); err != nil {
return nil, err
}
return rsub, nil
}
// GetAll método para obtener la lista de las subscripciones
func (sub *Subscription) GetAll(queryParams url.Values) (*ResponseSubscriptionAll, error) {
res, err := do("GET", subscriptionURL, queryParams, nil)
if err != nil {
return nil, err
}
rsub := &ResponseSubscriptionAll{}
if err = json.Unmarshal(res, rsub); err != nil {
return nil, err
}
return rsub, nil
}
// Update método para agregar o remplazar información a los valores de la metadata de una Subscripción
func (sub *Subscription) Update(id string, metadata map[string]string) (*ResponseSubscription, error) {
if id == "" || len(metadata) == 0 {
return nil, ErrParameter
}
j, err := json.Marshal(
struct {
Metadata map[string]string `json:"metadata"`
}{
metadata,
},
)
if err != nil {
return nil, err
}
res, err := do("PATCH", subscriptionURL+"/"+id, nil, bytes.NewBuffer(j))
if err != nil {
return nil, err
}
rsub := &ResponseSubscription{}
if err = json.Unmarshal(res, rsub); err != nil {
return nil, err
}
return rsub, nil
}
// Delete método para eliminar una Subscripción por id
func (sub *Subscription) Delete(id string) error {
if id == "" {
return ErrParameter
}
_, err := do("DELETE", subscriptionURL+"/"+id, nil, nil)
if err != nil {
return err
}
return nil
}