forked from braintree-go/braintree-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
261 lines (223 loc) · 5.15 KB
/
response.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package braintree
import (
"compress/gzip"
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/dietdoctor/braintree-go/xmlnil"
)
type Response struct {
*http.Response
Body []byte
}
// TODO: remove dedicated unmarshal methods (redundant)
func (r *Response) entityName() (string, error) {
var b struct {
XMLName xml.Name
}
if err := xml.Unmarshal(r.Body, &b); err != nil {
return "", err
}
return b.XMLName.Local, nil
}
func (r *Response) merchantAccount() (*MerchantAccount, error) {
var b MerchantAccount
if err := xml.Unmarshal(r.Body, &b); err != nil {
return nil, err
}
return &b, nil
}
func (r *Response) transaction() (*Transaction, error) {
var b Transaction
if err := xml.Unmarshal(r.Body, &b); err != nil {
return nil, err
}
return &b, nil
}
func (r *Response) transactionLineItems() (TransactionLineItems, error) {
var b TransactionLineItems
if err := xml.Unmarshal(r.Body, &b); err != nil {
return nil, err
}
return b, nil
}
func (r *Response) paymentMethod() (PaymentMethod, error) {
entityName, err := r.entityName()
if err != nil {
return nil, err
}
switch entityName {
case "credit-card":
return r.creditCard()
case "paypal-account":
return r.paypalAccount()
case "venmo-account":
return r.venmoAccount()
case "android-pay-card":
return r.androidPayCard()
case "apple-pay-card":
return r.applePayCard()
}
return nil, fmt.Errorf("Unrecognized payment method %#v", entityName)
}
func (r *Response) paymentMethodNonce() (*PaymentMethodNonce, error) {
var n PaymentMethodNonce
if err := xml.Unmarshal(r.Body, &n); err != nil {
return nil, err
}
return &n, nil
}
func (r *Response) creditCard() (*CreditCard, error) {
var b CreditCard
if err := xml.Unmarshal(r.Body, &b); err != nil {
return nil, err
}
return &b, nil
}
func (r *Response) paypalAccount() (*PayPalAccount, error) {
var b PayPalAccount
if err := xml.Unmarshal(r.Body, &b); err != nil {
return nil, err
}
return &b, nil
}
func (r *Response) venmoAccount() (*VenmoAccount, error) {
var b VenmoAccount
if err := xml.Unmarshal(r.Body, &b); err != nil {
return nil, err
}
return &b, nil
}
func (r *Response) androidPayCard() (*AndroidPayCard, error) {
var b AndroidPayCard
if err := xml.Unmarshal(r.Body, &b); err != nil {
return nil, err
}
return &b, nil
}
func (r *Response) applePayCard() (*ApplePayCard, error) {
var b ApplePayCard
if err := xml.Unmarshal(r.Body, &b); err != nil {
return nil, err
}
return &b, nil
}
func (r *Response) customer() (*Customer, error) {
var b Customer
if err := xml.Unmarshal(r.Body, &b); err != nil {
return nil, err
}
return &b, nil
}
func (r *Response) subscription() (*Subscription, error) {
var b Subscription
if err := xml.Unmarshal(r.Body, &b); err != nil {
return nil, err
}
return &b, nil
}
func (r *Response) settlement() (*SettlementBatchSummary, error) {
var b SettlementBatchSummary
if err := xml.Unmarshal(r.Body, &b); err != nil {
return nil, err
}
return &b, nil
}
func (r *Response) address() (*Address, error) {
var b Address
if err := xml.Unmarshal(r.Body, &b); err != nil {
return nil, err
}
return &b, nil
}
func (r *Response) addOns() ([]AddOn, error) {
var b AddOnList
if err := xml.Unmarshal(r.Body, &b); err != nil {
return nil, err
}
return b.AddOns, nil
}
func (r *Response) discounts() ([]Discount, error) {
var b DiscountList
if err := xml.Unmarshal(r.Body, &b); err != nil {
return nil, err
}
return b.Discounts, nil
}
func (r *Response) dispute() (*Dispute, error) {
var b Dispute
if err := xml.Unmarshal(r.Body, &b); err != nil {
return nil, err
}
return &b, nil
}
func (r *Response) disputeEvidence() (*DisputeEvidence, error) {
var b DisputeEvidence
if err := xml.Unmarshal(r.Body, &b); err != nil {
return nil, err
}
return &b, nil
}
func (r *Response) unpackBody() error {
if len(r.Body) == 0 {
reader := r.Response.Body
contentEncoding := strings.ToLower(r.Response.Header.Get("Content-Encoding"))
if contentEncoding == "gzip" {
gzipReader, err := gzip.NewReader(reader)
if err != nil {
return err
}
reader = gzipReader
}
defer func() { _ = r.Response.Body.Close() }()
buf, err := ioutil.ReadAll(reader)
if err != nil {
return err
}
strippedBuf, err := xmlnil.StripNilElements(buf)
if err == nil {
r.Body = strippedBuf
} else {
r.Body = buf
}
}
return nil
}
func (r *Response) apiError() error {
var b BraintreeError
err := xml.Unmarshal(r.Body, &b)
if err == nil && b.ErrorMessage != "" {
b.statusCode = r.StatusCode
return &b
}
if r.StatusCode > 299 {
return httpError(r.StatusCode)
}
return nil
}
type APIError interface {
error
StatusCode() int
}
type httpError int
func (e httpError) StatusCode() int {
return int(e)
}
func (e httpError) Error() string {
return fmt.Sprintf("%s (%d)", http.StatusText(e.StatusCode()), e.StatusCode())
}
type invalidResponseError struct {
resp *Response
}
type InvalidResponseError interface {
error
Response() *Response
}
func (e *invalidResponseError) Error() string {
return fmt.Sprintf("braintree returned invalid response (%d)", e.resp.StatusCode)
}
func (e *invalidResponseError) Response() *Response {
return e.resp
}