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