Compare commits
No commits in common. "a564ecb342c464310810c0f5457edde3b8af2d6a" and "8e482aa712d84fe455f10fec9340d90d5ac2549b" have entirely different histories.
a564ecb342
...
8e482aa712
@ -21,29 +21,14 @@ func (app *application) VirtualTerminal(w http.ResponseWriter, r *http.Request)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type TransactionData struct {
|
func (app *application) PaymentSucceeded(w http.ResponseWriter, r *http.Request) {
|
||||||
FirstName string
|
|
||||||
LastName string
|
|
||||||
Email string
|
|
||||||
PaymentIntentID string
|
|
||||||
PaymentMethodID string
|
|
||||||
PaymentAmount int
|
|
||||||
PaymentCurrency string
|
|
||||||
LastFour string
|
|
||||||
ExpiryMonth int
|
|
||||||
ExpiryYear int
|
|
||||||
BankReturnCode string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (app *application) GetTransactionData(r *http.Request) (TransactionData, error) {
|
|
||||||
var txnData TransactionData
|
|
||||||
|
|
||||||
err := r.ParseForm()
|
err := r.ParseForm()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.errorLog.Println(err)
|
app.errorLog.Println(err)
|
||||||
return txnData, err
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// read posted data
|
||||||
firstName := r.Form.Get("first_name")
|
firstName := r.Form.Get("first_name")
|
||||||
lastName := r.Form.Get("last_name")
|
lastName := r.Form.Get("last_name")
|
||||||
email := r.Form.Get("cardholder_email")
|
email := r.Form.Get("cardholder_email")
|
||||||
@ -51,9 +36,7 @@ func (app *application) GetTransactionData(r *http.Request) (TransactionData, er
|
|||||||
paymentMethod := r.Form.Get("payment_method")
|
paymentMethod := r.Form.Get("payment_method")
|
||||||
paymentAmount := r.Form.Get("payment_amount")
|
paymentAmount := r.Form.Get("payment_amount")
|
||||||
paymentCurrency := r.Form.Get("payment_currency")
|
paymentCurrency := r.Form.Get("payment_currency")
|
||||||
amount, _ := strconv.Atoi(paymentAmount)
|
widgetID, _ := strconv.Atoi(r.Form.Get("product_id"))
|
||||||
|
|
||||||
// TODO: validation of the data
|
|
||||||
|
|
||||||
card := cards.Card{
|
card := cards.Card{
|
||||||
Secret: app.config.stripe.secret,
|
Secret: app.config.stripe.secret,
|
||||||
@ -63,67 +46,34 @@ func (app *application) GetTransactionData(r *http.Request) (TransactionData, er
|
|||||||
pi, err := card.RetrievePaymentIntent(paymentIntent)
|
pi, err := card.RetrievePaymentIntent(paymentIntent)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.errorLog.Println(err)
|
app.errorLog.Println(err)
|
||||||
return txnData, err
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
pm, err := card.GetPaymentMethod(paymentMethod)
|
pm, err := card.GetPaymentMethod(paymentMethod)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.errorLog.Println(err)
|
app.errorLog.Println(err)
|
||||||
return txnData, err
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
lastFour := pm.Card.Last4
|
lastFour := pm.Card.Last4
|
||||||
expiryMonth := pm.Card.ExpMonth
|
expiryMonth := pm.Card.ExpMonth
|
||||||
expiryYear := pm.Card.ExpYear
|
expiryYear := pm.Card.ExpYear
|
||||||
|
|
||||||
txnData = TransactionData{
|
customerID, err := app.SaveCustomer(firstName, lastName, email)
|
||||||
FirstName: firstName,
|
|
||||||
LastName: lastName,
|
|
||||||
Email: email,
|
|
||||||
PaymentIntentID: paymentIntent,
|
|
||||||
PaymentMethodID: paymentMethod,
|
|
||||||
PaymentAmount: amount,
|
|
||||||
PaymentCurrency: paymentCurrency,
|
|
||||||
LastFour: lastFour,
|
|
||||||
ExpiryMonth: int(expiryMonth),
|
|
||||||
ExpiryYear: int(expiryYear),
|
|
||||||
BankReturnCode: pi.LatestCharge.ID,
|
|
||||||
}
|
|
||||||
return txnData, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (app *application) PaymentSucceeded(w http.ResponseWriter, r *http.Request) {
|
|
||||||
// read posted data
|
|
||||||
err := r.ParseForm()
|
|
||||||
if err != nil {
|
|
||||||
app.errorLog.Println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
widgetID, _ := strconv.Atoi(r.Form.Get("product_id"))
|
|
||||||
|
|
||||||
txnData, err := app.GetTransactionData(r)
|
|
||||||
if err != nil {
|
|
||||||
app.errorLog.Println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
customerID, err := app.SaveCustomer(txnData.FirstName, txnData.LastName, txnData.Email)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.errorLog.Println(err)
|
app.errorLog.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
app.infoLog.Printf("custumer id: %d", customerID)
|
app.infoLog.Printf("custumer id: %d", customerID)
|
||||||
|
|
||||||
|
amount, _ := strconv.Atoi(paymentAmount)
|
||||||
transaction := models.Transaction{
|
transaction := models.Transaction{
|
||||||
Amount: txnData.PaymentAmount,
|
Amount: amount,
|
||||||
Currency: txnData.PaymentCurrency,
|
Currency: paymentCurrency,
|
||||||
LastFour: txnData.LastFour,
|
LastFour: lastFour,
|
||||||
ExpiryMonth: txnData.ExpiryMonth,
|
ExpiryMonth: int(expiryMonth),
|
||||||
ExpiryYear: txnData.ExpiryYear,
|
ExpiryYear: int(expiryYear),
|
||||||
PaymentIntent: txnData.PaymentIntentID,
|
BankReturnCode: pi.LatestCharge.ID,
|
||||||
PaymentMethod: txnData.PaymentMethodID,
|
|
||||||
BankReturnCode: txnData.BankReturnCode,
|
|
||||||
TransactionStatusID: 2, // TODO: use an enum
|
TransactionStatusID: 2, // TODO: use an enum
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -140,7 +90,7 @@ func (app *application) PaymentSucceeded(w http.ResponseWriter, r *http.Request)
|
|||||||
CustomerID: customerID,
|
CustomerID: customerID,
|
||||||
StatusID: 1,
|
StatusID: 1,
|
||||||
Quantity: 1,
|
Quantity: 1,
|
||||||
Amount: txnData.PaymentAmount,
|
Amount: amount,
|
||||||
}
|
}
|
||||||
orderID, err := app.SaveOrder(order)
|
orderID, err := app.SaveOrder(order)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -149,57 +99,22 @@ func (app *application) PaymentSucceeded(w http.ResponseWriter, r *http.Request)
|
|||||||
}
|
}
|
||||||
app.infoLog.Printf("order id: %d", orderID)
|
app.infoLog.Printf("order id: %d", orderID)
|
||||||
|
|
||||||
app.Session.Put(r.Context(), "txn", txnData)
|
|
||||||
|
|
||||||
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{})
|
data := make(map[string]interface{})
|
||||||
data["txn"] = txn
|
data["email"] = email
|
||||||
if err := app.renderTemplate(w, r, "receipt", &templateData{
|
data["pi"] = paymentIntent
|
||||||
Data: data,
|
data["pm"] = paymentMethod
|
||||||
}); err != nil {
|
data["pa"] = paymentAmount
|
||||||
app.errorLog.Println(err)
|
data["pc"] = paymentCurrency
|
||||||
}
|
data["last_four"] = lastFour
|
||||||
}
|
data["expiry_month"] = expiryMonth
|
||||||
|
data["expiry_year"] = expiryYear
|
||||||
|
data["bank_return_code"] = pi.LatestCharge.ID
|
||||||
|
data["first_name"] = firstName
|
||||||
|
data["last_name"] = lastName
|
||||||
|
|
||||||
func (app *application) VirtualTerminalReceipt(w http.ResponseWriter, r *http.Request) {
|
// should write this data to session, and then redirect user to the new page
|
||||||
txn := app.Session.Pop(r.Context(), "txn").(TransactionData)
|
|
||||||
data := make(map[string]interface{})
|
if err := app.renderTemplate(w, r, "succeeded", &templateData{
|
||||||
data["txn"] = txn
|
|
||||||
if err := app.renderTemplate(w, r, "virtual-terminal-receipt", &templateData{
|
|
||||||
Data: data,
|
Data: data,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
app.errorLog.Println(err)
|
app.errorLog.Println(err)
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/gob"
|
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
@ -64,8 +63,6 @@ func (app *application) serve() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
gob.Register(TransactionData{})
|
|
||||||
|
|
||||||
var cfg config
|
var cfg config
|
||||||
|
|
||||||
flag.IntVar(&cfg.port, "port", 4000, "Server port to listen on")
|
flag.IntVar(&cfg.port, "port", 4000, "Server port to listen on")
|
||||||
|
@ -1,9 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
func SessionLoad(next http.Handler) http.Handler {
|
|
||||||
return session.LoadAndSave(next)
|
|
||||||
}
|
|
@ -29,7 +29,7 @@ var functions = template.FuncMap{
|
|||||||
}
|
}
|
||||||
|
|
||||||
func formatCurrency(n int) string {
|
func formatCurrency(n int) string {
|
||||||
f := float32(n) / float32(100)
|
f := float32(n / 100)
|
||||||
return fmt.Sprintf("€%.2f", f)
|
return fmt.Sprintf("€%.2f", f)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,12 +12,8 @@ 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("/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)
|
mux.Post("/payment-succeeded", app.PaymentSucceeded)
|
||||||
|
mux.Get("/widget/{id}", app.ChargeOnce)
|
||||||
|
|
||||||
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))
|
||||||
|
@ -1,8 +0,0 @@
|
|||||||
{{ template "base" . }}
|
|
||||||
{{ define "title" }}
|
|
||||||
Widget
|
|
||||||
{{ end }}
|
|
||||||
{{ define "content" }}
|
|
||||||
<h2 class="mt-5">Widget</h2>
|
|
||||||
{{ end }}
|
|
||||||
|
|
@ -1,18 +0,0 @@
|
|||||||
{{ template "base" . }}
|
|
||||||
{{ define "title" }}
|
|
||||||
Payment Succedded!
|
|
||||||
{{ end }}
|
|
||||||
{{ define "content" }}
|
|
||||||
{{$txn := index .Data "txn"}}
|
|
||||||
<h2 class="mt-5">Payment Succeeded</h2>
|
|
||||||
<hr>
|
|
||||||
<p>Payment Intent: {{$txn.PaymentIntentID}}</p>
|
|
||||||
<p>Customer Name: {{ $txn.FirstName }} {{ $txn.LastName }}</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 }}
|
|
17
cmd/web/templates/succeeded.page.gohtml
Normal file
17
cmd/web/templates/succeeded.page.gohtml
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
{{ 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>Customer Name: {{ index .Data "first_name" }} {{ index .Data "last_name" }}</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>
|
||||||
|
<p>Last Four: {{ index .Data "last_four" }}</p>
|
||||||
|
<p>Bank Return Code: {{ index .Data "bank_return_code" }}</p>
|
||||||
|
<p>Expiry Date: {{ index .Data "expiry_month" }}/{{index .Data "expiry_year"}}</p>
|
||||||
|
{{ end }}
|
@ -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="/virtual-terminal-payment-succeeded"
|
<form action="/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="val({{.API}})">Charge Card</a>
|
onclick="termVal()">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,20 +81,14 @@ Virtual Terminal
|
|||||||
<script src="/static/js/stripe.js"></script>
|
<script src="/static/js/stripe.js"></script>
|
||||||
<script>
|
<script>
|
||||||
stripeInit('{{.StripePubKey}}');
|
stripeInit('{{.StripePubKey}}');
|
||||||
document.getElementById("charge_amount").addEventListener("change", (evt) => {
|
function termVal() {
|
||||||
if (evt.target.value !== "") {
|
const chargeAmount = document.getElementById("charge_amount");
|
||||||
document.getElementById("amount").value = parseInt((evt.target.value * 100), 10);
|
if (chargeAmount.value !== "") {
|
||||||
|
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 }}
|
||||||
|
@ -1,18 +0,0 @@
|
|||||||
{{ 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 }}
|
|
||||||
|
|
@ -72,8 +72,6 @@ type Transaction struct {
|
|||||||
LastFour string `json:"last_four"`
|
LastFour string `json:"last_four"`
|
||||||
ExpiryMonth int `json:"expiry_month"`
|
ExpiryMonth int `json:"expiry_month"`
|
||||||
ExpiryYear int `json:"expiry_year"`
|
ExpiryYear int `json:"expiry_year"`
|
||||||
PaymentIntent string `json:"payment_intent"`
|
|
||||||
PaymentMethod string `json:"payment_method"`
|
|
||||||
BankReturnCode string `json:"bank_return_code"`
|
BankReturnCode string `json:"bank_return_code"`
|
||||||
TransactionStatusID int `json:"transaction_status_id"`
|
TransactionStatusID int `json:"transaction_status_id"`
|
||||||
CreatedAt time.Time `json:"-"`
|
CreatedAt time.Time `json:"-"`
|
||||||
@ -136,9 +134,9 @@ func (m *DBModel) InsertTransaction(txn Transaction) (int, error) {
|
|||||||
|
|
||||||
stmt := `INSERT INTO transactions
|
stmt := `INSERT INTO transactions
|
||||||
(amount, currency, last_four, expiry_month, expiry_year,
|
(amount, currency, last_four, expiry_month, expiry_year,
|
||||||
payment_intent, payment_method, bank_return_code,
|
bank_return_code,
|
||||||
transaction_status_id, created_at, updated_at)
|
transaction_status_id, created_at, updated_at)
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`
|
||||||
|
|
||||||
result, err := m.DB.ExecContext(ctx, stmt,
|
result, err := m.DB.ExecContext(ctx, stmt,
|
||||||
txn.Amount,
|
txn.Amount,
|
||||||
@ -146,8 +144,6 @@ func (m *DBModel) InsertTransaction(txn Transaction) (int, error) {
|
|||||||
txn.LastFour,
|
txn.LastFour,
|
||||||
txn.ExpiryMonth,
|
txn.ExpiryMonth,
|
||||||
txn.ExpiryYear,
|
txn.ExpiryYear,
|
||||||
txn.PaymentIntent,
|
|
||||||
txn.PaymentMethod,
|
|
||||||
txn.BankReturnCode,
|
txn.BankReturnCode,
|
||||||
txn.TransactionStatusID,
|
txn.TransactionStatusID,
|
||||||
time.Now(),
|
time.Now(),
|
||||||
|
@ -1,2 +0,0 @@
|
|||||||
drop_column("transactions", "payment_intent")
|
|
||||||
drop_column("transactions", "payment_method")
|
|
@ -1,3 +0,0 @@
|
|||||||
add_column("transactions", "payment_intent", "string", {"default":""})
|
|
||||||
add_column("transactions", "payment_method", "string", {"default":""})
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user