added client tg for sending rspay event

This commit is contained in:
Pavel 2024-10-20 16:40:30 +03:00
parent f08eea18cf
commit f92d613124
9 changed files with 189 additions and 84 deletions

@ -6,6 +6,7 @@ import (
"fmt"
"github.com/themakers/hlog"
"go.uber.org/zap/zapcore"
tb "gopkg.in/tucnak/telebot.v2"
"os/signal"
"penahub.gitlab.yandexcloud.net/backend/penahub_common/mongo"
"penahub.gitlab.yandexcloud.net/external/trashlog/app"
@ -22,7 +23,6 @@ import (
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/server"
"penahub.gitlab.yandexcloud.net/pena-services/customer/pkg/closer"
"penahub.gitlab.yandexcloud.net/pena-services/customer/pkg/kafka"
tb "gopkg.in/tucnak/telebot.v2"
)
const (
@ -95,24 +95,6 @@ func Run(config *models.Config, logger *zap.Logger, build Build) (appErr error)
return err
}
clients := initialize.NewClients(initialize.ClientsDeps{
Logger: logger,
AuthURL: &config.Service.AuthMicroservice.URL,
HubadminURL: &config.Service.HubadminMicroservice.URL,
CurrencyURL: &config.Service.CurrencyMicroservice.URL,
DiscountServiceConfiguration: &config.Service.DiscountMicroservice,
PaymentServiceConfiguration: &config.Service.PaymentMicroservice,
VerificationURL: &config.Service.VerificationMicroservice.URL,
TemplategenURL: &config.Service.TemplategenMicroserviceURL.URL,
MailClient: &config.Service.Mail,
CodewordServiceHost: &config.Service.CodewordMicroservice,
})
repositories := initialize.NewRepositories(initialize.RepositoriesDeps{
Logger: logger,
MongoDB: mongoDB,
})
notificationBot, err := tb.NewBot(tb.Settings{
Token: config.Service.NotificationBotToken,
Verbose: false,
@ -126,15 +108,35 @@ func Run(config *models.Config, logger *zap.Logger, build Build) (appErr error)
return err
}
clients := initialize.NewClients(initialize.ClientsDeps{
Logger: logger,
AuthURL: &config.Service.AuthMicroservice.URL,
HubadminURL: &config.Service.HubadminMicroservice.URL,
CurrencyURL: &config.Service.CurrencyMicroservice.URL,
DiscountServiceConfiguration: &config.Service.DiscountMicroservice,
PaymentServiceConfiguration: &config.Service.PaymentMicroservice,
VerificationURL: &config.Service.VerificationMicroservice.URL,
TemplategenURL: &config.Service.TemplategenMicroserviceURL.URL,
MailClient: &config.Service.Mail,
CodewordServiceHost: &config.Service.CodewordMicroservice,
Notifier: notificationBot,
NotificationRsPayChannel: config.Service.NotificationRsPayChannel,
})
repositories := initialize.NewRepositories(initialize.RepositoriesDeps{
Logger: logger,
MongoDB: mongoDB,
})
services := initialize.NewServices(initialize.ServicesDeps{
Logger: logger,
Repositories: repositories,
Clients: clients,
ConfigurationGRPC: &config.GRPC,
Brokers: brokers,
Notifier: notificationBot,
NotificationChannel: config.Service.NotificationChannel,
AdminURL: config.Service.AdminURL,
Logger: logger,
Repositories: repositories,
Clients: clients,
ConfigurationGRPC: &config.GRPC,
Brokers: brokers,
Notifier: notificationBot,
NotificationChannel: config.Service.NotificationChannel,
AdminURL: config.Service.AdminURL,
})
rpcControllers := initialize.NewRpcControllers(initialize.RpcControllersDeps{

@ -3,6 +3,7 @@ package initialize
import (
"github.com/gofiber/fiber/v2"
"go.uber.org/zap"
"gopkg.in/tucnak/telebot.v2"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/interface/client"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models"
)
@ -18,6 +19,8 @@ type ClientsDeps struct {
TemplategenURL *models.TemplategenMicroserviceURL
MailClient *models.MailConfiguration
CodewordServiceHost *models.CodewordMicroserviceConfiguration
NotificationRsPayChannel int64
Notifier *telebot.Bot
}
type Clients struct {
@ -30,6 +33,7 @@ type Clients struct {
TemplateClient *client.TemplateClient
MailClient *client.MailClient
CodewordClient *client.CodewordClient
TelegramClient *client.TelegramClient
}
func NewClients(deps ClientsDeps) *Clients {
@ -79,5 +83,9 @@ func NewClients(deps ClientsDeps) *Clients {
Logger: deps.Logger,
CodewordServiceHost: deps.CodewordServiceHost.HostGRPC,
}),
TelegramClient: client.NewTelegramClient(client.TelegramClientDeps{
Notifier: deps.Notifier,
NotifierPayChannel: deps.NotificationRsPayChannel,
}),
}
}

@ -132,6 +132,7 @@ func NewHttpControllers(deps HttpControllersDeps) *HttpController {
VerifyClient: deps.Clients.VerificationClient,
MailClient: deps.Clients.MailClient,
Logger: deps.Logger,
TelegramClient: deps.Clients.TelegramClient,
}),
}
}

