Create a handler for the POST request after a user is subscribed

This commit is contained in:
vinchent 2024-08-12 13:49:50 +02:00
parent 01690d0236
commit 0b35136d3f
3 changed files with 40 additions and 8 deletions

View File

@ -10,8 +10,12 @@ import (
) )
type stripePayload struct { type stripePayload struct {
Currency string `json:"currency"` Currency string `json:"currency"`
Amount string `json:"amount"` Amount string `json:"amount"`
PaymentMethod string `json:"payment_method"`
Email string `json:"email"`
LastFour string `json:"last_four"`
Plan string `json:"plan"`
} }
type jsonResponse struct { type jsonResponse struct {
@ -83,3 +87,32 @@ func (app *application) GetWidgetByID(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
w.Write(out) w.Write(out)
} }
func (app *application) CreateCustomerAndSubscribeToPlan(w http.ResponseWriter, r *http.Request) {
var data stripePayload
err := json.NewDecoder(r.Body).Decode(&data)
if err != nil {
app.errorLog.Println(err)
return
}
app.infoLog.Println(data.Email, data.LastFour, data.PaymentMethod, data.Plan)
okay := true
msg := ""
resp := jsonResponse{
OK: okay,
Message: msg,
}
out, err := json.MarshalIndent(resp, "", " ")
if err != nil {
app.errorLog.Println(err)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(out)
}

View File

@ -20,5 +20,6 @@ func (app *application) routes() http.Handler {
mux.Post("/api/payment-intent", app.GetPaymentIntent) mux.Post("/api/payment-intent", app.GetPaymentIntent)
mux.Get("/api/widget/{id}", app.GetWidgetByID) mux.Get("/api/widget/{id}", app.GetWidgetByID)
mux.Post("/api/create-customer-and-subscribe-to-plan", app.CreateCustomerAndSubscribeToPlan)
return mux return mux
} }

View File

@ -4,7 +4,7 @@ Bronze Plan
{{ end }} {{ end }}
{{ define "content" }} {{ define "content" }}
{{$widget := index .Data "widget"}} {{$widget := index .Data "widget"}}
<h2 class="mt-3 text-center">Bronze Plan: {{formatCurrency $widget.Price}}</h2> <h2 class="mt-3 text-center">Bronze Plan: {{ formatCurrency $widget.Price }}</h2>
<hr> <hr>
<div class="alert alert-danger text-center d-none" id="card-messages"></div> <div class="alert alert-danger text-center d-none" id="card-messages"></div>
<form action="/payment-succeeded-temp" <form action="/payment-succeeded-temp"
@ -62,10 +62,7 @@ Bronze Plan
<div class="alert-success text-center" id="card-success" role="alert"></div> <div class="alert-success text-center" id="card-success" role="alert"></div>
</div> </div>
<hr> <hr>
<a href="javascript:void(0)" <a href="javascript:void(0)" id="pay-button" class="btn btn-primary">Pay {{ formatCurrency $widget.Price }}/month</a>
id="pay-button"
class="btn btn-primary"
onclick="val({{.API}})">Pay {{formatCurrency $widget.Price}}/month</a>
<div class="text-center d-none" id="processing-payment"> <div class="text-center d-none" id="processing-payment">
<div class="spinner-border text-primary" role="status"> <div class="spinner-border text-primary" role="status">
<span class="visually-hidden">Loading...</span> <span class="visually-hidden">Loading...</span>
@ -90,13 +87,14 @@ Bronze Plan
</form> </form>
{{ end }} {{ end }}
{{ define "js" }} {{ define "js" }}
{{$widget := index .Data "widget"}}
<script src="https://js.stripe.com/v3/"></script> <script src="https://js.stripe.com/v3/"></script>
<script type="module"> <script type="module">
import {stripeInit} from "/static/js/common.js"; import {stripeInit} from "/static/js/common.js";
import {val} from "/static/js/stripe-plan.js" import {val} from "/static/js/stripe-plan.js"
stripeInit('{{.StripePubKey}}'); stripeInit('{{.StripePubKey}}');
document.getElementById("pay-button").addEventListener("click", () => { document.getElementById("pay-button").addEventListener("click", () => {
val({{.API}}); val({{$widget.PlanID}}, {{.API}});
}) })
</script> </script>
{{ end }} {{ end }}