-
Notifications
You must be signed in to change notification settings - Fork 7
/
api.go
109 lines (96 loc) · 2.52 KB
/
api.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
package tdb_cg
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"github.com/Fibocloud/payment-sdks/utils"
)
var (
QPayAuthToken = utils.API{
Url: "/auth/token",
Method: http.MethodPost,
}
QPayAuthRefresh = utils.API{
Url: "/auth/refresh",
Method: http.MethodPost,
}
QPayPaymentGet = utils.API{
Url: "/payment/get/",
Method: http.MethodGet,
}
QPayPaymentCheck = utils.API{
Url: "/payment/check/",
Method: http.MethodGet,
}
QPayInvoiceCreate = utils.API{
Url: "/bill/create",
Method: http.MethodPost,
}
QPayInvoiceGet = utils.API{
Url: "/invoice/",
Method: http.MethodGet,
}
)
// func (q *qpay) ExpireTokenForce() {
// q.loginObject.ExpiresIn = 0
// }
func (q *tdbcg) httpRequest(body interface{}, api utils.API, urlExt string) (response []byte, err error) {
var requestByte []byte
var requestBody *bytes.Reader
if body == nil {
requestBody = bytes.NewReader(nil)
} else {
requestByte, _ = json.Marshal(body)
requestBody = bytes.NewReader(requestByte)
}
// pfxData, err := os.ReadFile(q.certPathPfx)
// if err != nil {
// fmt.Println("Error reading PFX file:", err)
// return nil, err
// }
// cerData, err := os.ReadFile(q.certPathCer)
// if err != nil {
// fmt.Println("Error reading PFX file:", err)
// return nil, err
// }
// _, certification, err := pkcs12.Decode(pfxData, q.certPass)
// derBytes, err := x509.MarshalCertificate(certification)
// pemCert := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
// cert, err := tls.LoadX509KeyPair(q.certPathCer, "keyandcerts.pem")
// if err != nil {
// fmt.Println("Error loading certificate:", err)
// return
// }
tlsCnf, err := LoadClientTLSCfg(q.certPathPfx, q.certPass, "")
if err != nil {
fmt.Println("Error reading PFX file:", err)
return nil, err
}
// Create a TLS configuration with the loaded certificate
// tlsConfig := &tls.Config{
// Certificates: []tls.Certificate{cert},
// }
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsCnf,
},
}
req, _ := http.NewRequest(api.Method, q.endpoint+api.Url+urlExt, requestBody)
req.Header.Add("Content-Type", utils.XmlContent)
req.Header.Add("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(q.loginID+":"+q.pwd)))
res, err := client.Do(req)
if err != nil {
fmt.Println(err.Error())
return nil, err
}
defer res.Body.Close()
response, _ = io.ReadAll(res.Body)
if res.StatusCode != 200 {
return nil, errors.New(string(response))
}
return
}