@ -2,23 +2,23 @@ package initialize
import (
"go.uber.org/zap"
tb "gopkg.in/tucnak/telebot.v2"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/service/broker/tariff"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/service/callback"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/service/history"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/service/wallet"
tb "gopkg.in/tucnak/telebot.v2"
)
type ServicesDeps struct {
Logger *zap.Logger
Repositories *Repositories
Clients *Clients
Brokers *Brokers
ConfigurationGRPC *models.ConfigurationGRPC
Notifier *tb.Bot
Logger *zap.Logger
Repositories *Repositories
Clients *Clients
Brokers *Brokers
ConfigurationGRPC *models.ConfigurationGRPC
Notifier *tb.Bot
NotificationChannel int64
AdminURL string
AdminURL string
}
type Services struct {
@ -59,9 +59,9 @@ func NewServices(deps ServicesDeps) *Services {
AccountRepository: deps.Repositories.AccountRepository,
WalletService: walletService,
HistoryService: historyService,
Notifier: deps.Notifier,
NotifyChannel: deps.NotificationChannel,
AdminURL: deps.AdminURL,
Notifier: deps.Notifier,
NotifyChannel: deps.NotificationChannel,
AdminURL: deps.AdminURL,
}),
TariffBrokerService: tariffBrokerService,
}

