git-svn-id: svn://losandesgames.com/alfheim-website@19 15359d88-9307-4e75-a9c1-e5686e5897df
This commit is contained in:
parent
721bb2eb20
commit
c3e894ffb5
42
handlers.go
42
handlers.go
@ -25,11 +25,12 @@ import "github.com/stripe/stripe-go/v78/customer"
|
||||
import "github.com/stripe/stripe-go/v78/subscription"
|
||||
|
||||
type templatedata struct {
|
||||
AuthenticatedUser int32
|
||||
FormErrors map[string]string
|
||||
Account Account
|
||||
Prices []stripe.Price
|
||||
ClientSecret string
|
||||
AuthenticatedUser int32
|
||||
FormErrors map[string]string
|
||||
Account Account
|
||||
Prices []stripe.Price
|
||||
ClientSecret string
|
||||
ActiveSubscription bool
|
||||
}
|
||||
|
||||
func favicon(w http.ResponseWriter, r *http.Request) {
|
||||
@ -53,7 +54,7 @@ func authenticated_user(w http.ResponseWriter, r *http.Request) int32 {
|
||||
|
||||
// check if the saved id exists in the database, otherwise it's a bad id and has to be removed from the cookies
|
||||
|
||||
exists := users.Exists_account(id)
|
||||
exists := users.ExistsAccount(id)
|
||||
|
||||
if !exists {
|
||||
session, _ := store.Get(r, "id")
|
||||
@ -74,7 +75,8 @@ func home(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
id := authenticated_user(w, r)
|
||||
account, err := users.Get_account(id)
|
||||
account, err := users.GetAccount(id)
|
||||
activesubscription := subscriptions.HasActiveSubscription(id)
|
||||
|
||||
text, err := template.ParseFiles("ui/base.html", "ui/index.html")
|
||||
if err != nil {
|
||||
@ -85,7 +87,7 @@ func home(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
|
||||
err := text.Execute(w, templatedata{AuthenticatedUser: id, Account: account})
|
||||
err := text.Execute(w, templatedata{AuthenticatedUser: id, Account: account, ActiveSubscription: activesubscription})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
http.Error(w, "Internal Server Error", 500)
|
||||
@ -174,12 +176,13 @@ func logout(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
id := authenticated_user(w, r)
|
||||
account, err := users.Get_account(id)
|
||||
account, err := users.GetAccount(id)
|
||||
activesubscription := subscriptions.HasActiveSubscription(id)
|
||||
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
|
||||
err := text.Execute(w, templatedata{AuthenticatedUser: id, Account: account})
|
||||
err := text.Execute(w, templatedata{AuthenticatedUser: id, Account: account, ActiveSubscription: activesubscription})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
http.Error(w, "Internal Server Error", 500)
|
||||
@ -202,7 +205,7 @@ func register(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
id := authenticated_user(w, r)
|
||||
account, err := users.Get_account(id)
|
||||
account, err := users.GetAccount(id)
|
||||
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
@ -263,7 +266,7 @@ func account(w http.ResponseWriter, r *http.Request) {
|
||||
//}
|
||||
|
||||
id := authenticated_user(w, r)
|
||||
account, err := users.Get_account(id)
|
||||
account, err := users.GetAccount(id)
|
||||
|
||||
fmt.Println(id, account)
|
||||
|
||||
@ -310,7 +313,7 @@ func deleteaccount(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func subscribe(w http.ResponseWriter, r *http.Request) {
|
||||
id := authenticated_user(w, r)
|
||||
account, err := users.Get_account(id)
|
||||
account, err := users.GetAccount(id)
|
||||
|
||||
params := &stripe.PriceListParams{}
|
||||
params.Limit = stripe.Int64(3)
|
||||
@ -346,7 +349,7 @@ func subscribe(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func subscribe_stripe(w http.ResponseWriter, r *http.Request) {
|
||||
id := authenticated_user(w, r)
|
||||
account, err := users.Get_account(id)
|
||||
account, err := users.GetAccount(id)
|
||||
|
||||
params := &stripe.CustomerSessionParams{
|
||||
Customer: stripe.String(account.StripeID),
|
||||
@ -376,7 +379,7 @@ func subscribe_stripe(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func managebilling(w http.ResponseWriter, r *http.Request) {
|
||||
id := authenticated_user(w, r)
|
||||
account, err := users.Get_account(id)
|
||||
account, err := users.GetAccount(id)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
@ -395,8 +398,6 @@ func managebilling(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func webhooks(w http.ResponseWriter, r *http.Request) {
|
||||
id := authenticated_user(w, r)
|
||||
|
||||
const MaxBodyBytes = int64(65536)
|
||||
r.Body = http.MaxBytesReader(w, r.Body, MaxBodyBytes)
|
||||
payload, err := ioutil.ReadAll(r.Body)
|
||||
@ -435,7 +436,7 @@ func webhooks(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
// Then define and call a func to handle the successful payment intent.
|
||||
// handlePaymentIntentSucceeded(paymentIntent)
|
||||
handle_checkout_session_completed(id, checkoutSession)
|
||||
handle_checkout_session_completed(checkoutSession)
|
||||
|
||||
case "payment_intent.succeeded":
|
||||
var paymentIntent stripe.PaymentIntent
|
||||
@ -475,7 +476,7 @@ func webhooks(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func handle_checkout_session_completed(userid int32, checkoutsession stripe.CheckoutSession) error {
|
||||
func handle_checkout_session_completed(checkoutsession stripe.CheckoutSession) error {
|
||||
//toprint, _ := json.MarshalIndent(checkoutSession, "", " ")
|
||||
//fmt.Println(string(toprint))
|
||||
|
||||
@ -485,7 +486,6 @@ func handle_checkout_session_completed(userid int32, checkoutsession stripe.Chec
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Println(userid)
|
||||
fmt.Println(subscription.Customer.ID)
|
||||
|
||||
//var status SubscriptionStatus
|
||||
@ -508,7 +508,7 @@ func handle_checkout_session_completed(userid int32, checkoutsession stripe.Chec
|
||||
// status = paused
|
||||
//}
|
||||
|
||||
subscriptions.Insert(userid, subscription.ID, checkoutsession.ID, subscription.Status)
|
||||
subscriptions.Insert(checkoutsession.Customer.ID, subscription.ID, checkoutsession.ID, subscription.Status)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
137
models.go
137
models.go
@ -14,13 +14,13 @@ import "fmt"
|
||||
import "github.com/stripe/stripe-go/v78"
|
||||
import "github.com/stripe/stripe-go/v78/customer"
|
||||
|
||||
var ErrRoRecord = errors.New("no matching record found")
|
||||
var ErrNoRecord = errors.New("no matching record found")
|
||||
var ErrInvalidCredentials = errors.New("invalid credentials")
|
||||
var ErrDuplicateEmail = errors.New("duplicate email")
|
||||
var ErrDuplicateUsername = errors.New("duplicate username")
|
||||
|
||||
type Account struct {
|
||||
Id int32
|
||||
ID int32
|
||||
Username string
|
||||
Password []byte
|
||||
Color int32
|
||||
@ -31,19 +31,27 @@ type Account struct {
|
||||
StripeID string
|
||||
}
|
||||
|
||||
type SubscriptionStatus int32
|
||||
type SubscriptionStatus string
|
||||
|
||||
const (
|
||||
incomplete SubscriptionStatus = iota
|
||||
incomplete_expired
|
||||
trialing
|
||||
active
|
||||
past_due
|
||||
canceled
|
||||
unpaid
|
||||
paused
|
||||
incomplete SubscriptionStatus = "incomplete"
|
||||
incomplete_expired SubscriptionStatus = "incomplete_expired"
|
||||
trialing SubscriptionStatus = "trialing"
|
||||
active SubscriptionStatus = "active"
|
||||
past_due SubscriptionStatus = "past_due"
|
||||
canceled SubscriptionStatus = "canceled"
|
||||
unpaid SubscriptionStatus = "unpaid"
|
||||
paused SubscriptionStatus = "paused"
|
||||
)
|
||||
|
||||
type Subscription struct {
|
||||
ID int32
|
||||
AccountID int32
|
||||
StripeSubscriptionID string
|
||||
StripeCheckoutID string
|
||||
Status SubscriptionStatus
|
||||
}
|
||||
|
||||
type Usermodel struct {
|
||||
DB *sql.DB
|
||||
}
|
||||
@ -92,7 +100,7 @@ func (m *Usermodel) Insert(username string, password string, firstname string, l
|
||||
}
|
||||
|
||||
func (m *Usermodel) Delete(id int32) error {
|
||||
account, err := users.Get_account(id)
|
||||
account, err := users.GetAccount(id)
|
||||
|
||||
if account.StripeID != "" {
|
||||
result, err := customer.Del(account.StripeID, nil)
|
||||
@ -113,15 +121,15 @@ func (m *Usermodel) Delete(id int32) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Usermodel) Get_account(id int32) (Account, error) {
|
||||
func (m *Usermodel) GetAccount(id int32) (Account, error) {
|
||||
if id == 0 {
|
||||
return Account{}, nil
|
||||
return Account{}, ErrNoRecord
|
||||
}
|
||||
stmt := `SELECT id, username, password, color, firstname, lastname, email, created, stripe_id FROM accounts WHERE id = $1`
|
||||
row := m.DB.QueryRow(stmt, id)
|
||||
|
||||
var account Account
|
||||
err := row.Scan(&account.Id, &account.Username, &account.Password, &account.Color, &account.Firstname, &account.Lastname, &account.Email, &account.Created, &account.StripeID)
|
||||
err := row.Scan(&account.ID, &account.Username, &account.Password, &account.Color, &account.Firstname, &account.Lastname, &account.Email, &account.Created, &account.StripeID)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
return Account{}, sql.ErrNoRows
|
||||
@ -151,7 +159,7 @@ func (m *Usermodel) Authenticate(username string, password string) (int32, error
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func (m *Usermodel) Exists_account(id int32) bool {
|
||||
func (m *Usermodel) ExistsAccount(id int32) bool {
|
||||
var exists bool
|
||||
stmt := `SELECT EXISTS(SELECT 1 FROM accounts WHERE id = $1)`
|
||||
row := m.DB.QueryRow(stmt, id)
|
||||
@ -165,20 +173,33 @@ func (m *Usermodel) Exists_account(id int32) bool {
|
||||
return exists
|
||||
}
|
||||
|
||||
func (m *SubscriptionModel) Insert(accountid int32, stripesubscriptionid string, stripecheckoutid string, status stripe.SubscriptionStatus) (int32, error) {
|
||||
func (m *SubscriptionModel) Insert(stripeid string, stripesubscriptionid string, stripecheckoutid string, status stripe.SubscriptionStatus) (int32, error) {
|
||||
var id int32
|
||||
stmt := `SELECT id FROM accounts WHERE stripe_id = $1`
|
||||
|
||||
|
||||
stmt := `INSERT INTO subscriptions (account_id, stripe_subscription_id, stripe_checkout_id, status) VALUES ($1, $2, $3, $4::subscription_status) RETURNING id`
|
||||
|
||||
var insertid int32
|
||||
|
||||
row := m.DB.QueryRow(stmt, accountid, string(stripesubscriptionid), string(stripecheckoutid), string(status))
|
||||
row := m.DB.QueryRow(stmt, stripeid)
|
||||
if row.Err() != nil {
|
||||
fmt.Println(row.Err())
|
||||
return 0, row.Err()
|
||||
}
|
||||
|
||||
err := row.Scan(&insertid)
|
||||
err := row.Scan(&id)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return 0, err
|
||||
}
|
||||
|
||||
stmt = `INSERT INTO subscriptions (account_id, stripe_subscription_id, stripe_checkout_id, status) VALUES ($1, $2, $3, $4::subscription_status) RETURNING id`
|
||||
|
||||
var insertid int32
|
||||
|
||||
row = m.DB.QueryRow(stmt, id, string(stripesubscriptionid), string(stripecheckoutid), string(status))
|
||||
if row.Err() != nil {
|
||||
fmt.Println(row.Err())
|
||||
return 0, row.Err()
|
||||
}
|
||||
|
||||
err = row.Scan(&insertid)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return 0, err
|
||||
@ -198,3 +219,71 @@ func (m *SubscriptionModel) Delete(id int32) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *SubscriptionModel) GetSubscription(id int32) (Subscription, error) {
|
||||
if id == 0 {
|
||||
return Subscription{}, ErrNoRecord
|
||||
}
|
||||
|
||||
stmt := `SELECT id, account_id, stripe_subscription_id, stripe_checkout_id, status FROM subscriptions WHERE id = $1`
|
||||
row := m.DB.QueryRow(stmt, id)
|
||||
|
||||
var subscription Subscription
|
||||
err := row.Scan(&subscription.ID, &subscription.AccountID, &subscription.StripeSubscriptionID, &subscription.StripeCheckoutID, &subscription.Status)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
return Subscription{}, sql.ErrNoRows
|
||||
} else if err != nil {
|
||||
return Subscription{}, err
|
||||
}
|
||||
|
||||
fmt.Println(subscription.Status)
|
||||
|
||||
return subscription, nil
|
||||
}
|
||||
|
||||
func (m *SubscriptionModel) GetSubscriptionsFromAccount(accountid int32) ([]Subscription, error) {
|
||||
if accountid == 0 {
|
||||
return nil, ErrNoRecord
|
||||
}
|
||||
|
||||
stmt := `SELECT id, account_id, stripe_subscription_id, stripe_checkout_id, status FROM subscriptions WHERE account_id = $1`
|
||||
rows, err := m.DB.Query(stmt, accountid)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var subscriptions []Subscription
|
||||
for rows.Next() {
|
||||
var subscription Subscription
|
||||
err := rows.Scan(&subscription.ID, &subscription.AccountID, &subscription.StripeSubscriptionID, &subscription.StripeCheckoutID, &subscription.Status)
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, sql.ErrNoRows
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
subscriptions = append(subscriptions, subscription)
|
||||
}
|
||||
|
||||
return subscriptions, nil
|
||||
}
|
||||
|
||||
func (m *SubscriptionModel) HasActiveSubscription(accountid int32) bool {
|
||||
subscriptions, err := m.GetSubscriptionsFromAccount(accountid)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return false
|
||||
}
|
||||
|
||||
for _, v := range subscriptions {
|
||||
if v.Status == active {
|
||||
return true
|
||||
} else if v.Status == trialing {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
BIN
static/Vollkorn-VariableFont_wght.ttf
Normal file
BIN
static/Vollkorn-VariableFont_wght.ttf
Normal file
Binary file not shown.
1
static/login_24dp_FILL0_wght400_GRAD0_opsz24.svg
Normal file
1
static/login_24dp_FILL0_wght400_GRAD0_opsz24.svg
Normal file
@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 -960 960 960" width="24px" fill="#000000"><path d="M480-120v-80h280v-560H480v-80h280q33 0 56.5 23.5T840-760v560q0 33-23.5 56.5T760-120H480Zm-80-160-55-58 102-102H120v-80h327L345-622l55-58 200 200-200 200Z"/></svg>
|
||||
|
After Width: | Height: | Size: 279 B |
@ -1,6 +1,7 @@
|
||||
html {
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
body {
|
||||
background-color: #3475CB;
|
||||
font-family: "Vollkorn";
|
||||
@ -13,7 +14,7 @@ body {
|
||||
}
|
||||
|
||||
h1 {
|
||||
font-size: 96px;
|
||||
font-size: 6rem;
|
||||
margin: 0px;
|
||||
}
|
||||
|
||||
@ -62,7 +63,7 @@ main {
|
||||
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
|
||||
border-radius: 15px;
|
||||
padding: 30px 40px;
|
||||
font-size: x-large;
|
||||
font-size: 1.5rem;
|
||||
}
|
||||
|
||||
.wrapper {
|
||||
@ -76,7 +77,7 @@ main {
|
||||
}
|
||||
|
||||
.wrapper h1 {
|
||||
font-size: xx-large;
|
||||
font-size: 1.5rem;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
@ -139,6 +140,10 @@ main {
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
input {
|
||||
font-family: inherit;
|
||||
}
|
||||
|
||||
footer {
|
||||
position: fixed;
|
||||
left: 0;
|
||||
|
||||
18
ui/base.html
18
ui/base.html
@ -3,15 +3,13 @@
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Alfheim</title>
|
||||
<style>
|
||||
@font-face {
|
||||
font-family: "Vollkorn";
|
||||
src: url(/static/Vollkorn-VariableFont_wght.ttf);
|
||||
}
|
||||
</style>
|
||||
<link href="/static/style.css" rel="stylesheet" />
|
||||
<link rel="preconnect" href="https://fonts.googleapis.com">
|
||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
||||
<link href="https://fonts.googleapis.com/css2?family=Vollkorn:ital,wght@0,400..900;1,400..900&display=swap" rel="stylesheet">
|
||||
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@24,400,0,0" />
|
||||
<script>
|
||||
/*to prevent Firefox FOUC, this must be here*/
|
||||
let FF_FOUC_FIX;
|
||||
</script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
@ -19,13 +17,15 @@
|
||||
<nav>
|
||||
<a href="/"><h1 class="maintitle">Alfheim</h1></a>
|
||||
<div class="navbuttons">
|
||||
{{if not .ActiveSubscription}}
|
||||
<a href="/subscribe"><div class="loginbutton"><strong>Subscribe</strong></div></a>
|
||||
{{end}}
|
||||
|
||||
{{if .AuthenticatedUser}}
|
||||
<a href="/account"><div class="loginbutton"><strong>Account</strong></div></a>
|
||||
<a href="/logout"><div class="loginbutton"><strong>Log out</strong></div></a>
|
||||
{{else}}
|
||||
<a href="/login"><div class="loginbutton"><strong>Log in</strong><span class="material-symbols-outlined">login</span></div></a>
|
||||
<a href="/login"><div class="loginbutton"><strong>Log in</strong><img src="/static/login_24dp_FILL0_wght400_GRAD0_opsz24.svg" alt=""/></div></a>
|
||||
<a href="/register"><div class="loginbutton"><strong>Register</strong></div></a>
|
||||
{{end}}
|
||||
</div>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user