This repository has been archived by the owner on Mar 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
common.go
50 lines (41 loc) · 1.47 KB
/
common.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
package jenius
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"fmt"
"strings"
"time"
)
func (gateway *CoreGateway) generateBtpnSignature(httpMethod string, relativeUrl string, btpnTimestamp string, requestBody string) string {
var stringToSignature string
if requestBody != "" {
stringToSignature = btpnCleanString(fmt.Sprintf("%v:%v:%v:%v:%v", httpMethod, relativeUrl, gateway.Client.JeniusApiKey, btpnTimestamp, requestBody))
} else {
stringToSignature = btpnCleanString(fmt.Sprintf("%v:%v:%v:%v", httpMethod, relativeUrl, gateway.Client.JeniusApiKey, btpnTimestamp))
}
// Create a new HMAC by defining the hash type and the key (as byte array)
h := hmac.New(sha256.New, []byte(gateway.Client.JeniusApiSecret))
// Write Data to it
h.Write([]byte(stringToSignature))
// Get result and encode as hexadecimal string
sha := base64.StdEncoding.EncodeToString(h.Sum(nil))
return sha
}
func (gateway *CoreGateway) getBtpnTimestamp() string {
// 'YYYY-MM-DDTHH24:MI:SS.ZZZ+07:00'
jakarta := time.FixedZone("Asia/Jakarta", 7*60*60)
t := time.Now().In(jakarta)
return t.Format("2006-01-02T15:04:05.000-07:00")
}
func btpnCleanString(any string) string {
s := strings.ReplaceAll(any, " ", "")
s = strings.ReplaceAll(s, "\r", "")
s = strings.ReplaceAll(s, "\n", "")
s = strings.ReplaceAll(s, "\t", "")
return s
}
func (gateway *CoreGateway) btpnConvertTimestamp(req int64) string {
timeReq := time.Unix(req, 0)
return timeReq.Format("2006-01-02T15:04:05.000-07:00")
}