-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
365 lines (277 loc) · 8.22 KB
/
helpers.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package pasdk
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
var requestClient *http.Client
func getAPIRequestClient() *http.Client {
if requestClient == nil {
requestClient = &http.Client{
Timeout: 30 * time.Second,
}
}
return requestClient
}
func getRequestURL() (string, *PASDKError) {
if testsAreRunning && !shouldRunIntegrationTests() {
return "", nil
}
if !strings.Contains(userCredentials.APIURL, "https:") {
return "", buildValidationFailedError("the API URL must contain the string \"https:\"")
}
if userCredentials.APIURL[len(userCredentials.APIURL)-1:] != "/" {
return userCredentials.APIURL + "/", nil
}
return userCredentials.APIURL, nil
}
// Returns an error if there is an issue with the credentials.
func checkCredentialsExist() *PASDKError {
if len(userCredentials.APIKey) == 0 {
return buildValidationFailedError("APIKey cannot be empty - call pasdk.Initialise to pass in your credentials")
}
if len(userCredentials.APISecret) == 0 {
return buildValidationFailedError("APISecret cannot be empty - call pasdk.Initialise to pass in your credentials")
}
if !testsAreRunning && len(userCredentials.APIURL) == 0 {
return buildValidationFailedError("APIURL cannot be empty - call pasdk.Initialise to pass in the URL you want to send a request to")
}
return nil
}
func makeAPIPOSTRequest[T interface{}](formData []string, endpoint string) (*T, *PASDKError) {
paErr := checkCredentialsExist()
if paErr != nil {
return nil, paErr
}
formValues := url.Values{}
for _, data := range formData {
parts := strings.Split(data, "=")
formValues.Set(parts[0], parts[1])
}
if testsAreRunning && !shouldRunIntegrationTests() {
response, err := getMockAPIResponse[T](endpoint)
return response, err
}
request, err := http.NewRequest("POST", endpoint, strings.NewReader(formValues.Encode()))
if err != nil {
return nil, buildUnexpectedError("creating API request failed: " + err.Error())
}
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
request.Header.Add("X-Origin", "payment-assist-go-sdk")
response, err := getAPIRequestClient().Do(request)
if err != nil {
return nil, buildUnexpectedError("API request failed: " + err.Error())
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
return nil, buildUnexpectedError("reading API response failed: " + err.Error())
}
paErr = checkStatusCode(response.StatusCode, string(body))
if paErr != nil {
return nil, paErr
}
output, paErr := decodeResponseJSON[T](body)
if paErr != nil {
return nil, paErr
}
return output, nil
}
// Returns an error if the status code indicated failure.
func checkStatusCode(statusCode int, requestBody string) *PASDKError {
if (statusCode >= 0 && statusCode < 200) ||
(statusCode >= 300 && statusCode < 400) ||
(statusCode >= 500 && statusCode < 600) {
return buildUnexpectedError("API request failed returning status code " + toString(statusCode) +
": " + requestBody)
}
if statusCode >= 400 && statusCode < 500 {
return buildRequestRefusedError("API refused your request returning status code " + toString(statusCode) +
": " + requestBody)
}
return nil
}
func makeAPIGETRequest[T interface{}](formData []string, endpoint string) (*T, *PASDKError) {
paErr := checkCredentialsExist()
if paErr != nil {
return nil, paErr
}
endpoint += "?"
for _, data := range formData {
parts := strings.Split(data, "=")
endpoint += parts[0] + "=" + url.QueryEscape(parts[1]) + "&"
}
endpoint = endpoint[:len(endpoint)-1]
if testsAreRunning && !shouldRunIntegrationTests() {
response, err := getMockAPIResponse[T](endpoint)
return response, err
}
request, err := http.NewRequest("GET", endpoint, nil)
if err != nil {
return nil, buildUnexpectedError("creating API request failed: " + err.Error())
}
request.Header.Add("X-Origin", "payment-assist-go-sdk")
response, err := getAPIRequestClient().Do(request)
if err != nil {
return nil, buildUnexpectedError("API request failed: " + err.Error())
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
return nil, buildUnexpectedError("reading API response failed: " + err.Error())
}
paErr = checkStatusCode(response.StatusCode, string(body))
if paErr != nil {
return nil, paErr
}
output, paErr := decodeResponseJSON[T](body)
if paErr != nil {
return nil, paErr
}
return output, nil
}
func catchGenericPanic[T interface{}](response **T, err **PASDKError) {
data := recover()
if data != nil {
*response = nil
*err = &PASDKError{
errorMessage: "there was an unexpected panic; this may indicate a bug, " +
"please contact support: " + fmt.Sprintf("%v", data),
IsUnexpectedError: true,
}
}
}
func buildValidationFailedError(message string) *PASDKError {
return &PASDKError{
IsValidationFailedError: true,
errorMessage: message,
}
}
func buildRequestRefusedError(message string) *PASDKError {
return &PASDKError{
IsRequestRefusedError: true,
errorMessage: message,
}
}
func buildUnexpectedError(message string) *PASDKError {
return &PASDKError{
errorMessage: message,
IsUnexpectedError: true,
}
}
// Returns an error if the request failed, or if something else went wrong.
func decodeResponseJSON[T interface{}](jsonData []byte) (*T, *PASDKError) {
if len(jsonData) == 0 {
return nil, buildUnexpectedError("the response from the API was malformed: the response body was empty")
}
// The data field can change depending on whether the request was successful or not,
// so we need to first check for success before unmarshaling it into T.
var statusResponseWrapper struct {
Status string `json:"status"`
}
err := json.Unmarshal(jsonData, &statusResponseWrapper)
if err != nil {
return nil, buildUnexpectedError("failed to parse API response: " + err.Error())
}
if statusResponseWrapper.Status == "error" {
return nil, buildRequestRefusedError("the API refused your request: " + string(jsonData))
}
if statusResponseWrapper.Status != "ok" {
return nil, buildUnexpectedError("the API returned an unexpected response: " + string(jsonData))
}
// Now we can be sure the response was successful.
var responseWrapper struct {
Status string `json:"status"`
Message *string `json:"msg"`
Data T `json:"data"`
}
err = json.Unmarshal(jsonData, &responseWrapper)
if err != nil {
return nil, buildUnexpectedError("parsing JSON failed: " + err.Error())
}
return &responseWrapper.Data, nil
}
// The keys of requestParams should already be in alphabetical order.
func generateSignature(requestParams []string, secret string) string {
requestParams = capitaliseParamKeys(requestParams)
requestString := strings.Join(requestParams, "&")
if len(requestString) > 0 {
requestString += "&"
}
hasher := hmac.New(sha256.New, []byte(secret))
hasher.Write([]byte(requestString))
return hex.EncodeToString(hasher.Sum(nil))
}
func capitaliseParamKeys(params []string) []string {
output := make([]string, 0, len(params))
for _, param := range params {
parts := strings.Split(param, "=")
output = append(output, strings.ToUpper(parts[0])+"="+parts[1])
}
return output
}
func removeEmptyParams(params []string) []string {
output := make([]string, 0, len(params))
for _, param := range params {
value := strings.Split(param, "=")[1]
if len(value) > 0 {
output = append(output, param)
}
}
return output
}
func toString(input interface{}) string {
if input == nil {
return ""
}
switch input := input.(type) {
case *string:
if input == nil {
return ""
}
return *input
case string:
return input
case int:
return strconv.Itoa(input)
case *int:
if input == nil {
return ""
}
return strconv.Itoa(*input)
case bool:
if input {
return "true"
}
return "false"
case *bool:
if input == nil {
return ""
}
if *input {
return "true"
}
return "false"
case float64:
return strconv.FormatFloat(input, 'f', -1, 64)
case *float64:
if input == nil {
return ""
}
return strconv.FormatFloat(*input, 'f', -1, 64)
default:
// Be stricter when running tests.
if testsAreRunning {
panic("unrecognised type")
}
return fmt.Sprintf("%v", input)
}
}