-
Notifications
You must be signed in to change notification settings - Fork 0
/
tpbankapi.go
132 lines (121 loc) · 3.86 KB
/
tpbankapi.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
package tpbankapi
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"math/rand"
"net/http"
"strconv"
)
const (
TOKEN_URL = "https://ebank.tpb.vn/gateway/api/auth/login"
HISTORY_URL = "https://ebank.tpb.vn/gateway/api/smart-search-presentation-service/v1/account-transactions/find"
)
type TPBank struct {
Username string
Password string
Token string
}
func NewTPBank(username string, password string) *TPBank {
return &TPBank{
Username: username,
Password: password,
}
}
func (tpbank *TPBank) Login() error {
postBody := map[string]string{
"username": tpbank.Username,
"password": tpbank.Password,
}
body, err := json.Marshal(postBody)
if err != nil {
return err
}
req, err := http.NewRequest("POST", TOKEN_URL, bytes.NewBuffer(body))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("DEVICE_ID", "LYjkjqGZ3HhGP5520GxPP2j94RDMC7Xje77MI7"+strconv.Itoa(rand.Intn(100000000)))
req.Header.Set("PLATFORM_VERSION", "91")
req.Header.Set("DEVICE_NAME", "Chrome")
req.Header.Set("SOURCE-APP", "HYDRO")
req.Header.Set("Authorization", "Bearer")
req.Header.Set("Accept", "aapplication/json, text/plain, */*")
req.Header.Set("Accept-Language", "en-US,en;q=0.9,vi;q=0.8")
req.Header.Set("Referer", " https://ebank.tpb.vn/retail/vX/login?returnUrl=%2Fmain")
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36")
req.Header.Set("PLATFORM_NAME", "WEB")
req.Header.Set("APP_VERSION", "1.3")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, err2 := ioutil.ReadAll(resp.Body)
if err2 != nil {
return err2
}
var json_resp map[string]string
json.Unmarshal(body, &json_resp)
if len(json_resp) == 0 {
return errors.New("login failed")
}
tpbank.Token = json_resp["access_token"]
return nil
}
// GetHistory returns the history of the account
func (tpbank *TPBank) GetHistory(stk_bank string, fromDate string, toDate string) ([]byte, error) {
postBody := map[string]string{
"fromDate": fromDate,
"toDate": toDate,
"currency": "VND",
"accountNo": stk_bank,
"keyword": "",
}
body, err := json.Marshal(postBody)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", HISTORY_URL, bytes.NewBuffer(body))
if err != nil {
return nil, err
}
if tpbank.Token == "" {
err := tpbank.Login()
if err != nil {
return nil, err
}
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("connection", "keep-alive")
req.Header.Set("DEVICE_ID", "LYjkjqGZ3HhGP5520GxPP2j94RDMC7Xje77MI75RYBVR")
req.Header.Set("PLATFORM_VERSION", "91")
req.Header.Set("DEVICE_NAME", "Chrome")
req.Header.Set("SOURCE-APP", "HYDRO")
req.Header.Set("Authorization", "Bearer "+tpbank.Token)
req.Header.Set("Accept", "application/json, text/plain, */*")
req.Header.Set("sec-ch-ua-mobile", "?0")
req.Header.Set("Accept-Language", "en-US,en;q=0.9,vi;q=0.8")
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36")
req.Header.Set("PLATFORM_NAME", "WEB")
req.Header.Set("APP_VERSION", "1.3")
req.Header.Set("Referer", " https://ebank.tpb.vn/retail/vX/main/inquiry/account/transaction?id="+stk_bank)
req.Header.Set("Cookie", "_ga=GA1.2.1679888794.1623516; _gid=GA1.2.580582711.16277; _gcl_au=1.1.756417552.1626666; Authorization="+tpbank.Token)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err2 := ioutil.ReadAll(resp.Body)
if err2 != nil {
return nil, err2
}
return body, nil
//creditDebitIndicator:
// + DBIT: Tru tien
// + CRDT: Cong tien
}