This repository was archived by the owner on Apr 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmiddleware_stripe.go
More file actions
57 lines (51 loc) · 1.32 KB
/
middleware_stripe.go
File metadata and controls
57 lines (51 loc) · 1.32 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
package triangulate
import (
"github.com/stripe/stripe-go/v72"
"github.com/stripe/stripe-go/v72/checkout/session"
"net/http"
)
func StripeSessionMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sessionID := ""
// We check for a get parameter with the session id
if r.Method == "GET" {
sessionID = r.URL.Query().Get(sessionIDParam)
}
if sessionID != "" {
// Fetch session object from stripe
s, err := session.Get(
sessionID,
nil,
)
// If no error then we seem to have a customer
if err == nil {
// Check if the customer paid
if s.PaymentStatus == stripe.CheckoutSessionPaymentStatusPaid && s.Customer != nil {
// Make sure a temporary session exists for the session id
mutex.Lock()
var t TempSession
if res := db.First(&t, "stripe_session_id = ?", sessionID); res.Error == nil {
user := User{
EmailHash: t.Email,
PasswordHash: t.Password,
StripeCustomerID: s.Customer.ID,
}
db.Create(&user)
if user.ID > 0 {
db.Delete(&t)
mutex.Unlock()
// login && redirect
loginAndRedirect(user, w, r)
return
} else {
mutex.Unlock()
}
} else {
mutex.Unlock()
}
}
}
}
next.ServeHTTP(w, r)
})
}