2023-06-13 13:22:51 +00:00
|
|
|
|
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"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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{
|
2023-06-19 19:39:34 +00:00
|
|
|
|
URL: receiver.configuration.URL.Payments,
|
2023-06-13 13:22:51 +00:00
|
|
|
|
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{
|
2023-06-19 19:39:34 +00:00
|
|
|
|
URL: receiver.configuration.URL.Payments,
|
2023-06-13 13:22:51 +00:00
|
|
|
|
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{
|
2023-06-19 19:39:34 +00:00
|
|
|
|
URL: receiver.configuration.URL.Payments,
|
2023-06-13 13:22:51 +00:00
|
|
|
|
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{
|
2023-06-19 19:39:34 +00:00
|
|
|
|
URL: receiver.configuration.URL.Payments,
|
2023-06-13 13:22:51 +00:00
|
|
|
|
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{
|
2023-06-19 19:39:34 +00:00
|
|
|
|
URL: receiver.configuration.URL.Payments,
|
2023-06-13 13:22:51 +00:00
|
|
|
|
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 {
|
2023-06-19 19:39:34 +00:00
|
|
|
|
url, err := url.JoinPath(receiver.configuration.URL.Webhooks, webhookID)
|
2023-06-13 13:22:51 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
receiver.logger.Error("failed to join path url on <DeleteWebhook> of <YandexClient>",
|
|
|
|
|
zap.Error(err),
|
2023-06-19 19:39:34 +00:00
|
|
|
|
zap.String("yandex webhook url", receiver.configuration.URL.Webhooks),
|
2023-06-13 13:22:51 +00:00
|
|
|
|
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{
|
2023-06-19 19:39:34 +00:00
|
|
|
|
URL: receiver.configuration.URL.Webhooks,
|
2023-06-13 13:22:51 +00:00
|
|
|
|
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),
|
2023-06-19 19:39:34 +00:00
|
|
|
|
zap.String("url", receiver.configuration.URL.Webhooks),
|
2023-06-13 13:22:51 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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{
|
2023-06-19 19:39:34 +00:00
|
|
|
|
URL: receiver.configuration.URL.Webhooks,
|
2023-06-13 13:22:51 +00:00
|
|
|
|
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),
|
2023-06-19 19:39:34 +00:00
|
|
|
|
zap.String("url", receiver.configuration.URL.Webhooks),
|
2023-06-13 13:22:51 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return "", errors.NewWithMessage("failed to create payment", errors.ErrInternalError)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response.Body.ID, nil
|
|
|
|
|
}
|