This repository has been archived by the owner on Mar 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pay.go
165 lines (134 loc) · 3.25 KB
/
pay.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
package pay
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"time"
"github.com/mitchellh/mapstructure"
)
const (
baseURL = "https://api.pay.busha.co"
defautHTTPTimeout = 60 * time.Second
)
type service struct {
client *Client
}
type logger interface {
Println(...interface{})
}
// Client ...
type Client struct {
common service
httpClient *http.Client
key string
webhookSecret string
BaseURL *url.URL
Charge *ChargeService
Webhook *WebhookService
LogEnabled bool
Logger logger
}
// Response response from busha-pay
type Response map[string]interface{}
// NewClient creates a new busha-pay
func NewClient(key string, httpClient *http.Client) *Client {
if httpClient == nil {
httpClient = &http.Client{Timeout: defautHTTPTimeout}
}
u, _ := url.Parse(baseURL)
client := &Client{
httpClient: httpClient,
key: key,
BaseURL: u,
Logger: log.New(os.Stdout, "", log.LstdFlags),
}
client.common.client = client
client.Charge = (*ChargeService)(&client.common)
client.Webhook = (*WebhookService)(&client.common)
return client
}
// SetWebhookSecret set webhook shared secret used for validate incoming requests
func (c *Client) SetWebhookSecret(secret string) {
c.webhookSecret = secret
}
// Call make the http request to busha pay.method, the http method. path is the url path.
// body is the http request body. v is the interface the response will be written to
func (c *Client) Call(method, path string, body, v interface{}) error {
var buf bytes.Buffer
if body != nil {
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.LogEnabled {
c.Logger.Println("Request error:", err)
}
return err
}
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
req.Header.Set("X-BP-Api-Key", c.key)
// req.Header.Set("User-Agent", userAgent)
resp, err := c.httpClient.Do(req)
if err != nil {
if c.LogEnabled {
c.Logger.Println("Response error:", err)
}
return err
}
defer resp.Body.Close()
return c.decodeResponse(resp, v)
}
func (c *Client) decodeResponse(r *http.Response, v interface{}) error {
var resp Response
respBody, err := ioutil.ReadAll(r.Body)
if err != nil {
if c.LogEnabled {
c.Logger.Println("Response error:", err)
}
return err
}
json.Unmarshal(respBody, &resp)
if r.StatusCode/100 != 2 {
message := r.Status
if v, ok := resp["error"]; ok {
var errResp Error
mapstruct(v, &errResp)
message = errResp.Message
}
if c.LogEnabled {
c.Logger.Println("Busha Pay error:", message)
}
return errors.New(message)
}
if data, ok := resp["data"]; ok {
buf, _ := json.Marshal(data)
return json.Unmarshal(buf, v)
}
// if response data does not contain data key, map entire response to v
buf, _ := json.Marshal(resp)
return json.Unmarshal(buf, v)
}
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
}