forked from blacklightcms/recurly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
accounts_service.go
156 lines (127 loc) · 4.11 KB
/
accounts_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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
package recurly
import (
"encoding/xml"
"fmt"
"net/http"
)
const (
// AccountStateActive is the status for active accounts.
AccountStateActive = "active"
// AccountStateClosed is the status for closed accounts.
AccountStateClosed = "closed"
)
var _ AccountsService = &accountsImpl{}
// accountsImpl handles communication with the accounts related methods
// of the recurly API.
type accountsImpl struct {
client *Client
}
// List returns a list of the accounts on your site.
// https://dev.recurly.com/docs/list-accounts
func (s *accountsImpl) List(params Params) (*Response, []Account, error) {
req, err := s.client.newRequest("GET", "accounts", params, nil)
if err != nil {
return nil, nil, err
}
var a struct {
XMLName xml.Name `xml:"accounts"`
Accounts []Account `xml:"account"`
}
resp, err := s.client.do(req, &a)
for i := range a.Accounts {
a.Accounts[i].BillingInfo = nil
}
return resp, a.Accounts, err
}
// Get returns information about a single account.
// https://dev.recurly.com/docs/get-account
func (s *accountsImpl) Get(code string) (*Response, *Account, error) {
action := fmt.Sprintf("accounts/%s", code)
req, err := s.client.newRequest("GET", action, nil, nil)
if err != nil {
return nil, nil, err
}
var a Account
resp, err := s.client.do(req, &a)
if err != nil || resp.StatusCode >= http.StatusBadRequest {
return resp, nil, err
}
a.BillingInfo = nil
return resp, &a, err
}
// LookupAccountBalance returns an account's balance.
// https://dev.recurly.com/v2.5/docs/lookup-account-balance
func (s *accountsImpl) LookupAccountBalance(code string) (*Response, *AccountBalance, error) {
action := fmt.Sprintf("accounts/%s/balance", code)
req, err := s.client.newRequest("GET", action, nil, nil)
if err != nil {
return nil, nil, err
}
var b AccountBalance
resp, err := s.client.do(req, &b)
b.AccountCode = code
return resp, &b, err
}
// Create will create a new account. You may optionally include billing information.
// https://dev.recurly.com/docs/create-an-account
func (s *accountsImpl) Create(a Account) (*Response, *Account, error) {
req, err := s.client.newRequest("POST", "accounts", nil, a)
if err != nil {
return nil, nil, err
}
var dst Account
resp, err := s.client.do(req, &dst)
dst.BillingInfo = nil
return resp, &dst, err
}
// Update will update an existing account.
// It's recommended to create a new account object with only the changes you
// want to make. The updated account object will be returned on success.
// https://dev.recurly.com/docs/update-account
func (s *accountsImpl) Update(code string, a Account) (*Response, *Account, error) {
action := fmt.Sprintf("accounts/%s", code)
req, err := s.client.newRequest("PUT", action, nil, a)
if err != nil {
return nil, nil, err
}
var dst Account
resp, err := s.client.do(req, &dst)
dst.BillingInfo = nil
return resp, &dst, err
}
// Close marks an account as closed and cancels any active subscriptions. Any
// saved billing information will also be permanently removed from the account.
// https://dev.recurly.com/docs/close-account
func (s *accountsImpl) Close(code string) (*Response, error) {
action := fmt.Sprintf("accounts/%s", code)
req, err := s.client.newRequest("DELETE", action, nil, nil)
if err != nil {
return nil, err
}
return s.client.do(req, nil)
}
// Reopen transitions a closed account back to active.
// https://dev.recurly.com/docs/reopen-account
func (s *accountsImpl) Reopen(code string) (*Response, error) {
action := fmt.Sprintf("accounts/%s/reopen", code)
req, err := s.client.newRequest("PUT", action, nil, nil)
if err != nil {
return nil, err
}
return s.client.do(req, nil)
}
// ListNotes returns a list of the notes on an account sorted in descending order.
// https://dev.recurly.com/docs/list-account-notes
func (s *accountsImpl) ListNotes(code string) (*Response, []Note, error) {
action := fmt.Sprintf("accounts/%s/notes", code)
req, err := s.client.newRequest("GET", action, nil, nil)
if err != nil {
return nil, nil, err
}
var n struct {
XMLName xml.Name `xml:"notes"`
Notes []Note `xml:"note"`
}
resp, err := s.client.do(req, &n)
return resp, n.Notes, err
}