108 lines
4.4 KiB
Go
108 lines
4.4 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"go.uber.org/zap"
|
|
"gitea.pena/PenaSide/treasurer/internal/errors"
|
|
"gitea.pena/PenaSide/treasurer/internal/models"
|
|
"gitea.pena/PenaSide/treasurer/internal/models/yandex"
|
|
"gitea.pena/PenaSide/treasurer/internal/utils"
|
|
"gitea.pena/PenaSide/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) {
|
|
receiver.logger.Info("fai<CreatePayment> of <YandexClient>", zap.Any("CRP",request))
|
|
response, err := client.Post[yandex.Payment, any](ctx, &client.RequestSettings{
|
|
URL: receiver.configuration.URL.Payments,
|
|
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) {
|
|
receiver.logger.Info("fai2<CreatePayment> of <YandexClient>", zap.Any("CRP",request))
|
|
response, err := client.Post[yandex.Payment, any](ctx, &client.RequestSettings{
|
|
URL: receiver.configuration.URL.Payments,
|
|
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) {
|
|
receiver.logger.Info("fai3<CreatePayment> of <YandexClient>", zap.Any("CRP",request))
|
|
response, err := client.Post[yandex.Payment, any](ctx, &client.RequestSettings{
|
|
URL: receiver.configuration.URL.Payments,
|
|
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
|
|
}
|