diff --git a/handlers.go b/handlers.go index 468db2e..cf048dd 100644 --- a/handlers.go +++ b/handlers.go @@ -15,12 +15,15 @@ import "errors"; import "runtime/debug"; import "github.com/stripe/stripe-go/v78"; import "github.com/stripe/stripe-go/v78/price"; +import "github.com/stripe/stripe-go/v78/customersession"; +import "github.com/stripe/stripe-go/v78/billingportal/session"; type templatedata struct { AuthenticatedUser int32; - FormErrors map[string]string; - Account Account; - Prices []stripe.Price + FormErrors map[string]string; + Account Account; + Prices []stripe.Price; + ClientSecret string; } func favicon(w http.ResponseWriter, r *http.Request) { @@ -315,10 +318,17 @@ func subscribe(w http.ResponseWriter, r *http.Request) { prices = append(prices, *results.Price()); } - text, err := template.ParseFiles("ui/base.html", "ui/subscribe.html"); + fm := template.FuncMap{ + "divide": func(a, b float64) float64 { + return a / b; + }, + }; + + text, err := template.New("base.html").Funcs(fm).ParseFiles("ui/base.html", "ui/subscribe.html"); if err != nil { http.Error(w, "Internal Server Error", 500); log.Fatal(err); + return; } err = text.Execute(w, templatedata{AuthenticatedUser: id, Account: account, Prices: prices}); @@ -327,3 +337,53 @@ func subscribe(w http.ResponseWriter, r *http.Request) { http.Error(w, "Internal Server Error", 500); } } + +func subscribe_stripe(w http.ResponseWriter, r *http.Request) { + id := authenticated_user(w, r); + account, err := users.Get_account(id); + + params := &stripe.CustomerSessionParams{ + Customer: stripe.String(account.StripeID), + Components: &stripe.CustomerSessionComponentsParams{ + PricingTable: &stripe.CustomerSessionComponentsPricingTableParams{ + Enabled: stripe.Bool(true), + }, + }, + }; + result, err := customersession.New(params); + if err != nil { + fmt.Println(err); + } + + text, err := template.ParseFiles("ui/base.html", "ui/subscribe_stripe.html"); + if err != nil { + http.Error(w, "Internal Server Error", 500); + log.Fatal(err); + } + + err = text.Execute(w, templatedata{AuthenticatedUser: id, Account: account, ClientSecret: result.ClientSecret}); + if err != nil { + log.Fatal(err); + http.Error(w, "Internal Server Error", 500); + } +} + +func managebilling(w http.ResponseWriter, r *http.Request) { + id := authenticated_user(w, r); + account, err := users.Get_account(id); + if err != nil { + fmt.Println(err); + } + + params := &stripe.BillingPortalSessionParams{ + Customer: stripe.String(account.StripeID), + ReturnURL: stripe.String("http://localhost:8080/account"), + }; + + result, err := session.New(params); + if err != nil { + fmt.Println(err); + } + + http.Redirect(w, r, result.URL, http.StatusSeeOther); +} diff --git a/main.go b/main.go index 455242f..d2fd44c 100644 --- a/main.go +++ b/main.go @@ -43,7 +43,6 @@ func main() { mux := http.NewServeMux(); - //rows, err := db.Query("SELECT * FROM accounts"); //if err != nil {; // log.Fatal(err); @@ -78,7 +77,8 @@ func main() { mux.HandleFunc("/register", register); mux.HandleFunc("/account", require_authenticated_user(account)); mux.HandleFunc("/deleteaccount", require_authenticated_user(deleteaccount)); - mux.HandleFunc("/subscribe", subscribe); + mux.HandleFunc("/subscribe", require_authenticated_user(subscribe_stripe)); + mux.HandleFunc("/managebilling", require_authenticated_user(managebilling)); log.Fatal(http.ListenAndServe(*addr, secure_headers(mux))); diff --git a/ui/account.html b/ui/account.html index 0a03bbc..def5802 100644 --- a/ui/account.html +++ b/ui/account.html @@ -13,4 +13,10 @@ + +
+
+ +
+
{{end}} diff --git a/ui/subscribe.html b/ui/subscribe.html index 4cef96f..fb1f4a2 100644 --- a/ui/subscribe.html +++ b/ui/subscribe.html @@ -1,7 +1,8 @@ {{define "body"}} {{range .Prices}}
- {{.}} + {{$price := divide .UnitAmountDecimal 100}} + {{.Currency}} {{printf "%.2f" $price}}
{{end}} {{end}} diff --git a/ui/subscribe_stripe.html b/ui/subscribe_stripe.html new file mode 100644 index 0000000..bbdf87e --- /dev/null +++ b/ui/subscribe_stripe.html @@ -0,0 +1,5 @@ +{{define "body"}} + + + +{{end}}