Compare commits
1 Commits
main
...
e891b39dc2
Author | SHA1 | Date | |
---|---|---|---|
e891b39dc2 |
2
.gitignore
vendored
2
.gitignore
vendored
@ -5,5 +5,3 @@ docker/docker-compose.yml
|
||||
cred.txt
|
||||
dist/
|
||||
.air.toml
|
||||
tmp/
|
||||
invoices/
|
||||
|
28
Makefile
28
Makefile
@ -1,11 +1,11 @@
|
||||
STRIPE_SECRET=$(shell sed '2q;d' cred.txt)
|
||||
STRIPE_KEY=$(shell sed '1q;d' cred.txt)
|
||||
STRIPE_KEY=$(shell sed '2q;d' cred.txt)
|
||||
GOSTRIPE_PORT=4000
|
||||
API_PORT=4001
|
||||
DSN=vinchent:secret@tcp(localhost:3306)/widgets?parseTime=true&tls=false
|
||||
DSN=root@tcp(localhost:6379)/widgets?parseTime=true&tls=false
|
||||
|
||||
## build: builds all binaries
|
||||
build: clean build_front build_back build_invoice
|
||||
build: clean build_front build_back
|
||||
@printf "All binaries built!\n"
|
||||
|
||||
## clean: cleans all binaries and runs go clean
|
||||
@ -15,12 +15,6 @@ clean:
|
||||
@go clean
|
||||
@echo "Cleaned!"
|
||||
|
||||
## build_invoice: builds the invoice microservice
|
||||
build_invoice:
|
||||
@echo "Building invoice microservice..."
|
||||
@go build -o dist/invoice ./cmd/micro/invoice
|
||||
@echo "Invoice microservice built!"
|
||||
|
||||
## build_front: builds the front end
|
||||
build_front:
|
||||
@echo "Building front end..."
|
||||
@ -34,13 +28,7 @@ build_back:
|
||||
@echo "Back end built!"
|
||||
|
||||
## start: starts front and back end
|
||||
start: start_front start_back start_invoice
|
||||
|
||||
## start_invoice: starts the invoice microservice
|
||||
start_invoice: build_invoice
|
||||
@echo "Starting the invoice microservice..."
|
||||
@./dist/invoice &
|
||||
@echo "invoice microservice running!"
|
||||
start: start_front start_back
|
||||
|
||||
## start_front: starts the front end
|
||||
start_front: build_front
|
||||
@ -55,15 +43,9 @@ start_back: build_back
|
||||
@echo "Back end running!"
|
||||
|
||||
## stop: stops the front and back end
|
||||
stop: stop_front stop_back stop_invoice
|
||||
stop: stop_front stop_back
|
||||
@echo "All applications stopped"
|
||||
|
||||
## stop_invoice: stops the invoice microservice
|
||||
stop_invoice:
|
||||
@echo "Stopping the invoice microservice..."
|
||||
@-pkill -SIGTERM -f "invoice"
|
||||
@echo "Stopped invoice microservice"
|
||||
|
||||
## stop_front: stops the front end
|
||||
stop_front:
|
||||
@echo "Stopping the front end..."
|
||||
|
@ -5,7 +5,6 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"myapp/internal/driver"
|
||||
"myapp/internal/models"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
@ -25,14 +24,6 @@ type config struct {
|
||||
secret string
|
||||
key string
|
||||
}
|
||||
smtp struct {
|
||||
host string
|
||||
port int
|
||||
username string
|
||||
password string
|
||||
}
|
||||
secretkey string
|
||||
frontend string
|
||||
}
|
||||
|
||||
type application struct {
|
||||
@ -40,7 +31,6 @@ type application struct {
|
||||
infoLog *log.Logger
|
||||
errorLog *log.Logger
|
||||
version string
|
||||
DB models.DBModel
|
||||
}
|
||||
|
||||
func (app *application) serve() error {
|
||||
@ -77,12 +67,6 @@ func main() {
|
||||
"root:example@tcp(localhost:3306)/widgets?parseTime=true&tls=false",
|
||||
"Application environment {development|production}",
|
||||
)
|
||||
flag.StringVar(&cfg.smtp.host, "smtphost", "0.0.0.0", "smtp host")
|
||||
flag.IntVar(&cfg.smtp.port, "smtpport", 1025, "smtp host")
|
||||
flag.StringVar(&cfg.smtp.username, "smtpuser", "user", "smtp user")
|
||||
flag.StringVar(&cfg.smtp.password, "smtppwd", "password", "smtp password")
|
||||
flag.StringVar(&cfg.secretkey, "secretkey", "b47df3d8380241c1177f13bdd69c6a60", "secret key")
|
||||
flag.StringVar(&cfg.frontend, "frontend", "http://localhost:4000", "frontend address")
|
||||
|
||||
flag.Parse()
|
||||
|
||||
@ -103,7 +87,6 @@ func main() {
|
||||
config: cfg,
|
||||
infoLog: infoLog,
|
||||
errorLog: errorLog,
|
||||
DB: models.DBModel{DB: conn},
|
||||
}
|
||||
|
||||
app.infoLog.Println("Connected to MariaDB")
|
||||
|
@ -1,41 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"myapp/internal/cards"
|
||||
"myapp/internal/cards/encryption"
|
||||
"myapp/internal/models"
|
||||
"myapp/internal/urlsigner"
|
||||
"myapp/internal/validator"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/stripe/stripe-go/v79"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
type stripePayload struct {
|
||||
Currency string `json:"currency"`
|
||||
Amount string `json:"amount"`
|
||||
PaymentMethod string `json:"payment_method"`
|
||||
Email string `json:"email"`
|
||||
CardBrand string `json:"card_brand"`
|
||||
ExpiryMonth int `json:"expiry_month"`
|
||||
ExpiryYear int `json:"expiry_year"`
|
||||
LastFour string `json:"last_four"`
|
||||
Plan string `json:"plan"`
|
||||
ProductID string `json:"product_id"`
|
||||
FirstName string `json:"first_name"`
|
||||
LastName string `json:"last_name"`
|
||||
}
|
||||
|
||||
type JSONResponse struct {
|
||||
type jsonResponse struct {
|
||||
OK bool `json:"ok"`
|
||||
Message string `json:"message,omitempty"`
|
||||
Content string `json:"content,omitempty"`
|
||||
@ -65,7 +42,7 @@ func (app *application) GetPaymentIntent(w http.ResponseWriter, r *http.Request)
|
||||
|
||||
pi, msg, err := card.Charge(payload.Currency, amount)
|
||||
if err != nil {
|
||||
j := JSONResponse{
|
||||
j := jsonResponse{
|
||||
OK: false,
|
||||
Message: msg,
|
||||
Content: "",
|
||||
@ -88,769 +65,3 @@ func (app *application) GetPaymentIntent(w http.ResponseWriter, r *http.Request)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(out)
|
||||
}
|
||||
|
||||
func (app *application) GetWidgetByID(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)
|
||||
}
|
||||
out, err := json.MarshalIndent(widget, "", " ")
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(out)
|
||||
}
|
||||
|
||||
type Invoice struct {
|
||||
ID int `json:"id"`
|
||||
Quantity int `json:"quantity"`
|
||||
Amount int `json:"amount"`
|
||||
Product string `json:"product"`
|
||||
FirstName string `json:"first_name"`
|
||||
LastName string `json:"last_name"`
|
||||
Email string `json:"email"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
func (app *application) CreateCustomerAndSubscribeToPlan(w http.ResponseWriter, r *http.Request) {
|
||||
var data stripePayload
|
||||
|
||||
err := json.NewDecoder(r.Body).Decode(&data)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
// validate data
|
||||
v := validator.New()
|
||||
v.Check(len(data.FirstName) > 1, "first_name", "must be at least 2 characters")
|
||||
if !v.Valid() {
|
||||
app.failedValidation(w, r, v.Errors)
|
||||
return
|
||||
}
|
||||
|
||||
app.infoLog.Println(data.Email, data.LastFour, data.PaymentMethod, data.Plan)
|
||||
|
||||
card := cards.Card{
|
||||
Secret: app.config.stripe.secret,
|
||||
Key: app.config.stripe.key,
|
||||
Currency: data.Currency,
|
||||
}
|
||||
|
||||
okay := true
|
||||
var subscription *stripe.Subscription
|
||||
txnMsg := "Transaction successful"
|
||||
|
||||
stripeCustomer, msg, err := card.CreateCustomer(data.PaymentMethod, data.Email)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
okay = false
|
||||
txnMsg = msg
|
||||
}
|
||||
|
||||
if okay {
|
||||
subscription, err = card.SubscribeToPlan(
|
||||
stripeCustomer,
|
||||
data.Plan,
|
||||
data.Email,
|
||||
data.LastFour,
|
||||
"",
|
||||
)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
okay = false
|
||||
txnMsg = "Error subscribing customer"
|
||||
}
|
||||
app.infoLog.Println("subscription id is", subscription.ID)
|
||||
}
|
||||
|
||||
if okay {
|
||||
productID, _ := strconv.Atoi(data.ProductID)
|
||||
customerID, err := app.SaveCustomer(data.FirstName, data.LastName, data.Email)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
// create a new txn
|
||||
amount, _ := strconv.Atoi(data.Amount)
|
||||
txn := models.Transaction{
|
||||
Amount: amount,
|
||||
Currency: "eur",
|
||||
LastFour: data.LastFour,
|
||||
ExpiryMonth: data.ExpiryMonth,
|
||||
ExpiryYear: data.ExpiryYear,
|
||||
TransactionStatusID: 2,
|
||||
PaymentIntent: subscription.ID,
|
||||
PaymentMethod: data.PaymentMethod,
|
||||
}
|
||||
txnID, err := app.SaveTransaction(txn)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
// create order
|
||||
order := models.Order{
|
||||
WidgetID: productID,
|
||||
TransactionID: txnID,
|
||||
CustomerID: customerID,
|
||||
StatusID: 1,
|
||||
Quantity: 1,
|
||||
Amount: amount,
|
||||
}
|
||||
|
||||
orderID, err := app.SaveOrder(order)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
inv := Invoice{
|
||||
ID: orderID,
|
||||
Quantity: order.Quantity,
|
||||
Amount: order.Amount,
|
||||
Product: "Bronze Plan",
|
||||
FirstName: data.FirstName,
|
||||
LastName: data.LastName,
|
||||
Email: data.Email,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
err = app.callInvoiceMicro(inv)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
// Don't stop the program
|
||||
}
|
||||
}
|
||||
|
||||
resp := JSONResponse{
|
||||
OK: okay,
|
||||
Message: txnMsg,
|
||||
}
|
||||
|
||||
out, err := json.MarshalIndent(resp, "", " ")
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(out)
|
||||
}
|
||||
|
||||
func (app *application) callInvoiceMicro(inv Invoice) error {
|
||||
// TODO: Do not hard code this.
|
||||
url := "http://localhost:5000/invoice/create-and-send"
|
||||
out, err := json.MarshalIndent(inv, "", "\t")
|
||||
if err != err {
|
||||
return err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(out))
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
app.infoLog.Println(resp.Body)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func (app *application) CreateAuthToken(w http.ResponseWriter, r *http.Request) {
|
||||
var userInput struct {
|
||||
Email string `json:"email"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
err := app.readJSON(w, r, &userInput)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
// get the user from the db by email, send error if invalid email
|
||||
user, err := app.DB.GetUserByEmail(userInput.Email)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
app.invalidCredentials(w)
|
||||
return
|
||||
}
|
||||
|
||||
// validate the password, send error if invalid password
|
||||
validPassword, err := app.passwordMatches(user.Password, userInput.Password)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
app.invalidCredentials(w)
|
||||
return
|
||||
}
|
||||
|
||||
if !validPassword {
|
||||
app.invalidCredentials(w)
|
||||
return
|
||||
}
|
||||
|
||||
// generate the token
|
||||
token, err := models.GenerateToken(user.ID, 24*time.Hour, models.ScopeAuthentication)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
// save to DB
|
||||
err = app.DB.InsertToken(token, user)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
// send response
|
||||
|
||||
var payload struct {
|
||||
OK bool `json:"ok"`
|
||||
Message string `json:"message"`
|
||||
Token *models.Token `json:"authentication_token"`
|
||||
}
|
||||
|
||||
payload.OK = true
|
||||
payload.Message = fmt.Sprintf("token for %s created", userInput.Email)
|
||||
payload.Token = token
|
||||
|
||||
_ = app.writeJSON(w, http.StatusOK, payload)
|
||||
}
|
||||
|
||||
func (app *application) authenticateToken(r *http.Request) (*models.User, error) {
|
||||
authorizationHeader := r.Header.Get("Authorization")
|
||||
if authorizationHeader == "" {
|
||||
return nil, errors.New("no authorization header received")
|
||||
}
|
||||
|
||||
headerParts := strings.Split(authorizationHeader, " ")
|
||||
if len(headerParts) != 2 || headerParts[0] != "Bearer" {
|
||||
return nil, errors.New("no authorization header received")
|
||||
}
|
||||
|
||||
token := headerParts[1]
|
||||
if len(token) != 26 {
|
||||
return nil, errors.New("authentication token wrong size")
|
||||
}
|
||||
|
||||
// get the user from the tokens table
|
||||
user, err := app.DB.GetUserForToken(token)
|
||||
if err != nil {
|
||||
return nil, errors.New("no matching user found")
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (app *application) CheckAuthentication(w http.ResponseWriter, r *http.Request) {
|
||||
// validate the token, and get associated user
|
||||
user, err := app.authenticateToken(r)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
app.invalidCredentials(w)
|
||||
return
|
||||
}
|
||||
|
||||
// valid user
|
||||
var payload JSONResponse
|
||||
payload.OK = true
|
||||
payload.Message = fmt.Sprintf("authenticated user %s", user.Email)
|
||||
app.writeJSON(w, http.StatusOK, payload)
|
||||
}
|
||||
|
||||
func (app *application) VirtualTerminalPaymentSucceeded(w http.ResponseWriter, r *http.Request) {
|
||||
var txnData struct {
|
||||
PaymentAmount int `json:"amount"`
|
||||
PaymentCurrency string `json:"currency"`
|
||||
FirstName string `json:"first_name"`
|
||||
LastName string `json:"last_name"`
|
||||
Email string `json:"email"`
|
||||
PaymentIntent string `json:"payment_intent"`
|
||||
PaymentMethod string `json:"payment_method"`
|
||||
BankReturnCode string `json:"bank_return_code"`
|
||||
ExpiryMonth int `json:"expiry_month"`
|
||||
ExpiryYear int `json:"expiry_year"`
|
||||
LastFour string `json:"last_four"`
|
||||
}
|
||||
|
||||
err := app.readJSON(w, r, &txnData)
|
||||
if err != nil {
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
card := cards.Card{
|
||||
Secret: app.config.stripe.secret,
|
||||
Key: app.config.stripe.key,
|
||||
}
|
||||
|
||||
pi, err := card.RetrievePaymentIntent(txnData.PaymentIntent)
|
||||
if err != nil {
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
pm, err := card.GetPaymentMethod(txnData.PaymentMethod)
|
||||
if err != nil {
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
txnData.LastFour = pm.Card.Last4
|
||||
txnData.ExpiryMonth = int(pm.Card.ExpMonth)
|
||||
txnData.ExpiryYear = int(pm.Card.ExpYear)
|
||||
|
||||
txn := models.Transaction{
|
||||
Amount: txnData.PaymentAmount,
|
||||
Currency: txnData.PaymentCurrency,
|
||||
LastFour: txnData.LastFour,
|
||||
ExpiryMonth: txnData.ExpiryMonth,
|
||||
ExpiryYear: txnData.ExpiryYear,
|
||||
BankReturnCode: pi.LatestCharge.ID,
|
||||
PaymentIntent: txnData.PaymentIntent,
|
||||
PaymentMethod: txnData.PaymentMethod,
|
||||
TransactionStatusID: 2,
|
||||
}
|
||||
|
||||
_, err = app.SaveTransaction(txn)
|
||||
if err != nil {
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
app.writeJSON(w, http.StatusOK, txn)
|
||||
}
|
||||
|
||||
func (app *application) SendPasswordResetEmail(w http.ResponseWriter, r *http.Request) {
|
||||
var payload struct {
|
||||
Email string `json:"email"`
|
||||
}
|
||||
|
||||
err := app.readJSON(w, r, &payload)
|
||||
if err != nil {
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
// verify that email exists
|
||||
_, err = app.DB.GetUserByEmail(payload.Email)
|
||||
if err != nil {
|
||||
resp := JSONResponse{
|
||||
OK: false,
|
||||
Message: "No matching email found on our system",
|
||||
}
|
||||
app.writeJSON(w, http.StatusAccepted, resp)
|
||||
return
|
||||
}
|
||||
|
||||
link := fmt.Sprintf("%s/reset-password?email=%s", app.config.frontend, payload.Email)
|
||||
sign := urlsigner.Signer{
|
||||
Secret: []byte(app.config.secretkey),
|
||||
}
|
||||
|
||||
signedLink := sign.GenerateTokenFromString(link)
|
||||
|
||||
var data struct {
|
||||
Link string
|
||||
}
|
||||
|
||||
data.Link = signedLink
|
||||
|
||||
// send mail
|
||||
err = app.SendMail(
|
||||
"info@widgets.com",
|
||||
payload.Email,
|
||||
"Password Reset Request",
|
||||
"password-reset",
|
||||
data,
|
||||
)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
resp := JSONResponse{
|
||||
OK: true,
|
||||
}
|
||||
|
||||
app.writeJSON(w, http.StatusCreated, resp)
|
||||
}
|
||||
|
||||
func (app *application) ResetPassword(w http.ResponseWriter, r *http.Request) {
|
||||
var payload struct {
|
||||
Email string `json:"email"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
err := app.readJSON(w, r, &payload)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
encryptor := encryption.Encryption{
|
||||
Key: []byte(app.config.secretkey),
|
||||
}
|
||||
|
||||
realEmail, err := encryptor.Decrypt(payload.Email)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
user, err := app.DB.GetUserByEmail(realEmail)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
newHash, err := bcrypt.GenerateFromPassword([]byte(payload.Password), 12)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
err = app.DB.UpdatePasswordForUser(user, string(newHash))
|
||||
if err != nil {
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
resp := JSONResponse{
|
||||
OK: true,
|
||||
Message: "Password reset.",
|
||||
}
|
||||
app.writeJSON(w, http.StatusCreated, resp)
|
||||
}
|
||||
|
||||
func (app *application) AllSales(w http.ResponseWriter, r *http.Request) {
|
||||
var payload struct {
|
||||
PageSize int `json:"page_size"`
|
||||
CurrentPage int `json:"page"`
|
||||
}
|
||||
err := app.readJSON(w, r, &payload)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
allSales, lastPage, totalRecords, err := app.DB.GetAllOrdersPaginated(
|
||||
false,
|
||||
payload.PageSize,
|
||||
payload.CurrentPage,
|
||||
)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
var resp struct {
|
||||
CurrentPage int `json:"current_page"`
|
||||
PageSize int `json:"page_size"`
|
||||
LastPage int `json:"last_page"`
|
||||
TotalRecords int `json:"total_records"`
|
||||
Orders []*models.Order `json:"orders"`
|
||||
}
|
||||
|
||||
resp.CurrentPage = payload.CurrentPage
|
||||
resp.PageSize = payload.PageSize
|
||||
resp.LastPage = lastPage
|
||||
resp.TotalRecords = totalRecords
|
||||
resp.Orders = allSales
|
||||
|
||||
app.writeJSON(w, http.StatusOK, resp)
|
||||
}
|
||||
|
||||
func (app *application) AllSubscriptions(w http.ResponseWriter, r *http.Request) {
|
||||
var payload struct {
|
||||
PageSize int `json:"page_size"`
|
||||
CurrentPage int `json:"page"`
|
||||
}
|
||||
err := app.readJSON(w, r, &payload)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
allSales, lastPage, totalRecords, err := app.DB.GetAllOrdersPaginated(
|
||||
true,
|
||||
payload.PageSize,
|
||||
payload.CurrentPage,
|
||||
)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
var resp struct {
|
||||
CurrentPage int `json:"current_page"`
|
||||
PageSize int `json:"page_size"`
|
||||
LastPage int `json:"last_page"`
|
||||
TotalRecords int `json:"total_records"`
|
||||
Orders []*models.Order `json:"orders"`
|
||||
}
|
||||
|
||||
resp.CurrentPage = payload.CurrentPage
|
||||
resp.PageSize = payload.PageSize
|
||||
resp.LastPage = lastPage
|
||||
resp.TotalRecords = totalRecords
|
||||
resp.Orders = allSales
|
||||
|
||||
app.writeJSON(w, http.StatusOK, resp)
|
||||
}
|
||||
|
||||
func (app *application) GetSale(w http.ResponseWriter, r *http.Request) {
|
||||
id := chi.URLParam(r, "id")
|
||||
orderID, _ := strconv.Atoi(id)
|
||||
|
||||
order, err := app.DB.GetOrderByID(orderID)
|
||||
if err != nil {
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
app.writeJSON(w, http.StatusOK, order)
|
||||
}
|
||||
|
||||
func (app *application) RefundCharge(w http.ResponseWriter, r *http.Request) {
|
||||
var chargeToRefund struct {
|
||||
ID int `json:"id"`
|
||||
PaymentIntent string `json:"pi"`
|
||||
Amount int `json:"amount"`
|
||||
Currency string `json:"currency"`
|
||||
}
|
||||
|
||||
err := app.readJSON(w, r, &chargeToRefund)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
// validate
|
||||
|
||||
card := cards.Card{
|
||||
Secret: app.config.stripe.secret,
|
||||
Key: app.config.stripe.key,
|
||||
Currency: chargeToRefund.Currency,
|
||||
}
|
||||
|
||||
err = card.Refund(chargeToRefund.PaymentIntent, chargeToRefund.Amount)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
// update status in DB
|
||||
err = app.DB.UpdateOrderStatus(chargeToRefund.ID, 2)
|
||||
if err != nil {
|
||||
app.badRequest(
|
||||
w,
|
||||
r,
|
||||
errors.New("the charge was refunded, but the database could not be updated"),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
var resp JSONResponse
|
||||
resp.OK = true
|
||||
resp.Message = "Charge refunded"
|
||||
|
||||
app.writeJSON(w, http.StatusOK, resp)
|
||||
}
|
||||
|
||||
func (app *application) CancelSubscription(w http.ResponseWriter, r *http.Request) {
|
||||
var subToCancel struct {
|
||||
ID int `json:"id"`
|
||||
PaymentIntent string `json:"pi"`
|
||||
Currency string `json:"currency"`
|
||||
}
|
||||
|
||||
err := app.readJSON(w, r, &subToCancel)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
// validate
|
||||
|
||||
card := cards.Card{
|
||||
Secret: app.config.stripe.secret,
|
||||
Key: app.config.stripe.key,
|
||||
Currency: subToCancel.Currency,
|
||||
}
|
||||
|
||||
err = card.CancelSubscription(subToCancel.PaymentIntent)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
// update status in DB
|
||||
err = app.DB.UpdateOrderStatus(subToCancel.ID, 3)
|
||||
if err != nil {
|
||||
app.badRequest(
|
||||
w,
|
||||
r,
|
||||
errors.New("the subscription was refunded, but the database could not be updated"),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
var resp JSONResponse
|
||||
resp.OK = true
|
||||
resp.Message = "Subscription canceled"
|
||||
|
||||
app.writeJSON(w, http.StatusOK, resp)
|
||||
}
|
||||
|
||||
func (app *application) AllUsers(w http.ResponseWriter, r *http.Request) {
|
||||
allUsers, err := app.DB.GetAllUsers()
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
app.writeJSON(w, http.StatusOK, allUsers)
|
||||
}
|
||||
|
||||
func (app *application) OneUser(w http.ResponseWriter, r *http.Request) {
|
||||
id := chi.URLParam(r, "id")
|
||||
userID, _ := strconv.Atoi(id)
|
||||
user, err := app.DB.GetOneUser(userID)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
app.writeJSON(w, http.StatusOK, user)
|
||||
}
|
||||
|
||||
func (app *application) EditUser(w http.ResponseWriter, r *http.Request) {
|
||||
id := chi.URLParam(r, "id")
|
||||
userID, _ := strconv.Atoi(id)
|
||||
|
||||
var user models.User
|
||||
err := app.readJSON(w, r, &user)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
if userID > 0 {
|
||||
err = app.DB.EditUser(user)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
if user.Password != "" {
|
||||
newHash, err := bcrypt.GenerateFromPassword([]byte(user.Password), 12)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
err = app.DB.UpdatePasswordForUser(user, string(newHash))
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
} else {
|
||||
newHash, err := bcrypt.GenerateFromPassword([]byte(user.Password), 12)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
err = app.DB.AddUser(user, string(newHash))
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
var resp JSONResponse
|
||||
resp.OK = true
|
||||
app.writeJSON(w, http.StatusOK, resp)
|
||||
}
|
||||
|
||||
func (app *application) DeleteUser(w http.ResponseWriter, r *http.Request) {
|
||||
id := chi.URLParam(r, "id")
|
||||
userID, _ := strconv.Atoi(id)
|
||||
err := app.DB.DeleteUser(userID)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
var resp JSONResponse
|
||||
resp.OK = true
|
||||
app.writeJSON(w, http.StatusOK, resp)
|
||||
}
|
||||
|
@ -1,114 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
func (app *application) readJSON(w http.ResponseWriter, r *http.Request, data interface{}) error {
|
||||
maxBytes := 1048576
|
||||
|
||||
r.Body = http.MaxBytesReader(w, r.Body, int64(maxBytes))
|
||||
|
||||
dec := json.NewDecoder(r.Body)
|
||||
err := dec.Decode(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Make sure there is only one entry.
|
||||
err = dec.Decode(&struct{}{})
|
||||
if err != io.EOF {
|
||||
return errors.New("body must only have a single JSON value")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// writeJSON writes arbitrary data out as JSON
|
||||
func (app *application) writeJSON(
|
||||
w http.ResponseWriter,
|
||||
status int, data interface{},
|
||||
headers ...http.Header,
|
||||
) error {
|
||||
out, err := json.MarshalIndent(data, "", "\t")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(headers) > 0 {
|
||||
for k, v := range headers[0] {
|
||||
w.Header()[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
w.Write(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (app *application) badRequest(w http.ResponseWriter, r *http.Request, err error) error {
|
||||
var payload JSONResponse
|
||||
|
||||
payload.OK = false
|
||||
payload.Message = err.Error()
|
||||
|
||||
out, err := json.MarshalIndent(payload, "", "\t")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusBadGateway)
|
||||
w.Write(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (app *application) invalidCredentials(w http.ResponseWriter) error {
|
||||
var payload JSONResponse
|
||||
payload.OK = false
|
||||
payload.Message = "invalid authentication credentials"
|
||||
|
||||
err := app.writeJSON(w, http.StatusUnauthorized, payload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (app *application) passwordMatches(hash, password string) (bool, error) {
|
||||
app.errorLog.Println(hash, password)
|
||||
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, bcrypt.ErrMismatchedHashAndPassword):
|
||||
return false, nil
|
||||
default:
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (app *application) failedValidation(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
errors map[string]string,
|
||||
) {
|
||||
var payload struct {
|
||||
OK bool `json:"ok"`
|
||||
Message string `json:"message"`
|
||||
Errors map[string]string `json:"errors"`
|
||||
}
|
||||
|
||||
payload.OK = false
|
||||
payload.Message = "failed validation"
|
||||
payload.Errors = errors
|
||||
app.writeJSON(w, http.StatusUnprocessableEntity, payload)
|
||||
}
|
@ -1,78 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"embed"
|
||||
"fmt"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
mail "github.com/xhit/go-simple-mail/v2"
|
||||
)
|
||||
|
||||
//go:embed templates
|
||||
var emailTemplateFS embed.FS
|
||||
|
||||
func (app *application) SendMail(from, to, subject, tmpl string, data interface{}) error {
|
||||
templateToRender := fmt.Sprintf("templates/%s.html.gohtml", tmpl)
|
||||
|
||||
t, err := template.New("email-html").ParseFS(emailTemplateFS, templateToRender)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
var tpl bytes.Buffer
|
||||
if err = t.ExecuteTemplate(&tpl, "body", data); err != nil {
|
||||
app.errorLog.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
formattedMessage := tpl.String()
|
||||
|
||||
templateToRender = fmt.Sprintf("templates/%s.plain.gohtml", tmpl)
|
||||
t, err = template.New("email-plain").ParseFS(emailTemplateFS, templateToRender)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
return err
|
||||
}
|
||||
if err = t.ExecuteTemplate(&tpl, "body", data); err != nil {
|
||||
app.errorLog.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
plainMessage := tpl.String()
|
||||
|
||||
app.infoLog.Println(formattedMessage, plainMessage)
|
||||
|
||||
// send the mail
|
||||
server := mail.NewSMTPClient()
|
||||
server.Host = app.config.smtp.host
|
||||
server.Port = app.config.smtp.port
|
||||
// NOTE: not needed for MailHog
|
||||
// server.Username = app.config.smtp.username
|
||||
// server.Password = app.config.smtp.password
|
||||
// server.Encryption = mail.EncryptionTLS
|
||||
server.KeepAlive = false
|
||||
server.ConnectTimeout = 10 * time.Second
|
||||
server.SendTimeout = 10 * time.Second
|
||||
|
||||
smtpClient, err := server.Connect()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
email := mail.NewMSG()
|
||||
email.SetFrom(from).AddTo(to).SetSubject(subject)
|
||||
email.SetBody(mail.TextHTML, formattedMessage)
|
||||
email.AddAlternative(mail.TextPlain, plainMessage)
|
||||
|
||||
err = email.Send(smtpClient)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
app.infoLog.Println("send mail")
|
||||
|
||||
return nil
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
package main
|
||||
|
||||
import "net/http"
|
||||
|
||||
func (app *application) Auth(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
_, err := app.authenticateToken(r)
|
||||
if err != nil {
|
||||
app.invalidCredentials(w)
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
@ -19,31 +19,5 @@ func (app *application) routes() http.Handler {
|
||||
}))
|
||||
|
||||
mux.Post("/api/payment-intent", app.GetPaymentIntent)
|
||||
mux.Get("/api/widget/{id}", app.GetWidgetByID)
|
||||
mux.Post("/api/create-customer-and-subscribe-to-plan", app.CreateCustomerAndSubscribeToPlan)
|
||||
|
||||
mux.Post("/api/authenticate", app.CreateAuthToken)
|
||||
mux.Post("/api/is-authenticated", app.CheckAuthentication)
|
||||
mux.Route("/api/admin", func(mux chi.Router) {
|
||||
mux.Use(app.Auth)
|
||||
|
||||
// mux.Get("/test", func(w http.ResponseWriter, r *http.Request) {
|
||||
// w.Write([]byte("got in"))
|
||||
// })
|
||||
|
||||
mux.Post("/virtual-terminal-succeeded", app.VirtualTerminalPaymentSucceeded)
|
||||
mux.Post("/all-sales", app.AllSales)
|
||||
mux.Post("/all-subscriptions", app.AllSubscriptions)
|
||||
mux.Post("/get-sale/{id}", app.GetSale)
|
||||
mux.Post("/refund", app.RefundCharge)
|
||||
mux.Post("/cancel-subscription", app.CancelSubscription)
|
||||
mux.Post("/all-users", app.AllUsers)
|
||||
mux.Post("/all-users/{id}", app.OneUser)
|
||||
mux.Post("/all-users/edit/{id}", app.EditUser)
|
||||
mux.Post("/all-users/delete/{id}", app.DeleteUser)
|
||||
})
|
||||
mux.Post("/api/forgot-password", app.SendPasswordResetEmail)
|
||||
mux.Post("/api/reset-password", app.ResetPassword)
|
||||
|
||||
return mux
|
||||
}
|
||||
|
@ -1,23 +0,0 @@
|
||||
{{ define "body" }}
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta name="viewport" content="width=device-width">
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
||||
</head>
|
||||
<body>
|
||||
<p>Hello:</p>
|
||||
<p>You recently requested a link to reset your password.</p>
|
||||
<p>Click on the link below to get started:</p>
|
||||
<p>
|
||||
<a href="{{.Link}}">{{.Link}}</a>
|
||||
</p>
|
||||
<p>This link expires in 10 minutes.</p>
|
||||
<p>
|
||||
--
|
||||
<br>
|
||||
Widget Co.
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
{{ end }}
|
@ -1,14 +0,0 @@
|
||||
{{ define "body" }}
|
||||
Hello:
|
||||
|
||||
You recently requested a link to reset your password.
|
||||
|
||||
Click on the link below to get started:
|
||||
|
||||
{{.Link}}
|
||||
|
||||
This link expires in 10 minutes.
|
||||
|
||||
--
|
||||
Widget Co.
|
||||
{{ end }}
|
@ -1,18 +0,0 @@
|
||||
{{ define "body" }}
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta name="viewport" content="width=device-width">
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
||||
</head>
|
||||
<body>
|
||||
<p>Hello:</p>
|
||||
<p>Please find your invoice attached.</p>
|
||||
<p>
|
||||
--
|
||||
<br>
|
||||
Widget Co.
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
{{ end }}
|
@ -1,9 +0,0 @@
|
||||
{{ define "body" }}
|
||||
Hello:
|
||||
|
||||
Please find your invoice attached.
|
||||
|
||||
--
|
||||
Widget Co.
|
||||
{{ end }}
|
||||
|
@ -1,86 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
type JSONResponse struct {
|
||||
OK bool `json:"ok"`
|
||||
Message string `json:"message,omitempty"`
|
||||
}
|
||||
|
||||
func (app *application) readJSON(w http.ResponseWriter, r *http.Request, data interface{}) error {
|
||||
maxBytes := 1048576
|
||||
|
||||
r.Body = http.MaxBytesReader(w, r.Body, int64(maxBytes))
|
||||
|
||||
dec := json.NewDecoder(r.Body)
|
||||
err := dec.Decode(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Make sure there is only one entry.
|
||||
err = dec.Decode(&struct{}{})
|
||||
if err != io.EOF {
|
||||
return errors.New("body must only have a single JSON value")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// writeJSON writes arbitrary data out as JSON
|
||||
func (app *application) writeJSON(
|
||||
w http.ResponseWriter,
|
||||
status int, data interface{},
|
||||
headers ...http.Header,
|
||||
) error {
|
||||
out, err := json.MarshalIndent(data, "", "\t")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(headers) > 0 {
|
||||
for k, v := range headers[0] {
|
||||
w.Header()[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
w.Write(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (app *application) badRequest(w http.ResponseWriter, r *http.Request, err error) error {
|
||||
var payload JSONResponse
|
||||
|
||||
payload.OK = false
|
||||
payload.Message = err.Error()
|
||||
|
||||
out, err := json.MarshalIndent(payload, "", "\t")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusBadGateway)
|
||||
w.Write(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (app *application) CreateDirIfNotExist(path string) error {
|
||||
const mode = 0755
|
||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||
err := os.Mkdir(path, mode)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
@ -1,159 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/phpdave11/gofpdf"
|
||||
"github.com/phpdave11/gofpdf/contrib/gofpdi"
|
||||
)
|
||||
|
||||
type Order struct {
|
||||
ID int `json:"id"`
|
||||
Quantity int `json:"quantity"`
|
||||
Amount int `json:"amount"`
|
||||
Product string `json:"product"`
|
||||
FirstName string `json:"first_name"`
|
||||
LastName string `json:"last_name"`
|
||||
Email string `json:"email"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
func (app *application) CreateAndSendInvoice(w http.ResponseWriter, r *http.Request) {
|
||||
// receive json
|
||||
var order Order
|
||||
|
||||
err := app.readJSON(w, r, &order)
|
||||
if err != nil {
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
app.infoLog.Println(order)
|
||||
|
||||
// generate a pdf invoice
|
||||
err = app.createInvoicePDF(order)
|
||||
if err != nil {
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
// create mail attachment
|
||||
attachments := []string{
|
||||
fmt.Sprintf("./invoices/%d.pdf", order.ID),
|
||||
}
|
||||
|
||||
// send mail with attachment
|
||||
err = app.SendMail("info@widgets.com", order.Email, "Your invoice", "invoice", attachments, nil)
|
||||
if err != nil {
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
// send response
|
||||
var resp JSONResponse
|
||||
resp.OK = true
|
||||
resp.Message = fmt.Sprintf("Invoice %d.pdf created and sent to %s", order.ID, order.Email)
|
||||
app.writeJSON(w, http.StatusCreated, resp)
|
||||
}
|
||||
|
||||
func (app *application) createInvoicePDF(order Order) error {
|
||||
pdf := gofpdf.New("P", "mm", "Letter", "")
|
||||
pdf.SetMargins(10, 13, 10)
|
||||
pdf.SetAutoPageBreak(true, 0)
|
||||
|
||||
importer := gofpdi.NewImporter()
|
||||
|
||||
t := importer.ImportPage(pdf, "./pdf-templates/invoice.pdf", 1, "/MediaBox")
|
||||
|
||||
pdf.AddPage()
|
||||
importer.UseImportedTemplate(pdf, t, 0, 0, 215.9, 0)
|
||||
|
||||
pdf.SetY(20)
|
||||
|
||||
// write info
|
||||
pdf.SetY(50)
|
||||
pdf.SetX(10)
|
||||
pdf.SetFont("Times", "", 11)
|
||||
pdf.CellFormat(
|
||||
97,
|
||||
8,
|
||||
fmt.Sprintf("Attention: %s %s", order.FirstName, order.LastName),
|
||||
"",
|
||||
0,
|
||||
"L",
|
||||
false,
|
||||
0,
|
||||
"",
|
||||
)
|
||||
pdf.Ln(5)
|
||||
pdf.CellFormat(
|
||||
97,
|
||||
8,
|
||||
order.Email,
|
||||
"",
|
||||
0,
|
||||
"L",
|
||||
false,
|
||||
0,
|
||||
"",
|
||||
)
|
||||
pdf.Ln(5)
|
||||
pdf.CellFormat(
|
||||
97,
|
||||
8,
|
||||
order.CreatedAt.Format("2006-01-02"),
|
||||
"",
|
||||
0,
|
||||
"L",
|
||||
false,
|
||||
0,
|
||||
"",
|
||||
)
|
||||
|
||||
pdf.SetX(58)
|
||||
pdf.SetY(93)
|
||||
pdf.CellFormat(
|
||||
155,
|
||||
8,
|
||||
order.Product,
|
||||
"",
|
||||
0,
|
||||
"L",
|
||||
false,
|
||||
0,
|
||||
"",
|
||||
)
|
||||
pdf.SetX(166)
|
||||
pdf.CellFormat(
|
||||
20,
|
||||
8,
|
||||
fmt.Sprintf("%d", order.Quantity),
|
||||
"",
|
||||
0,
|
||||
"C",
|
||||
false,
|
||||
0,
|
||||
"",
|
||||
)
|
||||
|
||||
pdf.SetX(185)
|
||||
pdf.CellFormat(
|
||||
20,
|
||||
8,
|
||||
fmt.Sprintf("$%.2f", float32(order.Amount)/100.0),
|
||||
"",
|
||||
0,
|
||||
"R",
|
||||
false,
|
||||
0,
|
||||
"",
|
||||
)
|
||||
|
||||
invoicePath := fmt.Sprintf("./invoices/%d.pdf", order.ID)
|
||||
err := pdf.OutputFileAndClose(invoicePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/cors"
|
||||
)
|
||||
|
||||
func (app *application) routes() http.Handler {
|
||||
mux := chi.NewRouter()
|
||||
|
||||
mux.Use(cors.Handler(cors.Options{
|
||||
AllowedOrigins: []string{"https://*", "http://*"},
|
||||
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
|
||||
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
|
||||
AllowCredentials: false,
|
||||
MaxAge: 300,
|
||||
}))
|
||||
|
||||
mux.Post("/invoice/create-and-send", app.CreateAndSendInvoice)
|
||||
return mux
|
||||
}
|
@ -1,79 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
version = "1.0.0"
|
||||
)
|
||||
|
||||
type config struct {
|
||||
port int
|
||||
smtp struct {
|
||||
host string
|
||||
port int
|
||||
username string
|
||||
password string
|
||||
}
|
||||
frontend string
|
||||
}
|
||||
|
||||
type application struct {
|
||||
config config
|
||||
infoLog *log.Logger
|
||||
errorLog *log.Logger
|
||||
version string
|
||||
}
|
||||
|
||||
func (app *application) serve() error {
|
||||
srv := &http.Server{
|
||||
Addr: fmt.Sprintf(":%d", app.config.port),
|
||||
Handler: app.routes(),
|
||||
IdleTimeout: 30 * time.Second,
|
||||
ReadTimeout: 10 * time.Second,
|
||||
ReadHeaderTimeout: 5 * time.Second,
|
||||
WriteTimeout: 5 * time.Second,
|
||||
}
|
||||
|
||||
app.infoLog.Printf(
|
||||
"Starting invoice microservice on port %d",
|
||||
app.config.port,
|
||||
)
|
||||
return srv.ListenAndServe()
|
||||
}
|
||||
|
||||
func main() {
|
||||
var cfg config
|
||||
|
||||
flag.IntVar(&cfg.port, "port", 5000, "Server port to listen on")
|
||||
flag.StringVar(&cfg.smtp.host, "smtphost", "0.0.0.0", "smtp host")
|
||||
flag.IntVar(&cfg.smtp.port, "smtpport", 1025, "smtp host")
|
||||
flag.StringVar(&cfg.smtp.username, "smtpuser", "user", "smtp user")
|
||||
flag.StringVar(&cfg.smtp.password, "smtppwd", "password", "smtp password")
|
||||
flag.StringVar(&cfg.frontend, "frontend", "http://localhost:4000", "frontend address")
|
||||
|
||||
flag.Parse()
|
||||
|
||||
infoLog := log.New(os.Stdout, "INFO\t", log.Ldate|log.Ltime)
|
||||
errorLog := log.New(os.Stdout, "ERROR\t", log.Ldate|log.Ltime|log.Lshortfile)
|
||||
|
||||
app := &application{
|
||||
version: version,
|
||||
config: cfg,
|
||||
infoLog: infoLog,
|
||||
errorLog: errorLog,
|
||||
}
|
||||
|
||||
app.CreateDirIfNotExist("./invoices")
|
||||
|
||||
err := app.serve()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
@ -1,88 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"embed"
|
||||
"fmt"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
mail "github.com/xhit/go-simple-mail/v2"
|
||||
)
|
||||
|
||||
//go:embed email-templates
|
||||
var emailTemplateFS embed.FS
|
||||
|
||||
func (app *application) SendMail(
|
||||
from, to, subject, tmpl string,
|
||||
attachments []string,
|
||||
data interface{},
|
||||
) error {
|
||||
templateToRender := fmt.Sprintf("email-templates/%s.html.gohtml", tmpl)
|
||||
|
||||
t, err := template.New("email-html").ParseFS(emailTemplateFS, templateToRender)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
var tpl bytes.Buffer
|
||||
if err = t.ExecuteTemplate(&tpl, "body", data); err != nil {
|
||||
app.errorLog.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
formattedMessage := tpl.String()
|
||||
|
||||
templateToRender = fmt.Sprintf("email-templates/%s.plain.gohtml", tmpl)
|
||||
t, err = template.New("email-plain").ParseFS(emailTemplateFS, templateToRender)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
return err
|
||||
}
|
||||
if err = t.ExecuteTemplate(&tpl, "body", data); err != nil {
|
||||
app.errorLog.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
plainMessage := tpl.String()
|
||||
|
||||
app.infoLog.Println(formattedMessage, plainMessage)
|
||||
|
||||
// send the mail
|
||||
server := mail.NewSMTPClient()
|
||||
server.Host = app.config.smtp.host
|
||||
server.Port = app.config.smtp.port
|
||||
// NOTE: not needed for MailHog
|
||||
// server.Username = app.config.smtp.username
|
||||
// server.Password = app.config.smtp.password
|
||||
// server.Encryption = mail.EncryptionTLS
|
||||
server.KeepAlive = false
|
||||
server.ConnectTimeout = 10 * time.Second
|
||||
server.SendTimeout = 10 * time.Second
|
||||
|
||||
smtpClient, err := server.Connect()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
email := mail.NewMSG()
|
||||
email.SetFrom(from).AddTo(to).SetSubject(subject)
|
||||
email.SetBody(mail.TextHTML, formattedMessage)
|
||||
email.AddAlternative(mail.TextPlain, plainMessage)
|
||||
|
||||
if len(attachments) > 0 {
|
||||
for _, x := range attachments {
|
||||
email.AddAttachment(x)
|
||||
}
|
||||
}
|
||||
|
||||
err = email.Send(smtpClient)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
app.infoLog.Println("send mail")
|
||||
|
||||
return nil
|
||||
}
|
@ -1,487 +1,52 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"myapp/internal/cards"
|
||||
"myapp/internal/cards/encryption"
|
||||
"myapp/internal/models"
|
||||
"myapp/internal/urlsigner"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
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 {
|
||||
stringMap := make(map[string]string)
|
||||
stringMap["publishable_key"] = app.config.stripe.key
|
||||
if err := app.renderTemplate(w, r, "terminal", &templateData{
|
||||
StringMap: stringMap,
|
||||
}, "stripe-js"); err != nil {
|
||||
app.errorLog.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
type TransactionData struct {
|
||||
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
|
||||
|
||||
func (app *application) PaymentSucceeded(w http.ResponseWriter, r *http.Request) {
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
return txnData, err
|
||||
return
|
||||
}
|
||||
|
||||
firstName := r.Form.Get("first_name")
|
||||
lastName := r.Form.Get("last_name")
|
||||
// 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")
|
||||
amount, _ := strconv.Atoi(paymentAmount)
|
||||
|
||||
// TODO: validation of the data
|
||||
|
||||
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 txnData, err
|
||||
}
|
||||
|
||||
pm, err := card.GetPaymentMethod(paymentMethod)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
return txnData, err
|
||||
}
|
||||
|
||||
lastFour := pm.Card.Last4
|
||||
expiryMonth := pm.Card.ExpMonth
|
||||
expiryYear := pm.Card.ExpYear
|
||||
|
||||
txnData = TransactionData{
|
||||
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
|
||||
}
|
||||
|
||||
type Invoice struct {
|
||||
ID int `json:"id"`
|
||||
Quantity int `json:"quantity"`
|
||||
Amount int `json:"amount"`
|
||||
Product string `json:"product"`
|
||||
FirstName string `json:"first_name"`
|
||||
LastName string `json:"last_name"`
|
||||
Email string `json:"email"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
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 {
|
||||
app.errorLog.Println(err)
|
||||
return
|
||||
}
|
||||
app.infoLog.Printf("custumer id: %d", customerID)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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: txnData.PaymentAmount,
|
||||
}
|
||||
orderID, err := app.SaveOrder(order)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
return
|
||||
}
|
||||
// call microservice
|
||||
inv := Invoice{
|
||||
ID: orderID,
|
||||
Quantity: order.Quantity,
|
||||
Amount: order.Amount,
|
||||
Product: "Widget",
|
||||
FirstName: txnData.FirstName,
|
||||
LastName: txnData.LastName,
|
||||
Email: txnData.Email,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
err = app.callInvoiceMicro(inv)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
// Don't stop the program
|
||||
}
|
||||
|
||||
app.infoLog.Printf("order id: %d", orderID)
|
||||
|
||||
app.Session.Put(r.Context(), "txn", txnData)
|
||||
|
||||
http.Redirect(w, r, "/receipt", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func (app *application) callInvoiceMicro(inv Invoice) error {
|
||||
// TODO: Do not hard code this.
|
||||
url := "http://localhost:5000/invoice/create-and-send"
|
||||
out, err := json.MarshalIndent(inv, "", "\t")
|
||||
if err != err {
|
||||
return err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(out))
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
app.infoLog.Println(resp.Body)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
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["txn"] = txn
|
||||
if err := app.renderTemplate(w, r, "receipt", &templateData{
|
||||
data["cardholder"] = cardHolder
|
||||
data["email"] = email
|
||||
data["pi"] = paymentIntent
|
||||
data["pm"] = paymentMethod
|
||||
data["pa"] = paymentAmount
|
||||
data["pc"] = paymentCurrency
|
||||
|
||||
if err := app.renderTemplate(w, r, "succeeded", &templateData{
|
||||
Data: data,
|
||||
}); err != nil {
|
||||
app.errorLog.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
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
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
|
||||
func (app *application) BronzePlan(w http.ResponseWriter, r *http.Request) {
|
||||
widget, err := app.DB.GetWidget(2)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
data := make(map[string]interface{})
|
||||
data["widget"] = widget
|
||||
|
||||
if err := app.renderTemplate(w, r, "bronze-plan", &templateData{
|
||||
Data: data,
|
||||
}); err != nil {
|
||||
app.errorLog.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (app *application) BronzePlanReceipt(w http.ResponseWriter, r *http.Request) {
|
||||
if err := app.renderTemplate(w, r, "receipt-plan", &templateData{}); err != nil {
|
||||
app.errorLog.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (app *application) LoginPage(w http.ResponseWriter, r *http.Request) {
|
||||
if err := app.renderTemplate(w, r, "login", &templateData{}); err != nil {
|
||||
app.errorLog.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (app *application) PostLoginPage(w http.ResponseWriter, r *http.Request) {
|
||||
app.Session.RenewToken(r.Context())
|
||||
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
email := r.Form.Get("email")
|
||||
password := r.Form.Get("password")
|
||||
|
||||
id, err := app.DB.Authenticate(email, password)
|
||||
if err != nil {
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
app.Session.Put(r.Context(), "userID", id)
|
||||
http.Redirect(w, r, "/", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func (app *application) Logout(w http.ResponseWriter, r *http.Request) {
|
||||
app.Session.Destroy(r.Context())
|
||||
app.Session.RenewToken(r.Context())
|
||||
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func (app *application) ForgotPassword(w http.ResponseWriter, r *http.Request) {
|
||||
if err := app.renderTemplate(w, r, "forgot-password", &templateData{}); err != nil {
|
||||
app.errorLog.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (app *application) ShowResetPassword(w http.ResponseWriter, r *http.Request) {
|
||||
email := r.URL.Query().Get("email")
|
||||
theURL := r.RequestURI
|
||||
testURL := fmt.Sprintf("%s%s", app.config.frontend, theURL)
|
||||
|
||||
signer := urlsigner.Signer{
|
||||
Secret: []byte(app.config.secretkey),
|
||||
}
|
||||
valid := signer.VerifyToken(testURL)
|
||||
if !valid {
|
||||
app.errorLog.Println("Invalid url - tampering detected")
|
||||
return
|
||||
}
|
||||
|
||||
// make sure not expired
|
||||
expired := signer.Expired(testURL, 10)
|
||||
if expired {
|
||||
app.errorLog.Println("Link expired")
|
||||
return
|
||||
}
|
||||
|
||||
encryptor := encryption.Encryption{
|
||||
Key: []byte(app.config.secretkey),
|
||||
}
|
||||
|
||||
encryptedEmail, err := encryptor.Encrypt(email)
|
||||
if err != nil {
|
||||
app.errorLog.Println("Encryption failed", err)
|
||||
return
|
||||
}
|
||||
|
||||
data := make(map[string]interface{})
|
||||
data["email"] = encryptedEmail
|
||||
if err := app.renderTemplate(w, r, "reset-password", &templateData{
|
||||
Data: data,
|
||||
}); err != nil {
|
||||
app.errorLog.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (app *application) AllSales(w http.ResponseWriter, r *http.Request) {
|
||||
if err := app.renderTemplate(w, r, "all-sales", &templateData{}); err != nil {
|
||||
app.errorLog.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (app *application) AllSubscriptions(w http.ResponseWriter, r *http.Request) {
|
||||
if err := app.renderTemplate(w, r, "all-subscriptions", &templateData{}); err != nil {
|
||||
app.errorLog.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (app *application) ShowSale(w http.ResponseWriter, r *http.Request) {
|
||||
stringMap := make(map[string]string)
|
||||
stringMap["title"] = "Sale"
|
||||
stringMap["cancel"] = "/admin/all-sales"
|
||||
stringMap["refund-url"] = "/api/admin/refund"
|
||||
stringMap["refund-btn"] = "Refund Order"
|
||||
stringMap["refund-badge"] = "Refunded"
|
||||
|
||||
intMap := make(map[string]int)
|
||||
intMap["is-refund"] = 1
|
||||
|
||||
if err := app.renderTemplate(w, r, "sale", &templateData{
|
||||
StringMap: stringMap,
|
||||
IntMap: intMap,
|
||||
}); err != nil {
|
||||
app.errorLog.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (app *application) ShowSubscriptions(w http.ResponseWriter, r *http.Request) {
|
||||
stringMap := make(map[string]string)
|
||||
stringMap["title"] = "Subscriptions"
|
||||
stringMap["cancel"] = "/admin/all-subscriptions"
|
||||
stringMap["refund-url"] = "/api/admin/cancel-subscription"
|
||||
stringMap["refund-btn"] = "Cancel Subscription"
|
||||
stringMap["refund-badge"] = "Cancelled"
|
||||
|
||||
intMap := make(map[string]int)
|
||||
intMap["is-refund"] = 0
|
||||
|
||||
if err := app.renderTemplate(w, r, "sale", &templateData{
|
||||
StringMap: stringMap,
|
||||
IntMap: intMap,
|
||||
}); err != nil {
|
||||
app.errorLog.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (app *application) AllUsers(w http.ResponseWriter, r *http.Request) {
|
||||
if err := app.renderTemplate(w, r, "all-users", &templateData{}); err != nil {
|
||||
app.errorLog.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (app *application) OneUser(w http.ResponseWriter, r *http.Request) {
|
||||
if err := app.renderTemplate(w, r, "one-user", &templateData{}); err != nil {
|
||||
if err := app.renderTemplate(w, r, "buy-once", nil, "stripe-js"); err != nil {
|
||||
app.errorLog.Println(err)
|
||||
}
|
||||
}
|
||||
|
@ -1,19 +1,14 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/gob"
|
||||
"flag"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log"
|
||||
"myapp/internal/driver"
|
||||
"myapp/internal/models"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/alexedwards/scs/mysqlstore"
|
||||
"github.com/alexedwards/scs/v2"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -21,8 +16,6 @@ const (
|
||||
cssVersion = "1"
|
||||
)
|
||||
|
||||
var session *scs.SessionManager
|
||||
|
||||
type config struct {
|
||||
port int
|
||||
env string
|
||||
@ -34,8 +27,6 @@ type config struct {
|
||||
secret string
|
||||
key string
|
||||
}
|
||||
secretkey string
|
||||
frontend string
|
||||
}
|
||||
|
||||
type application struct {
|
||||
@ -44,8 +35,6 @@ type application struct {
|
||||
errorLog *log.Logger
|
||||
templateCache map[string]*template.Template
|
||||
version string
|
||||
DB models.DBModel
|
||||
Session *scs.SessionManager
|
||||
}
|
||||
|
||||
func (app *application) serve() error {
|
||||
@ -67,8 +56,6 @@ func (app *application) serve() error {
|
||||
}
|
||||
|
||||
func main() {
|
||||
gob.Register(TransactionData{})
|
||||
|
||||
var cfg config
|
||||
|
||||
flag.IntVar(&cfg.port, "port", 4000, "Server port to listen on")
|
||||
@ -85,8 +72,6 @@ func main() {
|
||||
"DSN",
|
||||
)
|
||||
flag.StringVar(&cfg.api, "api", "http://localhost:4001", "URL to api")
|
||||
flag.StringVar(&cfg.secretkey, "secretkey", "b47df3d8380241c1177f13bdd69c6a60", "secret key")
|
||||
flag.StringVar(&cfg.frontend, "frontend", "http://localhost:4000", "frontend address")
|
||||
|
||||
flag.Parse()
|
||||
|
||||
@ -102,11 +87,6 @@ func main() {
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
// set up session
|
||||
session = scs.New()
|
||||
session.Lifetime = 24 * time.Hour
|
||||
session.Store = mysqlstore.New(conn)
|
||||
|
||||
tc := make(map[string]*template.Template)
|
||||
|
||||
app := &application{
|
||||
@ -115,12 +95,8 @@ func main() {
|
||||
errorLog: errorLog,
|
||||
templateCache: tc,
|
||||
version: version,
|
||||
DB: models.DBModel{DB: conn},
|
||||
Session: session,
|
||||
}
|
||||
|
||||
go app.ListenToWsChannel()
|
||||
|
||||
app.infoLog.Println("Connected to MariaDB")
|
||||
|
||||
err = app.serve()
|
||||
|
@ -1,18 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func SessionLoad(next http.Handler) http.Handler {
|
||||
return session.LoadAndSave(next)
|
||||
}
|
||||
|
||||
func (app *application) Auth(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if !app.Session.Exists(r.Context(), "userID") {
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
@ -20,34 +20,15 @@ type templateData struct {
|
||||
API string
|
||||
CSSVersion string
|
||||
IsAuthenticated int
|
||||
UserID int
|
||||
StripeSecretKey string
|
||||
StripePubKey string
|
||||
}
|
||||
|
||||
var functions = template.FuncMap{
|
||||
"formatCurrency": formatCurrency,
|
||||
}
|
||||
|
||||
func formatCurrency(n int) string {
|
||||
f := float32(n) / float32(100)
|
||||
return fmt.Sprintf("€%.2f", f)
|
||||
}
|
||||
var functions = template.FuncMap{}
|
||||
|
||||
//go:embed templates
|
||||
var templateFS embed.FS
|
||||
|
||||
func (app *application) addDefaultData(td *templateData, r *http.Request) *templateData {
|
||||
td.API = app.config.api
|
||||
td.StripePubKey = app.config.stripe.key
|
||||
td.StripeSecretKey = app.config.stripe.secret
|
||||
|
||||
if app.Session.Exists(r.Context(), "userID") {
|
||||
td.IsAuthenticated = 1
|
||||
td.UserID = app.Session.GetInt(r.Context(), "userID")
|
||||
} else {
|
||||
td.IsAuthenticated = 0
|
||||
}
|
||||
return td
|
||||
}
|
||||
|
||||
|
@ -8,39 +8,10 @@ import (
|
||||
|
||||
func (app *application) routes() http.Handler {
|
||||
mux := chi.NewRouter()
|
||||
mux.Use(SessionLoad)
|
||||
|
||||
mux.Get("/", app.Home)
|
||||
|
||||
mux.Get("/ws", app.WsEndPoint)
|
||||
|
||||
mux.Route("/admin", func(mux chi.Router) {
|
||||
mux.Use(app.Auth)
|
||||
mux.Get("/virtual-terminal", app.VirtualTerminal)
|
||||
mux.Get("/all-sales", app.AllSales)
|
||||
mux.Get("/all-subscriptions", app.AllSubscriptions)
|
||||
mux.Get("/sales/{id}", app.ShowSale)
|
||||
mux.Get("/subscriptions/{id}", app.ShowSubscriptions)
|
||||
mux.Get("/all-users", app.AllUsers)
|
||||
mux.Get("/all-users/{id}", app.OneUser)
|
||||
})
|
||||
|
||||
// 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.Get("/plans/bronze", app.BronzePlan)
|
||||
mux.Get("/receipt/bronze", app.BronzePlanReceipt)
|
||||
|
||||
// auth routes
|
||||
mux.Get("/login", app.LoginPage)
|
||||
mux.Post("/login", app.PostLoginPage)
|
||||
mux.Get("/logout", app.Logout)
|
||||
mux.Get("/forgot-password", app.ForgotPassword)
|
||||
mux.Get("/reset-password", app.ShowResetPassword)
|
||||
mux.Get("/charge-once", app.ChargeOnce)
|
||||
|
||||
fileServer := http.FileServer(http.Dir("./static"))
|
||||
mux.Handle("/static/*", http.StripPrefix("/static", fileServer))
|
||||
|
@ -1,38 +0,0 @@
|
||||
{{ template "base" . }}
|
||||
{{ define "title" }}
|
||||
All Sales
|
||||
{{ end }}
|
||||
{{ define "content" }}
|
||||
<h2 class="mt-5">All Sales</h2>
|
||||
<hr>
|
||||
<table id="sales-table" class="table table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Transaction</th>
|
||||
<th>Customer</th>
|
||||
<th>Product</th>
|
||||
<th>Amount</th>
|
||||
<th>Status</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
</tbody>
|
||||
</table>
|
||||
<nav>
|
||||
<ul id="paginator" class="pagination">
|
||||
</ul>
|
||||
</nav>
|
||||
{{ end }}
|
||||
{{ define "js" }}
|
||||
<script type="module">
|
||||
import {pageSize} from "/static/js/common.js"
|
||||
import {showTable, currentPage} from "/static/js/all-sales.js"
|
||||
|
||||
let curPage = currentPage;
|
||||
if (sessionStorage.getItem("cur-page") !== null) {
|
||||
curPage = sessionStorage.getItem("cur-page");
|
||||
sessionStorage.removeItem("cur-page");
|
||||
}
|
||||
showTable({{.API}}, pageSize, curPage);
|
||||
</script>
|
||||
{{ end }}
|
@ -1,41 +0,0 @@
|
||||
{{template "base" .}}
|
||||
|
||||
{{define "title"}}
|
||||
All Subscriptions
|
||||
{{end}}
|
||||
|
||||
{{define "content"}}
|
||||
<h2 class="mt-5">All Subscriptions</h2>
|
||||
<hr>
|
||||
<table id="subscriptions-table" class="table table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Transaction</th>
|
||||
<th>Customer</th>
|
||||
<th>Product</th>
|
||||
<th>Amount</th>
|
||||
<th>Status</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
</tbody>
|
||||
</table>
|
||||
<nav>
|
||||
<ul id="paginator" class="pagination">
|
||||
</ul>
|
||||
</nav>
|
||||
{{end}}
|
||||
|
||||
{{define "js"}}
|
||||
<script type="module">
|
||||
import {pageSize} from "/static/js/common.js"
|
||||
import {showTable, currentPage} from "/static/js/all-subscriptions.js"
|
||||
|
||||
let curPage = currentPage;
|
||||
if (sessionStorage.getItem("cur-page") !== null) {
|
||||
curPage = sessionStorage.getItem("cur-page");
|
||||
sessionStorage.removeItem("cur-page");
|
||||
}
|
||||
showTable({{.API}}, pageSize, curPage);
|
||||
</script>
|
||||
{{end}}
|
@ -1,28 +0,0 @@
|
||||
{{ template "base". }}
|
||||
{{ define "title" }}
|
||||
All Users
|
||||
{{ end }}
|
||||
{{ define "content" }}
|
||||
<h2 class="mt-5">All Admin users</h2>
|
||||
<hr>
|
||||
<div class="float-end">
|
||||
<a class="btn btn-outline-secondary" href="/admin/all-users/0">Add User</a>
|
||||
</div>
|
||||
<div class="clearfix"></div>
|
||||
<table id="user-table" class="table table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>User</th>
|
||||
<th>Email</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
</tbody>
|
||||
</table>
|
||||
{{ end }}
|
||||
{{ define "js" }}
|
||||
<script type="module">
|
||||
import {showUsers} from "/static/js/users.js"
|
||||
showUsers({{.API}});
|
||||
</script>
|
||||
{{ end }}
|
@ -32,6 +32,9 @@
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" aria-current="page" href="/">Home</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="/virtual-terminal">Virtual Terminal</a>
|
||||
</li>
|
||||
<li class="nav-item dropdown">
|
||||
<a class="nav-link dropdown-toggle"
|
||||
href="#"
|
||||
@ -40,59 +43,13 @@
|
||||
aria-expanded="false">Products</a>
|
||||
<ul class="dropdown-menu">
|
||||
<li>
|
||||
<a class="dropdown-item" href="/widget/1">Buy one widget</a>
|
||||
<a class="dropdown-item" href="/charge-once">Buy one widget</a>
|
||||
</li>
|
||||
<li>
|
||||
<a class="dropdown-item" href="/plans/bronze">Subscription</a>
|
||||
<a class="dropdown-item" href="#">Subscription</a>
|
||||
</li>
|
||||
</ul>
|
||||
</li>
|
||||
{{ if eq .IsAuthenticated 1 }}
|
||||
<li class="nav-item dropdown">
|
||||
<a class="nav-link dropdown-toggle"
|
||||
href="#"
|
||||
role="button"
|
||||
data-bs-toggle="dropdown"
|
||||
aria-expanded="false">Admin</a>
|
||||
<ul class="dropdown-menu">
|
||||
<li>
|
||||
<a class="dropdown-item" href="/admin/virtual-terminal">Virtual Terminal</a>
|
||||
</li>
|
||||
<li>
|
||||
<hr class="dropdown-divider">
|
||||
</li>
|
||||
<li>
|
||||
<a class="dropdown-item" href="/admin/all-sales">All Sales</a>
|
||||
</li>
|
||||
<li>
|
||||
<a class="dropdown-item" href="/admin/all-subscriptions">All Subscriptions</a>
|
||||
</li>
|
||||
<li>
|
||||
<hr class="dropdown-divider">
|
||||
</li>
|
||||
<li>
|
||||
<a class="dropdown-item" href="/admin/all-users">All Users</a>
|
||||
</li>
|
||||
<li>
|
||||
<hr class="dropdown-divider">
|
||||
</li>
|
||||
<li>
|
||||
<a class="dropdown-item" href="/logout">Logout</a>
|
||||
</li>
|
||||
</ul>
|
||||
</li>
|
||||
{{ end }}
|
||||
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
|
||||
{{ if eq .IsAuthenticated 1 }}
|
||||
<li id="logout-link" class="nav-item">
|
||||
<a href="/logout" class="nav-link">Logout</a>
|
||||
</li>
|
||||
{{ else }}
|
||||
<li id="login-link" class="nav-item">
|
||||
<a href="/login" class="nav-link">Login</a>
|
||||
</li>
|
||||
{{ end }}
|
||||
</ul>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
@ -105,12 +62,6 @@
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"
|
||||
integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
|
||||
crossorigin="anonymous"></script>
|
||||
<script type="module">
|
||||
import {wsConn, socket} from "/static/js/base.js"
|
||||
document.addEventListener("DOMContentLoaded", function() {
|
||||
wsConn({{.IsAuthenticated}}, {{.UserID}});
|
||||
});
|
||||
</script>
|
||||
{{ block "js" . }}
|
||||
{{ end }}
|
||||
</body>
|
||||
|
@ -1,104 +0,0 @@
|
||||
{{ template "base" . }}
|
||||
{{ define "title" }}
|
||||
Bronze Plan
|
||||
{{ end }}
|
||||
{{ define "content" }}
|
||||
{{$widget := index .Data "widget"}}
|
||||
<h2 class="mt-3 text-center">Bronze Plan: {{ formatCurrency $widget.Price }}</h2>
|
||||
<hr>
|
||||
<div class="alert alert-danger text-center d-none" id="card-messages"></div>
|
||||
<form action="/payment-succeeded-temp"
|
||||
method="post"
|
||||
name="charge_form"
|
||||
id="charge_form"
|
||||
class="d-blick needs-validation charge-form"
|
||||
autocomplete="off"
|
||||
novalidate="">
|
||||
<input type="hidden"
|
||||
id="product_id"
|
||||
name="product_id"
|
||||
value="{{$widget.ID}}">
|
||||
<input type="hidden" id="amount" name="amount" value="{{$widget.Price}}">
|
||||
<p class="mt-2 mb-3 text-center">{{$widget.Description}}</p>
|
||||
<hr>
|
||||
<div class="mb-3">
|
||||
<label for="first_name" class="form-label">First Name</label>
|
||||
<input type="text"
|
||||
id="first_name"
|
||||
name="first_name"
|
||||
autocomplete="first_name-new"
|
||||
required=""
|
||||
class="form-control">
|
||||
<div id="first_name-help" class="d-none"></div>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="last-name" class="form-label">Last Name</label>
|
||||
<input type="text"
|
||||
id="last-name"
|
||||
name="last_name"
|
||||
autocomplete="last-name-new"
|
||||
required=""
|
||||
class="form-control">
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="cardholder-email" class="form-label">Email</label>
|
||||
<input type="text"
|
||||
id="cardholder-email"
|
||||
name="cardholder_email"
|
||||
autocomplete="cardholder-email-new"
|
||||
required=""
|
||||
class="form-control">
|
||||
<div class="mb-3">
|
||||
<label for="cardholder-name" class="form-label">Name on card</label>
|
||||
<input type="text"
|
||||
id="cardholder-name"
|
||||
name="cardholder_name"
|
||||
autocomplete="cardholder-name-new"
|
||||
required=""
|
||||
class="form-control">
|
||||
</div>
|
||||
</div>
|
||||
<!-- card number will be built by stripe -->
|
||||
<div class="mb-3">
|
||||
<label for="card-element" class="form-label">Credit Card</label>
|
||||
<div class="form-control" id="card-element"></div>
|
||||
<div class="alert-danger text-center" id="card-errors" role="alert"></div>
|
||||
<div class="alert-success text-center" id="card-success" role="alert"></div>
|
||||
</div>
|
||||
<hr>
|
||||
<a href="javascript:void(0)" id="pay-button" class="btn btn-primary">Pay {{ formatCurrency $widget.Price }}/month</a>
|
||||
<div class="text-center d-none" id="processing-payment">
|
||||
<div class="spinner-border text-primary" role="status">
|
||||
<span class="visually-hidden">Loading...</span>
|
||||
</div>
|
||||
</div>
|
||||
<input id="payment_intent"
|
||||
type="hidden"
|
||||
name="payment_intent"
|
||||
value="payment_intent">
|
||||
<input id="payment_method"
|
||||
type="hidden"
|
||||
name="payment_method"
|
||||
value="payment_method">
|
||||
<input id="payment_amount"
|
||||
type="hidden"
|
||||
name="payment_amount"
|
||||
value="payment_amount">
|
||||
<input id="payment_currency"
|
||||
type="hidden"
|
||||
name="payment_currency"
|
||||
value="payment_currency">
|
||||
</form>
|
||||
{{ end }}
|
||||
{{ define "js" }}
|
||||
{{$widget := index .Data "widget"}}
|
||||
<script src="https://js.stripe.com/v3/"></script>
|
||||
<script type="module">
|
||||
import {stripeInit} from "/static/js/common.js";
|
||||
import {val} from "/static/js/stripe-plan.js"
|
||||
stripeInit('{{.StripePubKey}}');
|
||||
document.getElementById("pay-button").addEventListener("click", () => {
|
||||
val({{$widget.PlanID}}, {{.API}});
|
||||
})
|
||||
</script>
|
||||
{{ end }}
|
@ -1,14 +1,14 @@
|
||||
{{ template "base" . }}
|
||||
{{ template "base" .}}
|
||||
{{ define "title" }}
|
||||
Buy one widget
|
||||
{{ end }}
|
||||
{{ define "content" }}
|
||||
{{$widget := index .Data "widget"}}
|
||||
<h2 class="mt-3 text-center">Buy One Widget</h2>
|
||||
<hr>
|
||||
<img src="/static/img/widget.jpeg"
|
||||
alt="widget"
|
||||
class="image-fluid rounded mx-auto d-block">
|
||||
|
||||
<div class="alert alert-danger text-center d-none" id="card-messages"></div>
|
||||
<form action="/payment-succeeded"
|
||||
method="post"
|
||||
@ -17,39 +17,17 @@ Buy one widget
|
||||
class="d-blick needs-validation charge-form"
|
||||
autocomplete="off"
|
||||
novalidate="">
|
||||
<input type="hidden" name="product_id" value="{{$widget.ID}}">
|
||||
<input type="hidden" id="amount" name="amount" value="{{$widget.Price}}">
|
||||
<h3 class="mt-2 mb-3 text-center">{{$widget.Name}}: {{ formatCurrency $widget.Price }}</h3>
|
||||
<p class="mt-2 mb-3 text-center">{{$widget.Description}}</p>
|
||||
<hr>
|
||||
<div class="mb-3">
|
||||
<label for="first-name" class="form-label">First Name</label>
|
||||
<label for="amount" class="form-label">Amount</label>
|
||||
<input type="text"
|
||||
id="first-name"
|
||||
name="first_name"
|
||||
autocomplete="first-name-new"
|
||||
id="amount"
|
||||
name="amount"
|
||||
autocomplete="amount-new"
|
||||
required=""
|
||||
class="form-control">
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="last-name" class="form-label">Last Name</label>
|
||||
<input type="text"
|
||||
id="last-name"
|
||||
name="last_name"
|
||||
autocomplete="last-name-new"
|
||||
required=""
|
||||
class="form-control">
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="cardholder-email" class="form-label">Email</label>
|
||||
<input type="text"
|
||||
id="cardholder-email"
|
||||
name="cardholder_email"
|
||||
autocomplete="cardholder-email-new"
|
||||
required=""
|
||||
class="form-control">
|
||||
<div class="mb-3">
|
||||
<label for="cardholder-name" class="form-label">Name on card</label>
|
||||
<label for="cardholder-name" class="form-label">Cardholder Name</label>
|
||||
<input type="text"
|
||||
id="cardholder-name"
|
||||
name="cardholder_name"
|
||||
@ -57,6 +35,14 @@ Buy one widget
|
||||
required=""
|
||||
class="form-control">
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="cardholder-email" class="form-label">Cardholder Email</label>
|
||||
<input type="text"
|
||||
id="cardholder-email"
|
||||
name="cardholder_email"
|
||||
autocomplete="cardholder-email-new"
|
||||
required=""
|
||||
class="form-control">
|
||||
</div>
|
||||
<!-- card number will be built by stripe -->
|
||||
<div class="mb-3">
|
||||
@ -66,7 +52,10 @@ Buy one widget
|
||||
<div class="alert-success text-center" id="card-success" role="alert"></div>
|
||||
</div>
|
||||
<hr>
|
||||
<a href="javascript:void(0)" id="pay-button" class="btn btn-primary">Charge Card</a>
|
||||
<a href="javascript:void(0)"
|
||||
id="pay-button"
|
||||
class="btn btn-primary"
|
||||
onclick="val()">Charge Card</a>
|
||||
<div class="text-center d-none" id="processing-payment">
|
||||
<div class="spinner-border text-primary" role="status">
|
||||
<span class="visually-hidden">Loading...</span>
|
||||
@ -91,13 +80,5 @@ Buy one widget
|
||||
</form>
|
||||
{{ end }}
|
||||
{{ define "js" }}
|
||||
<script src="https://js.stripe.com/v3/"></script>
|
||||
<script type="module">
|
||||
import {stripeInit} from "/static/js/common.js";
|
||||
import {val} from "/static/js/stripe.js"
|
||||
stripeInit('{{.StripePubKey}}');
|
||||
document.getElementById("pay-button").addEventListener("click", () => {
|
||||
val({{.API}});
|
||||
})
|
||||
</script>
|
||||
{{ template "stripe-js" . }}
|
||||
{{ end }}
|
||||
|
@ -1,48 +0,0 @@
|
||||
{{template "base" .}}
|
||||
{{define "title"}}
|
||||
Forgot Password
|
||||
{{end}}
|
||||
|
||||
{{define "content"}}
|
||||
<div class="row">
|
||||
<div class="col-md-6 offset-md-3">
|
||||
<div class="alert alert-danger text-center d-none" id="forgot-messages"></div>
|
||||
<form action=""
|
||||
method="post"
|
||||
name="forgot-form"
|
||||
id="forgot-form"
|
||||
class="d-blick needs-validation"
|
||||
autocomplete="off"
|
||||
novalidate="">
|
||||
<h2 class="mt-2 mb-3 text-center">Forgot Password</h2>
|
||||
<hr>
|
||||
<div class="mb-3">
|
||||
<label for="-email" class="form-label">Email</label>
|
||||
<input type="text"
|
||||
id="email"
|
||||
name="email"
|
||||
autocomplete="email-new"
|
||||
required=""
|
||||
class="form-control">
|
||||
</div>
|
||||
<hr>
|
||||
<a href="javascript:void(0)" id="reset-btn" class="btn btn-primary">Reset Password</a>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
{{end}}
|
||||
{{define "js"}}
|
||||
<script type="module">
|
||||
import {forgot} from "/static/js/login.js"
|
||||
document.getElementById("reset-btn").addEventListener("click", (event) => {
|
||||
event.preventDefault();
|
||||
forgot({{.API}});
|
||||
})
|
||||
document.getElementById("email").addEventListener("keypress", (event) => {
|
||||
if (event.key === "Enter") {
|
||||
event.preventDefault();
|
||||
forgot({{.API}});
|
||||
}
|
||||
})
|
||||
</script>
|
||||
{{end}}
|
@ -1,8 +0,0 @@
|
||||
{{ template "base" . }}
|
||||
{{ define "title" }}
|
||||
Widget
|
||||
{{ end }}
|
||||
{{ define "content" }}
|
||||
<h2 class="mt-5">Widget</h2>
|
||||
{{ end }}
|
||||
|
@ -1,52 +0,0 @@
|
||||
{{ template "base" . }}
|
||||
{{ define "title" }}
|
||||
Login
|
||||
{{ end }}
|
||||
{{ define "content" }}
|
||||
<div class="row">
|
||||
<div class="col-md-6 offset-md-3">
|
||||
<div class="alert alert-danger text-center d-none" id="login-messages"></div>
|
||||
<form action="/login"
|
||||
method="post"
|
||||
name="login-form"
|
||||
id="login-form"
|
||||
class="d-blick needs-validation"
|
||||
autocomplete="off"
|
||||
novalidate="">
|
||||
<h2 class="mt-2 mb-3 text-center">Login</h2>
|
||||
<hr>
|
||||
<div class="mb-3">
|
||||
<label for="-email" class="form-label">Email</label>
|
||||
<input type="text"
|
||||
id="email"
|
||||
name="email"
|
||||
autocomplete="email-new"
|
||||
required=""
|
||||
class="form-control">
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="password" class="form-label">Password</label>
|
||||
<input type="password"
|
||||
id="password"
|
||||
name="password"
|
||||
autocomplete="password-new"
|
||||
required=""
|
||||
class="form-control">
|
||||
</div>
|
||||
<hr>
|
||||
<a href="javascript:void(0)" id="login-btn" class="btn btn-primary">Login</a>
|
||||
<p class="mt-2">
|
||||
<small><a href="/forgot-password">Forgot password</a></small>
|
||||
</p>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
{{ end }}
|
||||
{{ define "js" }}
|
||||
<script type="module">
|
||||
import {val} from "/static/js/login.js"
|
||||
document.getElementById("login-btn").addEventListener("click", () => {
|
||||
val({{.API}});
|
||||
})
|
||||
</script>
|
||||
{{ end }}
|
@ -1,81 +0,0 @@
|
||||
{{ template "base". }}
|
||||
{{ define "title" }}
|
||||
Admin User
|
||||
{{ end }}
|
||||
{{ define "content" }}
|
||||
<h2 class="mt-5">Admin user</h2>
|
||||
<hr>
|
||||
<form method="post"
|
||||
action=""
|
||||
name="user_form"
|
||||
id="user_form"
|
||||
class="needs-validation"
|
||||
autocomplete="off"
|
||||
novalidate>
|
||||
<div class="mb-3">
|
||||
<label for="first_name" class="form-label">First Name</label>
|
||||
<input type="text"
|
||||
name="first_name"
|
||||
id="first_name"
|
||||
class="form-control"
|
||||
required=""
|
||||
autocomplete="first_name-new">
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="last_name" class="form-label">Last Name</label>
|
||||
<input type="text"
|
||||
name="last_name"
|
||||
id="last_name"
|
||||
class="form-control"
|
||||
required=""
|
||||
autocomplete="last_name-new">
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="email" class="form-label">Email</label>
|
||||
<input type="text"
|
||||
name="email"
|
||||
id="email"
|
||||
class="form-control"
|
||||
required=""
|
||||
autocomplete="email-new">
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="password" class="form-label">Password</label>
|
||||
<input type="password"
|
||||
name="password"
|
||||
id="password"
|
||||
class="form-control"
|
||||
autocomplete="password-new">
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="verify_password" class="form-label">Verify Password</label>
|
||||
<input type="password"
|
||||
name="verify_password"
|
||||
id="verify_password"
|
||||
class="form-control"
|
||||
autocomplete="veryfy_password-new">
|
||||
</div>
|
||||
<hr>
|
||||
<div class="float-start">
|
||||
<a href="javascript:void(0)" class="btn btn-primary" id="saveBtn">Save Changes</a>
|
||||
<a href="/admin/all-users" class="btn btn-warning" id="cancelBtn">Cancel</a>
|
||||
</div>
|
||||
<div class="float-end">
|
||||
<a href="javascript:void(0)" class="btn btn-danger d-none" id="deleteBtn">Delete</a>
|
||||
</div>
|
||||
<div class="clearfix"></div>
|
||||
</form>
|
||||
{{ end }}
|
||||
{{ define "js" }}
|
||||
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
|
||||
<script type="module">
|
||||
import {showUser, saveUser, deleteUser} from "/static/js/users.js"
|
||||
showUser({{.API}}, {{.UserID}});
|
||||
document.getElementById("saveBtn").addEventListener("click", (evt) => {
|
||||
saveUser({{.API}}, evt);
|
||||
});
|
||||
document.getElementById("deleteBtn").addEventListener("click", () => {
|
||||
deleteUser({{.API}});
|
||||
});
|
||||
</script>
|
||||
{{ end }}
|
@ -1,24 +0,0 @@
|
||||
{{ template "base" . }}
|
||||
{{ define "title" }}
|
||||
Payment Succedded!
|
||||
{{ end }}
|
||||
{{ define "content" }}
|
||||
{{$txn := index .Data "txn"}}
|
||||
<h2 class="mt-5">Payment Succeeded</h2>
|
||||
<hr>
|
||||
<p>Customer Name: <span id="first-name"></span> <span id="last-name"></span></p>
|
||||
<p>Payment Amount: <span id="amount"></span></p>
|
||||
<p>Last Four: <span id="last-four"></span></p>
|
||||
{{ end }}
|
||||
|
||||
{{define "js"}}
|
||||
<script>
|
||||
if (sessionStorage.first_name) {
|
||||
document.getElementById("first-name").innerHTML = sessionStorage.first_name;
|
||||
document.getElementById("last-name").innerHTML = sessionStorage.last_name;
|
||||
document.getElementById("amount").innerHTML = sessionStorage.amount;
|
||||
document.getElementById("last-four").innerHTML = sessionStorage.last_four;
|
||||
sessionStorage.clear();
|
||||
}
|
||||
</script>
|
||||
{{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 }}
|
@ -1,51 +0,0 @@
|
||||
{{template "base" .}}
|
||||
{{define "title"}}
|
||||
Reset Password
|
||||
{{end}}
|
||||
|
||||
{{define "content"}}
|
||||
<div class="row">
|
||||
<div class="col-md-6 offset-md-3">
|
||||
<div class="alert alert-danger text-center d-none" id="reset-messages"></div>
|
||||
<form action=""
|
||||
method="post"
|
||||
name="reset-form"
|
||||
id="reset-form"
|
||||
class="d-blick needs-validation"
|
||||
autocomplete="off"
|
||||
novalidate="">
|
||||
<h2 class="mt-2 mb-3 text-center">Reset Password</h2>
|
||||
<hr>
|
||||
<div class="mb-3">
|
||||
<label for="password" class="form-label">Password</label>
|
||||
<input type="password"
|
||||
id="password"
|
||||
name="password"
|
||||
autocomplete="password-new"
|
||||
required=""
|
||||
class="form-control">
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="verify-password" class="form-label">Verify Password</label>
|
||||
<input type="password"
|
||||
id="verify-password"
|
||||
name="verify-password"
|
||||
autocomplete="verify-password-new"
|
||||
required=""
|
||||
class="form-control">
|
||||
</div>
|
||||
<hr>
|
||||
<a href="javascript:void(0)" id="reset-btn" class="btn btn-primary">Reset Password</a>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
{{end}}
|
||||
{{define "js"}}
|
||||
<script type="module">
|
||||
import {reset} from "/static/js/login.js"
|
||||
document.getElementById("reset-btn").addEventListener("click", () => {
|
||||
reset({{.API}}, {{index .Data "email"}});
|
||||
})
|
||||
</script>
|
||||
{{end}}
|
||||
|
@ -1,41 +0,0 @@
|
||||
{{ template "base" . }}
|
||||
{{ define "title" }}
|
||||
{{ index .StringMap "title" }}
|
||||
{{ end }}
|
||||
{{ define "content" }}
|
||||
<h2 class="mt-5">Sale</h2>
|
||||
<span id="refunded" class="badge bg-danger d-none">{{index .StringMap "refund-badge"}}</span>
|
||||
<span id="charged" class="badge bg-success d-none">Charged</span>
|
||||
<hr>
|
||||
<div class="alert alert-danger text-center d-none" id="messages"></div>
|
||||
<div>
|
||||
<strong>Order No:</strong> <span id="order-no"></span>
|
||||
<br>
|
||||
<strong>Customer:</strong> <span id="customer"></span>
|
||||
<br>
|
||||
<strong>Product:</strong> <span id="product"></span>
|
||||
<br>
|
||||
<strong>Quantity:</strong> <span id="quantity"></span>
|
||||
<br>
|
||||
<strong>Total Sale:</strong> <span id="amount"></span>
|
||||
<br>
|
||||
</div>
|
||||
<hr>
|
||||
<a href='{{ index .StringMap "cancel" }}' class="btn btn-info">Cancel</a>
|
||||
<a id="refund-btn" href="#!" class="btn btn-warning d-none">{{index .StringMap "refund-btn"}}</a>
|
||||
<input type="hidden" id="pi" value="">
|
||||
<input type="hidden" id="charge-amount" value="">
|
||||
<input type="hidden" id="currency" value="">
|
||||
{{ end }}
|
||||
{{ define "js" }}
|
||||
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
|
||||
<script type="module">
|
||||
import {showInfo, refund} from "/static/js/sale.js"
|
||||
showInfo({{.API}});
|
||||
|
||||
const api = {{.API}} + {{index .StringMap "refund-url"}}
|
||||
document.getElementById("refund-btn").addEventListener("click", function(event) {
|
||||
refund(api, {{index .IntMap "is-refund"}});
|
||||
});
|
||||
</script>
|
||||
{{ end }}
|
14
cmd/web/templates/succeeded.page.gohtml
Normal file
14
cmd/web/templates/succeeded.page.gohtml
Normal file
@ -0,0 +1,14 @@
|
||||
{{ 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>Cardholder: {{ index .Data "cardholder" }}</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>
|
||||
{{ end }}
|
@ -6,7 +6,7 @@ Virtual Terminal
|
||||
<h2 class="mt-3 text-center">Virtual Terminal</h2>
|
||||
<hr>
|
||||
<div class="alert alert-danger text-center d-none" id="card-messages"></div>
|
||||
<form action=""
|
||||
<form action="/payment-succeeded"
|
||||
method="post"
|
||||
name="charge_form"
|
||||
id="charge_form"
|
||||
@ -16,12 +16,11 @@ Virtual Terminal
|
||||
<div class="mb-3">
|
||||
<label for="amount" class="form-label">Amount</label>
|
||||
<input type="text"
|
||||
id="charge_amount"
|
||||
name="charge_amount"
|
||||
autocomplete="charge_amount-new"
|
||||
id="amount"
|
||||
name="amount"
|
||||
autocomplete="amount-new"
|
||||
required=""
|
||||
class="form-control">
|
||||
<input type="hidden" id="amount" name="amount" value="value">
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="cardholder-name" class="form-label">Cardholder Name</label>
|
||||
@ -49,7 +48,10 @@ Virtual Terminal
|
||||
<div class="alert-success text-center" id="card-success" role="alert"></div>
|
||||
</div>
|
||||
<hr>
|
||||
<a href="javascript:void(0)" id="pay-button" class="btn btn-primary">Charge Card</a>
|
||||
<a href="javascript:void(0)"
|
||||
id="pay-button"
|
||||
class="btn btn-primary"
|
||||
onclick="val()">Charge Card</a>
|
||||
<div class="text-center d-none" id="processing-payment">
|
||||
<div class="spinner-border text-primary" role="status">
|
||||
<span class="visually-hidden">Loading...</span>
|
||||
@ -72,40 +74,7 @@ Virtual Terminal
|
||||
name="payment_currency"
|
||||
value="payment_currency">
|
||||
</form>
|
||||
<div class="row">
|
||||
<div class="col-md-5 offset-md-3 d-none" id="receipt">
|
||||
<h3 class="mt-3 text-center">Receipt</h3>
|
||||
<hr>
|
||||
<p>
|
||||
<strong>Bank Return Code</strong>: <span id="bank-return-code"></span>
|
||||
</p>
|
||||
<p>
|
||||
<a href="/admin/virtual-terminal" class="btn btn-primary">
|
||||
Charge another card
|
||||
</a>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
{{ end }}
|
||||
{{ define "js" }}
|
||||
<script src="https://js.stripe.com/v3/"></script>
|
||||
<script type="module">
|
||||
import {stripeInit, checkAuth} from "/static/js/common.js";
|
||||
import {val} from "/static/js/terminal.js"
|
||||
|
||||
checkAuth({{.API}});
|
||||
stripeInit('{{.StripePubKey}}');
|
||||
document.getElementById("charge_amount").addEventListener("change", (evt) => {
|
||||
if (evt.target.value !== "") {
|
||||
document.getElementById("amount").value = parseInt((evt.target.value * 100), 10);
|
||||
} else {
|
||||
document.getElementById("amount").value = 0;
|
||||
}
|
||||
});
|
||||
|
||||
document.getElementById("pay-button").addEventListener("click", () => {
|
||||
val({{.API}});
|
||||
})
|
||||
|
||||
</script>
|
||||
{{ template "stripe-js" . }}
|
||||
{{ end }}
|
||||
|
@ -1,17 +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 }}
|
@ -1,109 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
type WebSocketConnection struct {
|
||||
*websocket.Conn
|
||||
}
|
||||
|
||||
type WsPayload struct {
|
||||
Action string `json:"action"`
|
||||
Message string `json:"message"`
|
||||
UserName string `json:"user_name"`
|
||||
MessageType string `json:"message_type"`
|
||||
UserID int `json:"user_id"`
|
||||
Conn WebSocketConnection `json:"-"`
|
||||
}
|
||||
|
||||
type WsJSONResponse struct {
|
||||
Action string `json:"action"`
|
||||
Message string `json:"message"`
|
||||
UserID int `json:"user_id"`
|
||||
}
|
||||
|
||||
var upgradeConnection = websocket.Upgrader{
|
||||
ReadBufferSize: 1024,
|
||||
WriteBufferSize: 1024,
|
||||
// since we don't expect any communication from the client side
|
||||
CheckOrigin: func(r *http.Request) bool { return true },
|
||||
}
|
||||
|
||||
var clients = make(map[WebSocketConnection]string)
|
||||
|
||||
var wsChan = make(chan WsPayload)
|
||||
|
||||
func (app *application) WsEndPoint(w http.ResponseWriter, r *http.Request) {
|
||||
ws, err := upgradeConnection.Upgrade(w, r, nil)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
app.infoLog.Printf("Client connected from %s\n", r.RemoteAddr)
|
||||
var response WsJSONResponse
|
||||
response.Message = "Connected to server"
|
||||
|
||||
err = ws.WriteJSON(response)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
conn := WebSocketConnection{Conn: ws}
|
||||
clients[conn] = ""
|
||||
|
||||
go app.ListenForWS(&conn)
|
||||
}
|
||||
|
||||
func (app *application) ListenForWS(conn *WebSocketConnection) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
app.errorLog.Println("ERROR:", fmt.Sprintf("%v", r))
|
||||
}
|
||||
}()
|
||||
|
||||
var payload WsPayload
|
||||
|
||||
for {
|
||||
err := conn.ReadJSON(&payload)
|
||||
if err != nil {
|
||||
// do nothing
|
||||
app.errorLog.Println(err)
|
||||
break
|
||||
}
|
||||
payload.Conn = *conn
|
||||
wsChan <- payload
|
||||
}
|
||||
}
|
||||
|
||||
func (app *application) ListenToWsChannel() {
|
||||
var response WsJSONResponse
|
||||
for {
|
||||
e := <-wsChan
|
||||
switch e.Action {
|
||||
case "deleteUser":
|
||||
response.Action = "logout"
|
||||
response.Message = "Your account has ben deleted"
|
||||
response.UserID = e.UserID
|
||||
app.broadcastToAll(response)
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (app *application) broadcastToAll(response WsJSONResponse) {
|
||||
for client := range clients {
|
||||
// broadcast to every connected client
|
||||
err := client.WriteJSON(response)
|
||||
if err != nil {
|
||||
app.errorLog.Printf("Websocket err on %s: %s", response.Action, err)
|
||||
_ = client.Close()
|
||||
delete(clients, client)
|
||||
}
|
||||
}
|
||||
}
|
16
go.mod
16
go.mod
@ -3,24 +3,10 @@ module myapp
|
||||
go 1.22.5
|
||||
|
||||
require (
|
||||
github.com/alexedwards/scs/mysqlstore v0.0.0-20240316134038-7e11d57e8885
|
||||
github.com/alexedwards/scs/v2 v2.8.0
|
||||
github.com/bwmarrin/go-alone v0.0.0-20190806015146-742bb55d1631
|
||||
github.com/go-chi/chi/v5 v5.1.0
|
||||
github.com/go-chi/cors v1.2.1
|
||||
github.com/go-sql-driver/mysql v1.8.1
|
||||
github.com/gorilla/websocket v1.5.3
|
||||
github.com/phpdave11/gofpdf v1.4.2
|
||||
github.com/stripe/stripe-go/v79 v79.6.0
|
||||
github.com/xhit/go-simple-mail/v2 v2.16.0
|
||||
golang.org/x/crypto v0.26.0
|
||||
)
|
||||
|
||||
require (
|
||||
filippo.io/edwards25519 v1.1.0 // indirect
|
||||
github.com/go-test/deep v1.1.1 // indirect
|
||||
github.com/phpdave11/gofpdi v1.0.12 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/toorop/go-dkim v0.0.0-20201103131630-e1cd1a0a5208 // indirect
|
||||
golang.org/x/sys v0.23.0 // indirect
|
||||
)
|
||||
require filippo.io/edwards25519 v1.1.0 // indirect
|
||||
|
38
go.sum
38
go.sum
@ -1,61 +1,27 @@
|
||||
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
|
||||
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
||||
github.com/alexedwards/scs/mysqlstore v0.0.0-20240316134038-7e11d57e8885 h1:C7QAamNjR5yz6di4KJWAKcnxueKBgq4L/JGXhlnu35w=
|
||||
github.com/alexedwards/scs/mysqlstore v0.0.0-20240316134038-7e11d57e8885/go.mod h1:p8jK3D80sw1PFrCSdlcJF1O75bp55HqbgDyyCLM0FrE=
|
||||
github.com/alexedwards/scs/v2 v2.8.0 h1:h31yUYoycPuL0zt14c0gd+oqxfRwIj6SOjHdKRZxhEw=
|
||||
github.com/alexedwards/scs/v2 v2.8.0/go.mod h1:ToaROZxyKukJKT/xLcVQAChi5k6+Pn1Gvmdl7h3RRj8=
|
||||
github.com/boombuler/barcode v1.0.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
|
||||
github.com/bwmarrin/go-alone v0.0.0-20190806015146-742bb55d1631 h1:Xb5rra6jJt5Z1JsZhIMby+IP5T8aU+Uc2RC9RzSxs9g=
|
||||
github.com/bwmarrin/go-alone v0.0.0-20190806015146-742bb55d1631/go.mod h1:P86Dksd9km5HGX5UMIocXvX87sEp2xUARle3by+9JZ4=
|
||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw=
|
||||
github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
|
||||
github.com/go-chi/cors v1.2.1 h1:xEC8UT3Rlp2QuWNEr4Fs/c2EAGVKBwy/1vHx3bppil4=
|
||||
github.com/go-chi/cors v1.2.1/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58=
|
||||
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
|
||||
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
|
||||
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
|
||||
github.com/go-test/deep v1.1.1 h1:0r/53hagsehfO4bzD2Pgr/+RgHqhmf+k1Bpse2cTu1U=
|
||||
github.com/go-test/deep v1.1.1/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
|
||||
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/jung-kurt/gofpdf v1.0.0/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
|
||||
github.com/phpdave11/gofpdf v1.4.2 h1:KPKiIbfwbvC/wOncwhrpRdXVj2CZTCFlw4wnoyjtHfQ=
|
||||
github.com/phpdave11/gofpdf v1.4.2/go.mod h1:zpO6xFn9yxo3YLyMvW8HcKWVdbNqgIfOOp2dXMnm1mY=
|
||||
github.com/phpdave11/gofpdi v1.0.12 h1:RZb9NG62cw/RW0rHAduVRo+98R8o/G1krcg2ns7DakQ=
|
||||
github.com/phpdave11/gofpdi v1.0.12/go.mod h1:vBmVV0Do6hSBHC8uKUQ71JGW+ZGQq74llk/7bXwjDoI=
|
||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/ruudk/golang-pdf417 v0.0.0-20181029194003-1af4ab5afa58/go.mod h1:6lfFZQK844Gfx8o5WFuvpxWRwnSoipWe/p622j1v06w=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stripe/stripe-go/v79 v79.6.0 h1:qSBV2f2rpLEEZTdTlVLzdmQJZNmfoo2E3hUEkFT8GBc=
|
||||
github.com/stripe/stripe-go/v79 v79.6.0/go.mod h1:cuH6X0zC8peY6f1AubHwgJ/fJSn2dh5pfiCr6CjyKVU=
|
||||
github.com/toorop/go-dkim v0.0.0-20201103131630-e1cd1a0a5208 h1:PM5hJF7HVfNWmCjMdEfbuOBNXSVF2cMFGgQTPdKCbwM=
|
||||
github.com/toorop/go-dkim v0.0.0-20201103131630-e1cd1a0a5208/go.mod h1:BzWtXXrXzZUvMacR0oF/fbDDgUPO8L36tDMmRAf14ns=
|
||||
github.com/xhit/go-simple-mail/v2 v2.16.0 h1:ouGy/Ww4kuaqu2E2UrDw7SvLaziWTB60ICLkIkNVccA=
|
||||
github.com/xhit/go-simple-mail/v2 v2.16.0/go.mod h1:b7P5ygho6SYE+VIqpxA6QkYfv4teeyG4MKqB3utRu98=
|
||||
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
|
||||
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
|
||||
golang.org/x/image v0.0.0-20190910094157-69e4b8554b2a/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
|
||||
golang.org/x/net v0.0.0-20210520170846-37e1c6afe023 h1:ADo5wSpq2gqaCGQWzk7S5vd//0iyyLeAratkEoG5dLE=
|
||||
golang.org/x/net v0.0.0-20210520170846-37e1c6afe023/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
|
||||
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM=
|
||||
golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
|
||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
|
||||
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||
|
@ -2,11 +2,7 @@ package cards
|
||||
|
||||
import (
|
||||
"github.com/stripe/stripe-go/v79"
|
||||
"github.com/stripe/stripe-go/v79/customer"
|
||||
"github.com/stripe/stripe-go/v79/paymentintent"
|
||||
"github.com/stripe/stripe-go/v79/paymentmethod"
|
||||
"github.com/stripe/stripe-go/v79/refund"
|
||||
"github.com/stripe/stripe-go/v79/subscription"
|
||||
)
|
||||
|
||||
type Card struct {
|
||||
@ -52,103 +48,6 @@ func (c *Card) CreatePaymentIntent(
|
||||
return pi, "", nil
|
||||
}
|
||||
|
||||
// GetPaymentMethod gets the payment method by payment intend id
|
||||
func (c *Card) GetPaymentMethod(s string) (*stripe.PaymentMethod, error) {
|
||||
stripe.Key = c.Secret
|
||||
|
||||
pm, err := paymentmethod.Get(s, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return pm, nil
|
||||
}
|
||||
|
||||
// RetrievePaymentMethod gets an existing payment intent by id
|
||||
func (c *Card) RetrievePaymentIntent(id string) (*stripe.PaymentIntent, error) {
|
||||
stripe.Key = c.Secret
|
||||
|
||||
pi, err := paymentintent.Get(id, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return pi, nil
|
||||
}
|
||||
|
||||
func (c *Card) SubscribeToPlan(
|
||||
cust *stripe.Customer,
|
||||
plan, email, last4, cardType string,
|
||||
) (*stripe.Subscription, error) {
|
||||
stripeCustomerID := cust.ID
|
||||
items := []*stripe.SubscriptionItemsParams{
|
||||
{Plan: stripe.String(plan)},
|
||||
}
|
||||
|
||||
params := &stripe.SubscriptionParams{
|
||||
Customer: stripe.String(stripeCustomerID),
|
||||
Items: items,
|
||||
}
|
||||
|
||||
params.AddMetadata("last_four", last4)
|
||||
params.AddMetadata("card_type", cardType)
|
||||
params.AddExpand("latest_invoice.payment_intent")
|
||||
subscription, err := subscription.New(params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return subscription, nil
|
||||
}
|
||||
|
||||
func (c *Card) CreateCustomer(pm, email string) (*stripe.Customer, string, error) {
|
||||
stripe.Key = c.Secret
|
||||
customerParams := &stripe.CustomerParams{
|
||||
PaymentMethod: stripe.String(pm),
|
||||
Email: stripe.String(email),
|
||||
InvoiceSettings: &stripe.CustomerInvoiceSettingsParams{
|
||||
DefaultPaymentMethod: stripe.String(pm),
|
||||
},
|
||||
}
|
||||
|
||||
cust, err := customer.New(customerParams)
|
||||
if err != nil {
|
||||
msg := ""
|
||||
if stripeErr, ok := err.(*stripe.Error); ok {
|
||||
msg = cardErrorMessage(stripeErr.Code)
|
||||
}
|
||||
return nil, msg, err
|
||||
}
|
||||
return cust, "", nil
|
||||
}
|
||||
|
||||
func (c *Card) Refund(pi string, amount int) error {
|
||||
stripe.Key = c.Secret
|
||||
amountToRefund := int64(amount)
|
||||
|
||||
refundParams := &stripe.RefundParams{
|
||||
Amount: &amountToRefund,
|
||||
PaymentIntent: &pi,
|
||||
}
|
||||
|
||||
_, err := refund.New(refundParams)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Card) CancelSubscription(subID string) error {
|
||||
stripe.Key = c.Secret
|
||||
|
||||
params := &stripe.SubscriptionParams{
|
||||
CancelAtPeriodEnd: stripe.Bool(true),
|
||||
}
|
||||
|
||||
_, err := subscription.Update(subID, params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func cardErrorMessage(code stripe.ErrorCode) string {
|
||||
msg := ""
|
||||
|
||||
|
@ -1,54 +0,0 @@
|
||||
package encryption
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"io"
|
||||
)
|
||||
|
||||
type Encryption struct {
|
||||
Key []byte
|
||||
}
|
||||
|
||||
func (e *Encryption) Encrypt(text string) (string, error) {
|
||||
plaintext := []byte(text)
|
||||
|
||||
block, err := aes.NewCipher(e.Key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
cipherText := make([]byte, aes.BlockSize+len(plaintext))
|
||||
iv := cipherText[:aes.BlockSize]
|
||||
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
stream := cipher.NewCFBEncrypter(block, iv)
|
||||
stream.XORKeyStream(cipherText[aes.BlockSize:], plaintext)
|
||||
|
||||
return base64.URLEncoding.EncodeToString(cipherText), nil
|
||||
}
|
||||
|
||||
func (e *Encryption) Decrypt(cryptoText string) (string, error) {
|
||||
cipherText, _ := base64.URLEncoding.DecodeString(cryptoText)
|
||||
|
||||
block, err := aes.NewCipher(e.Key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if len(cipherText) < aes.BlockSize {
|
||||
return "", err
|
||||
}
|
||||
|
||||
iv := cipherText[:aes.BlockSize]
|
||||
cipherText = cipherText[aes.BlockSize:]
|
||||
|
||||
stream := cipher.NewCFBDecrypter(block, iv)
|
||||
stream.XORKeyStream(cipherText, cipherText)
|
||||
|
||||
return string(cipherText), nil
|
||||
}
|
@ -19,8 +19,7 @@ func OpenDB(dsn string) (*sql.DB, error) {
|
||||
db.SetMaxOpenConns(10)
|
||||
db.SetMaxIdleConns(10)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
ctx, _ := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
err = db.PingContext(ctx)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
|
@ -1,643 +0,0 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
// DBModel is the type for database connection values
|
||||
type DBModel struct {
|
||||
DB *sql.DB
|
||||
}
|
||||
|
||||
// Models is the wrapper for all models
|
||||
type Models struct {
|
||||
DB DBModel
|
||||
}
|
||||
|
||||
// NewModels returns a model type with database connection pool
|
||||
func NewModels(db *sql.DB) Models {
|
||||
return Models{
|
||||
DB: DBModel{DB: db},
|
||||
}
|
||||
}
|
||||
|
||||
// Widget is the type for all widgets
|
||||
type Widget struct {
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
InventoryLevel int `json:"inventory_level"`
|
||||
Price int `json:"price"`
|
||||
CreatedAt time.Time `json:"-"`
|
||||
UpdatedAt time.Time `json:"-"`
|
||||
Image string `json:"image"`
|
||||
IsRecurring bool `json:"is_recurring"`
|
||||
PlanID string `json:"plan_id"`
|
||||
}
|
||||
|
||||
// Order is the type for all orders
|
||||
type Order struct {
|
||||
ID int `json:"id"`
|
||||
WidgetID int `json:"widget_id"`
|
||||
TransactionID int `json:"transaction_id"`
|
||||
CustomerID int `json:"customer_id"`
|
||||
StatusID int `json:"status_id"`
|
||||
Quantity int `json:"quantity"`
|
||||
Amount int `json:"amount"`
|
||||
CreatedAt time.Time `json:"-"`
|
||||
UpdatedAt time.Time `json:"-"`
|
||||
Widget Widget `json:"widget"`
|
||||
Transaction Transaction `json:"transaction"`
|
||||
Customer Customer `json:"customer"`
|
||||
}
|
||||
|
||||
// Status is the type for orders statuses
|
||||
type Status struct {
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
CreatedAt time.Time `json:"-"`
|
||||
UpdatedAt time.Time `json:"-"`
|
||||
}
|
||||
|
||||
// TransactionStatus is the type for transaction statuses
|
||||
type TransactionStatus struct {
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
CreatedAt time.Time `json:"-"`
|
||||
UpdatedAt time.Time `json:"-"`
|
||||
}
|
||||
|
||||
// Transaction is the type for transactions
|
||||
type Transaction struct {
|
||||
ID int `json:"id"`
|
||||
Amount int `json:"amount"`
|
||||
Currency string `json:"currency"`
|
||||
LastFour string `json:"last_four"`
|
||||
ExpiryMonth int `json:"expiry_month"`
|
||||
ExpiryYear int `json:"expiry_year"`
|
||||
PaymentIntent string `json:"payment_intent"`
|
||||
PaymentMethod string `json:"payment_method"`
|
||||
BankReturnCode string `json:"bank_return_code"`
|
||||
TransactionStatusID int `json:"transaction_status_id"`
|
||||
CreatedAt time.Time `json:"-"`
|
||||
UpdatedAt time.Time `json:"-"`
|
||||
}
|
||||
|
||||
// User is the type for users
|
||||
type User struct {
|
||||
ID int `json:"id"`
|
||||
FirstName string `json:"first_name"`
|
||||
LastName string `json:"last_name"`
|
||||
Email string `json:"email"`
|
||||
Password string `json:"password"`
|
||||
CreatedAt time.Time `json:"-"`
|
||||
UpdatedAt time.Time `json:"-"`
|
||||
}
|
||||
|
||||
// Customer is the type for customers
|
||||
type Customer struct {
|
||||
ID int `json:"id"`
|
||||
FirstName string `json:"first_name"`
|
||||
LastName string `json:"last_name"`
|
||||
Email string `json:"email"`
|
||||
CreatedAt time.Time `json:"-"`
|
||||
UpdatedAt time.Time `json:"-"`
|
||||
}
|
||||
|
||||
func (m *DBModel) GetWidget(id int) (Widget, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
var widget Widget
|
||||
|
||||
query := `SELECT id, name, description, inventory_level, price,
|
||||
coalesce(image, ''),
|
||||
is_recurring, plan_id,
|
||||
created_at, updated_at
|
||||
FROM widgets WHERE id = ?;`
|
||||
|
||||
row := m.DB.QueryRowContext(ctx, query, id)
|
||||
err := row.Scan(
|
||||
&widget.ID,
|
||||
&widget.Name,
|
||||
&widget.Description,
|
||||
&widget.InventoryLevel,
|
||||
&widget.Price,
|
||||
&widget.Image,
|
||||
&widget.IsRecurring,
|
||||
&widget.PlanID,
|
||||
&widget.CreatedAt,
|
||||
&widget.UpdatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
return widget, err
|
||||
}
|
||||
return widget, nil
|
||||
}
|
||||
|
||||
// InsertTransaction inserts a new txn, and returns its id
|
||||
func (m *DBModel) InsertTransaction(txn Transaction) (int, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
stmt := `INSERT INTO transactions
|
||||
(amount, currency, last_four, expiry_month, expiry_year,
|
||||
payment_intent, payment_method, bank_return_code,
|
||||
transaction_status_id, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);`
|
||||
|
||||
result, err := m.DB.ExecContext(ctx, stmt,
|
||||
txn.Amount,
|
||||
txn.Currency,
|
||||
txn.LastFour,
|
||||
txn.ExpiryMonth,
|
||||
txn.ExpiryYear,
|
||||
txn.PaymentIntent,
|
||||
txn.PaymentMethod,
|
||||
txn.BankReturnCode,
|
||||
txn.TransactionStatusID,
|
||||
time.Now(),
|
||||
time.Now(),
|
||||
)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
id, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return int(id), nil
|
||||
}
|
||||
|
||||
// InsertOrder inserts a new order, and returns its id
|
||||
func (m *DBModel) InsertOrder(order Order) (int, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
stmt := `INSERT INTO orders
|
||||
(widget_id, transaction_id, customer_id, status_id, quantity,
|
||||
amount, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?);`
|
||||
|
||||
result, err := m.DB.ExecContext(ctx, stmt,
|
||||
order.WidgetID,
|
||||
order.TransactionID,
|
||||
order.CustomerID,
|
||||
order.StatusID,
|
||||
order.Quantity,
|
||||
order.Amount,
|
||||
time.Now(),
|
||||
time.Now(),
|
||||
)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
id, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return int(id), nil
|
||||
}
|
||||
|
||||
// InsertCustomer inserts a new customer, and returns its id
|
||||
func (m *DBModel) InsertCustomer(customer Customer) (int, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
stmt := `INSERT INTO customers
|
||||
(first_name, last_name, email, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?);`
|
||||
|
||||
result, err := m.DB.ExecContext(ctx, stmt,
|
||||
customer.FirstName,
|
||||
customer.LastName,
|
||||
customer.Email,
|
||||
time.Now(),
|
||||
time.Now(),
|
||||
)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
id, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return int(id), nil
|
||||
}
|
||||
|
||||
// GetUserByEmail gets a user by email address
|
||||
func (m *DBModel) GetUserByEmail(email string) (User, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
email = strings.ToLower(email)
|
||||
var u User
|
||||
|
||||
query := `SELECT id, first_name, last_name, email, password, created_at, updated_at
|
||||
FROM users
|
||||
WHERE email = ?;`
|
||||
|
||||
row := m.DB.QueryRowContext(ctx, query, email)
|
||||
|
||||
err := row.Scan(
|
||||
&u.ID,
|
||||
&u.FirstName,
|
||||
&u.LastName,
|
||||
&u.Email,
|
||||
&u.Password,
|
||||
&u.CreatedAt,
|
||||
&u.UpdatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
return u, err
|
||||
}
|
||||
|
||||
return u, nil
|
||||
}
|
||||
|
||||
func (m *DBModel) Authenticate(email, password string) (int, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
var id int
|
||||
var hashedPassword string
|
||||
|
||||
row := m.DB.QueryRowContext(ctx, "SELECT id, password from users WHERE email = ?;", email)
|
||||
err := row.Scan(&id, &hashedPassword)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
err = bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
|
||||
if err == bcrypt.ErrMismatchedHashAndPassword {
|
||||
return 0, errors.New("incorrect password")
|
||||
} else if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func (m *DBModel) UpdatePasswordForUser(u User, hash string) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
stmt := `UPDATE users SET password = ? where id = ?;`
|
||||
|
||||
_, err := m.DB.ExecContext(ctx, stmt, hash, u.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *DBModel) GetAllOrders(isRecurring bool) ([]*Order, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
orders := []*Order{}
|
||||
|
||||
query := `
|
||||
SELECT
|
||||
o.id, o.widget_id, o.transaction_id, o.customer_id,
|
||||
o.status_id, o.quantity, o.amount, o.created_at, o.updated_at,
|
||||
w.id, w.name, t.id, t.amount, t.currency, t.last_four,
|
||||
t.expiry_month, t.expiry_year, t.payment_intent, t.bank_return_code,
|
||||
c.id, c.first_name, c.last_name, c.email
|
||||
FROM orders o
|
||||
LEFT JOIN widgets w on (o.widget_id = w.id)
|
||||
LEFT JOIN transactions t on (o.transaction_id = t.id)
|
||||
LEFT JOIN customers c on (o.customer_id = c.id)
|
||||
WHERE
|
||||
w.is_recurring = ?
|
||||
ORDER BY
|
||||
o.created_at DESC;
|
||||
`
|
||||
|
||||
rows, err := m.DB.QueryContext(ctx, query, isRecurring)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var o Order
|
||||
err = rows.Scan(
|
||||
&o.ID,
|
||||
&o.WidgetID,
|
||||
&o.TransactionID,
|
||||
&o.CustomerID,
|
||||
&o.StatusID,
|
||||
&o.Quantity,
|
||||
&o.Amount,
|
||||
&o.CreatedAt,
|
||||
&o.UpdatedAt,
|
||||
&o.Widget.ID,
|
||||
&o.Widget.Name,
|
||||
&o.Transaction.ID,
|
||||
&o.Transaction.Amount,
|
||||
&o.Transaction.Currency,
|
||||
&o.Transaction.LastFour,
|
||||
&o.Transaction.ExpiryMonth,
|
||||
&o.Transaction.ExpiryYear,
|
||||
&o.Transaction.PaymentIntent,
|
||||
&o.Transaction.BankReturnCode,
|
||||
&o.Customer.ID,
|
||||
&o.Customer.FirstName,
|
||||
&o.Customer.LastName,
|
||||
&o.Customer.Email,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
orders = append(orders, &o)
|
||||
}
|
||||
return orders, nil
|
||||
}
|
||||
|
||||
func (m *DBModel) GetAllOrdersPaginated(
|
||||
isRecurring bool,
|
||||
pageSize, page int,
|
||||
) ([]*Order, int, int, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
offset := (page - 1) * pageSize
|
||||
|
||||
orders := []*Order{}
|
||||
|
||||
query := `
|
||||
SELECT
|
||||
o.id, o.widget_id, o.transaction_id, o.customer_id,
|
||||
o.status_id, o.quantity, o.amount, o.created_at, o.updated_at,
|
||||
w.id, w.name, t.id, t.amount, t.currency, t.last_four,
|
||||
t.expiry_month, t.expiry_year, t.payment_intent, t.bank_return_code,
|
||||
c.id, c.first_name, c.last_name, c.email
|
||||
FROM orders o
|
||||
LEFT JOIN widgets w on (o.widget_id = w.id)
|
||||
LEFT JOIN transactions t on (o.transaction_id = t.id)
|
||||
LEFT JOIN customers c on (o.customer_id = c.id)
|
||||
WHERE
|
||||
w.is_recurring = ?
|
||||
ORDER BY
|
||||
o.created_at DESC
|
||||
LIMIT ? OFFSET ?;
|
||||
`
|
||||
|
||||
rows, err := m.DB.QueryContext(ctx, query, isRecurring, pageSize, offset)
|
||||
if err != nil {
|
||||
return nil, 0, 0, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var o Order
|
||||
err = rows.Scan(
|
||||
&o.ID,
|
||||
&o.WidgetID,
|
||||
&o.TransactionID,
|
||||
&o.CustomerID,
|
||||
&o.StatusID,
|
||||
&o.Quantity,
|
||||
&o.Amount,
|
||||
&o.CreatedAt,
|
||||
&o.UpdatedAt,
|
||||
&o.Widget.ID,
|
||||
&o.Widget.Name,
|
||||
&o.Transaction.ID,
|
||||
&o.Transaction.Amount,
|
||||
&o.Transaction.Currency,
|
||||
&o.Transaction.LastFour,
|
||||
&o.Transaction.ExpiryMonth,
|
||||
&o.Transaction.ExpiryYear,
|
||||
&o.Transaction.PaymentIntent,
|
||||
&o.Transaction.BankReturnCode,
|
||||
&o.Customer.ID,
|
||||
&o.Customer.FirstName,
|
||||
&o.Customer.LastName,
|
||||
&o.Customer.Email,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, 0, 0, err
|
||||
}
|
||||
orders = append(orders, &o)
|
||||
}
|
||||
|
||||
query = `
|
||||
SELECT COUNT(o.id)
|
||||
FROM orders o
|
||||
LEFT JOIN widgets w on (o.widget_id = w.id)
|
||||
WHERE w.is_recurring = ?;
|
||||
`
|
||||
|
||||
var totalRecords int
|
||||
countRow := m.DB.QueryRowContext(ctx, query, isRecurring)
|
||||
err = countRow.Scan(&totalRecords)
|
||||
if err != nil {
|
||||
return nil, 0, 0, err
|
||||
}
|
||||
|
||||
lastPage := totalRecords / pageSize
|
||||
if totalRecords%pageSize != 0 {
|
||||
lastPage++
|
||||
}
|
||||
|
||||
return orders, lastPage, totalRecords, nil
|
||||
}
|
||||
|
||||
func (m *DBModel) GetOrderByID(ID int) (Order, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
query := `
|
||||
SELECT
|
||||
o.id, o.widget_id, o.transaction_id, o.customer_id,
|
||||
o.status_id, o.quantity, o.amount, o.created_at, o.updated_at,
|
||||
w.id, w.name, t.id, t.amount, t.currency, t.last_four,
|
||||
t.expiry_month, t.expiry_year, t.payment_intent, t.bank_return_code,
|
||||
c.id, c.first_name, c.last_name, c.email
|
||||
FROM orders o
|
||||
LEFT JOIN widgets w on (o.widget_id = w.id)
|
||||
LEFT JOIN transactions t on (o.transaction_id = t.id)
|
||||
LEFT JOIN customers c on (o.customer_id = c.id)
|
||||
WHERE
|
||||
o.id = ?;
|
||||
`
|
||||
|
||||
var o Order
|
||||
row := m.DB.QueryRowContext(ctx, query, ID)
|
||||
|
||||
err := row.Scan(
|
||||
&o.ID,
|
||||
&o.WidgetID,
|
||||
&o.TransactionID,
|
||||
&o.CustomerID,
|
||||
&o.StatusID,
|
||||
&o.Quantity,
|
||||
&o.Amount,
|
||||
&o.CreatedAt,
|
||||
&o.UpdatedAt,
|
||||
&o.Widget.ID,
|
||||
&o.Widget.Name,
|
||||
&o.Transaction.ID,
|
||||
&o.Transaction.Amount,
|
||||
&o.Transaction.Currency,
|
||||
&o.Transaction.LastFour,
|
||||
&o.Transaction.ExpiryMonth,
|
||||
&o.Transaction.ExpiryYear,
|
||||
&o.Transaction.PaymentIntent,
|
||||
&o.Transaction.BankReturnCode,
|
||||
&o.Customer.ID,
|
||||
&o.Customer.FirstName,
|
||||
&o.Customer.LastName,
|
||||
&o.Customer.Email,
|
||||
)
|
||||
if err != nil {
|
||||
return o, err
|
||||
}
|
||||
return o, nil
|
||||
}
|
||||
|
||||
func (m *DBModel) UpdateOrderStatus(id, statusID int) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
stmt := `UPDATE orders SET status_id = ? where id = ?`
|
||||
|
||||
_, err := m.DB.ExecContext(ctx, stmt, statusID, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *DBModel) GetAllUsers() ([]*User, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
var users []*User
|
||||
query := `
|
||||
SELECT id, last_name, first_name, email, created_at, updated_at
|
||||
FROM users
|
||||
ORDER BY last_name, first_name;
|
||||
`
|
||||
|
||||
rows, err := m.DB.QueryContext(ctx, query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var u User
|
||||
err = rows.Scan(
|
||||
&u.ID,
|
||||
&u.LastName,
|
||||
&u.FirstName,
|
||||
&u.Email,
|
||||
&u.CreatedAt,
|
||||
&u.UpdatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
users = append(users, &u)
|
||||
}
|
||||
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func (m *DBModel) GetOneUser(id int) (User, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
var u User
|
||||
query := `
|
||||
SELECT id, last_name, first_name, email, created_at, updated_at
|
||||
FROM users
|
||||
WHERE id = ?;
|
||||
`
|
||||
|
||||
row := m.DB.QueryRowContext(ctx, query, id)
|
||||
|
||||
err := row.Scan(
|
||||
&u.ID,
|
||||
&u.LastName,
|
||||
&u.FirstName,
|
||||
&u.Email,
|
||||
&u.CreatedAt,
|
||||
&u.UpdatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
return u, err
|
||||
}
|
||||
|
||||
return u, nil
|
||||
}
|
||||
|
||||
func (m *DBModel) EditUser(u User) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
stmt := `
|
||||
UPDATE users
|
||||
SET last_name = ?, first_name = ?, email = ?, updated_at = ?
|
||||
WHERE id = ?;
|
||||
`
|
||||
|
||||
_, err := m.DB.ExecContext(ctx, stmt, u.LastName, u.FirstName, u.Email, time.Now(), u.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *DBModel) AddUser(u User, hash string) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
stmt := `
|
||||
INSERT INTO users
|
||||
(last_name, first_name, email, password, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?);
|
||||
`
|
||||
|
||||
_, err := m.DB.ExecContext(ctx, stmt,
|
||||
u.LastName, u.FirstName, u.Email, hash, time.Now(), time.Now())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *DBModel) DeleteUser(id int) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
stmt := `
|
||||
DELETE FROM users
|
||||
WHERE id = ?;
|
||||
`
|
||||
|
||||
_, err := m.DB.ExecContext(ctx, stmt, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
stmt = `
|
||||
DELETE FROM tokens
|
||||
WHERE id = ?;
|
||||
`
|
||||
_, err = m.DB.ExecContext(ctx, stmt, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -1,98 +0,0 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/base32"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
ScopeAuthentication = "authentication"
|
||||
)
|
||||
|
||||
// Token is the type for authentication tokens
|
||||
type Token struct {
|
||||
PlainText string `json:"token"`
|
||||
UserID int64 `json:"-"`
|
||||
Hash []byte `json:"-"`
|
||||
Expiry time.Time `json:"expiry"`
|
||||
Scope string `json:"-"`
|
||||
}
|
||||
|
||||
// GenerateToken Generates a token that lasts for ttl, and returns it
|
||||
func GenerateToken(userID int, ttl time.Duration, scope string) (*Token, error) {
|
||||
token := &Token{
|
||||
UserID: int64(userID),
|
||||
Expiry: time.Now().Add(ttl),
|
||||
Scope: scope,
|
||||
}
|
||||
|
||||
randomBytes := make([]byte, 16)
|
||||
_, err := rand.Read(randomBytes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
token.PlainText = base32.StdEncoding.WithPadding((base32.NoPadding)).
|
||||
EncodeToString((randomBytes))
|
||||
hash := sha256.Sum256([]byte(token.PlainText))
|
||||
token.Hash = hash[:]
|
||||
return token, nil
|
||||
}
|
||||
|
||||
func (m *DBModel) InsertToken(t *Token, u User) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
// delete existing tokens
|
||||
stmt := `DELETE FROM tokens WHERE user_id = ?`
|
||||
|
||||
_, err := m.DB.ExecContext(ctx, stmt, u.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
stmt = `INSERT INTO tokens
|
||||
(user_id, name, email, token_hash, expiry_at, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)`
|
||||
|
||||
_, err = m.DB.ExecContext(ctx, stmt,
|
||||
u.ID,
|
||||
u.LastName,
|
||||
u.Email,
|
||||
t.Hash,
|
||||
t.Expiry,
|
||||
time.Now(),
|
||||
time.Now(),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *DBModel) GetUserForToken(token string) (*User, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
tokenHash := sha256.Sum256([]byte(token))
|
||||
var user User
|
||||
|
||||
query := `SELECT u.id, u.first_name, u.last_name, u.email
|
||||
FROM users u
|
||||
INNER JOIN tokens t on (u.id = t.user_id)
|
||||
WHERE t.token_hash = ? AND t.expiry_at > ?`
|
||||
|
||||
err := m.DB.QueryRowContext(ctx, query, tokenHash[:], time.Now()).Scan(
|
||||
&user.ID,
|
||||
&user.FirstName,
|
||||
&user.LastName,
|
||||
&user.Email,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &user, nil
|
||||
}
|
@ -1,45 +0,0 @@
|
||||
package urlsigner
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
goalone "github.com/bwmarrin/go-alone"
|
||||
)
|
||||
|
||||
type Signer struct {
|
||||
Secret []byte
|
||||
}
|
||||
|
||||
func (s *Signer) GenerateTokenFromString(data string) string {
|
||||
var urlToSign string
|
||||
|
||||
crypt := goalone.New(s.Secret, goalone.Timestamp)
|
||||
if strings.Contains(data, "?") {
|
||||
urlToSign = fmt.Sprintf("%s&hash=", data)
|
||||
} else {
|
||||
urlToSign = fmt.Sprintf("%s?hash=", data)
|
||||
}
|
||||
|
||||
tokenBytes := crypt.Sign([]byte(urlToSign))
|
||||
token := string(tokenBytes)
|
||||
return token
|
||||
}
|
||||
|
||||
func (s *Signer) VerifyToken(token string) bool {
|
||||
crypt := goalone.New(s.Secret, goalone.Timestamp)
|
||||
_, err := crypt.Unsign([]byte(token))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (s *Signer) Expired(token string, minutesUntilExpire int) bool {
|
||||
crypt := goalone.New(s.Secret, goalone.Timestamp)
|
||||
ts := crypt.Parse([]byte(token))
|
||||
|
||||
return time.Since(ts.Timestamp) > time.Duration(minutesUntilExpire)*time.Minute
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
package validator
|
||||
|
||||
type Validator struct {
|
||||
Errors map[string]string
|
||||
}
|
||||
|
||||
func New() *Validator {
|
||||
return &Validator{Errors: make(map[string]string)}
|
||||
}
|
||||
|
||||
func (v *Validator) Valid() bool {
|
||||
return len(v.Errors) == 0
|
||||
}
|
||||
|
||||
func (v *Validator) AddError(key, message string) {
|
||||
if _, exists := v.Errors[key]; !exists {
|
||||
v.Errors[key] = message
|
||||
}
|
||||
}
|
||||
|
||||
func (v *Validator) Check(ok bool, key, message string) {
|
||||
if !ok {
|
||||
v.AddError(key, message)
|
||||
}
|
||||
}
|
BIN
migrations/.DS_Store
vendored
BIN
migrations/.DS_Store
vendored
Binary file not shown.
@ -1,11 +0,0 @@
|
||||
create_table("widgets") {
|
||||
t.Column("id", "integer", {primary: true})
|
||||
t.Column("name", "string", {"default": ""})
|
||||
t.Column("description", "text", {"default": ""})
|
||||
t.Column("inventory_level", "integer", {})
|
||||
t.Column("price", "integer", {})
|
||||
}
|
||||
|
||||
sql("alter table widgets alter column created_at set default now();")
|
||||
sql("alter table widgets alter column updated_at set default now();")
|
||||
|
@ -1,13 +0,0 @@
|
||||
create_table("transaction_statuses") {
|
||||
t.Column("id", "integer", {primary: true})
|
||||
t.Column("name", "string", {})
|
||||
}
|
||||
|
||||
sql("alter table transaction_statuses alter column created_at set default now();")
|
||||
sql("alter table transaction_statuses alter column updated_at set default now();")
|
||||
|
||||
sql("insert into transaction_statuses (name) values ('Pending');")
|
||||
sql("insert into transaction_statuses (name) values ('Cleared');")
|
||||
sql("insert into transaction_statuses (name) values ('Declined');")
|
||||
sql("insert into transaction_statuses (name) values ('Refunded');")
|
||||
sql("insert into transaction_statuses (name) values ('Partially refunded');")
|
@ -1 +0,0 @@
|
||||
drop_table("transactions")
|
@ -1,16 +0,0 @@
|
||||
create_table("transactions") {
|
||||
t.Column("id", "integer", {primary: true})
|
||||
t.Column("amount", "integer", {})
|
||||
t.Column("currency", "string", {})
|
||||
t.Column("last_four", "string", {})
|
||||
t.Column("bank_return_code", "string", {})
|
||||
t.Column("transaction_status_id", "integer", {"unsigned": true})
|
||||
}
|
||||
|
||||
sql("alter table transactions alter column created_at set default now();")
|
||||
sql("alter table transactions alter column updated_at set default now();")
|
||||
|
||||
add_foreign_key("transactions", "transaction_status_id", {"transaction_statuses": ["id"]}, {
|
||||
"on_delete": "cascade",
|
||||
"on_update": "cascade",
|
||||
})
|
@ -1 +0,0 @@
|
||||
drop_table("orders")
|
@ -1,21 +0,0 @@
|
||||
create_table("orders") {
|
||||
t.Column("id", "integer", {primary: true})
|
||||
t.Column("widget_id", "integer", {"unsigned":true})
|
||||
t.Column("transaction_id", "integer", {"unsigned":true})
|
||||
t.Column("status_id", "integer", {"unsigned":true})
|
||||
t.Column("quantity", "integer", {})
|
||||
t.Column("amount", "integer", {})
|
||||
}
|
||||
|
||||
sql("alter table orders alter column created_at set default now();")
|
||||
sql("alter table orders alter column updated_at set default now();")
|
||||
|
||||
add_foreign_key("orders", "widget_id", {"widgets": ["id"]}, {
|
||||
"on_delete": "cascade",
|
||||
"on_update": "cascade",
|
||||
})
|
||||
|
||||
add_foreign_key("orders", "transaction_id", {"transactions": ["id"]}, {
|
||||
"on_delete": "cascade",
|
||||
"on_update": "cascade",
|
||||
})
|
@ -1 +0,0 @@
|
||||
drop_table("statuses")
|
@ -1,16 +0,0 @@
|
||||
create_table("statuses") {
|
||||
t.Column("id", "integer", {primary: true})
|
||||
t.Column("name", "string", {})
|
||||
}
|
||||
|
||||
sql("alter table statuses alter column created_at set default now();")
|
||||
sql("alter table statuses alter column updated_at set default now();")
|
||||
|
||||
sql("insert into statuses (name) values ('Cleared');")
|
||||
sql("insert into statuses (name) values ('Refunded');")
|
||||
sql("insert into statuses (name) values ('Cancelled');")
|
||||
|
||||
add_foreign_key("orders", "status_id", {"statuses": ["id"]}, {
|
||||
"on_delete": "cascade",
|
||||
"on_update": "cascade",
|
||||
})
|
@ -1 +0,0 @@
|
||||
drop_table("users")
|
@ -1,12 +0,0 @@
|
||||
create_table("users") {
|
||||
t.Column("id", "integer", {primary: true})
|
||||
t.Column("first_name", "string", {"size": 255})
|
||||
t.Column("last_name", "string", {"size": 255})
|
||||
t.Column("email", "string", {})
|
||||
t.Column("password", "string", {"size": 60})
|
||||
}
|
||||
|
||||
sql("alter table users alter column created_at set default now();")
|
||||
sql("alter table users alter column updated_at set default now();")
|
||||
|
||||
sql("insert into users (first_name, last_name, email, password) values ('Admin','User','admin@example.com', '$2a$12$VR1wDmweaF3ZTVgEHiJrNOSi8VcS4j0eamr96A/7iOe8vlum3O3/q');")
|
@ -1 +0,0 @@
|
||||
drop_column("widgets", "image")
|
@ -1 +0,0 @@
|
||||
add_column("widgets", "image", "string", {"default":""})
|
@ -1 +0,0 @@
|
||||
drop_table("customers")
|
@ -1,9 +0,0 @@
|
||||
create_table("customers") {
|
||||
t.Column("id", "integer", {primary: true})
|
||||
t.Column("first_name", "string", {"size": 255})
|
||||
t.Column("last_name", "string", {"size": 255})
|
||||
t.Column("email", "string", {})
|
||||
}
|
||||
|
||||
sql("alter table customers alter column created_at set default now();")
|
||||
sql("alter table customers alter column updated_at set default now();")
|
@ -1,3 +0,0 @@
|
||||
drop_column("transactions", "expiry_month")
|
||||
drop_column("transactions", "expiry_year")
|
||||
|
@ -1,3 +0,0 @@
|
||||
add_column("transactions", "expiry_month", "integer", {"default":0})
|
||||
add_column("transactions", "expiry_year", "integer", {"default":0})
|
||||
|
@ -1 +0,0 @@
|
||||
drop_column("orders", "customer_id")
|
@ -1,6 +0,0 @@
|
||||
add_column("orders", "customer_id", "integer", {"unsigned":true})
|
||||
|
||||
add_foreign_key("orders", "customer_id", {"customers": ["id"]}, {
|
||||
"on_delete": "cascade",
|
||||
"on_update": "cascade",
|
||||
})
|
@ -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":""})
|
||||
|
@ -1,2 +0,0 @@
|
||||
drop_column("widgets", "is_recurring")
|
||||
drop_column("widgets", "plan_id")
|
@ -1,2 +0,0 @@
|
||||
add_column("widgets", "is_recurring", "bool", {"default": 0})
|
||||
add_column("widgets", "plan_id", "string", {"default": ""})
|
@ -1,2 +0,0 @@
|
||||
drop_table("tokens")
|
||||
|
@ -1,11 +0,0 @@
|
||||
create_table("tokens") {
|
||||
t.Column("id", "integer", {primary: true})
|
||||
t.Column("user_id", "integer", {"unsigned": true})
|
||||
t.Column("name", "string", {"size": 255})
|
||||
t.Column("email", "string", {})
|
||||
t.Column("token_hash", "string", {})
|
||||
}
|
||||
|
||||
sql("ALTER TABLE tokens MODIFY token_hash varbinary(255);")
|
||||
sql("ALTER TABLE tokens ALTER COLUMN created_at SET DEFAULT now();")
|
||||
sql("ALTER TABLE tokens ALTER COLUMN updated_at SET DEFAULT now();")
|
@ -1 +0,0 @@
|
||||
drop_column("tokens, "expiry_at")
|
@ -1,2 +0,0 @@
|
||||
add_column("tokens", "expiry_at", "timestamp", {})
|
||||
|
@ -1 +0,0 @@
|
||||
DROP TABLE sessions;
|
@ -1,7 +0,0 @@
|
||||
CREATE TABLE sessions (
|
||||
token CHAR(43) PRIMARY KEY,
|
||||
data BLOB NOT NULL,
|
||||
expiry TIMESTAMP(6) NOT NULL
|
||||
);
|
||||
|
||||
CREATE INDEX sessions_expiry_idx ON sessions (expiry);
|
@ -1 +0,0 @@
|
||||
drop_table("widgets")
|
@ -1,3 +0,0 @@
|
||||
sql("insert into widgets (name, description, inventory_level, price, created_at, updated_at, image, is_recurring, plan_id) values ('Widget', 'A very nice widget.', 10, 1000, now(), now(), '/static/img/widget.jpeg', 0, '');")
|
||||
sql("insert into widgets (name, description, inventory_level, price, created_at, updated_at, image, is_recurring, plan_id) values ('Bronze Plan', 'Get three widgets for the price of two every month', 10, 2000, now(), now(), '', 1, 'price_1Pmhmi2Mpv4bW2TDiyY6UsrE');")
|
||||
|
Binary file not shown.
@ -1,75 +0,0 @@
|
||||
import { formatCurrency, paginator } from "./common.js"
|
||||
|
||||
//TODO: This should be put into the localStorage if we click into a sale and
|
||||
//get back, we may want to stay at the same page before.
|
||||
export let currentPage = 1;
|
||||
|
||||
export function showTable(api, ps, cp) {
|
||||
const token = localStorage.getItem("token");
|
||||
const tbody = document.getElementById("sales-table").getElementsByTagName("tbody")[0];
|
||||
|
||||
// reset tbody
|
||||
tbody.innerHTML = ``
|
||||
|
||||
const body = {
|
||||
page_size: parseInt(ps, 10),
|
||||
page: parseInt(cp, 10),
|
||||
};
|
||||
|
||||
const requestOptions = {
|
||||
method: 'post',
|
||||
headers: {
|
||||
'Accept': `application/json`,
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Bearer ' + token,
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
};
|
||||
|
||||
fetch(api + "/api/admin/all-sales", requestOptions)
|
||||
.then(response => response.json())
|
||||
.then(function (data) {
|
||||
if (data.orders) {
|
||||
data.orders.forEach(function (i) {
|
||||
let newRow = tbody.insertRow();
|
||||
let newCell = newRow.insertCell();
|
||||
|
||||
newCell.innerHTML = `<a href="#!">Order ${i.id}</a>`;
|
||||
newCell.addEventListener("click", function (evt) {
|
||||
// put the current_page into sessionStorage
|
||||
sessionStorage.setItem("cur-page", data.current_page);
|
||||
|
||||
// redirect
|
||||
location.href = `/admin/sales/${i.id}`;
|
||||
});
|
||||
|
||||
newCell = newRow.insertCell();
|
||||
let item = document.createTextNode(i.customer.last_name + ", " + i.customer.first_name);
|
||||
newCell.appendChild(item)
|
||||
|
||||
newCell = newRow.insertCell();
|
||||
item = document.createTextNode(i.widget.name);
|
||||
newCell.appendChild(item)
|
||||
|
||||
let cur = formatCurrency(i.transaction.amount)
|
||||
newCell = newRow.insertCell();
|
||||
item = document.createTextNode(cur);
|
||||
newCell.appendChild(item)
|
||||
|
||||
newCell = newRow.insertCell();
|
||||
if (i.status_id != 1) {
|
||||
newCell.innerHTML = `<span class="badge bg-danger">Refunded</span>`
|
||||
} else {
|
||||
newCell.innerHTML = `<span class="badge bg-success">Charged</span>`
|
||||
}
|
||||
paginator(api, data.last_page, data.current_page, showTable)
|
||||
});
|
||||
} else {
|
||||
let newRow = tbody.insertRow();
|
||||
let newCell = newRow.insertCell();
|
||||
newCell.setAttribute("colspan", "5");
|
||||
newCell.innerHTML = "No data available";
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -1,73 +0,0 @@
|
||||
import { formatCurrency, paginator } from "./common.js"
|
||||
|
||||
export let currentPage = 1;
|
||||
|
||||
export function showTable(api, ps, cp) {
|
||||
let token = localStorage.getItem("token");
|
||||
let tbody = document.getElementById("subscriptions-table").getElementsByTagName("tbody")[0];
|
||||
|
||||
// reset tbody
|
||||
tbody.innerHTML = ``
|
||||
|
||||
const body = {
|
||||
page_size: parseInt(ps, 10),
|
||||
page: parseInt(cp, 10),
|
||||
};
|
||||
|
||||
const requestOptions = {
|
||||
method: 'post',
|
||||
headers: {
|
||||
'Accept': `application/json`,
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Bearer ' + token,
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
};
|
||||
|
||||
fetch(api + "/api/admin/all-subscriptions", requestOptions)
|
||||
.then(response => response.json())
|
||||
.then(function (data) {
|
||||
if (data.orders) {
|
||||
data.orders.forEach(function (i) {
|
||||
let newRow = tbody.insertRow();
|
||||
let newCell = newRow.insertCell();
|
||||
|
||||
newCell.innerHTML = `<a href="#!">Order ${i.id}</a>`;
|
||||
newCell.addEventListener("click", function (evt) {
|
||||
// put the current_page into sessionStorage
|
||||
sessionStorage.setItem("cur-page", data.current_page);
|
||||
|
||||
// redirect
|
||||
location.href = `/admin/subscriptions/${i.id}`;
|
||||
});
|
||||
|
||||
newCell = newRow.insertCell();
|
||||
let item = document.createTextNode(i.customer.last_name + ", " + i.customer.first_name);
|
||||
newCell.appendChild(item)
|
||||
|
||||
newCell = newRow.insertCell();
|
||||
item = document.createTextNode(i.widget.name);
|
||||
newCell.appendChild(item)
|
||||
|
||||
let cur = formatCurrency(i.transaction.amount)
|
||||
newCell = newRow.insertCell();
|
||||
item = document.createTextNode(cur + "/month");
|
||||
newCell.appendChild(item)
|
||||
|
||||
newCell = newRow.insertCell();
|
||||
if (i.status_id != 1) {
|
||||
newCell.innerHTML = `<span class="badge bg-danger">Cancelled</span>`
|
||||
} else {
|
||||
newCell.innerHTML = `<span class="badge bg-success">Charged</span>`
|
||||
}
|
||||
paginator(api, data.last_page, data.current_page, showTable)
|
||||
});
|
||||
} else {
|
||||
let newRow = tbody.insertRow();
|
||||
let newCell = newRow.insertCell();
|
||||
newCell.setAttribute("colspan", "4");
|
||||
newCell.innerHTML = "No data available";
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -1,48 +0,0 @@
|
||||
export let socket;
|
||||
|
||||
export function wsConn(is_authenticated, user_id) {
|
||||
if (is_authenticated !== 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
socket = new WebSocket("ws://localhost:4000/ws")
|
||||
socket.onopen = () => {
|
||||
console.log("Successfully connected to websockets")
|
||||
}
|
||||
|
||||
socket.onclose = event => {};
|
||||
socket.onerror = error => {};
|
||||
|
||||
socket.onmessage = msg => {
|
||||
let data = JSON.parse(msg.data);
|
||||
|
||||
switch (data.action) {
|
||||
case "logout":
|
||||
if (data.user_id === user_id) {
|
||||
logout()
|
||||
}
|
||||
break;
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// let loginLink = document.getElementById("login-link");
|
||||
// let vtLink = document.getElementById("vt-link");
|
||||
//
|
||||
// document.addEventListener("DOMContentLoaded", function () {
|
||||
// if (localStorage.getItem("token") !== null) {
|
||||
// loginLink.innerHTML = '<a href="#!" onclick="logout()" class="nav-link">Logout</a>';
|
||||
// vtLink.classList.remove('d-none');
|
||||
// } else {
|
||||
// loginLink.innerHTML = '<a href="/login" class="nav-link">Login</a>';
|
||||
// }
|
||||
// loginLink.classList.remove('d-none')
|
||||
// });
|
||||
|
||||
function logout() {
|
||||
localStorage.removeItem("token");
|
||||
localStorage.removeItem("token_expiry");
|
||||
location.href = "/logout";
|
||||
}
|
||||
|
@ -1,137 +0,0 @@
|
||||
export let card;
|
||||
export let stripe;
|
||||
const payButton = document.getElementById("pay-button");
|
||||
export const processing = document.getElementById("processing-payment");
|
||||
|
||||
export let pageSize = 2;
|
||||
|
||||
export function hidePayButton() {
|
||||
payButton.classList.add("d-none");
|
||||
processing.classList.remove("d-none");
|
||||
}
|
||||
|
||||
export function showPayButton() {
|
||||
payButton.classList.remove("d-none");
|
||||
processing.classList.add("d-none");
|
||||
}
|
||||
|
||||
export function showError(element, msg) {
|
||||
const messages = document.getElementById(element);
|
||||
messages.classList.add("alert-danger");
|
||||
messages.classList.remove("alert-success");
|
||||
messages.classList.remove("d-none");
|
||||
messages.innerText = msg;
|
||||
}
|
||||
|
||||
export function showSuccess(element, msg) {
|
||||
const messages = document.getElementById(element);
|
||||
messages.classList.add("alert-success");
|
||||
messages.classList.remove("alert-danger");
|
||||
messages.classList.remove("d-none");
|
||||
messages.innerText = msg;
|
||||
}
|
||||
|
||||
export function stripeInit(pubKey) {
|
||||
stripe = Stripe(pubKey);
|
||||
|
||||
(function () {
|
||||
// create stripe & elements
|
||||
const elements = stripe.elements();
|
||||
const style = {
|
||||
base: {
|
||||
fontSize: '16px',
|
||||
lineHeight: '24px',
|
||||
}
|
||||
};
|
||||
|
||||
// create card entry
|
||||
card = elements.create('card', {
|
||||
style: style,
|
||||
hidePostalCode: true,
|
||||
});
|
||||
card.mount("#card-element");
|
||||
|
||||
// check for input errors
|
||||
card.addEventListener('change', function (event) {
|
||||
var displayError = document.getElementById("card-errors");
|
||||
if (event.error) {
|
||||
displayError.classList.remove('d-none');
|
||||
displayError.textContent = event.error.message;
|
||||
} else {
|
||||
displayError.classList.add('d-none');
|
||||
displayError.textContent = "";
|
||||
}
|
||||
});
|
||||
})();
|
||||
}
|
||||
|
||||
export function checkAuth(api) {
|
||||
if (localStorage.getItem("token") === null) {
|
||||
location.href = "/login";
|
||||
return
|
||||
} else {
|
||||
let token = localStorage.getItem("token")
|
||||
const myHeaders = new Headers();
|
||||
myHeaders.append("Content-Type", "application/json");
|
||||
myHeaders.append("Authorization", "Bearer " + token);
|
||||
|
||||
const requestOptions = {
|
||||
method: "POST",
|
||||
headers: myHeaders,
|
||||
}
|
||||
|
||||
fetch(api + "/api/is-authenticated", requestOptions)
|
||||
.then(response => response.json())
|
||||
.then(function(data) {
|
||||
if (data.ok === false) {
|
||||
console.log("not logged in");
|
||||
location.href = "/login"
|
||||
} else {
|
||||
console.log("Logged in");
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export function formatCurrency(amount) {
|
||||
let c = parseFloat(amount/100);
|
||||
return c.toLocaleString("fr-FR", {
|
||||
style:"currency",
|
||||
currency: "EUR",
|
||||
});
|
||||
}
|
||||
|
||||
export function paginator(api, pages, curPage, showTable) {
|
||||
// Check the border case
|
||||
if (curPage >= pages) {
|
||||
curPage = pages
|
||||
}
|
||||
|
||||
const p = document.getElementById("paginator")
|
||||
let html = `<li class="page-item"><a href="#!" class="page-link pager" data-page="${curPage - 1}"><</a></li>`;
|
||||
|
||||
for (var i = 0; i < pages; i++) {
|
||||
if (i + 1 == curPage) {
|
||||
html += `<li class="page-item"><a href="#!" class="page-link pager active" data-page="${i + 1}">${i + 1}</a></li>`;
|
||||
|
||||
} else {
|
||||
|
||||
html += `<li class="page-item"><a href="#!" class="page-link pager" data-page="${i + 1}">${i + 1}</a></li>`;
|
||||
}
|
||||
}
|
||||
|
||||
html += `<li class="page-item"><a href="#!" class="page-link pager" data-page="${curPage + 1}">></a></li>`;
|
||||
|
||||
p.innerHTML = html;
|
||||
|
||||
let pageBtns = document.getElementsByClassName("pager");
|
||||
for (var j = 0; j < pageBtns.length; j++) {
|
||||
pageBtns[j].addEventListener("click", function (evt) {
|
||||
let desiredPage = evt.target.getAttribute("data-page");
|
||||
if ((desiredPage > 0) && (desiredPage <= pages)) {
|
||||
console.log("would go to page", desiredPage);
|
||||
showTable(api, pageSize, desiredPage);
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
@ -1,127 +0,0 @@
|
||||
import {
|
||||
showError,
|
||||
showSuccess,
|
||||
} from './common.js';
|
||||
|
||||
export function val(api) {
|
||||
let form = document.getElementById("login-form");
|
||||
|
||||
if (form.checkValidity() === false) {
|
||||
this.event.preventDefault();
|
||||
this.event.stopPropagation();
|
||||
form.classList.add("was-validated");
|
||||
return;
|
||||
}
|
||||
form.classList.add("was-validated");
|
||||
|
||||
let payload = {
|
||||
email: document.getElementById("email").value,
|
||||
password: document.getElementById("password").value,
|
||||
};
|
||||
|
||||
const requestOptions = {
|
||||
method: 'post',
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(payload),
|
||||
};
|
||||
|
||||
fetch(api + "/api/authenticate", requestOptions)
|
||||
.then(response => response.json())
|
||||
.then(response => {
|
||||
console.log(response)
|
||||
if (response.ok === true) {
|
||||
localStorage.setItem("token", response.authentication_token.token);
|
||||
localStorage.setItem("token_expiry", response.authentication_token.expiry);
|
||||
showSuccess("login-messages", "Login successful.")
|
||||
// location.href = "/";
|
||||
document.getElementById("login-form").submit()
|
||||
} else {
|
||||
showError("login-messages", response.message)
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
export function forgot(api) {
|
||||
let form = document.getElementById("forgot-form");
|
||||
|
||||
if (form.checkValidity() === false) {
|
||||
// this.event.preventDefault();
|
||||
// this.event.stopPropagation();
|
||||
form.classList.add("was-validated");
|
||||
return;
|
||||
}
|
||||
form.classList.add("was-validated");
|
||||
|
||||
let payload = {
|
||||
email: document.getElementById("email").value,
|
||||
};
|
||||
|
||||
const requestOptions = {
|
||||
method: 'post',
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(payload),
|
||||
};
|
||||
|
||||
fetch(api + "/api/forgot-password", requestOptions)
|
||||
.then(response => response.json())
|
||||
.then(response => {
|
||||
console.log(response)
|
||||
if (response.ok === true) {
|
||||
showSuccess("forgot-messages", "Password reset email sent")
|
||||
} else {
|
||||
showError("forgot-messages", response.message)
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function reset(api, email) {
|
||||
let form = document.getElementById("reset-form");
|
||||
|
||||
if (form.checkValidity() === false) {
|
||||
// this.event.preventDefault();
|
||||
// this.event.stopPropagation();
|
||||
form.classList.add("was-validated");
|
||||
return;
|
||||
}
|
||||
form.classList.add("was-validated");
|
||||
|
||||
if (document.getElementById("password").value !== document.getElementById("verify-password").value) {
|
||||
showError("reset-messages", "Passwords do not match.")
|
||||
return
|
||||
}
|
||||
|
||||
let payload = {
|
||||
password: document.getElementById("password").value,
|
||||
email: email,
|
||||
};
|
||||
|
||||
const requestOptions = {
|
||||
method: 'post',
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(payload),
|
||||
};
|
||||
|
||||
fetch(api + "/api/reset-password", requestOptions)
|
||||
.then(response => response.json())
|
||||
.then(response => {
|
||||
console.log(response)
|
||||
if (response.ok === true) {
|
||||
showSuccess("reset-messages", "Password reset")
|
||||
setTimeout(function () {
|
||||
location.href = "/login"
|
||||
}, 2000)
|
||||
} else {
|
||||
showError("reset-messages", response.message)
|
||||
}
|
||||
});
|
||||
}
|
@ -1,85 +0,0 @@
|
||||
import { formatCurrency, showError, showSuccess } from "./common.js"
|
||||
|
||||
const id = window.location.pathname.split("/").pop();
|
||||
const token = localStorage.getItem("token");
|
||||
|
||||
export function showInfo(api) {
|
||||
const requestOptions = {
|
||||
method: 'post',
|
||||
headers: {
|
||||
'Accept': `application/json`,
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Bearer ' + token,
|
||||
},
|
||||
};
|
||||
|
||||
fetch(api + "/api/admin/get-sale/" + id, requestOptions)
|
||||
.then(response => response.json())
|
||||
.then(function (data) {
|
||||
console.log(data);
|
||||
if (data) {
|
||||
document.getElementById("order-no").innerHTML = data.id
|
||||
document.getElementById("customer").innerHTML = data.customer.first_name + " " + data.customer.last_name
|
||||
document.getElementById("product").innerHTML = data.widget.name
|
||||
document.getElementById("quantity").innerHTML = data.quantity
|
||||
document.getElementById("amount").innerHTML = formatCurrency(data.transaction.amount)
|
||||
document.getElementById("pi").value = data.transaction.payment_intent;
|
||||
document.getElementById("charge-amount").value = data.transaction.amount;
|
||||
document.getElementById("currency").value = data.transaction.currency;
|
||||
if (data.status_id === 1) {
|
||||
document.getElementById("refund-btn").classList.remove("d-none");
|
||||
document.getElementById("refunded").classList.add("d-none");
|
||||
document.getElementById("charged").classList.remove("d-none");
|
||||
} else {
|
||||
document.getElementById("refund-btn").classList.add("d-none");
|
||||
document.getElementById("refunded").classList.remove("d-none");
|
||||
document.getElementById("charged").classList.add("d-none");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function refund(api, isRefund) {
|
||||
Swal.fire({
|
||||
title: "Are you sure?",
|
||||
text: "You won't be able to undo this!",
|
||||
icon: "warning",
|
||||
showCancelButton: true,
|
||||
confirmButtonColor: "#3085d6",
|
||||
cancelButtonColor: "#d33",
|
||||
confirmButtonText: isRefund === 1 ? "Refund" : "Cancel subscription"
|
||||
}).then((result) => {
|
||||
if (result.isConfirmed) {
|
||||
let payload = {
|
||||
pi: document.getElementById("pi").value,
|
||||
currency: document.getElementById("currency").value,
|
||||
amount: parseInt(document.getElementById("charge-amount").value, 10),
|
||||
id: parseInt(id, 10),
|
||||
};
|
||||
const requestOptions = {
|
||||
method: 'post',
|
||||
headers: {
|
||||
'Accept': `application/json`,
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Bearer ' + token,
|
||||
},
|
||||
body: JSON.stringify(payload),
|
||||
};
|
||||
|
||||
fetch(api, requestOptions)
|
||||
.then(response => response.json())
|
||||
.then(function (data) {
|
||||
console.log(data);
|
||||
if (!data.ok) {
|
||||
showError("messages", data.message)
|
||||
|
||||
} else {
|
||||
showSuccess("messages", isRefund === 1 ? "Refunded" : "Subscription cancelled")
|
||||
document.getElementById("refund-btn").classList.add("d-none");
|
||||
document.getElementById("refunded").classList.remove("d-none");
|
||||
document.getElementById("charged").classList.add("d-none");
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
@ -1,90 +0,0 @@
|
||||
import {
|
||||
hidePayButton,
|
||||
showPayButton,
|
||||
showError,
|
||||
showSuccess,
|
||||
stripe,
|
||||
card,
|
||||
processing,
|
||||
} from './common.js';
|
||||
|
||||
export function val(plan_id, api) {
|
||||
let form = document.getElementById("charge_form");
|
||||
|
||||
if (form.checkValidity() === false) {
|
||||
this.event.preventDefault();
|
||||
this.event.stopPropagation();
|
||||
form.classList.add("was-validated");
|
||||
return;
|
||||
}
|
||||
form.classList.add("was-validated");
|
||||
hidePayButton();
|
||||
|
||||
// let amountToCharge = document.getElementById("amount").value;
|
||||
|
||||
stripe.createPaymentMethod({
|
||||
type: 'card',
|
||||
card: card,
|
||||
billing_details: {
|
||||
email: document.getElementById("cardholder-email").value,
|
||||
},
|
||||
}).then((result) => {
|
||||
stripePaymentMethodHandler(result, plan_id, api);
|
||||
});
|
||||
}
|
||||
|
||||
function stripePaymentMethodHandler(result, plan_id, api) {
|
||||
if (result.error) {
|
||||
showError("card-messages", result.error.message);
|
||||
} else {
|
||||
// create a customer and subscribe to plan
|
||||
let payload = {
|
||||
product_id: document.getElementById("product_id").value,
|
||||
plan: plan_id,
|
||||
payment_method: result.paymentMethod.id,
|
||||
email: document.getElementById("cardholder-email").value,
|
||||
last_four: result.paymentMethod.card.last4,
|
||||
card_brand: result.paymentMethod.card.brand,
|
||||
expiry_month: result.paymentMethod.card.exp_month,
|
||||
expiry_year: result.paymentMethod.card.exp_year,
|
||||
first_name: document.getElementById("first_name").value,
|
||||
last_name: document.getElementById("last-name").value,
|
||||
amount: document.getElementById("amount").value,
|
||||
};
|
||||
|
||||
const requestOptions = {
|
||||
method: 'post',
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(payload),
|
||||
};
|
||||
|
||||
fetch(api + "/api/create-customer-and-subscribe-to-plan", requestOptions)
|
||||
.then(response => response.json())
|
||||
.then(function (data) {
|
||||
console.log(data);
|
||||
if (data.ok === false) {
|
||||
document.getElementById("charge_form").classList.remove("was-validated")
|
||||
Object.entries(data.errors).forEach((i) => {
|
||||
const [key, value] = i
|
||||
document.getElementById(key).classList.add("is-invalid");
|
||||
document.getElementById(key + "-help").classList.remove("valid-feedback");
|
||||
document.getElementById(key + "-help").classList.remove("d-none");
|
||||
document.getElementById(key + "-help").classList.add("invalid-feedback");
|
||||
document.getElementById(key + "-help").innerText = value;
|
||||
});
|
||||
showPayButton();
|
||||
return
|
||||
}
|
||||
processing.classList.add("d-none");
|
||||
showSuccess("card-messages", "Transaction successful!");
|
||||
sessionStorage.first_name = document.getElementById("first_name").value;
|
||||
sessionStorage.last_name = document.getElementById("last-name").value;
|
||||
sessionStorage.amount = document.getElementById("amount").value;
|
||||
sessionStorage.last_four = result.paymentMethod.card.last4;
|
||||
location.href = "/receipt/bronze";
|
||||
});
|
||||
}
|
||||
}
|
@ -1,79 +0,0 @@
|
||||
import {
|
||||
hidePayButton,
|
||||
showPayButton,
|
||||
showError,
|
||||
showSuccess,
|
||||
stripe,
|
||||
card,
|
||||
processing,
|
||||
} from './common.js';
|
||||
|
||||
export function val(api) {
|
||||
let form = document.getElementById("charge_form");
|
||||
|
||||
if (form.checkValidity() === false) {
|
||||
this.event.preventDefault();
|
||||
this.event.stopPropagation();
|
||||
form.classList.add("was-validated");
|
||||
return;
|
||||
}
|
||||
form.classList.add("was-validated");
|
||||
hidePayButton();
|
||||
|
||||
let amountToCharge = document.getElementById("amount").value;
|
||||
console.log(amountToCharge);
|
||||
let payload = {
|
||||
amount: amountToCharge,
|
||||
currency: 'eur',
|
||||
};
|
||||
|
||||
const requestOptions = {
|
||||
method: 'post',
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(payload),
|
||||
};
|
||||
|
||||
fetch(api + "/api/payment-intent", requestOptions)
|
||||
.then(response => response.text())
|
||||
.then(response => {
|
||||
let data;
|
||||
try {
|
||||
data = JSON.parse(response);
|
||||
stripe.confirmCardPayment(data.client_secret, {
|
||||
payment_method: {
|
||||
card: card,
|
||||
billing_details: {
|
||||
name: document.getElementById("cardholder-name").value,
|
||||
}
|
||||
}
|
||||
}).then(function (result) {
|
||||
if (result.error) {
|
||||
// card declined, or sth went wrong with the card
|
||||
showError("card-messages", result.error.message);
|
||||
showPayButton();
|
||||
} else if (result.paymentIntent) {
|
||||
if (result.paymentIntent.status === "succeeded") {
|
||||
// we have charged the card
|
||||
document.getElementById("payment_intent").value = result.paymentIntent.id;
|
||||
document.getElementById("payment_method").value = result.paymentIntent.payment_method;
|
||||
document.getElementById("payment_amount").value = result.paymentIntent.amount;
|
||||
document.getElementById("payment_currency").value = result.paymentIntent.currency;
|
||||
processing.classList.add("d-none");
|
||||
showSuccess("card-messages", "Trasaction successful!");
|
||||
document.getElementById("charge_form").submit();
|
||||
}
|
||||
}
|
||||
})
|
||||
console.log(data);
|
||||
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
showCardError("Invalid response from payment gateway!");
|
||||
showPayButton();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -1,105 +0,0 @@
|
||||
import {
|
||||
hidePayButton,
|
||||
showPayButton,
|
||||
showError,
|
||||
showSuccess,
|
||||
stripe,
|
||||
card,
|
||||
processing,
|
||||
} from './common.js';
|
||||
|
||||
export function val(api) {
|
||||
let form = document.getElementById("charge_form");
|
||||
|
||||
if (form.checkValidity() === false) {
|
||||
this.event.preventDefault();
|
||||
this.event.stopPropagation();
|
||||
form.classList.add("was-validated");
|
||||
return;
|
||||
}
|
||||
form.classList.add("was-validated");
|
||||
hidePayButton();
|
||||
|
||||
let amountToCharge = document.getElementById("amount").value;
|
||||
console.log(amountToCharge);
|
||||
let payload = {
|
||||
amount: amountToCharge,
|
||||
currency: 'eur',
|
||||
};
|
||||
|
||||
const requestOptions = {
|
||||
method: 'post',
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(payload),
|
||||
};
|
||||
|
||||
fetch(api + "/api/payment-intent", requestOptions)
|
||||
.then(response => response.text())
|
||||
.then(response => {
|
||||
let data;
|
||||
try {
|
||||
data = JSON.parse(response);
|
||||
stripe.confirmCardPayment(data.client_secret, {
|
||||
payment_method: {
|
||||
card: card,
|
||||
billing_details: {
|
||||
name: document.getElementById("cardholder-name").value,
|
||||
}
|
||||
}
|
||||
}).then(function (result) {
|
||||
if (result.error) {
|
||||
// card declined, or sth went wrong with the card
|
||||
showError("card-messages", result.error.message);
|
||||
showPayButton();
|
||||
} else if (result.paymentIntent) {
|
||||
if (result.paymentIntent.status === "succeeded") {
|
||||
saveTransaction(api, result);
|
||||
}
|
||||
}
|
||||
})
|
||||
console.log(data);
|
||||
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
showCardError("Invalid response from payment gateway!");
|
||||
showPayButton();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function saveTransaction(api, result) {
|
||||
let payload = {
|
||||
amount: parseInt(document.getElementById("amount").value, 10),
|
||||
currency: result.paymentIntent.currency,
|
||||
first_name: "",
|
||||
last_name: "",
|
||||
email: document.getElementById("cardholder-email").value,
|
||||
payment_intent: result.paymentIntent.id,
|
||||
payment_method: result.paymentIntent.payment_method,
|
||||
};
|
||||
|
||||
let token = localStorage.getItem("token");
|
||||
const requestOptions = {
|
||||
method: "post",
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Bearer ' + token,
|
||||
},
|
||||
body: JSON.stringify(payload),
|
||||
};
|
||||
fetch(api + "/api/admin/virtual-terminal-succeeded", requestOptions)
|
||||
.then(response => response.json())
|
||||
.then(function (data) {
|
||||
console.log(data);
|
||||
processing.classList.add("d-none");
|
||||
showSuccess("card-messages", "Trasaction successful!");
|
||||
document.getElementById("bank-return-code").innerHTML = data.bank_return_code;
|
||||
document.getElementById("receipt").classList.remove("d-none");
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -1,161 +0,0 @@
|
||||
import {socket} from "./base.js"
|
||||
|
||||
export function showUsers(api) {
|
||||
const tbody = document.getElementById("user-table").getElementsByTagName("tbody")[0];
|
||||
const token = localStorage.getItem("token");
|
||||
|
||||
const requestOptions = {
|
||||
method: 'post',
|
||||
headers: {
|
||||
'Accept': `application/json`,
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Bearer ' + token,
|
||||
},
|
||||
};
|
||||
|
||||
fetch(api + "/api/admin/all-users", requestOptions)
|
||||
.then(response => response.json())
|
||||
.then(function (data) {
|
||||
console.log(data)
|
||||
if (data) {
|
||||
data.forEach(i => {
|
||||
let newRow = tbody.insertRow();
|
||||
let newCell = newRow.insertCell();
|
||||
|
||||
newCell.innerHTML = `<a href="/admin/all-users/${i.id}">${i.last_name}, ${i.first_name}</a>`;
|
||||
|
||||
newCell = newRow.insertCell();
|
||||
let item = document.createTextNode(i.email);
|
||||
newCell.appendChild(item);
|
||||
});
|
||||
} else {
|
||||
let newRow = tbody.insertRow();
|
||||
let newCell = newRow.insertCell();
|
||||
newCell.setAttribute("colspan", "2");
|
||||
newCell.innerHTML = "no data available";
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function showUser(api, userID) {
|
||||
const token = localStorage.getItem("token");
|
||||
let id = window.location.pathname.split("/").pop();
|
||||
let delBtn = document.getElementById("deleteBtn");
|
||||
|
||||
if (id === "0") {
|
||||
return
|
||||
}
|
||||
|
||||
if (userID !== parseInt(id, 10)) {
|
||||
delBtn.classList.remove("d-none")
|
||||
}
|
||||
const requestOptions = {
|
||||
method: 'post',
|
||||
headers: {
|
||||
'Accept': `application/json`,
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Bearer ' + token,
|
||||
},
|
||||
};
|
||||
|
||||
fetch(api + `/api/admin/all-users/${id}`, requestOptions)
|
||||
.then(response => response.json())
|
||||
.then(function (data) {
|
||||
console.log(data);
|
||||
document.getElementById("first_name").value = data.first_name;
|
||||
document.getElementById("last_name").value = data.last_name;
|
||||
document.getElementById("email").value = data.email;
|
||||
});
|
||||
}
|
||||
|
||||
export function saveUser(api, event) {
|
||||
const token = localStorage.getItem("token");
|
||||
let form = document.getElementById("user_form");
|
||||
let id = window.location.pathname.split("/").pop();
|
||||
|
||||
if (form.checkValidity() === false) {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
form.classList.add("was-validated");
|
||||
return;
|
||||
}
|
||||
|
||||
form.classList.add("was-validated");
|
||||
|
||||
if (document.getElementById("password").value !== document.getElementById("verify_password").value) {
|
||||
Swal.fire("Password do not match!");
|
||||
return
|
||||
}
|
||||
|
||||
let payload = {
|
||||
id: parseInt(id),
|
||||
first_name: document.getElementById("first_name").value,
|
||||
last_name: document.getElementById("last_name").value,
|
||||
email: document.getElementById("email").value,
|
||||
password: document.getElementById("password").value,
|
||||
}
|
||||
|
||||
const requestOptions = {
|
||||
method: 'post',
|
||||
headers: {
|
||||
'Accept': `application/json`,
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Bearer ' + token,
|
||||
},
|
||||
body: JSON.stringify(payload)
|
||||
}
|
||||
|
||||
fetch(api + `/api/admin/all-users/edit/${id}`, requestOptions)
|
||||
.then(response => response.json())
|
||||
.then(function (data) {
|
||||
console.log(data);
|
||||
if (!data.ok) {
|
||||
Swal.fire("Error" + data.message)
|
||||
} else {
|
||||
location.href = "/admin/all-users"
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
export function deleteUser(api) {
|
||||
const token = localStorage.getItem("token");
|
||||
let id = window.location.pathname.split("/").pop();
|
||||
|
||||
Swal.fire({
|
||||
title: "Are you sure?",
|
||||
text: "You won't be able to undo this!",
|
||||
icon: "warning",
|
||||
showCancelButton: true,
|
||||
confirmButtonColor: "#3085d6",
|
||||
cancelButtonColor: "#d33",
|
||||
confirmButtonText: "Delete user"
|
||||
}).then((result) => {
|
||||
if (result.isConfirmed) {
|
||||
const requestOptions = {
|
||||
method: 'post',
|
||||
headers: {
|
||||
'Accept': `application/json`,
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Bearer ' + token,
|
||||
},
|
||||
};
|
||||
|
||||
fetch(api + `/api/admin/all-users/delete/${id}`, requestOptions)
|
||||
.then(response => response.json())
|
||||
.then(function (data) {
|
||||
console.log(data);
|
||||
if (!data.ok) {
|
||||
Swal.fire("Error" + data.message)
|
||||
} else {
|
||||
let jsonData = {
|
||||
action: "deleteUser",
|
||||
user_id: parseInt(id),
|
||||
};
|
||||
socket.send(JSON.stringify(jsonData));
|
||||
location.href = "/admin/all-users"
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user