Revising virtual terminal

This commit is contained in:
vinchent 2024-08-11 12:30:57 +02:00
parent 5ad4464b28
commit a564ecb342
5 changed files with 79 additions and 11 deletions

View File

@ -154,6 +154,36 @@ func (app *application) PaymentSucceeded(w http.ResponseWriter, r *http.Request)
http.Redirect(w, r, "/receipt", http.StatusSeeOther) http.Redirect(w, r, "/receipt", http.StatusSeeOther)
} }
func (app *application) VirtualTerminalPaymentSucceeded(w http.ResponseWriter, r *http.Request) {
txnData, err := app.GetTransactionData(r)
if err != nil {
app.errorLog.Println(err)
return
}
transaction := models.Transaction{
Amount: txnData.PaymentAmount,
Currency: txnData.PaymentCurrency,
LastFour: txnData.LastFour,
ExpiryMonth: txnData.ExpiryMonth,
ExpiryYear: txnData.ExpiryYear,
PaymentIntent: txnData.PaymentIntentID,
PaymentMethod: txnData.PaymentMethodID,
BankReturnCode: txnData.BankReturnCode,
TransactionStatusID: 2, // TODO: use an enum
}
_, err = app.SaveTransaction(transaction)
if err != nil {
app.errorLog.Println(err)
return
}
app.Session.Put(r.Context(), "txn", txnData)
http.Redirect(w, r, "/virtual-terminal-receipt", http.StatusSeeOther)
}
func (app *application) Receipt(w http.ResponseWriter, r *http.Request) { func (app *application) Receipt(w http.ResponseWriter, r *http.Request) {
txn := app.Session.Pop(r.Context(), "txn").(TransactionData) txn := app.Session.Pop(r.Context(), "txn").(TransactionData)
data := make(map[string]interface{}) data := make(map[string]interface{})
@ -165,6 +195,17 @@ func (app *application) Receipt(w http.ResponseWriter, r *http.Request) {
} }
} }
func (app *application) VirtualTerminalReceipt(w http.ResponseWriter, r *http.Request) {
txn := app.Session.Pop(r.Context(), "txn").(TransactionData)
data := make(map[string]interface{})
data["txn"] = txn
if err := app.renderTemplate(w, r, "virtual-terminal-receipt", &templateData{
Data: data,
}); err != nil {
app.errorLog.Println(err)
}
}
func (app *application) SaveCustomer(firstName, lastName, email string) (int, error) { func (app *application) SaveCustomer(firstName, lastName, email string) (int, error) {
customer := models.Customer{ customer := models.Customer{
FirstName: firstName, FirstName: firstName,

View File

@ -29,7 +29,7 @@ var functions = template.FuncMap{
} }
func formatCurrency(n int) string { func formatCurrency(n int) string {
f := float32(n / 100) f := float32(n) / float32(100)
return fmt.Sprintf("€%.2f", f) return fmt.Sprintf("€%.2f", f)
} }

View File

@ -12,9 +12,12 @@ func (app *application) routes() http.Handler {
mux.Get("/", app.Home) mux.Get("/", app.Home)
mux.Get("/virtual-terminal", app.VirtualTerminal) mux.Get("/virtual-terminal", app.VirtualTerminal)
mux.Post("/payment-succeeded", app.PaymentSucceeded) mux.Post("/virtual-terminal-payment-succeeded", app.VirtualTerminalPaymentSucceeded)
mux.Get("/receipt", app.Receipt) mux.Get("/virtual-terminal-receipt", app.VirtualTerminalReceipt)
mux.Get("/widget/{id}", app.ChargeOnce) mux.Get("/widget/{id}", app.ChargeOnce)
mux.Get("/receipt", app.Receipt)
mux.Post("/payment-succeeded", app.PaymentSucceeded)
fileServer := http.FileServer(http.Dir("./static")) fileServer := http.FileServer(http.Dir("./static"))
mux.Handle("/static/*", http.StripPrefix("/static", fileServer)) mux.Handle("/static/*", http.StripPrefix("/static", fileServer))

View File

@ -6,7 +6,7 @@ Virtual Terminal
<h2 class="mt-3 text-center">Virtual Terminal</h2> <h2 class="mt-3 text-center">Virtual Terminal</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" <form action="/virtual-terminal-payment-succeeded"
method="post" method="post"
name="charge_form" name="charge_form"
id="charge_form" id="charge_form"
@ -52,7 +52,7 @@ Virtual Terminal
<a href="javascript:void(0)" <a href="javascript:void(0)"
id="pay-button" id="pay-button"
class="btn btn-primary" class="btn btn-primary"
onclick="termVal()">Charge Card</a> onclick="val({{.API}})">Charge Card</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>
@ -81,14 +81,20 @@ Virtual Terminal
<script src="/static/js/stripe.js"></script> <script src="/static/js/stripe.js"></script>
<script> <script>
stripeInit('{{.StripePubKey}}'); stripeInit('{{.StripePubKey}}');
function termVal() { document.getElementById("charge_amount").addEventListener("change", (evt) => {
const chargeAmount = document.getElementById("charge_amount"); if (evt.target.value !== "") {
if (chargeAmount.value !== "") { document.getElementById("amount").value = parseInt((evt.target.value * 100), 10);
document.getElementById("amount").value = chargeAmount.value * 100;
} else { } else {
document.getElementById("amount").value = 0; document.getElementById("amount").value = 0;
} }
val({{.API}}); });
} // function termVal() {
// const chargeAmount = document.getElementById("charge_amount");
// if (chargeAmount.value !== "") {
// document.getElementById("amount").value = parseInt((chargeAmount.value * 100), 10);
// } else {
// document.getElementById("amount").value = 0;
// }
// }
</script> </script>
{{ end }} {{ end }}

View File

@ -0,0 +1,18 @@
{{ template "base" . }}
{{ define "title" }}
Payment Succedded!
{{ end }}
{{ define "content" }}
{{$txn := index .Data "txn"}}
<h2 class="mt-5">Virtual Terminal Payment Succeeded</h2>
<hr>
<p>Payment Intent: {{$txn.PaymentIntentID}}</p>
<p>Email: {{ $txn.Email }}</p>
<p>Payment Method: {{ $txn.PaymentMethodID }}</p>
<p>Payment Amount: {{ $txn.PaymentAmount }}</p>
<p>Currency: {{ $txn.PaymentCurrency }}</p>
<p>Last Four: {{ $txn.LastFour }}</p>
<p>Bank Return Code: {{ $txn.BankReturnCode }}</p>
<p>Expiry Date: {{ $txn.ExpiryMonth }}/{{$txn.ExpiryYear}}</p>
{{ end }}