-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathaccount.go
148 lines (114 loc) · 3.71 KB
/
account.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
package dwolla
import (
"context"
"errors"
"fmt"
"net/url"
"strconv"
)
// AccountService is the account service interface
//
// see: https://docsv2.dwolla.com/#accounts
type AccountService interface {
Retrieve(context.Context) (*Account, error)
}
// AccountServiceOp is an implementation of the account service interface
type AccountServiceOp struct {
client *Client
}
// Account is a dwolla account
type Account struct {
Resource
ID string `json:"id"`
Name string `json:"name"`
TimezoneOffset float32 `json:"timezoneOffset"`
Type string `json:"type"`
}
// Retrieve retrieves the dwolla account
//
// see: https://docsv2.dwolla.com/#retrieve-account-details
func (a *AccountServiceOp) Retrieve(ctx context.Context) (*Account, error) {
root, err := a.client.Root(ctx)
if err != nil {
return nil, err
}
var account Account
if _, ok := root.Links["account"]; !ok {
return nil, errors.New("No account resource link")
}
if err := a.client.Get(ctx, root.Links["account"].Href, nil, nil, &account); err != nil {
return nil, err
}
account.client = a.client
return &account, nil
}
// CreateFundingSource creates a funding source for the account
//
// see: https://docsv2.dwolla.com/#create-a-funding-source-for-an-account
func (a *Account) CreateFundingSource(ctx context.Context, body *FundingSourceRequest) (*FundingSource, error) {
var source FundingSource
if err := a.client.Post(ctx, "funding-sources", body, nil, &source); err != nil {
return nil, err
}
source.client = a.client
return &source, nil
}
// ListFundingSources returns the account's funding sources
//
// see: https://docsv2.dwolla.com/#list-funding-sources-for-an-account
func (a *Account) ListFundingSources(ctx context.Context, removed bool) (*FundingSources, error) {
var sources FundingSources
if _, ok := a.Links["funding-sources"]; !ok {
return nil, errors.New("No funding sources resource link")
}
params := &url.Values{}
params.Add("removed", strconv.FormatBool(removed))
if err := a.client.Get(ctx, a.Links["funding-sources"].Href, params, nil, &sources); err != nil {
return nil, err
}
sources.client = a.client
if _, ok := sources.Embedded["funding-sources"]; ok {
for i := range sources.Embedded["funding-sources"] {
sources.Embedded["funding-sources"][i].client = a.client
}
}
return &sources, nil
}
// ListMassPayments returns mass payments for the account
//
// see: https://docsv2.dwolla.com/#list-mass-payments-for-an-account
func (a *Account) ListMassPayments(ctx context.Context, params *url.Values) (*MassPayments, error) {
var payments MassPayments
if _, ok := a.Links["self"]; !ok {
return nil, errors.New("No self resource link")
}
if err := a.client.Get(ctx, fmt.Sprintf("%s/mass-payments", a.Links["self"].Href), params, nil, &payments); err != nil {
return nil, err
}
payments.client = a.client
if _, ok := payments.Embedded["mass-payments"]; ok {
for i := range payments.Embedded["mass-payments"] {
payments.Embedded["mass-payments"][i].client = a.client
}
}
return &payments, nil
}
// ListTransfers returns the account's transfers
//
// see: https://docsv2.dwolla.com/#list-and-search-transfers-for-an-account
func (a *Account) ListTransfers(ctx context.Context, params *url.Values) (*Transfers, error) {
var transfers Transfers
if _, ok := a.Links["transfers"]; !ok {
return nil, errors.New("No transfers resource link")
}
if err := a.client.Get(ctx, a.Links["transfers"].Href, params, nil, &transfers); err != nil {
return nil, err
}
transfers.client = a.client
if _, ok := transfers.Embedded["transfers"]; ok {
for i := range transfers.Embedded["transfers"] {
transfers.Embedded["transfers"][i].client = a.client
}
}
return &transfers, nil
}