forked from rpip/paystack-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpaystack.go
273 lines (229 loc) · 7.04 KB
/
paystack.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
package paystack
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strconv"
"time"
"github.com/mitchellh/mapstructure"
)
const (
// library version
version = "0.1.0"
// defaultHTTPTimeout is the default timeout on the http client
defaultHTTPTimeout = 60 * time.Second
// base URL for all Paystack API requests
baseURL = "https://api.paystack.co"
// User agent used when communicating with the Paystack API.
// userAgent = "paystack-go/" + version
userAgent = "Mozilla/5.0 (Unknown; Linux) AppleWebKit/538.1 (KHTML, like Gecko) Chrome/v1.0.0 Safari/538.1"
)
type service struct {
client *Client
}
// Client manages communication with the Paystack API
type Client struct {
common service // Reuse a single struct instead of allocating one for each service on the heap.
client *http.Client // HTTP client used to communicate with the API.
// the API Key used to authenticate all Paystack API requests
key string
baseURL *url.URL
logger Logger
// Services supported by the Paystack API.
// Miscellaneous actions are directly implemented on the Client object
Customer *CustomerService
Transaction *TransactionService
SubAccount *SubAccountService
Plan *PlanService
Subscription *SubscriptionService
Page *PageService
Settlement *SettlementService
Transfer *TransferService
Charge *ChargeService
Bank *BankService
BulkCharge *BulkChargeService
LoggingEnabled bool
Log Logger
}
// Logger interface for custom loggers
type Logger interface {
Printf(format string, v ...interface{})
}
// Metadata is an key-value pairs added to Paystack API requests
type Metadata map[string]interface{}
// Response represents arbitrary response data
type Response map[string]interface{}
// RequestValues aliased to url.Values as a workaround
type RequestValues url.Values
// MarshalJSON to handle custom JSON decoding for RequestValues
func (v RequestValues) MarshalJSON() ([]byte, error) {
m := make(map[string]interface{}, 3)
for k, val := range v {
m[k] = val[0]
}
return json.Marshal(m)
}
// ListMeta is pagination metadata for paginated responses from the Paystack API
type ListMeta struct {
Total int `json:"total"`
Skipped int `json:"skipped"`
PerPage int `json:"perPage"`
Page int `json:"page"`
PageCount int `json:"pageCount"`
}
// NewClient creates a new Paystack API client with the given API key
// and HTTP client, allowing overriding of the HTTP client to use.
// This is useful if you're running in a Google AppEngine environment
// where the http.DefaultClient is not available.
func NewClient(key string, httpClient *http.Client) *Client {
if httpClient == nil {
httpClient = &http.Client{Timeout: defaultHTTPTimeout}
}
u, _ := url.Parse(baseURL)
c := &Client{
client: httpClient,
key: key,
baseURL: u,
LoggingEnabled: true,
Log: log.New(os.Stderr, "", log.LstdFlags),
}
c.common.client = c
c.Customer = (*CustomerService)(&c.common)
c.Transaction = (*TransactionService)(&c.common)
c.SubAccount = (*SubAccountService)(&c.common)
c.Plan = (*PlanService)(&c.common)
c.Subscription = (*SubscriptionService)(&c.common)
c.Page = (*PageService)(&c.common)
c.Settlement = (*SettlementService)(&c.common)
c.Transfer = (*TransferService)(&c.common)
c.Charge = (*ChargeService)(&c.common)
c.Bank = (*BankService)(&c.common)
c.BulkCharge = (*BulkChargeService)(&c.common)
return c
}
// Call actually does the HTTP request to Paystack API
func (c *Client) Call(method, path string, body, v interface{}) error {
var buf io.ReadWriter
if body != nil {
buf = new(bytes.Buffer)
err := json.NewEncoder(buf).Encode(body)
if err != nil {
return err
}
}
u, _ := c.baseURL.Parse(path)
req, err := http.NewRequest(method, u.String(), buf)
if err != nil {
if c.LoggingEnabled {
c.Log.Printf("Cannot create Paystack request: %v\n", err)
}
return err
}
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
req.Header.Set("Authorization", "Bearer "+c.key)
req.Header.Set("User-Agent", userAgent)
if c.LoggingEnabled {
c.Log.Printf("Requesting %v %v%v\n", req.Method, req.URL.Host, req.URL.Path)
c.Log.Printf("POST request data %v\n", buf)
}
start := time.Now()
resp, err := c.client.Do(req)
if err != nil {
return err
}
if c.LoggingEnabled {
c.Log.Printf("Completed in %v\n", time.Since(start))
}
defer resp.Body.Close()
return c.decodeResponse(resp, v)
}
// ResolveCardBIN docs https://developers.paystack.co/v1.0/reference#resolve-card-bin
func (c *Client) ResolveCardBIN(bin int) (Response, error) {
u := fmt.Sprintf("/decision/bin/%d", bin)
resp := Response{}
err := c.Call("GET", u, nil, &resp)
return resp, err
}
// CheckBalance docs https://developers.paystack.co/v1.0/reference#resolve-card-bin
func (c *Client) CheckBalance() (Response, error) {
resp := Response{}
err := c.Call("GET", "balance", nil, &resp)
// check balance 'data' node is an array
resp2 := resp["data"].([]interface{})[0].(map[string]interface{})
return resp2, err
}
// GetSessionTimeout fetches payment session timeout
func (c *Client) GetSessionTimeout() (Response, error) {
resp := Response{}
err := c.Call("GET", "/integration/payment_session_timeout", nil, &resp)
return resp, err
}
// UpdateSessionTimeout updates payment session timeout
func (c *Client) UpdateSessionTimeout(timeout int) (Response, error) {
data := url.Values{}
data.Add("timeout", strconv.Itoa(timeout))
resp := Response{}
u := "/integration/payment_session_timeout"
err := c.Call("PUT", u, data, &resp)
return resp, err
}
// INTERNALS
func paginateURL(path string, count, offset int) string {
return fmt.Sprintf("%s?perPage=%d&page=%d", path, count, offset)
}
func mapstruct(data interface{}, v interface{}) error {
config := &mapstructure.DecoderConfig{
Result: v,
TagName: "json",
WeaklyTypedInput: true,
}
decoder, err := mapstructure.NewDecoder(config)
if err != nil {
return err
}
err = decoder.Decode(data)
return err
}
func mustGetTestKey() string {
key := os.Getenv("PAYSTACK_KEY")
if len(key) == 0 {
panic("PAYSTACK_KEY environment variable is not set\n")
}
return key
}
// decodeResponse decodes the JSON response from the Twitter API.
// The actual response will be written to the `v` parameter
func (c *Client) decodeResponse(httpResp *http.Response, v interface{}) error {
var resp Response
respBody, err := ioutil.ReadAll(httpResp.Body)
json.Unmarshal(respBody, &resp)
if status, _ := resp["status"].(bool); !status || httpResp.StatusCode >= 400 {
if c.LoggingEnabled {
c.Log.Printf("Paystack error: %+v", err)
c.Log.Printf("HTTP Response: %+v", resp)
}
return newAPIError(httpResp)
}
if c.LoggingEnabled {
c.Log.Printf("Paystack response: %v\n", resp)
}
if data, ok := resp["data"]; ok {
switch t := resp["data"].(type) {
case map[string]interface{}:
return mapstruct(data, v)
default:
_ = t
return mapstruct(resp, v)
}
}
// if response data does not contain data key, map entire response to v
return mapstruct(resp, v)
}