Compare commits
No commits in common. "082c75283d4347881af9bb9c51627018ce763e23" and "491f482fc750bfc5c2410fd0848f66b36fdb5eeb" have entirely different histories.
082c75283d
...
491f482fc7
@ -1,7 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -104,17 +103,6 @@ func (app *application) GetWidgetByID(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.Write(out)
|
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) {
|
func (app *application) CreateCustomerAndSubscribeToPlan(w http.ResponseWriter, r *http.Request) {
|
||||||
var data stripePayload
|
var data stripePayload
|
||||||
|
|
||||||
@ -195,27 +183,11 @@ func (app *application) CreateCustomerAndSubscribeToPlan(w http.ResponseWriter,
|
|||||||
Amount: amount,
|
Amount: amount,
|
||||||
}
|
}
|
||||||
|
|
||||||
orderID, err := app.SaveOrder(order)
|
_, err = app.SaveOrder(order)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.errorLog.Println(err)
|
app.errorLog.Println(err)
|
||||||
return
|
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{
|
resp := JSONResponse{
|
||||||
@ -233,30 +205,6 @@ func (app *application) CreateCustomerAndSubscribeToPlan(w http.ResponseWriter,
|
|||||||
w.Write(out)
|
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) {
|
func (app *application) SaveCustomer(firstName, lastName, email string) (int, error) {
|
||||||
customer := models.Customer{
|
customer := models.Customer{
|
||||||
FirstName: firstName,
|
FirstName: firstName,
|
||||||
|
@ -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 }}
|
|
||||||
|
|
@ -17,38 +17,37 @@ type Order struct {
|
|||||||
FirstName string `json:"first_name"`
|
FirstName string `json:"first_name"`
|
||||||
LastName string `json:"last_name"`
|
LastName string `json:"last_name"`
|
||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
CreatedAt time.Time `json:"created_at"`
|
CreatedAt time.Time `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *application) CreateAndSendInvoice(w http.ResponseWriter, r *http.Request) {
|
func (app *application) CreateAndSendInvoice(w http.ResponseWriter, r *http.Request) {
|
||||||
// receive json
|
// receive json
|
||||||
var order Order
|
var order Order
|
||||||
|
|
||||||
err := app.readJSON(w, r, &order)
|
order.ID = 100
|
||||||
if err != nil {
|
order.Email = "me@here.com"
|
||||||
app.badRequest(w, r, err)
|
order.FirstName = "John"
|
||||||
return
|
order.LastName = "Smith"
|
||||||
}
|
order.Quantity = 1
|
||||||
app.infoLog.Println(order)
|
order.Amount = 1000
|
||||||
|
order.Product = "Widget"
|
||||||
|
order.CreatedAt = time.Now()
|
||||||
|
// err = app.readJSON(w, r, &order)
|
||||||
|
// if err != nil {
|
||||||
|
// app.badRequest(w, r, err)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
|
||||||
// generate a pdf invoice
|
// generate a pdf invoice
|
||||||
err = app.createInvoicePDF(order)
|
err := app.createInvoicePDF(order)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.badRequest(w, r, err)
|
app.badRequest(w, r, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// create mail attachment
|
// create mail
|
||||||
attachments := []string{
|
|
||||||
fmt.Sprintf("./invoices/%d.pdf", order.ID),
|
|
||||||
}
|
|
||||||
|
|
||||||
// send mail with attachment
|
// 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
|
// send response
|
||||||
var resp JSONResponse
|
var resp JSONResponse
|
||||||
|
@ -18,6 +18,6 @@ func (app *application) routes() http.Handler {
|
|||||||
MaxAge: 300,
|
MaxAge: 300,
|
||||||
}))
|
}))
|
||||||
|
|
||||||
mux.Post("/invoice/create-and-send", app.CreateAndSendInvoice)
|
mux.Get("/invoice/create-and-send", app.CreateAndSendInvoice)
|
||||||
return mux
|
return mux
|
||||||
}
|
}
|
||||||
|
@ -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,8 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"myapp/internal/cards"
|
"myapp/internal/cards"
|
||||||
"myapp/internal/cards/encryption"
|
"myapp/internal/cards/encryption"
|
||||||
@ -10,7 +8,6 @@ import (
|
|||||||
"myapp/internal/urlsigner"
|
"myapp/internal/urlsigner"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
)
|
)
|
||||||
@ -98,17 +95,6 @@ func (app *application) GetTransactionData(r *http.Request) (TransactionData, er
|
|||||||
return txnData, nil
|
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) {
|
func (app *application) PaymentSucceeded(w http.ResponseWriter, r *http.Request) {
|
||||||
// read posted data
|
// read posted data
|
||||||
err := r.ParseForm()
|
err := r.ParseForm()
|
||||||
@ -164,24 +150,6 @@ func (app *application) PaymentSucceeded(w http.ResponseWriter, r *http.Request)
|
|||||||
app.errorLog.Println(err)
|
app.errorLog.Println(err)
|
||||||
return
|
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.infoLog.Printf("order id: %d", orderID)
|
||||||
|
|
||||||
app.Session.Put(r.Context(), "txn", txnData)
|
app.Session.Put(r.Context(), "txn", txnData)
|
||||||
@ -189,30 +157,6 @@ func (app *application) PaymentSucceeded(w http.ResponseWriter, r *http.Request)
|
|||||||
http.Redirect(w, r, "/receipt", http.StatusSeeOther)
|
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) {
|
func (app *application) VirtualTerminalPaymentSucceeded(w http.ResponseWriter, r *http.Request) {
|
||||||
txnData, err := app.GetTransactionData(r)
|
txnData, err := app.GetTransactionData(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user