From f92d613124f19ff79b3b479c3e3c732f59b15d18 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sun, 20 Oct 2024 16:40:30 +0300 Subject: [PATCH] added client tg for sending rspay event --- internal/app/app.go | 56 +++++------ internal/initialize/clients.go | 8 ++ internal/initialize/controllers.go | 1 + internal/initialize/services.go | 22 ++--- internal/interface/client/mail.go | 4 +- internal/interface/client/telegram.go | 48 ++++++++++ internal/interface/client/telegram_test.go | 37 +++++++ .../http/wallet_client/controllers.go | 96 ++++++++++--------- internal/models/config.go | 1 + 9 files changed, 189 insertions(+), 84 deletions(-) create mode 100644 internal/interface/client/telegram.go create mode 100644 internal/interface/client/telegram_test.go diff --git a/internal/app/app.go b/internal/app/app.go index ac9100f..52f84f0 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -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{ diff --git a/internal/initialize/clients.go b/internal/initialize/clients.go index b3de905..4357fe6 100644 --- a/internal/initialize/clients.go +++ b/internal/initialize/clients.go @@ -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, + }), } } diff --git a/internal/initialize/controllers.go b/internal/initialize/controllers.go index f005fd7..9f3e7c1 100644 --- a/internal/initialize/controllers.go +++ b/internal/initialize/controllers.go @@ -132,6 +132,7 @@ func NewHttpControllers(deps HttpControllersDeps) *HttpController { VerifyClient: deps.Clients.VerificationClient, MailClient: deps.Clients.MailClient, Logger: deps.Logger, + TelegramClient: deps.Clients.TelegramClient, }), } } diff --git a/internal/initialize/services.go b/internal/initialize/services.go index 067f8ab..b73c873 100644 --- a/internal/initialize/services.go +++ b/internal/initialize/services.go @@ -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, } diff --git a/internal/interface/client/mail.go b/internal/interface/client/mail.go index ffcb23d..81333f9 100644 --- a/internal/interface/client/mail.go +++ b/internal/interface/client/mail.go @@ -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("

Поступила заявка на оплату через Р/С от пользователя с почтой %s (%s)

"+ "

Вот файлы его верификации:

"+ - "

Запрос на оплату: %d рублей

", userEmail, verification.UserID, money) + "

Запрос на оплату: %f рублей

", userEmail, verification.UserID, money) for _, file := range verification.Files { body += fmt.Sprintf("

%s: %s

", 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) } diff --git a/internal/interface/client/telegram.go b/internal/interface/client/telegram.go new file mode 100644 index 0000000..63dba7f --- /dev/null +++ b/internal/interface/client/telegram.go @@ -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 +} diff --git a/internal/interface/client/telegram_test.go b/internal/interface/client/telegram_test.go new file mode 100644 index 0000000..b15f44b --- /dev/null +++ b/internal/interface/client/telegram_test.go @@ -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") +} diff --git a/internal/interface/controller/http/wallet_client/controllers.go b/internal/interface/controller/http/wallet_client/controllers.go index ae61e5a..76bcadc 100644 --- a/internal/interface/controller/http/wallet_client/controllers.go +++ b/internal/interface/controller/http/wallet_client/controllers.go @@ -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 on ", 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, diff --git a/internal/models/config.go b/internal/models/config.go index a0de81a..ecbce7d 100644 --- a/internal/models/config.go +++ b/internal/models/config.go @@ -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 {