-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
87 lines (74 loc) · 1.84 KB
/
utils.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
package veritrans
import (
"bytes"
"crypto/sha256"
"encoding/json"
"fmt"
"io/ioutil"
"math"
"math/rand"
"net/http"
"net/url"
"time"
)
// GetAfterOneMonth function
func GetAfterOneMonth() string {
nowTime := time.Now()
expiredAt := nowTime.AddDate(0, 1, 0)
return expiredAt.Format("01/06")
}
// GetAfterOneYear function
func GetAfterOneYear() string {
nowTime := time.Now()
expiredAt := nowTime.AddDate(1, 0, 0)
return expiredAt.Format("01/06")
}
// GetRandomID function
func GetRandomID(digit int) int {
rand.Seed(time.Now().UnixNano())
low := int(math.Pow10(digit - 1))
high := int(math.Pow10(digit) - 1)
return low + rand.Intn(high-low)
}
// ProcessRequest function
func ProcessRequest(requestURL string, connectionParam *ConnectionParam) (*ConnectionResponse, error) {
var err error
paramByte, err := json.Marshal(connectionParam)
if err != nil {
return nil, err
}
parsedURL, err := url.ParseRequestURI(requestURL)
if err != nil {
return nil, err
}
httpClient := &http.Client{}
body := bytes.NewBuffer(paramByte)
req, err := http.NewRequest("POST", parsedURL.String(), body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
res, err := httpClient.Do(req)
if err != nil {
return nil, err
}
resBody, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
var connectionRes ConnectionResponse
err = json.Unmarshal(resBody, &connectionRes)
return &connectionRes, err
}
// SetHash is a handler to make hash data of params
func SetHash(connectionParam *ConnectionParam, merchantID, password string) error {
paramJSON, err := json.Marshal(connectionParam.Params)
if err != nil {
return err
}
hash := []byte(fmt.Sprintf("%s%s%s", merchantID, paramJSON, password))
sha := sha256.New()
sha.Write(hash)
connectionParam.AuthHash = fmt.Sprintf("%x", sha.Sum(nil))
return nil
}