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

82 lines
2.5 KiB
Go
Raw Normal View History

2023-07-19 11:47:46 +02:00
package mail
import (
"strings"
"time"
2023-12-26 10:58:26 +01:00
"gitea.urkob.com/urko/btc-pay-checker/internal/services/mail/templates"
"gitea.urkob.com/urko/emailsender/pkg/email"
2023-07-19 11:47:46 +02:00
)
const (
2023-12-26 10:58:26 +01:00
mime = "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n"
okSubject = "BTC Pay Checker successful"
failSubject = "BTC Pay Checker failed"
2023-07-19 11:47:46 +02:00
)
type MailService struct {
2023-12-26 10:58:26 +01:00
emailSrv *email.EmailService
2023-07-19 11:47:46 +02:00
}
2023-12-26 10:58:26 +01:00
func NewMailService(emailSrv *email.EmailService) *MailService {
return &MailService{
emailSrv: emailSrv,
}
2023-07-19 11:47:46 +02:00
}
2023-12-26 10:58:26 +01:00
type SendOK struct {
Amount float64
ExplorerUrl string
Tx string
CustomerID string
OrderID string
Block string
Timestamp time.Time
To string
SupportEmail string
2023-07-19 11:47:46 +02:00
}
func (m *MailService) SendProviderConfirm(data SendOK) error {
2023-12-26 10:58:26 +01:00
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)
2023-12-26 10:58:26 +01:00
return m.emailSrv.SendEmail(email.EmailMessage{
To: data.To,
Subject: okSubject,
Body: template,
})
2023-07-19 11:47:46 +02:00
}
func (m *MailService) SendClientConfirm(data SendOK) error {
2023-12-26 10:58:26 +01:00
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)
2023-12-26 10:58:26 +01:00
return m.emailSrv.SendEmail(email.EmailMessage{
To: data.To,
Subject: okSubject,
Body: template,
})
2023-07-19 11:47:46 +02:00
}
func (m *MailService) SendFail(data SendOK) error {
2023-12-26 10:58:26 +01:00
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)
2023-12-26 10:58:26 +01:00
template = strings.Replace(template, "{{support_email}}", data.SupportEmail, -1)
2023-07-19 11:47:46 +02:00
// TODO: Alert client too
2023-12-26 10:58:26 +01:00
return m.emailSrv.SendEmail(email.EmailMessage{
To: data.To,
Subject: failSubject,
Body: template,
})
2023-07-19 11:47:46 +02:00
}