81 lines
2.4 KiB
Go
81 lines
2.4 KiB
Go
package payments
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/noderunners/nip05api/internal/audit"
|
|
"github.com/noderunners/nip05api/internal/dm"
|
|
"github.com/noderunners/nip05api/internal/invoice"
|
|
"github.com/noderunners/nip05api/internal/nostr"
|
|
"github.com/noderunners/nip05api/internal/user"
|
|
"github.com/noderunners/nip05api/internal/webhook"
|
|
)
|
|
|
|
func (w *Worker) dispatchEvents(ctx context.Context, u *user.User, p *invoice.PendingInvoice, ev dm.EventType) {
|
|
vars := buildVars(u, w.domain, w.frontend)
|
|
if err := w.dms.Send(ctx, ev, u.Pubkey, vars); err != nil {
|
|
slog.Error("dm enqueue", "err", err)
|
|
}
|
|
confirmed := time.Now().UTC()
|
|
data := map[string]any{
|
|
"pubkey": u.Pubkey,
|
|
"npub": nostr.HexToNpub(u.Pubkey),
|
|
"username": u.Username,
|
|
"subscription_type": string(u.SubscriptionType),
|
|
"is_lifetime": u.IsLifetime(),
|
|
"years": p.Years,
|
|
"amount_sats": p.AmountSats,
|
|
"payment_hash": p.PaymentHash,
|
|
"is_renewal": p.IsRenewal,
|
|
"confirmed_at": confirmed.Format(time.RFC3339),
|
|
"confirmed_at_unix": confirmed.Unix(),
|
|
}
|
|
if u.IsLifetime() {
|
|
data["duration"] = "lifetime"
|
|
} else {
|
|
data["duration"] = fmt.Sprintf("%dy", p.Years)
|
|
}
|
|
if u.ExpiresAt != nil {
|
|
data["expires_at"] = u.ExpiresAt.UTC().Format(time.RFC3339)
|
|
}
|
|
if err := w.hooks.Enqueue(ctx, webhook.EventUserPaid, data); err != nil {
|
|
slog.Error("webhook enqueue", "err", err)
|
|
}
|
|
w.audit.Log(ctx, audit.ActionPaymentConfirmed, audit.ActorSystem, u.Pubkey, map[string]any{
|
|
"payment_hash": p.PaymentHash,
|
|
"amount_sats": p.AmountSats,
|
|
"is_renewal": p.IsRenewal,
|
|
"event": string(ev),
|
|
})
|
|
slog.Info("payment confirmed",
|
|
"pubkey", u.Pubkey, "username", u.Username,
|
|
"amount_sats", p.AmountSats, "renewal", p.IsRenewal)
|
|
}
|
|
|
|
func buildVars(u *user.User, domain, frontend string) map[string]string {
|
|
expires := "lifetime"
|
|
days := ""
|
|
if u.ExpiresAt != nil {
|
|
expires = u.ExpiresAt.Format("2006-01-02")
|
|
d := int(time.Until(*u.ExpiresAt).Hours() / 24)
|
|
if d < 0 {
|
|
d = 0
|
|
}
|
|
days = strconv.Itoa(d)
|
|
}
|
|
return map[string]string{
|
|
"username": u.Username,
|
|
"npub": nostr.HexToNpub(u.Pubkey),
|
|
"pubkey": u.Pubkey,
|
|
"domain": domain,
|
|
"expires_at": expires,
|
|
"days_remaining": days,
|
|
"frontend_url": frontend,
|
|
"subscription_type": string(u.SubscriptionType),
|
|
}
|
|
}
|