-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandlers_kofi.go
More file actions
182 lines (164 loc) · 4.52 KB
/
handlers_kofi.go
File metadata and controls
182 lines (164 loc) · 4.52 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"encoding/json"
"fmt"
"os"
"time"
"github.com/gin-gonic/gin"
)
// webhook that kofi sends to us when transactions are made.
func handleKofiTransaction(c *gin.Context) {
data := c.PostForm("data")
if data == "" {
c.JSON(400, gin.H{"error": "No data found"})
return
}
parsedData := make(map[string]any)
err := json.Unmarshal([]byte(data), &parsedData)
if err != nil {
c.JSON(400, gin.H{"error": "Invalid data format"})
return
}
verification := parsedData["verification_token"]
fmt.Println("Verification: " + getStringOrEmpty(verification))
if verification != os.Getenv("KOFI_CODE") {
c.JSON(400, gin.H{"error": "Invalid verification code"})
return
}
fmt.Println(JSONStringify(parsedData))
email := getStringOrEmpty(parsedData["email"])
foundBy := "None"
accounts := []User{}
var account User = nil
discord_id := getStringOrEmpty(parsedData["discord_id"])
if discord_id != "" {
accounts, err = getAccountsBy("discord_id", discord_id, -1)
if err == nil {
foundBy = "Discord"
}
}
if email != "" && len(accounts) == 0 {
accounts, err = getAccountsBy("email", email, -1)
if err == nil {
foundBy = "Email"
}
}
accountInfo := "No linked account found"
if len(accounts) > 0 {
account = accounts[0]
accountInfo = fmt.Sprintf("**Username:** %s", account.GetUsername())
}
switch getStringOrEmpty(parsedData["type"]) {
case "Donation":
// TODO: handle donations
case "Subscription":
name := getStringOrEmpty(parsedData["tier_name"])
if name == "" {
// this exits if its a monthly donation, subscriptions have teir_name
c.JSON(400, gin.H{"error": "No tier name found"})
return
}
sendDiscordWebhook([]map[string]any{
{
"title": "New Subscription",
"description": fmt.Sprintf(
"**From:** %s\n**Amount:** %s %s\n**Message:** %s\n**Email:** %s\n\n[View on Ko-fi](%s)\n**Found By:** %s\n\n%s",
parsedData["from_name"],
parsedData["amount"],
parsedData["currency"],
parsedData["message"],
parsedData["email"],
parsedData["url"],
foundBy,
accountInfo,
),
"timestamp": time.Now().Format(time.RFC3339),
},
})
if len(accounts) == 0 {
c.JSON(400, gin.H{"error": fmt.Sprintf("No accounts found for %s", foundBy)})
return
}
account := accounts[0]
sub := account.GetSubscription()
sub.Tier = name
sub.Active = true
sub.Next_billing = int64(time.Now().Add(time.Hour * 24 * 31).UnixMilli())
account.SetSubscription(sub)
go saveUsers()
c.JSON(200, gin.H{"status": "success"})
case "Shop Order":
shopItems, ok := parsedData["shop_items"].([]any)
if !ok {
c.JSON(400, gin.H{"error": "Invalid shop_items format"})
return
}
for _, shop_item := range shopItems {
item, ok := shop_item.(map[string]any)
if !ok {
continue
}
sendDiscordWebhook([]map[string]any{
{
"title": "New Shop Order",
"description": fmt.Sprintf("**User:** %s\n**Amount:** %s %s\n**Message:** %s\n**Email:** %s\n\n[View on Ko-fi](%s)\n**Found By:** %s\n\n%s",
parsedData["from_name"],
parsedData["amount"],
parsedData["currency"],
parsedData["message"],
parsedData["email"],
parsedData["url"],
foundBy,
accountInfo,
),
"timestamp": time.Now().Format(time.RFC3339),
},
})
switch item["direct_link_code"].(string) {
case "eebeb7269f":
// add 50 rotur credits to the user
now := time.Now().UnixMilli()
balance := float64(account.GetCredits()) + 50
account.SetBalance(balance)
account.addTransaction(Transaction{
Note: "50 credit purchase",
User: Username("rotur").Id(),
Timestamp: now,
Amount: 50,
Type: "transfer",
NewTotal: balance,
})
go saveUsers()
}
}
}
c.JSON(200, gin.H{"status": "success"})
}
func setSubscription(c *gin.Context) {
if !authenticateAdmin(c) {
return
}
var data map[string]any
if err := c.ShouldBindJSON(&data); err != nil {
c.JSON(400, gin.H{"error": "Invalid request body"})
return
}
username := Username(data["username"].(string))
tier := data["tier"].(string)
if username == "" || tier == "" {
c.JSON(400, gin.H{"error": "Username and tier are required"})
return
}
user, err := getAccountByUsername(username)
if err != nil {
c.JSON(404, gin.H{"error": "User not found"})
return
}
user.SetSubscription(subscription{
Tier: tier,
Active: true,
Next_billing: int64(time.Now().Add(time.Hour * 24 * 30).UnixMilli()),
})
go saveUsers()
c.JSON(200, gin.H{"message": "Subscription updated successfully"})
}