forked from coaidev/coai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpayment.go
102 lines (84 loc) · 2.44 KB
/
payment.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
package auth
import (
"chat/utils"
"database/sql"
"errors"
"github.com/go-redis/redis/v8"
"github.com/goccy/go-json"
"github.com/spf13/viper"
)
type BalanceResponse struct {
Status bool `json:"status" required:"true"`
Balance float32 `json:"balance"`
}
type PaymentResponse struct {
Status bool `json:"status" required:"true"`
Type bool `json:"type"`
}
func GenerateOrder() string {
return utils.Sha2Encrypt(utils.GenerateChar(32))
}
func GetBalance(username string) float32 {
if !useDeeptrain() {
return 0.
}
order := GenerateOrder()
res, err := utils.Post(getDeeptrainApi("/app/balance"), map[string]string{
"Content-Type": "application/json",
}, map[string]interface{}{
"password": viper.GetString("auth.access"),
"user": username,
"hash": utils.Sha2Encrypt(username + viper.GetString("auth.salt")),
"order": order,
"sign": utils.Sha2Encrypt(username + order + viper.GetString("auth.sign")),
})
if err != nil || res == nil || res.(map[string]interface{})["status"] == false {
return 0.
}
converter, _ := json.Marshal(res)
resp, _ := utils.Unmarshal[BalanceResponse](converter)
return resp.Balance
}
func Pay(username string, amount float32) bool {
if !useDeeptrain() {
return false
}
order := GenerateOrder()
res, err := utils.Post(getDeeptrainApi("/app/payment"), map[string]string{
"Content-Type": "application/json",
}, map[string]interface{}{
"password": viper.GetString("auth.access"),
"user": username,
"hash": utils.Sha2Encrypt(username + viper.GetString("auth.salt")),
"order": order,
"amount": amount,
"sign": utils.Sha2Encrypt(username + order + viper.GetString("auth.sign")),
})
if err != nil || res == nil || res.(map[string]interface{})["status"] == false {
return false
}
converter, _ := json.Marshal(res)
resp, _ := utils.Unmarshal[PaymentResponse](converter)
return resp.Type
}
func (u *User) Pay(db *sql.DB, cache *redis.Client, amount float32) bool {
if useDeeptrain() {
state := Pay(u.Username, amount)
if state {
incrBillingRequest(cache, int64(amount*100))
}
return state
}
return u.PayedQuotaAsAmount(db, amount)
}
func BuyQuota(db *sql.DB, cache *redis.Client, user *User, quota int) error {
money := float32(quota) * 0.1
if !useDeeptrain() {
return errors.New("cannot find payment provider")
}
if user.Pay(db, cache, money) {
user.IncreaseQuota(db, float32(quota))
return nil
}
return errors.New("do not have enough money")
}