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
/
core.go
154 lines (129 loc) · 5.1 KB
/
core.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
package jenius
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/url"
"strings"
)
// CoreGateway struct
type CoreGateway struct {
Client Client
}
// Call : base method to call Core API
func (gateway *CoreGateway) Call(method, path string, header map[string]string, body io.Reader, v interface{}, x interface{}) error {
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
if path == gateway.Client.JeniusOauthTokenUrl {
path = gateway.Client.JeniusTokenBaseUrl + path
} else {
path = gateway.Client.JeniusBaseUrl + path
}
return gateway.Client.Call(method, path, header, body, v, x)
}
func (gateway *CoreGateway) GetToken() (TokenResponse, FailedResponse, error) {
respSuccess := TokenResponse{}
respFailed := FailedResponse{}
data := url.Values{}
data.Set("grant_type", "client_credentials")
headers := map[string]string{
"Content-Type": "application/x-www-form-urlencoded",
}
err := gateway.Call("POST", gateway.Client.JeniusOauthTokenUrl, headers, strings.NewReader(data.Encode()), &respSuccess, &respFailed)
if err != nil {
return respSuccess, respFailed, err
}
return respSuccess, respFailed, nil
}
func (gateway *CoreGateway) PayStatus(req *PayStatusReq) (SuccessResponse, FailedResponse, error) {
respSuccess := SuccessResponse{}
respFailed := FailedResponse{}
btpnTimestamp := gateway.getBtpnTimestamp()
btpnOriginalTimestamp := gateway.btpnConvertTimestamp(req.TransactionTime)
btpnSignature := gateway.generateBtpnSignature(
"GET",
gateway.Client.JeniusPayStatusUrl,
btpnTimestamp,
"",
)
headers := map[string]string{
"Authorization": fmt.Sprintf("Bearer %v", req.Token),
"BTPN-Signature": btpnSignature,
"BTPN-ApiKey": gateway.Client.JeniusApiKey,
"BTPN-Timestamp": btpnTimestamp,
"X-Channel-Id": gateway.Client.JeniusXChannelId,
"X-Node": "Jenius Pay",
"X-Original-Transmission-Date-Time": btpnOriginalTimestamp,
"X-Transmission-Date-Time": btpnTimestamp,
"X-Reference-No": req.ReferenceNo,
"Content-Type": "application/json",
}
err := gateway.Call("GET", gateway.Client.JeniusPayStatusUrl, headers, nil, &respSuccess, &respFailed)
if err != nil {
return respSuccess, respFailed, err
}
return respSuccess, respFailed, nil
}
func (gateway *CoreGateway) PayRequest(req *PayRequestReq, reqBody *PayRequestReqBody) (SuccessResponse, FailedResponse, error) {
respSuccess := SuccessResponse{}
respFailed := FailedResponse{}
jsonReq, _ := json.Marshal(reqBody)
btpnTimestamp := gateway.getBtpnTimestamp()
btpnOriginalTimestamp := gateway.btpnConvertTimestamp(reqBody.CreatedAt)
btpnSignature := gateway.generateBtpnSignature(
"POST",
gateway.Client.JeniusPayRequestUrl,
btpnTimestamp,
string(jsonReq),
)
headers := map[string]string{
"Authorization": fmt.Sprintf("Bearer %v", req.Token),
"BTPN-Signature": btpnSignature,
"BTPN-ApiKey": gateway.Client.JeniusApiKey,
"BTPN-Timestamp": btpnTimestamp,
"X-Channel-Id": gateway.Client.JeniusXChannelId,
"X-Node": "Jenius Pay",
"X-Original-Transmission-Date-Time": btpnOriginalTimestamp,
"X-Transmission-Date-Time": btpnTimestamp,
"X-Reference-No": req.ReferenceNo,
"Content-Type": "application/json",
}
err := gateway.Call("POST", gateway.Client.JeniusPayRequestUrl, headers, bytes.NewBuffer(jsonReq), &respSuccess, &respFailed)
if err != nil {
return respSuccess, respFailed, err
}
return respSuccess, respFailed, nil
}
func (gateway *CoreGateway) PayRefund(req *PayRefundReq) (SuccessResponse, FailedResponse, error) {
respSuccess := SuccessResponse{}
respFailed := FailedResponse{}
jsonReq, _ := json.Marshal(req)
btpnTimestamp := gateway.getBtpnTimestamp()
btpnOriginalTimestamp := gateway.btpnConvertTimestamp(req.TransactionTime)
btpnSignature := gateway.generateBtpnSignature(
"DELETE",
fmt.Sprint(gateway.Client.JeniusPayRefundUrl, "?approval=", req.ApprovalCode),
btpnTimestamp,
"",
)
headers := map[string]string{
"Authorization": fmt.Sprintf("Bearer %v", req.Token),
"BTPN-Signature": btpnSignature,
"BTPN-ApiKey": gateway.Client.JeniusApiKey,
"BTPN-Timestamp": btpnTimestamp,
"X-Channel-Id": gateway.Client.JeniusXChannelId,
"X-Node": "Jenius Pay",
"X-Original-Transmission-Date-Time": btpnOriginalTimestamp,
"X-Transmission-Date-Time": btpnTimestamp,
"X-Reference-No": req.ReferenceNo,
"X-Amount": req.Amount,
"Content-Type": "application/json",
}
err := gateway.Call("DELETE", fmt.Sprint(gateway.Client.JeniusPayRefundUrl, "?approval=", req.ApprovalCode), headers, bytes.NewBuffer(jsonReq), &respSuccess, &respFailed)
if err != nil {
return respSuccess, respFailed, err
}
return respSuccess, respFailed, nil
}