-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathvisa.go
More file actions
111 lines (96 loc) · 2.82 KB
/
Copy pathvisa.go
File metadata and controls
111 lines (96 loc) · 2.82 KB
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
package visa
import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/base64"
"fmt"
"io/ioutil"
"net/http"
)
const API_PROD = "https://api.visa.com"
const API_SANDBOX = "https://sandbox.api.visa.com"
const API_PORT = 443
var USER_ID = ""
var USER_PASSWORD = ""
var API_URL = API_SANDBOX // Default to sandbox
// FUND TRANSFER API
// - Pull funds
// - Push funds
// - Refund funds
// mVISA API
// Watch list screening API
func setVariables(user_id string, user_password string) {
USER_ID = user_id
USER_PASSWORD = user_password
}
func getApiUrl(production bool) {
switch production {
case false:
API_URL = API_SANDBOX
break
case true:
API_URL = API_PROD
break
}
}
func Client(userId string, userPassword string, url string, reqType string, production bool, body []byte, transactionID string, acceptType string) (response []byte, err error) {
setVariables(userId, userPassword)
authHeader := createAuthHeader()
//fmt.Printf("Body: %v\n", bytes.NewBuffer(body))
req, err := http.NewRequest(reqType, url, bytes.NewBuffer(body))
req.Header.Set("X-Client-Transaction-ID", transactionID)
req.Header.Set("Authorization:Basic ", authHeader)
req.Header.Set("Content-Type", "application/json")
// Accept Header needs to be switched for specific calls
// https://github.com/visa/SampleCode/issues/13
switch acceptType {
case "octet-stream":
req.Header.Set("Accept", "application/octet-stream")
case "json":
req.Header.Set("Accept", "application/json")
}
// Load client cert
cert, err := tls.LoadX509KeyPair(SSL_PUBLIC_KEY_PATH, SSL_PRIVATE_KEY_PATH)
if err != nil {
return nil, fmt.Errorf("Could not load key pair: %v", err)
}
// Load CA cert
caCert, err := ioutil.ReadFile(SSL_CAPRIVATE_KEY_PATH)
if err != nil {
return nil, fmt.Errorf("Could not load CA key: %v", err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
// Setup HTTPS client
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{cert},
RootCAs: caCertPool,
InsecureSkipVerify: true, //@FIXME: This call *must* be secure
}
tlsConfig.BuildNameToCertificate()
transport := &http.Transport{TLSClientConfig: tlsConfig}
client := &http.Client{Transport: transport}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("Could not load HTTPS client: %v", err)
}
defer resp.Body.Close()
// HTTP 200 and 404 are not errors
response, _ = ioutil.ReadAll(resp.Body)
switch resp.StatusCode {
case 200, 202, 404:
return response, nil
break
default:
return nil, fmt.Errorf("HTTP error: %s, %v", resp.Status, string(response))
break
}
fmt.Printf("Response: %v\n", string(response))
return response, nil
}
func createAuthHeader() (authHeader string) {
// Auth header = \ase64(userid:user_password)
authHeader = base64.StdEncoding.EncodeToString([]byte(USER_ID + ":" + USER_PASSWORD))
return
}