-
Notifications
You must be signed in to change notification settings - Fork 2
/
xfers.go
170 lines (153 loc) · 4.19 KB
/
xfers.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
package xfers
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
)
const (
// EndpointSandbox Xfers API endpoint for sandbox / testing
EndpointSandbox = "https://sandbox.xfers.io/api/v3"
// Endpoint Xfers API endpoint for production
Endpoint = "https://www.xfers.io/api/v3"
// Fee Xfers Commission Fee
Fee = 0.89 // as of 29 Jan 2016
)
// Client The main Xfers client interface
type Client struct {
Endpoint string
key string
isSandbox bool
client *http.Client
}
// NewClient Initialize Xfers Client
func NewClient(key string, usesSandbox bool) (*Client, error) {
if key == "" {
return nil, errors.New("Missing API Key")
}
xClient := Client{}
xClient.isSandbox = usesSandbox
xClient.key = key
xClient.client = new(http.Client)
if xClient.isSandbox {
xClient.Endpoint = EndpointSandbox
} else {
xClient.Endpoint = Endpoint
}
return &xClient, nil
}
// PerformRequest is a Helper function to perform api call to Xfers server
// and provide common exception checking
func (xClient *Client) PerformRequest(req *http.Request) ([]byte, error) {
req.Header.Add("X-XFERS-USER-API-KEY", xClient.key)
req.Header.Add("Content-Type", "application/json")
resp, err := xClient.client.Do(req)
defer resp.Body.Close()
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if body == nil {
return nil, errors.New("Missing body")
}
if string(body[0:1]) != "[" {
errorResponse := Error{}
err = json.Unmarshal(body, &errorResponse)
if err != nil {
return nil, err
}
if errorResponse.Error != "" {
return nil, errors.New(errorResponse.Error)
}
}
return body, err
}
// GetAccountInfo get the account info of the specified Xfers Client
func (xClient *Client) GetAccountInfo() (Account, error) {
xfersAccount := Account{}
req, err := http.NewRequest("GET", xClient.Endpoint+"/user", nil)
if err != nil {
return xfersAccount, err
}
resp, err := xClient.PerformRequest(req)
if err != nil {
return xfersAccount, err
}
err = json.Unmarshal(resp, &xfersAccount)
return xfersAccount, err
}
// CreateCharge create a new Xfers Charge
func (xClient *Client) CreateCharge(param ChargeReqParam) (Charge, error) {
xfersCharge := Charge{}
param.Redirect = false // Force to JSON Response!
paramJSON, err := json.Marshal(param)
if err != nil {
return xfersCharge, err
}
req, err := http.NewRequest("POST", xClient.Endpoint+"/charges", bytes.NewReader(paramJSON))
if err != nil {
return xfersCharge, err
}
resp, err := xClient.PerformRequest(req)
if err != nil {
return xfersCharge, err
}
err = json.Unmarshal(resp, &xfersCharge)
return xfersCharge, err
}
// RetrieveCharge retrieve a particular charge
func (xClient *Client) RetrieveCharge(id string) (Charge, error) {
xfersCharge := Charge{}
req, err := http.NewRequest("GET", xClient.Endpoint+"/charges/"+id, nil)
if err != nil {
return xfersCharge, err
}
resp, err := xClient.PerformRequest(req)
if err != nil {
return xfersCharge, err
}
err = json.Unmarshal(resp, &xfersCharge)
return xfersCharge, err
}
// ListAllCharges list last 10 created charges
func (xClient *Client) ListAllCharges() ([]Charge, error) {
xfersCharges := []Charge{}
// supported params
// customer - only returns specified customer ID
// ending_before - cursor for position, use order_id
// starting_after
// limit
req, err := http.NewRequest("GET", xClient.Endpoint+"/charges", nil)
if err != nil {
return xfersCharges, err
}
resp, err := xClient.PerformRequest(req)
if err != nil {
return xfersCharges, err
}
err = json.Unmarshal(resp, &xfersCharges)
return xfersCharges, err
}
// VerifyCharge verify the notification call from the Xfers server
// Make sure the values in the request is valid
func (xClient *Client) VerifyCharge(id string, param VerifyParam) (Msg, error) {
xfersMsg := Msg{}
paramJSON, err := json.Marshal(param)
if err != nil {
return xfersMsg, err
}
req, err := http.NewRequest("POST", xClient.Endpoint+"/charges/"+id+"/validate", bytes.NewReader(paramJSON))
if err != nil {
return xfersMsg, err
}
resp, err := xClient.PerformRequest(req)
if err != nil {
return xfersMsg, err
}
err = json.Unmarshal(resp, &xfersMsg)
return xfersMsg, err
}