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

@ -12,6 +12,10 @@ import (
type stripePayload struct {
Currency string `json:"currency"`
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 {
@ -83,3 +87,32 @@ func (app *application) GetWidgetByID(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
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.Get("/api/widget/{id}", app.GetWidgetByID)
mux.Post("/api/create-customer-and-subscribe-to-plan", app.CreateCustomerAndSubscribeToPlan)
return mux
}

View File

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