@ -36,7 +36,7 @@ func NewMailClient(deps MailClientDeps) *MailClient {
func (receiver *MailClient) SendMessage(userEmail string, verification *models.Verification, money float32) errors.Error {
body := fmt.Sprintf("<p>Поступила заявка на оплату через Р/С от пользователя с почтой %s (%s)</p>"+
"<p>Вот файлы его верификации:</p>"+
"<p>Запрос на оплату: %d рублей</p>", userEmail, verification.UserID, money)
"<p>Запрос на оплату: %f рублей</p>", userEmail, verification.UserID, money)
for _, file := range verification.Files {
body += fmt.Sprintf("<p>%s: <a href=\"%s\">%s</a></p>", file.Name, file.URL, file.URL)
@ -75,7 +75,7 @@ func (receiver *MailClient) SendMessage(userEmail string, verification *models.V
}
if statusCode != fiber.StatusOK {
err := fmt.Errorf("the SMTP service returned an error: %s", statusCode)
err := fmt.Errorf("the SMTP service returned an error: %d", statusCode)
return handleError(receiver.deps.Logger, "Error sending email", err)
}

@ -0,0 +1,48 @@
package client
import (
"fmt"
tb "gopkg.in/tucnak/telebot.v2"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/errors"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models"
)
type TelegramClient struct {
notifier *tb.Bot
notifierPayChannel int64
}
type TelegramClientDeps struct {
Notifier *tb.Bot
NotifierPayChannel int64
}
func NewTelegramClient(deps TelegramClientDeps) *TelegramClient {
return &TelegramClient{
notifier: deps.Notifier,
notifierPayChannel: deps.NotifierPayChannel,
}
}
func (t *TelegramClient) NotifyRsPay(userEmail string, verification *models.Verification, money float32) errors.Error {
message := fmt.Sprintf(
"Поступила заявка на оплату через Р/С от пользователя с почтой %s (%s)\n"+
"Вот файлы его верификации:\n"+
"Запрос на оплату: %f рублей\n",
userEmail, verification.UserID, money,
)
for _, file := range verification.Files {
message += fmt.Sprintf("%s: %s\n", file.Name, file.URL)
}
_, err := t.notifier.Send(
&tb.Chat{ID: t.notifierPayChannel},
message,
)
if err != nil {
return errors.New(fmt.Errorf("failed to send tg RS PAY message: %v", err), errors.ErrInternalError)
}
return nil
}

@ -0,0 +1,37 @@
package client
import (
"fmt"
tb "gopkg.in/tucnak/telebot.v2"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models"
"testing"
"time"
)
func Test_Telegram(t *testing.T) {
tbBot, err := tb.NewBot(tb.Settings{
Token: "6712573453:AAFqTOsgwe_j48ZQ1GzWKQDT5Nwr-SAWjz8",
Verbose: false,
ParseMode: tb.ModeHTML,
Poller: &tb.LongPoller{
Timeout: time.Second,
},
})
if err != nil {
fmt.Println(err)
return
}
tgClient := NewTelegramClient(TelegramClientDeps{
Notifier: tbBot,
NotifierPayChannel: -1002217604546,
})
err = tgClient.NotifyRsPay("test@email", &models.Verification{UserID: "test"}, 100.11111)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("good")
}

@ -26,6 +26,7 @@ type Deps struct {
VerifyClient *client.VerificationClient
MailClient *client.MailClient
Logger *zap.Logger
TelegramClient *client.TelegramClient
}
type WalletController struct {
@ -38,6 +39,7 @@ type WalletController struct {
verifyClient *client.VerificationClient
mailClient *client.MailClient
logger *zap.Logger
telegramClient *client.TelegramClient
}
func NewWalletController(deps Deps) *WalletController {
@ -51,6 +53,7 @@ func NewWalletController(deps Deps) *WalletController {
verifyClient: deps.VerifyClient,
mailClient: deps.MailClient,
logger: deps.Logger,
telegramClient: deps.TelegramClient,
}
}
@ -59,7 +62,7 @@ func (receiver *WalletController) RequestMoney(ctx *fiber.Ctx) error {
if !ok || userID == "" {
return receiver.middleWare.NoAuth(ctx)
}
token,_ := receiver.middleWare.ExtractToken(ctx)
token, _ := receiver.middleWare.ExtractToken(ctx)
hlogger := log_mw.ExtractLogger(ctx)
@ -99,7 +102,7 @@ func (receiver *WalletController) RequestMoney(ctx *fiber.Ctx) error {
}
func (receiver *WalletController) GetPaymentLink(ctx context.Context, request *models.GetPaymentLinkRequest, account *models.Account, token string) (string, errors.Error) {
auth, userErr := receiver.authClient.GetUser(ctx , request.UserID)
auth, userErr := receiver.authClient.GetUser(ctx, request.UserID)
if userErr != nil {
receiver.logger.Error("failed to get user on <GetPaymentLink> on <PaymentService>",
zap.Error(userErr),
@ -147,17 +150,17 @@ func (receiver *WalletController) GetPaymentLinkBankCard(ctx context.Context, re
ClientIP: request.ClientIP,
CallbackHostGRPC: []string{receiver.grpc.Domen},
ReturnURL: request.Body.ReturnURL,
Customer: &treasurer.Customer{
Customer: &treasurer.Customer{
FullName: account.Name.Orgname + ". " + account.Name.FirstName + " " + account.Name.Secondname,
INN: ver.TaxNumber,
Email: auth.Login,
Phone: auth.PhoneNumber,
INN: ver.TaxNumber,
Email: auth.Login,
Phone: auth.PhoneNumber,
},
Items: []*treasurer.Item{&treasurer.Item{
Description: "Пополнение пользовательского баланса платформы Pena Hub",
Quantity: fmt.Sprintf("%.2f",float64(request.Body.Amount)/100),
Currency: "RUB",
Money: fmt.Sprintf("%.2f",float64(request.Body.Amount)/100),
Quantity: fmt.Sprintf("%.2f", float64(request.Body.Amount)/100),
Currency: "RUB",
Money: fmt.Sprintf("%.2f", float64(request.Body.Amount)/100),
}},
},
})
@ -178,17 +181,17 @@ func (receiver *WalletController) GetPaymentLinkYooMoney(ctx context.Context, re
ClientIP: request.ClientIP,
CallbackHostGRPC: []string{receiver.grpc.Domen},
ReturnURL: request.Body.ReturnURL,
Customer: &treasurer.Customer{
Customer: &treasurer.Customer{
FullName: account.Name.Orgname + ", " + account.Name.FirstName + " " + account.Name.Secondname,
INN: ver.TaxNumber,
Email: auth.Login,
Phone: auth.PhoneNumber,
INN: ver.TaxNumber,
Email: auth.Login,
Phone: auth.PhoneNumber,
},
Items: []*treasurer.Item{&treasurer.Item{
Description: "Пополнение пользовательского баланса платформы Pena Hub",
Quantity: fmt.Sprintf("%.2f",float64(request.Body.Amount)/100),
Currency: "RUB",
Money: fmt.Sprintf("%.2f",float64(request.Body.Amount)/100),
Quantity: fmt.Sprintf("%.2f", float64(request.Body.Amount)/100),
Currency: "RUB",
Money: fmt.Sprintf("%.2f", float64(request.Body.Amount)/100),
}},
},
})
@ -209,17 +212,17 @@ func (receiver *WalletController) GetPaymentLinkSberPay(ctx context.Context, req
ClientIP: request.ClientIP,
CallbackHostGRPC: []string{receiver.grpc.Domen},
ReturnURL: request.Body.ReturnURL,
Customer: &treasurer.Customer{
Customer: &treasurer.Customer{
FullName: account.Name.Orgname + ", " + account.Name.FirstName + " " + account.Name.Secondname,
INN: ver.TaxNumber,
Email: auth.Login,
Phone: auth.PhoneNumber,
INN: ver.TaxNumber,
Email: auth.Login,
Phone: auth.PhoneNumber,
},
Items: []*treasurer.Item{&treasurer.Item{
Description: "Пополнение пользовательского баланса платформы Pena Hub",
Quantity: fmt.Sprintf("%.2f",float64(request.Body.Amount)/100),
Currency: "RUB",
Money: fmt.Sprintf("%.2f",float64(request.Body.Amount)/100),
Quantity: fmt.Sprintf("%.2f", float64(request.Body.Amount)/100),
Currency: "RUB",
Money: fmt.Sprintf("%.2f", float64(request.Body.Amount)/100),
}},
},
})
@ -240,17 +243,17 @@ func (receiver *WalletController) GetPaymentLinkTinkoff(ctx context.Context, req
ClientIP: request.ClientIP,
CallbackHostGRPC: []string{receiver.grpc.Domen},
ReturnURL: request.Body.ReturnURL,
Customer: &treasurer.Customer{
Customer: &treasurer.Customer{
FullName: account.Name.Orgname + ", " + account.Name.FirstName + " " + account.Name.Secondname,
INN: ver.TaxNumber,
Email: auth.Login,
Phone: auth.PhoneNumber,
INN: ver.TaxNumber,
Email: auth.Login,
Phone: auth.PhoneNumber,
},
Items: []*treasurer.Item{&treasurer.Item{
Description: "Пополнение пользовательского баланса платформы Pena Hub",
Quantity: fmt.Sprintf("%.2f",float64(request.Body.Amount)/100),
Currency: "RUB",
Money: fmt.Sprintf("%.2f", float64(request.Body.Amount)/100),
Quantity: fmt.Sprintf("%.2f", float64(request.Body.Amount)/100),
Currency: "RUB",
Money: fmt.Sprintf("%.2f", float64(request.Body.Amount)/100),
}},
},
})
@ -271,17 +274,17 @@ func (receiver *WalletController) GetPaymentLinkSBP(ctx context.Context, request
ClientIP: request.ClientIP,
CallbackHostGRPC: []string{receiver.grpc.Domen},
ReturnURL: request.Body.ReturnURL,
Customer: &treasurer.Customer{
Customer: &treasurer.Customer{
FullName: account.Name.Orgname + ", " + account.Name.FirstName + " " + account.Name.Secondname,
INN: ver.TaxNumber,
Email: auth.Login,
Phone: auth.PhoneNumber,
INN: ver.TaxNumber,
Email: auth.Login,
Phone: auth.PhoneNumber,
},
Items: []*treasurer.Item{&treasurer.Item{
Description: "Пополнение пользовательского баланса платформы Pena Hub",
Quantity: fmt.Sprintf("%.2f", float64(request.Body.Amount)/100),
Currency: "RUB",
Money: fmt.Sprintf("%.2f", float64(request.Body.Amount)/100),
Quantity: fmt.Sprintf("%.2f", float64(request.Body.Amount)/100),
Currency: "RUB",
Money: fmt.Sprintf("%.2f", float64(request.Body.Amount)/100),
}},
},
})
@ -302,17 +305,17 @@ func (receiver *WalletController) GetPaymentLinkB2B(ctx context.Context, request
ClientIP: request.ClientIP,
CallbackHostGRPC: []string{receiver.grpc.Domen},
ReturnURL: request.Body.ReturnURL,
Customer: &treasurer.Customer{
Customer: &treasurer.Customer{
FullName: account.Name.Orgname + ", " + account.Name.FirstName + " " + account.Name.Secondname,
INN: ver.TaxNumber,
Email: auth.Login,
Phone: auth.PhoneNumber,
INN: ver.TaxNumber,
Email: auth.Login,
Phone: auth.PhoneNumber,
},
Items: []*treasurer.Item{&treasurer.Item{
Description: "Пополнение пользовательского баланса платформы Pena Hub",
Quantity: fmt.Sprintf("%.2f",float64(request.Body.Amount)/100),
Currency: "RUB",
Money: fmt.Sprintf("%.2f",float64(request.Body.Amount)/100),
Quantity: fmt.Sprintf("%.2f", float64(request.Body.Amount)/100),
Currency: "RUB",
Money: fmt.Sprintf("%.2f", float64(request.Body.Amount)/100),
}},
},
})
@ -414,6 +417,11 @@ func (receiver *WalletController) PostWalletRspay(ctx *fiber.Ctx) error {
return receiver.middleWare.ErrorOld(ctx, err)
}
err = receiver.telegramClient.NotifyRsPay(authData.Login, verification, *req.Money)
if err != nil {
return receiver.middleWare.ErrorOld(ctx, err)
}
hlogger.Emit(models.InfoRSPay{
CtxUserID: userID,
CtxAccountID: user.ID,

@ -46,6 +46,7 @@ type ServiceConfiguration struct {
NotificationBotToken string `env:"NOTIFICATION_BOT_TOKEN"`
NotificationChannel int64 `env:"NOTIFICATION_CHANNEL"`
AdminURL string `env:"ADMIN_FRONT_URL"`
NotificationRsPayChannel int64 `env:"NOTIFICATION_RS_PAY_CHANNEL"`
}
type KafkaConfiguration struct {