-
Notifications
You must be signed in to change notification settings - Fork 12
/
response.go
152 lines (123 loc) · 3.6 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
package adyen
import (
"encoding/json"
"fmt"
"net/http"
)
// APIError - handle error (non 200 status) response from Adyen
type APIError struct {
ErrorType string `json:"errorType"`
ErrorCode string `json:"errorCode"`
Message string `json:"message"`
Status int32 `json:"status"`
}
// Response - Adyen API response structure
type Response struct {
*http.Response
Body []byte
}
// handleHTTPError - handle non 200 response from Adyen and create Error response instance
func (r *Response) handleHTTPError() error {
var aerr APIError
if err := json.Unmarshal(r.Body, &aerr); err != nil {
return err
}
if aerr.Status >= http.StatusBadRequest {
return aerr
}
return nil
}
// Error - error interface for ApiError
func (e APIError) Error() string {
return fmt.Sprintf("[%s][%d]: (%s) %s", e.ErrorType, e.Status, e.ErrorCode, e.Message)
}
// authorize - generate Adyen Authorize API Response
func (r *Response) authorize() (*AuthoriseResponse, error) {
var a AuthoriseResponse
if err := json.Unmarshal(r.Body, &a); err != nil {
return nil, err
}
return &a, nil
}
// capture - generate Adyen Capture API Response
func (r *Response) capture() (*CaptureResponse, error) {
var a CaptureResponse
if err := json.Unmarshal(r.Body, &a); err != nil {
return nil, err
}
return &a, nil
}
// cancel - generate Adyen Cancel API Response
func (r *Response) cancel() (*CancelResponse, error) {
var a CancelResponse
if err := json.Unmarshal(r.Body, &a); err != nil {
return nil, err
}
return &a, nil
}
// cancelOrRefund - generate Adyen CancelOrRefund API Response
func (r *Response) cancelOrRefund() (*CancelOrRefundResponse, error) {
var a CancelOrRefundResponse
if err := json.Unmarshal(r.Body, &a); err != nil {
return nil, err
}
return &a, nil
}
// refund - generate Adyen Refund API Response
func (r *Response) refund() (*RefundResponse, error) {
var a RefundResponse
if err := json.Unmarshal(r.Body, &a); err != nil {
return nil, err
}
return &a, nil
}
// adjustAuthorisation - generate Adyen Refund API Response
func (r *Response) adjustAuthorisation() (*AdjustAuthorisationResponse, error) {
var a AdjustAuthorisationResponse
if err := json.Unmarshal(r.Body, &a); err != nil {
return nil, err
}
return &a, nil
}
// technicalCancel - generate Adyen Technical Cancel API Response
func (r *Response) technicalCancel() (*TechnicalCancelResponse, error) {
var a TechnicalCancelResponse
if err := json.Unmarshal(r.Body, &a); err != nil {
return nil, err
}
return &a, nil
}
// directoryLookup - generate Adyen Directory Lookup response
func (r *Response) directoryLookup() (*DirectoryLookupResponse, error) {
var a DirectoryLookupResponse
if err := json.Unmarshal(r.Body, &a); err != nil {
return nil, err
}
return &a, nil
}
// listRecurringDetails - generate Adyen List Recurring Details response
func (r *Response) listRecurringDetails() (*RecurringDetailsResult, error) {
var a RecurringDetailsResult
if err := json.Unmarshal(r.Body, &a); err != nil {
return nil, err
}
return &a, nil
}
// disableRecurring - generate Adyen disable recurring
//
// Link - https://docs.adyen.com/developers/api-reference/recurring-api#disableresult
func (r *Response) disableRecurring() (*RecurringDisableResponse, error) {
var a RecurringDisableResponse
if err := json.Unmarshal(r.Body, &a); err != nil {
return nil, err
}
return &a, nil
}
// paymentMethods - generate Adyen CheckoutAPI paymentMethods response.
func (r *Response) paymentMethods() (*PaymentMethodsResponse, error) {
var a PaymentMethodsResponse
if err := json.Unmarshal(r.Body, &a); err != nil {
return nil, err
}
return &a, nil
}