-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpay.go
174 lines (148 loc) · 3.99 KB
/
webpay.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
package webpay
import (
"crypto/tls"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
simplejson "github.com/bitly/go-simplejson"
)
type WebPayClient struct {
header map[string]string
options map[string]string
authToken string
Charge Charge
Customer Customer
Recursion Recursion
Token Token
Event Event
Account Account
mode string
}
func NewWebPayClient(auth_token string) WebPayClient {
cli := WebPayClient{
authToken: auth_token,
header: map[string]string{
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "application/json",
"User-Agent": "webpay/2.1.1 golang",
"Accept-Language": "en",
"Authorization": "Bearer " + auth_token,
},
options: map[string]string{
"api_base": "https://api.webpay.jp/v1",
},
}
cli.Charge = NewCharge(&cli)
cli.Customer = NewCustomer(&cli)
cli.Recursion = NewRecursion(&cli)
cli.Token = NewToken(&cli)
cli.Event = NewEvent(&cli)
cli.Account = NewAccount(&cli)
cli.mode = "real"
return cli
}
func (cli WebPayClient) SetAcceptLanguage(lang string) {
cli.header["Accept-Language"] = lang
}
func (cli WebPayClient) Post(path string, params url.Values) (*simplejson.Json, error) {
return cli.Request("POST", path, params)
}
func (cli WebPayClient) Delete(path string, params url.Values) (*simplejson.Json, error) {
return cli.Request("DELETE", path, params)
}
func (cli WebPayClient) Get(path string, params url.Values) (*simplejson.Json, error) {
return cli.Request("GET", path, params)
}
func (cli WebPayClient) Request(method, path string, params url.Values) (*simplejson.Json, error) {
if cli.mode != "real" {
return cli.returnMockJson(method, path, params)
}
u := cli.options["api_base"] + "/" + path
var client *http.Client
if strings.HasPrefix(u, "https://") {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client = &http.Client{Transport: tr}
} else {
client = &http.Client{}
}
req, _ := http.NewRequest(
method,
u,
strings.NewReader(params.Encode()),
)
for k, v := range cli.header {
req.Header.Add(k, v)
}
req.SetBasicAuth(cli.authToken, "")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return nil, err
}
// fmt.Println(string(body))
js, err := simplejson.NewJson(body)
if err != nil {
return nil, err
}
return js, nil
}
// GetId returns 'id' of the JSON.
func GetId(json *simplejson.Json) (string, error) {
return json.Get("id").String()
}
// getAllPathWithQuery creates path with query from map args.
func getAllPathWithQuery(path string, args map[string]int) string {
query := url.Values{}
if count, exists := args["count"]; exists {
query.Add("count", strconv.Itoa(count))
}
if offset, exists := args["offset"]; exists {
query.Add("offset", strconv.Itoa(offset))
}
if created, exists := args["created"]; exists {
query.Add("created", strconv.Itoa(created))
}
if gt, exists := args["gt"]; exists {
query.Add("gt", strconv.Itoa(gt))
}
if gte, exists := args["gte"]; exists {
query.Add("gte", strconv.Itoa(gte))
}
if lt, exists := args["lt"]; exists {
query.Add("lt", strconv.Itoa(lt))
}
if lte, exists := args["lte"]; exists {
query.Add("lte", strconv.Itoa(lte))
}
return fmt.Sprintf("%s?%s", path, query.Encode())
}
// returnMockJson return JSON which is read from under test_data dir for testing.
func (cli WebPayClient) returnMockJson(method, path string, params url.Values) (*simplejson.Json, error) {
path = strings.Replace(path, "?", "_", -1)
path = strings.Replace(path, "=", "_", -1)
path = strings.Replace(path, "&", "_", -1)
path = strings.Replace(path, "/", "_", -1)
if method == "DELETE" {
path = "delete_" + path
}
// fmt.Println(path)
dat, err := ioutil.ReadFile("test_data/" + path)
if err != nil {
js, _ := simplejson.NewJson([]byte("{}"))
return js, nil
}
js, err := simplejson.NewJson(dat)
if err != nil {
return nil, err
}
return js, nil
}