forked from blacklightcms/recurly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_ons_service.go
94 lines (77 loc) · 2.51 KB
/
add_ons_service.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
package recurly
import (
"encoding/xml"
"fmt"
"net/http"
)
var _ AddOnsService = &addOnsImpl{}
// addOnsImpl handles communication with the add ons related methods
// of the recurly API.
type addOnsImpl struct {
client *Client
}
// List returns a list of add ons for a plan.
// https://dev.recurly.com/docs/list-add-ons-for-a-plan
func (s *addOnsImpl) List(planCode string, params Params) (*Response, []AddOn, error) {
action := fmt.Sprintf("plans/%s/add_ons", planCode)
req, err := s.client.newRequest("GET", action, params, nil)
if err != nil {
return nil, nil, err
}
var p struct {
XMLName xml.Name `xml:"add_ons"`
AddOns []AddOn `xml:"add_on"`
}
resp, err := s.client.do(req, &p)
return resp, p.AddOns, err
}
// Get returns information about an add on.
// https://dev.recurly.com/docs/lookup-an-add-on
func (s *addOnsImpl) Get(planCode string, code string) (*Response, *AddOn, error) {
action := fmt.Sprintf("plans/%s/add_ons/%s", planCode, code)
req, err := s.client.newRequest("GET", action, nil, nil)
if err != nil {
return nil, nil, err
}
var dst AddOn
resp, err := s.client.do(req, &dst)
if err != nil || resp.StatusCode >= http.StatusBadRequest {
return resp, nil, err
}
return resp, &dst, err
}
// Create adds an add on to a plan.
// https://dev.recurly.com/docs/create-an-add-on
func (s *addOnsImpl) Create(planCode string, a AddOn) (*Response, *AddOn, error) {
action := fmt.Sprintf("plans/%s/add_ons", planCode)
req, err := s.client.newRequest("POST", action, nil, a)
if err != nil {
return nil, nil, err
}
var dst AddOn
resp, err := s.client.do(req, &dst)
return resp, &dst, err
}
// Update will update the pricing information or description for an add-on.
// Subscriptions who have already subscribed to the add-on will not receive the new pricing.
// https://dev.recurly.com/docs/update-an-add-on
func (s *addOnsImpl) Update(planCode string, code string, a AddOn) (*Response, *AddOn, error) {
action := fmt.Sprintf("plans/%s/add_ons/%s", planCode, code)
req, err := s.client.newRequest("PUT", action, nil, a)
if err != nil {
return nil, nil, err
}
var dst AddOn
resp, err := s.client.do(req, &dst)
return resp, &dst, err
}
// Delete will remove an add on from a plan.
// https://dev.recurly.com/docs/delete-an-add-on
func (s *addOnsImpl) Delete(planCode string, code string) (*Response, error) {
action := fmt.Sprintf("plans/%s/add_ons/%s", planCode, code)
req, err := s.client.newRequest("DELETE", action, nil, nil)
if err != nil {
return nil, err
}
return s.client.do(req, nil)
}