udemy-go-web-2/cmd/web/handlers.go

178 lines
4.1 KiB
Go
Raw Normal View History

package main
2024-08-03 20:26:40 +00:00
import (
"myapp/internal/cards"
2024-08-10 09:10:27 +00:00
"myapp/internal/models"
2024-08-03 20:26:40 +00:00
"net/http"
2024-08-07 09:56:53 +00:00
"strconv"
"github.com/go-chi/chi/v5"
2024-08-03 20:26:40 +00:00
)
2024-08-10 09:10:27 +00:00
func (app *application) Home(w http.ResponseWriter, r *http.Request) {
if err := app.renderTemplate(w, r, "home", &templateData{}); err != nil {
app.errorLog.Println(err)
}
}
func (app *application) VirtualTerminal(w http.ResponseWriter, r *http.Request) {
if err := app.renderTemplate(w, r, "terminal", &templateData{}); err != nil {
2024-08-03 20:26:40 +00:00
app.errorLog.Println(err)
}
}
2024-08-04 16:17:56 +00:00
func (app *application) PaymentSucceeded(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
app.errorLog.Println(err)
return
}
// read posted data
2024-08-10 09:10:27 +00:00
firstName := r.Form.Get("first_name")
lastName := r.Form.Get("last_name")
2024-08-04 16:17:56 +00:00
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")
2024-08-10 09:10:27 +00:00
widgetID, _ := strconv.Atoi(r.Form.Get("product_id"))
2024-08-04 16:17:56 +00:00
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
2024-08-10 09:10:27 +00:00
customerID, err := app.SaveCustomer(firstName, lastName, email)
if err != nil {
app.errorLog.Println(err)
return
}
app.infoLog.Printf("custumer id: %d", customerID)
amount, _ := strconv.Atoi(paymentAmount)
transaction := models.Transaction{
Amount: amount,
Currency: paymentCurrency,
LastFour: lastFour,
ExpiryMonth: int(expiryMonth),
ExpiryYear: int(expiryYear),
BankReturnCode: pi.LatestCharge.ID,
TransactionStatusID: 2, // TODO: use an enum
}
txnID, err := app.SaveTransaction(transaction)
if err != nil {
app.errorLog.Println(err)
return
}
app.infoLog.Printf("transaction id: %d", txnID)
order := models.Order{
WidgetID: widgetID,
TransactionID: txnID,
CustomerID: customerID,
StatusID: 1,
Quantity: 1,
Amount: amount,
}
orderID, err := app.SaveOrder(order)
if err != nil {
app.errorLog.Println(err)
return
}
app.infoLog.Printf("order id: %d", orderID)
2024-08-04 16:17:56 +00:00
data := make(map[string]interface{})
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
2024-08-10 09:10:27 +00:00
data["first_name"] = firstName
data["last_name"] = lastName
// should write this data to session, and then redirect user to the new page
2024-08-04 16:17:56 +00:00
if err := app.renderTemplate(w, r, "succeeded", &templateData{
Data: data,
}); err != nil {
app.errorLog.Println(err)
}
}
2024-08-06 11:29:32 +00:00
2024-08-10 09:10:27 +00:00
func (app *application) SaveCustomer(firstName, lastName, email string) (int, error) {
customer := models.Customer{
FirstName: firstName,
LastName: lastName,
Email: email,
}
id, err := app.DB.InsertCustomer(customer)
if err != nil {
return 0, err
}
return id, nil
}
func (app *application) SaveTransaction(txn models.Transaction) (int, error) {
txnID, err := app.DB.InsertTransaction(txn)
if err != nil {
app.errorLog.Println(err)
return 0, err
}
return txnID, nil
}
func (app *application) SaveOrder(order models.Order) (int, error) {
id, err := app.DB.InsertOrder(order)
if err != nil {
app.errorLog.Println(err)
return 0, err
}
return id, nil
}
2024-08-06 11:29:32 +00:00
// ChargeOnce displays the page to buy one widget
func (app *application) ChargeOnce(w http.ResponseWriter, r *http.Request) {
2024-08-07 09:56:53 +00:00
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 {
2024-08-06 11:29:32 +00:00
app.errorLog.Println(err)
}
}