btc-pay-checker/internal/services/mail/mail.go

82 lines
2.5 KiB
Go

package mail
import (
"strings"
"time"
"gitea.urkob.com/urko/btc-pay-checker/internal/services/mail/templates"
"gitea.urkob.com/urko/emailsender/pkg/email"
)
const (
mime = "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n"
okSubject = "BTC Pay Checker successful"
failSubject = "BTC Pay Checker failed"
)
type MailService struct {
emailSrv *email.EmailService
}
func NewMailService(emailSrv *email.EmailService) *MailService {
return &MailService{
emailSrv: emailSrv,
}
}
type SendOK struct {
Amount float64
ExplorerUrl string
Tx string
CustomerID string
OrderID string
Block string
Timestamp time.Time
To string
SupportEmail string
}
func (m *MailService) SendProviderConfirm(data SendOK) error {
template := strings.Replace(templates.ProviderConfirm, "{{explorer_url}}", data.ExplorerUrl, -1)
template = strings.Replace(template, "{{customer_id}}", data.CustomerID, -1)
template = strings.Replace(template, "{{order_id}}", data.OrderID, -1)
template = strings.Replace(template, "{{tx}}", data.Tx, -1)
template = strings.Replace(template, "{{block}}", data.Block, -1)
template = strings.Replace(template, "{{timestamp}}", data.Timestamp.Format(time.RFC3339), -1)
return m.emailSrv.SendEmail(email.EmailMessage{
To: data.To,
Subject: okSubject,
Body: template,
})
}
func (m *MailService) SendClientConfirm(data SendOK) error {
template := strings.Replace(templates.ClientConfirm, "{{explorer_url}}", data.ExplorerUrl, -1)
template = strings.Replace(template, "{{customer_id}}", data.CustomerID, -1)
template = strings.Replace(template, "{{order_id}}", data.OrderID, -1)
template = strings.Replace(template, "{{tx}}", data.Tx, -1)
template = strings.Replace(template, "{{block}}", data.Block, -1)
template = strings.Replace(template, "{{timestamp}}", data.Timestamp.Format(time.RFC3339), -1)
return m.emailSrv.SendEmail(email.EmailMessage{
To: data.To,
Subject: okSubject,
Body: template,
})
}
func (m *MailService) SendFail(data SendOK) error {
template := strings.Replace(templates.Error, "{{explorer_url}}", data.ExplorerUrl, -1)
template = strings.Replace(template, "{{tx_id}}", data.Tx, -1)
template = strings.Replace(template, "{{block_hash}}", data.Block, -1)
template = strings.Replace(template, "{{support_email}}", data.SupportEmail, -1)
// TODO: Alert client too
return m.emailSrv.SendEmail(email.EmailMessage{
To: data.To,
Subject: failSubject,
Body: template,
})
}