-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmass_payment.go
211 lines (169 loc) · 6.86 KB
/
mass_payment.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package dwolla
import (
"context"
"errors"
"fmt"
"net/url"
)
const (
// MassPaymentStatusDeferred is when a mass payment is deferred
MassPaymentStatusDeferred MassPaymentStatus = "deferred"
// MassPaymentStatusPending is when the mass payment is pending
MassPaymentStatusPending MassPaymentStatus = "pending"
// MassPaymentStatusProcessing is when the mass payment is processing
MassPaymentStatusProcessing MassPaymentStatus = "processing"
// MassPaymentStatusComplete is when the mass payment is complete
MassPaymentStatusComplete MassPaymentStatus = "complete"
// MassPaymentStatusCancelled is when the mass payment is cancelled
MassPaymentStatusCancelled MassPaymentStatus = "cancelled"
)
const (
// MassPaymentItemStatusPending is when a mass payment item is pending
MassPaymentItemStatusPending MassPaymentItemStatus = "pending"
// MassPaymentItemStatusSuccess is when amass payment item is successful
MassPaymentItemStatusSuccess MassPaymentItemStatus = "success"
// MassPaymentItemStatusFailed is when a mass payment item failed
MassPaymentItemStatusFailed MassPaymentItemStatus = "failed"
)
// MassPaymentService is the mass payment service interface
//
// see: https://docsv2.dwolla.com/#mass-payments
type MassPaymentService interface {
Create(context.Context, *MassPayment) (*MassPayment, error)
Retrieve(context.Context, string) (*MassPayment, error)
Update(context.Context, string, MassPaymentStatus) (*MassPayment, error)
}
// MassPaymentServiceOp is an implementation of the mass payment interface
type MassPaymentServiceOp struct {
client *Client
}
// MassPaymentStatus is a mass payment status
type MassPaymentStatus string
// MassPayment is a dwolla mass payment
type MassPayment struct {
Resource
ID string `json:"id,omitempty"`
Status MassPaymentStatus `json:"status,omitempty"`
ACHDetails *ACHDetails `json:"achDetails,omitempty"`
Clearing *Clearing `json:"clearing,omitempty"`
Items []MassPaymentItem `json:"items,omitempty"`
Embedded map[string][]MassPaymentItem `json:"_embedded,omitempty"`
Created string `json:"created,omitempty"`
MetaData MetaData `json:"metadata,omitempty"`
Total Amount `json:"total,omitempty"`
TotalFees Amount `json:"totalFees,omitempty"`
CorrelationID string `json:"correlationId,omitempty"`
}
// MassPayments is a collection of mass payments
type MassPayments struct {
Collection
Embedded map[string][]MassPayment `json:"_embedded"`
}
// MassPaymentItemStatus is a mass payment item status
type MassPaymentItemStatus string
// MassPaymentItem is a dwolla mass payment item
type MassPaymentItem struct {
Resource
ID string `json:"id,omitempty"`
Status MassPaymentItemStatus `json:"status,omitempty"`
Amount Amount `json:"amount,omitempty"`
MetaData MetaData `json:"metadata,omitempty"`
CorrelationID string `json:"correlationId,omitempty"`
Embedded HALErrors `json:"_embedded,omitempty"`
}
// MassPaymentItems is a collection of mass payment items
type MassPaymentItems struct {
Collection
Embedded map[string][]MassPaymentItem `json:"_embedded"`
Total int `json:"total"`
}
// Create initiates a mass payment
//
// see: https://docsv2.dwolla.com/#initiate-a-mass-payment
func (m *MassPaymentServiceOp) Create(ctx context.Context, body *MassPayment) (*MassPayment, error) {
var payment MassPayment
if err := m.client.Post(ctx, "mass-payments", body, nil, &payment); err != nil {
return nil, err
}
payment.client = m.client
return &payment, nil
}
// Retrieve retrieves the mass payment matching the id
//
// see: https://docsv2.dwolla.com/#retrieve-a-mass-payment
func (m *MassPaymentServiceOp) Retrieve(ctx context.Context, id string) (*MassPayment, error) {
var payment MassPayment
if err := m.client.Get(ctx, fmt.Sprintf("mass-payments/%s", id), nil, nil, &payment); err != nil {
return nil, err
}
payment.client = m.client
return &payment, nil
}
// Update updates a mass payment's status
//
// see: https://docsv2.dwolla.com/#update-a-mass-payment
func (m *MassPaymentServiceOp) Update(ctx context.Context, id string, status MassPaymentStatus) (*MassPayment, error) {
var payment MassPayment
body := &MassPayment{Status: status}
if err := m.client.Post(ctx, fmt.Sprintf("mass-payments/%s", id), body, nil, &payment); err != nil {
return nil, err
}
payment.client = m.client
return &payment, nil
}
// ListItems returns a collection of items for the mass payment
//
// see: https://docsv2.dwolla.com/#list-items-for-a-mass-payment
func (m *MassPayment) ListItems(ctx context.Context, params *url.Values) (*MassPaymentItems, error) {
var items MassPaymentItems
if _, ok := m.Links["items"]; !ok {
return nil, errors.New("No items resource link")
}
if err := m.client.Get(ctx, m.Links["items"].Href, params, nil, &items); err != nil {
return nil, err
}
items.client = m.client
for i := range items.Embedded["items"] {
items.Embedded["items"][i].client = m.client
}
return &items, nil
}
// RetrieveItem returns a mass payment item matching id
//
// see: https://docsv2.dwolla.com/#retrieve-a-mass-payment-item
func (m *MassPayment) RetrieveItem(ctx context.Context, id string) (*MassPaymentItem, error) {
var item MassPaymentItem
if err := m.client.Get(ctx, fmt.Sprintf("mass-payment-items/%s", id), nil, nil, &item); err != nil {
return nil, err
}
item.client = m.client
return &item, nil
}
// RetrieveSource retrieves the mass payment funding source
func (m *MassPayment) RetrieveSource(ctx context.Context) (*FundingSource, error) {
if _, ok := m.Links["source"]; !ok {
return nil, errors.New("No source resource link")
}
return m.client.FundingSource.Retrieve(ctx, m.Links["source"].Href)
}
// RetrieveDestination retrieves the destination for the item
func (m *MassPaymentItem) RetrieveDestination(ctx context.Context) (*Customer, error) {
if _, ok := m.Links["destination"]; !ok {
return nil, errors.New("No destination resource link")
}
return m.client.Customer.Retrieve(ctx, m.Links["destination"].Href)
}
// RetrieveMassPayment retrieves the mass payment for the item
func (m *MassPaymentItem) RetrieveMassPayment(ctx context.Context) (*MassPayment, error) {
if _, ok := m.Links["mass-payment"]; !ok {
return nil, errors.New("No mass payment resource link")
}
return m.client.MassPayment.Retrieve(ctx, m.Links["mass-payment"].Href)
}
// RetrieveTransfer retrieves the transfer for the item
func (m *MassPaymentItem) RetrieveTransfer(ctx context.Context) (*Transfer, error) {
if _, ok := m.Links["transfer"]; !ok {
return nil, errors.New("No transfer resource link")
}
return m.client.Transfer.Retrieve(ctx, m.Links["transfer"].Href)
}