-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
187 lines (158 loc) · 5 KB
/
main.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
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
183
184
185
186
187
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log/slog"
"net/http"
"os"
"github.com/go-chi/chi/v5"
"github.com/stripe/stripe-go/v76"
csession "github.com/stripe/stripe-go/v76/checkout/session"
"github.com/stripe/stripe-go/v76/webhook"
"gorm.io/gorm"
DB "iris/v2/database"
"iris/v2/menu"
"iris/v2/models"
)
func webhookHandler(w http.ResponseWriter, r *http.Request) {
const MaxBodyBytes = int64(65536)
r.Body = http.MaxBytesReader(w, r.Body, MaxBodyBytes)
body, err := ioutil.ReadAll(r.Body)
if err != nil {
slog.Warn("Error reading request body: %v\n", err)
w.WriteHeader(http.StatusServiceUnavailable)
return
}
settings := DB.GetSettings()
stripe.Key = settings.ApiKey
event, err := webhook.ConstructEvent(body, r.Header.Get("Stripe-Signature"), settings.WebhookEndpointSecret)
if err != nil {
slog.Error("Error verifying webhook signature: " + err.Error())
w.WriteHeader(http.StatusBadRequest) // Return a 400 error on a bad signature
return
}
// If error raised while handling event, return bad request.
if !checkoutWebhookHandler(event) {
w.WriteHeader(http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusOK)
}
func checkoutWebhookHandler(event stripe.Event) bool {
// Handle the checkout.session.completed event
if event.Type == "checkout.session.completed" {
var session stripe.CheckoutSession
err := json.Unmarshal(event.Data.Raw, &session)
if err != nil {
slog.Error("Error parsing webhook JSON: " + err.Error())
return false
}
if session.PaymentStatus == "paid" {
activeSessions := &models.ActiveSession{}
db_res := DB.Conn.First(&activeSessions)
if db_res.Error != nil {
if errors.Is(db_res.Error, gorm.ErrRecordNotFound) {
return true
}
slog.Error("Error while reading: " + db_res.Error.Error())
return false
}
internalLinkID := activeSessions.Sessions.Relation[session.ID]
link := &models.SessionLink{}
db_res = DB.Conn.Where(&models.SessionLink{LinkID: internalLinkID}).First(&link)
if db_res.Error != nil {
if errors.Is(db_res.Error, gorm.ErrRecordNotFound) {
return true
}
slog.Error("Error while reading: " + db_res.Error.Error())
return false
}
link.Used++
if link.Used == link.MaxUses {
link.Active = false
}
slog.Info("Link " + link.LinkID + " now used " + fmt.Sprintf("%d", link.Used) + " times")
db_res = DB.Conn.Save(&link)
if db_res.Error != nil {
slog.Error("Error while saving link paid event to database: " + db_res.Error.Error())
return false
}
if len(link.TrackingInventoryIDs) > 0 {
// Expand line_items field
i := csession.ListLineItems(&stripe.CheckoutSessionListLineItemsParams{
Session: stripe.String(session.ID),
})
boughtItems := map[string]int64{}
for i.Next() {
boughtItems[i.LineItem().Price.Product.ID] = i.LineItem().Quantity
}
for _, trackingItem := range link.TrackingInventoryIDs {
menu.UpdateInventory(trackingItem, boughtItems[trackingItem])
slog.Info(fmt.Sprintf("Inventory item %s was bought %d times in link %s", trackingItem, boughtItems[trackingItem], link.LinkID))
}
}
}
}
return true
}
func successHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("<html><body><h1>" + DB.GetSettings().PaymentConfirmationMessage + "</h1></body></html>"))
}
func checkoutSessionHandler(w http.ResponseWriter, r *http.Request) {
linkID := chi.URLParam(r, "linkID")
// Get link from database
session := &models.SessionLink{}
db_res := DB.Conn.Where(&models.SessionLink{LinkID: linkID}).First(&session)
if db_res.Error != nil || !session.Active {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("Link invalid"))
return
}
stripe.Key = DB.GetSettings().ApiKey
res, err := csession.New(&session.Params.CheckoutSessionParams)
if err != nil {
slog.Error("Error while creating session: " + err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
// Save session to database
activeSession := &models.ActiveSession{}
db_res = DB.Conn.FirstOrCreate(&activeSession)
if db_res.Error != nil {
slog.Error("Error while adding to active session: " + err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
if activeSession.Sessions.Relation == nil {
activeSession.Sessions.Relation = map[string]string{}
}
activeSession.Sessions.Relation[res.ID] = session.LinkID
DB.Conn.Save(&activeSession)
// redirect to session url
http.Redirect(w, r, res.URL, http.StatusFound)
}
func serveWeb() {
r := chi.NewRouter()
r.Get("/pay/{linkID}", checkoutSessionHandler)
r.Post("/webhook", webhookHandler)
r.Get("/success", successHandler)
fmt.Printf("Starting server on port 4242, please reverse proxy %s to this port.", DB.GetSettings().Domain)
http.ListenAndServe(":4242", r)
}
func main() {
if _, err := os.Stat("./data.db"); err != nil {
DB.Init()
menu.ChangeApiKey()
menu.ChangeEndpointSecret()
menu.ChangeDomain()
} else {
DB.Init()
}
if len(os.Args) > 1 && os.Args[1] == "menu" {
menu.Entry()
} else {
serveWeb()
}
}