git-svn-id: svn://losandesgames.com/alfheim-website@14 15359d88-9307-4e75-a9c1-e5686e5897df
This commit is contained in:
Vicente Ferrari Smith 2024-05-20 22:50:06 +00:00
parent 09c553609f
commit 98e641c55c
5 changed files with 79 additions and 7 deletions

View File

@ -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);
}

View File

@ -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)));

View File

@ -13,4 +13,10 @@
</div>
</form>
</div>
<div class="wrapper">
<form method="POST" action="/managebilling">
<button type="submit">Manage billing</button>
</form>
</div>
{{end}}

View File

@ -1,7 +1,8 @@
{{define "body"}}
{{range .Prices}}
<div class="wrapper">
{{.}}
{{$price := divide .UnitAmountDecimal 100}}
{{.Currency}} {{printf "%.2f" $price}}
</div>
{{end}}
{{end}}

5
ui/subscribe_stripe.html Normal file
View File

@ -0,0 +1,5 @@
{{define "body"}}
<script async src="https://js.stripe.com/v3/pricing-table.js"></script>
<stripe-pricing-table pricing-table-id="prctbl_1PIaJpKUHKCjyTmcy1ONKQiT" publishable-key="pk_test_51PGebgKUHKCjyTmcyuhj7C5lfHfKFLCxRJ2opoqxL3mGEHSRaMvOHYKKgX4MdFfESW78dssjyunboUcFhg3LTwmn005PmzVIXw" customer-session-client-secret={{.ClientSecret}}>
</stripe-pricing-table>
{{end}}