254 lines
10 KiB
Go
254 lines
10 KiB
Go
![]() |
package client
|
|||
|
|
|||
|
import (
|
|||
|
"context"
|
|||
|
"fmt"
|
|||
|
"net/url"
|
|||
|
|
|||
|
"go.uber.org/zap"
|
|||
|
"penahub.gitlab.yandexcloud.net/external/treasurer/internal/errors"
|
|||
|
"penahub.gitlab.yandexcloud.net/external/treasurer/internal/models"
|
|||
|
"penahub.gitlab.yandexcloud.net/external/treasurer/internal/models/yandex"
|
|||
|
"penahub.gitlab.yandexcloud.net/external/treasurer/internal/utils"
|
|||
|
"penahub.gitlab.yandexcloud.net/external/treasurer/pkg/client"
|
|||
|
)
|
|||
|
|
|||
|
const (
|
|||
|
yandexPaymentsMockURL = "http://mock:8080/payments"
|
|||
|
yandexPaymentsProdURL = "https://api.yookassa.ru/v3/payments"
|
|||
|
|
|||
|
yandexWebhooksMockURL = "http://mock:8080/webhooks"
|
|||
|
yandexWebhooksProdURL = "https://api.yookassa.ru/v3/webhooks"
|
|||
|
)
|
|||
|
|
|||
|
const (
|
|||
|
yandexPaymentsURL = yandexPaymentsMockURL
|
|||
|
yandexWebhooksURL = yandexWebhooksMockURL
|
|||
|
)
|
|||
|
|
|||
|
type YandexClientDeps struct {
|
|||
|
Logger *zap.Logger
|
|||
|
Configuration *models.YoomomeyConfiguration
|
|||
|
}
|
|||
|
|
|||
|
type YandexClient struct {
|
|||
|
logger *zap.Logger
|
|||
|
configuration *models.YoomomeyConfiguration
|
|||
|
}
|
|||
|
|
|||
|
func NewYandexClient(deps YandexClientDeps) (*YandexClient, errors.Error) {
|
|||
|
if deps.Logger == nil {
|
|||
|
return nil, errors.NewWithMessage("Logger is nil on <NewYandexClient>", errors.ErrInvalidArgs)
|
|||
|
}
|
|||
|
|
|||
|
if deps.Configuration == nil {
|
|||
|
return nil, errors.NewWithMessage("Configuration is nil on <NewYandexClient>", errors.ErrInvalidArgs)
|
|||
|
}
|
|||
|
|
|||
|
return &YandexClient{
|
|||
|
logger: deps.Logger,
|
|||
|
configuration: deps.Configuration,
|
|||
|
}, nil
|
|||
|
}
|
|||
|
|
|||
|
func (receiver *YandexClient) CreatePayment(ctx context.Context, idempotenceKey string, request *yandex.CreatePaymentRequest[yandex.PaymentMethodType]) (*yandex.Payment, errors.Error) {
|
|||
|
response, err := client.Post[yandex.Payment, any](ctx, &client.RequestSettings{
|
|||
|
URL: yandexPaymentsURL,
|
|||
|
Body: request,
|
|||
|
Headers: map[string]string{
|
|||
|
"Content-Type": "application/json",
|
|||
|
"Idempotence-Key": idempotenceKey,
|
|||
|
"Authorization": utils.ConvertYoomoneySercetsToAuth("Basic", receiver.configuration.StoreID, receiver.configuration.SecretKey),
|
|||
|
},
|
|||
|
})
|
|||
|
if err != nil {
|
|||
|
receiver.logger.Error("failed to make request on <CreatePayment> of <YandexClient>", zap.Error(err))
|
|||
|
return nil, errors.NewWithError(fmt.Errorf("failed to make request: %w", err), errors.ErrInternalError)
|
|||
|
}
|
|||
|
if response.Error != nil {
|
|||
|
receiver.logger.Error("failed to create payment on <CreatePayment> of <YandexClient>", zap.Any("response", response.Error))
|
|||
|
return nil, errors.NewWithMessage("failed to create payment", errors.ErrInternalError)
|
|||
|
}
|
|||
|
|
|||
|
return response.Body, nil
|
|||
|
}
|
|||
|
|
|||
|
func (receiver *YandexClient) CreatePaymentB2B(ctx context.Context, idempotenceKey string, request *yandex.CreatePaymentRequest[yandex.PaymentMethodB2B]) (*yandex.Payment, errors.Error) {
|
|||
|
response, err := client.Post[yandex.Payment, any](ctx, &client.RequestSettings{
|
|||
|
URL: yandexPaymentsURL,
|
|||
|
Body: request,
|
|||
|
Headers: map[string]string{
|
|||
|
"Content-Type": "application/json",
|
|||
|
"Idempotence-Key": idempotenceKey,
|
|||
|
"Authorization": utils.ConvertYoomoneySercetsToAuth("Basic", receiver.configuration.StoreID, receiver.configuration.SecretKey),
|
|||
|
},
|
|||
|
})
|
|||
|
if err != nil {
|
|||
|
receiver.logger.Error("failed to make request on <CreatePaymentB2B> of <YandexClient>", zap.Error(err))
|
|||
|
return nil, errors.NewWithError(fmt.Errorf("failed to make request: %w", err), errors.ErrInternalError)
|
|||
|
}
|
|||
|
if response.Error != nil {
|
|||
|
receiver.logger.Error("failed to create payment on <CreatePaymentB2B> of <YandexClient>", zap.Any("response", response.Error))
|
|||
|
return nil, errors.NewWithMessage("failed to create payment", errors.ErrInternalError)
|
|||
|
}
|
|||
|
|
|||
|
return response.Body, nil
|
|||
|
}
|
|||
|
|
|||
|
func (receiver *YandexClient) CreatePaymentBankCard(ctx context.Context, idempotenceKey string, request *yandex.CreatePaymentRequest[yandex.PaymentMethodBankCard]) (*yandex.Payment, errors.Error) {
|
|||
|
response, err := client.Post[yandex.Payment, any](ctx, &client.RequestSettings{
|
|||
|
URL: yandexPaymentsURL,
|
|||
|
Body: request,
|
|||
|
Headers: map[string]string{
|
|||
|
"Content-Type": "application/json",
|
|||
|
"Idempotence-Key": idempotenceKey,
|
|||
|
"Authorization": utils.ConvertYoomoneySercetsToAuth("Basic", receiver.configuration.StoreID, receiver.configuration.SecretKey),
|
|||
|
},
|
|||
|
})
|
|||
|
if err != nil {
|
|||
|
receiver.logger.Error("failed to make request on <CreatePaymentBankCard> of <YandexClient>", zap.Error(err))
|
|||
|
return nil, errors.NewWithError(fmt.Errorf("failed to make request: %w", err), errors.ErrInternalError)
|
|||
|
}
|
|||
|
if response.Error != nil {
|
|||
|
receiver.logger.Error("failed to create payment on <CreatePaymentBankCard> of <YandexClient>", zap.Any("response", response.Error))
|
|||
|
return nil, errors.NewWithMessage("failed to create payment", errors.ErrInternalError)
|
|||
|
}
|
|||
|
|
|||
|
return response.Body, nil
|
|||
|
}
|
|||
|
|
|||
|
func (receiver *YandexClient) CreatePaymentLogin(ctx context.Context, idempotenceKey string, request *yandex.CreatePaymentRequest[yandex.PaymentMethodLogin]) (*yandex.Payment, errors.Error) {
|
|||
|
response, err := client.Post[yandex.Payment, any](ctx, &client.RequestSettings{
|
|||
|
URL: yandexPaymentsURL,
|
|||
|
Body: request,
|
|||
|
Headers: map[string]string{
|
|||
|
"Content-Type": "application/json",
|
|||
|
"Idempotence-Key": idempotenceKey,
|
|||
|
"Authorization": utils.ConvertYoomoneySercetsToAuth("Basic", receiver.configuration.StoreID, receiver.configuration.SecretKey),
|
|||
|
},
|
|||
|
})
|
|||
|
if err != nil {
|
|||
|
receiver.logger.Error("failed to make request on <CreatePaymentLogin> of <YandexClient>", zap.Error(err))
|
|||
|
return nil, errors.NewWithError(fmt.Errorf("failed to make request: %w", err), errors.ErrInternalError)
|
|||
|
}
|
|||
|
if response.Error != nil {
|
|||
|
receiver.logger.Error("failed to create payment on <CreatePaymentLogin> of <YandexClient>", zap.Any("response", response.Error))
|
|||
|
return nil, errors.NewWithMessage("failed to create payment", errors.ErrInternalError)
|
|||
|
}
|
|||
|
|
|||
|
return response.Body, nil
|
|||
|
}
|
|||
|
|
|||
|
func (receiver *YandexClient) CreatePaymentPhone(ctx context.Context, idempotenceKey string, request *yandex.CreatePaymentRequest[yandex.PaymentMethodPhone]) (*yandex.Payment, errors.Error) {
|
|||
|
response, err := client.Post[yandex.Payment, any](ctx, &client.RequestSettings{
|
|||
|
URL: yandexPaymentsURL,
|
|||
|
Body: request,
|
|||
|
Headers: map[string]string{
|
|||
|
"Content-Type": "application/json",
|
|||
|
"Idempotence-Key": idempotenceKey,
|
|||
|
"Authorization": utils.ConvertYoomoneySercetsToAuth("Basic", receiver.configuration.StoreID, receiver.configuration.SecretKey),
|
|||
|
},
|
|||
|
})
|
|||
|
if err != nil {
|
|||
|
receiver.logger.Error("failed to make request on <CreatePaymentPhone> of <YandexClient>", zap.Error(err))
|
|||
|
return nil, errors.NewWithError(fmt.Errorf("failed to make request: %w", err), errors.ErrInternalError)
|
|||
|
}
|
|||
|
if response.Error != nil {
|
|||
|
receiver.logger.Error("failed to create payment on <CreatePaymentPhone> of <YandexClient>", zap.Any("response", response.Error))
|
|||
|
return nil, errors.NewWithMessage("failed to create payment", errors.ErrInternalError)
|
|||
|
}
|
|||
|
|
|||
|
return response.Body, nil
|
|||
|
}
|
|||
|
|
|||
|
func (receiver *YandexClient) DeleteWebhook(ctx context.Context, webhookID string) errors.Error {
|
|||
|
url, err := url.JoinPath(yandexWebhooksURL, webhookID)
|
|||
|
if err != nil {
|
|||
|
receiver.logger.Error("failed to join path url on <DeleteWebhook> of <YandexClient>",
|
|||
|
zap.Error(err),
|
|||
|
zap.String("yandex webhook url", yandexWebhooksURL),
|
|||
|
zap.String("webhookID", webhookID),
|
|||
|
)
|
|||
|
|
|||
|
return errors.NewWithError(fmt.Errorf("failed to join url path: %w", err), errors.ErrInternalError)
|
|||
|
}
|
|||
|
|
|||
|
response, err := client.Delete[any, any](ctx, &client.RequestSettings{
|
|||
|
URL: url,
|
|||
|
Headers: map[string]string{
|
|||
|
"Content-Type": "application/json",
|
|||
|
|
|||
|
// TODO: узнать о получении access token
|
|||
|
"Authorization": utils.ConvertYoomoneySercetsToAuth("Basic", receiver.configuration.StoreID, receiver.configuration.SecretKey),
|
|||
|
},
|
|||
|
})
|
|||
|
if err != nil {
|
|||
|
receiver.logger.Error("failed to make request on <DeleteWebhook> of <YandexClient>", zap.Error(err))
|
|||
|
return errors.NewWithError(fmt.Errorf("failed to make request: %w", err), errors.ErrInternalError)
|
|||
|
}
|
|||
|
if response.Error != nil {
|
|||
|
receiver.logger.Error("failed to delete webhook on <DeleteWebhook> of <YandexClient>",
|
|||
|
zap.Any("response", response.Error),
|
|||
|
zap.String("url", url),
|
|||
|
)
|
|||
|
|
|||
|
return errors.NewWithMessage("failed to create payment", errors.ErrInternalError)
|
|||
|
}
|
|||
|
|
|||
|
return nil
|
|||
|
}
|
|||
|
|
|||
|
func (receiver *YandexClient) GetWebhookEvents(ctx context.Context) ([]yandex.Webhook, errors.Error) {
|
|||
|
response, err := client.Delete[[]yandex.Webhook, any](ctx, &client.RequestSettings{
|
|||
|
URL: yandexWebhooksURL,
|
|||
|
Headers: map[string]string{
|
|||
|
"Content-Type": "application/json",
|
|||
|
|
|||
|
// TODO: узнать о получении access token
|
|||
|
"Authorization": utils.ConvertYoomoneySercetsToAuth("Basic", receiver.configuration.StoreID, receiver.configuration.SecretKey),
|
|||
|
},
|
|||
|
})
|
|||
|
if err != nil {
|
|||
|
receiver.logger.Error("failed to make request on <DeleteWebhook> of <YandexClient>", zap.Error(err))
|
|||
|
return []yandex.Webhook{}, errors.NewWithError(fmt.Errorf("failed to make request: %w", err), errors.ErrInternalError)
|
|||
|
}
|
|||
|
if response.Error != nil {
|
|||
|
receiver.logger.Error("failed to delete webhook on <DeleteWebhook> of <YandexClient>",
|
|||
|
zap.Any("response", response.Error),
|
|||
|
zap.String("url", yandexWebhooksURL),
|
|||
|
)
|
|||
|
|
|||
|
return []yandex.Webhook{}, errors.NewWithMessage("failed to create payment", errors.ErrInternalError)
|
|||
|
}
|
|||
|
|
|||
|
return *response.Body, nil
|
|||
|
}
|
|||
|
|
|||
|
func (receiver *YandexClient) SetWebhookEvent(ctx context.Context, idempotenceKey string, request *yandex.CreateWebhookRequest) (string, errors.Error) {
|
|||
|
response, err := client.Post[yandex.Webhook, any](ctx, &client.RequestSettings{
|
|||
|
URL: yandexWebhooksURL,
|
|||
|
Body: request,
|
|||
|
Headers: map[string]string{
|
|||
|
"Content-Type": "application/json",
|
|||
|
"Idempotence-Key": idempotenceKey,
|
|||
|
|
|||
|
// TODO: узнать о получении access token
|
|||
|
"Authorization": utils.ConvertYoomoneySercetsToAuth("Basic", receiver.configuration.StoreID, receiver.configuration.SecretKey),
|
|||
|
},
|
|||
|
})
|
|||
|
if err != nil {
|
|||
|
receiver.logger.Error("failed to make request on <DeleteWebhook> of <YandexClient>", zap.Error(err))
|
|||
|
return "", errors.NewWithError(fmt.Errorf("failed to make request: %w", err), errors.ErrInternalError)
|
|||
|
}
|
|||
|
if response.Error != nil {
|
|||
|
receiver.logger.Error("failed to delete webhook on <DeleteWebhook> of <YandexClient>",
|
|||
|
zap.Any("response", response.Error),
|
|||
|
zap.String("url", yandexWebhooksURL),
|
|||
|
)
|
|||
|
|
|||
|
return "", errors.NewWithMessage("failed to create payment", errors.ErrInternalError)
|
|||
|
}
|
|||
|
|
|||
|
return response.Body.ID, nil
|
|||
|
}
|