91 lines
2.1 KiB
Go
91 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"myapp/internal/cards"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
)
|
|
|
|
func (app *application) VirtualTerminal(w http.ResponseWriter, r *http.Request) {
|
|
if err := app.renderTemplate(w, r, "terminal", &templateData{}); err != nil {
|
|
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")
|
|
|
|
card := cards.Card{
|
|
Secret: app.config.stripe.secret,
|
|
Key: app.config.stripe.key,
|
|
}
|
|
|
|
pi, err := card.RetrievePaymentIntent(paymentIntent)
|
|
if err != nil {
|
|
app.errorLog.Println(err)
|
|
return
|
|
}
|
|
|
|
pm, err := card.GetPaymentMethod(paymentMethod)
|
|
if err != nil {
|
|
app.errorLog.Println(err)
|
|
return
|
|
}
|
|
|
|
lastFour := pm.Card.Last4
|
|
expiryMonth := pm.Card.ExpMonth
|
|
expiryYear := pm.Card.ExpYear
|
|
|
|
data := make(map[string]interface{})
|
|
data["cardholder"] = cardHolder
|
|
data["email"] = email
|
|
data["pi"] = paymentIntent
|
|
data["pm"] = paymentMethod
|
|
data["pa"] = paymentAmount
|
|
data["pc"] = paymentCurrency
|
|
data["last_four"] = lastFour
|
|
data["expiry_month"] = expiryMonth
|
|
data["expiry_year"] = expiryYear
|
|
data["bank_return_code"] = pi.LatestCharge.ID
|
|
|
|
if err := app.renderTemplate(w, r, "succeeded", &templateData{
|
|
Data: data,
|
|
}); err != nil {
|
|
app.errorLog.Println(err)
|
|
}
|
|
}
|
|
|
|
// ChargeOnce displays the page to buy one widget
|
|
func (app *application) ChargeOnce(w http.ResponseWriter, r *http.Request) {
|
|
id := chi.URLParam(r, "id")
|
|
widgetID, _ := strconv.Atoi(id)
|
|
|
|
widget, err := app.DB.GetWidget(widgetID)
|
|
if err != nil {
|
|
app.errorLog.Println(err)
|
|
}
|
|
|
|
data := make(map[string]interface{})
|
|
data["widget"] = widget
|
|
|
|
if err := app.renderTemplate(w, r, "buy-once", &templateData{
|
|
Data: data,
|
|
}); err != nil {
|
|
app.errorLog.Println(err)
|
|
}
|
|
}
|