From a564ecb342c464310810c0f5457edde3b8af2d6a Mon Sep 17 00:00:00 2001 From: vinchent Date: Sun, 11 Aug 2024 12:30:57 +0200 Subject: [PATCH] Revising virtual terminal --- cmd/web/handlers.go | 41 +++++++++++++++++++ cmd/web/render.go | 2 +- cmd/web/routes.go | 7 +++- cmd/web/templates/terminal.page.gohtml | 22 ++++++---- .../virtual-terminal-receipt.page.gohtml | 18 ++++++++ 5 files changed, 79 insertions(+), 11 deletions(-) create mode 100644 cmd/web/templates/virtual-terminal-receipt.page.gohtml diff --git a/cmd/web/handlers.go b/cmd/web/handlers.go index bddd642..8c674e4 100644 --- a/cmd/web/handlers.go +++ b/cmd/web/handlers.go @@ -154,6 +154,36 @@ func (app *application) PaymentSucceeded(w http.ResponseWriter, r *http.Request) 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) { txn := app.Session.Pop(r.Context(), "txn").(TransactionData) 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) { customer := models.Customer{ FirstName: firstName, diff --git a/cmd/web/render.go b/cmd/web/render.go index 50ddf36..0f22e8d 100644 --- a/cmd/web/render.go +++ b/cmd/web/render.go @@ -29,7 +29,7 @@ var functions = template.FuncMap{ } func formatCurrency(n int) string { - f := float32(n / 100) + f := float32(n) / float32(100) return fmt.Sprintf("€%.2f", f) } diff --git a/cmd/web/routes.go b/cmd/web/routes.go index adc51bc..afd3910 100644 --- a/cmd/web/routes.go +++ b/cmd/web/routes.go @@ -12,9 +12,12 @@ func (app *application) routes() http.Handler { mux.Get("/", app.Home) mux.Get("/virtual-terminal", app.VirtualTerminal) - mux.Post("/payment-succeeded", app.PaymentSucceeded) - mux.Get("/receipt", app.Receipt) + mux.Post("/virtual-terminal-payment-succeeded", app.VirtualTerminalPaymentSucceeded) + mux.Get("/virtual-terminal-receipt", app.VirtualTerminalReceipt) + mux.Get("/widget/{id}", app.ChargeOnce) + mux.Get("/receipt", app.Receipt) + mux.Post("/payment-succeeded", app.PaymentSucceeded) fileServer := http.FileServer(http.Dir("./static")) mux.Handle("/static/*", http.StripPrefix("/static", fileServer)) diff --git a/cmd/web/templates/terminal.page.gohtml b/cmd/web/templates/terminal.page.gohtml index 511b270..0463e73 100644 --- a/cmd/web/templates/terminal.page.gohtml +++ b/cmd/web/templates/terminal.page.gohtml @@ -6,7 +6,7 @@ Virtual Terminal

Virtual Terminal


-
Charge Card + onclick="val({{.API}})">Charge Card
Loading... @@ -81,14 +81,20 @@ Virtual Terminal {{ end }} diff --git a/cmd/web/templates/virtual-terminal-receipt.page.gohtml b/cmd/web/templates/virtual-terminal-receipt.page.gohtml new file mode 100644 index 0000000..7211fd0 --- /dev/null +++ b/cmd/web/templates/virtual-terminal-receipt.page.gohtml @@ -0,0 +1,18 @@ +{{ template "base" . }} +{{ define "title" }} +Payment Succedded! +{{ end }} +{{ define "content" }} +{{$txn := index .Data "txn"}} +

Virtual Terminal Payment Succeeded

+
+

Payment Intent: {{$txn.PaymentIntentID}}

+

Email: {{ $txn.Email }}

+

Payment Method: {{ $txn.PaymentMethodID }}

+

Payment Amount: {{ $txn.PaymentAmount }}

+

Currency: {{ $txn.PaymentCurrency }}

+

Last Four: {{ $txn.LastFour }}

+

Bank Return Code: {{ $txn.BankReturnCode }}

+

Expiry Date: {{ $txn.ExpiryMonth }}/{{$txn.ExpiryYear}}

+{{ end }} +