-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_operations.go
76 lines (55 loc) · 1.68 KB
/
api_operations.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
package ipaymu
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"strings"
"time"
"github.com/ferdhika31/iPaymu-go/consts"
)
func GenerateSignature(body string, method string, cfg Config) string {
va := cfg.VirtualAccount
secret := cfg.ApiSecret
requestBody := fmt.Sprintf("%x", StringSha256(body))
stringToSign := strings.ToUpper(method) + ":" + va + ":" + requestBody + ":" + secret
return StringHMACSha256(stringToSign, secret)
}
func Call(method string, url string, body io.Reader, cfg Config) (Response, error) {
var API_URL = "https://my.ipaymu.com"
if cfg.Env == consts.Sandbox {
API_URL = "http://sandbox.ipaymu.com"
}
var getURL = API_URL + url
req, _ := http.NewRequest(method, getURL, body)
t := time.Now()
timestamp := fmt.Sprintf("%d%02d%02d%02d%02d%02d",
t.Year(), t.Month(), t.Day(),
t.Hour(), t.Minute(), t.Second())
jsonBody := fmt.Sprintf("%s", body)
signature := fmt.Sprintf("%s", GenerateSignature(string(jsonBody), method, cfg))
// fmt.Println("Signature : " + signature)
// fmt.Println("timestamps : " + string(timestamp))
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
req.Header.Add("va", cfg.VirtualAccount)
req.Header.Add("signature", string(signature))
req.Header.Add("timestamp", string(timestamp))
resp, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Printf(fmt.Sprint(err))
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("Failed reading the request body: %s\n", err)
}
data := Response{}
err = json.Unmarshal(b, &data)
if err != nil {
log.Printf("Failed unmarshaling: %s\n", err)
}
return data, nil
}