Generating a receipt page

This commit is contained in:
vinchent 2024-08-04 18:17:56 +02:00
parent 48695949e3
commit bfaf8965b3
5 changed files with 49 additions and 3 deletions

View File

@ -9,3 +9,33 @@ func (app *application) VirtualTerminal(w http.ResponseWriter, r *http.Request)
app.errorLog.Println(err)
}
}
func (app *application) PaymentSucceeded(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
app.errorLog.Println(err)
return
}
// read posted data
cardHolder := r.Form.Get("cardholder_name")
email := r.Form.Get("cardholder_email")
paymentIntent := r.Form.Get("payment_intent")
paymentMethod := r.Form.Get("payment_method")
paymentAmount := r.Form.Get("payment_amount")
paymentCurrency := r.Form.Get("payment_currency")
data := make(map[string]interface{})
data["cardholder"] = cardHolder
data["email"] = email
data["pi"] = paymentIntent
data["pm"] = paymentMethod
data["pa"] = paymentAmount
data["pc"] = paymentCurrency
if err := app.renderTemplate(w, r, "succeeded", &templateData{
Data: data,
}); err != nil {
app.errorLog.Println(err)
}
}

View File

@ -28,7 +28,7 @@ var functions = template.FuncMap{}
var templateFS embed.FS
func (app *application) addDefaultData(td *templateData, r *http.Request) *templateData {
return nil
return td
}
func (app *application) renderTemplate(

View File

@ -10,6 +10,7 @@ func (app *application) routes() http.Handler {
mux := chi.NewRouter()
mux.Get("/virtual-terminal", app.VirtualTerminal)
mux.Post("/payment-succeeded", app.PaymentSucceeded)
return mux
}

View File

@ -0,0 +1,14 @@
{{ template "base" . }}
{{ define "title" }}
Payment Succedded!
{{ end }}
{{ define "content" }}
<h2 class="mt-5">Payment Succeeded</h2>
<hr>
<p>Payment Intent: {{ index .Data "pi" }}</p>
<p>Cardholder: {{ index .Data "cardholder" }}</p>
<p>Email: {{ index .Data "email" }}</p>
<p>Payment Method: {{ index .Data "pm" }}</p>
<p>Payment Amount: {{ index .Data "pa" }}</p>
<p>Currency: {{ index .Data "pc" }}</p>
{{ end }}

View File

@ -6,7 +6,7 @@ Virtual Terminal
<h2 class="mt-3 text-center">Virtual Terminal</h2>
<hr>
<div class="alert alert-danger text-center d-none" id="card-messages"></div>
<form action="/payment-succecded"
<form action="/payment-succeeded"
method="post"
name="charge_form"
id="charge_form"
@ -149,12 +149,13 @@ function val() {
} else if (result.paymentIntent) {
if (result.paymentIntent.status === "succeeded") {
// we have charged the card
document.getElementById("payment_intent").value = result.paymentIntent.id;
document.getElementById("payment_method").value = result.paymentIntent.payment_method_types[0];
document.getElementById("payment_amount").value = result.paymentIntent.amount;
document.getElementById("payment_currency").value = result.paymentIntent.currency;
processing.classList.add("d-none");
showCardSuccess();
// would submit the form
document.getElementById("charge_form").submit();
}
}
})