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

This commit is contained in:
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 {
Currency string `json:"currency"`
Amount string `json:"amount"`
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
}