2023-06-19 19:39:34 +00:00
|
|
|
package mock
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"github.com/walkerus/go-wiremock"
|
|
|
|
"go.uber.org/zap"
|
2024-12-16 13:47:40 +00:00
|
|
|
"gitea.pena/PenaSide/treasurer/internal/errors"
|
|
|
|
"gitea.pena/PenaSide/treasurer/internal/models"
|
|
|
|
"gitea.pena/PenaSide/treasurer/internal/models/yandex"
|
2023-06-19 19:39:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type YoomoneyMockServiceDeps struct {
|
|
|
|
Logger *zap.Logger
|
|
|
|
YoomoneyURL *models.YoomomeyURL
|
|
|
|
}
|
|
|
|
|
|
|
|
type YoomoneyMockService struct {
|
|
|
|
logger *zap.Logger
|
|
|
|
yoomoneyURL *models.YoomomeyURL
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewYoomoneyMockService(deps YoomoneyMockServiceDeps) (*YoomoneyMockService, errors.Error) {
|
|
|
|
if deps.Logger == nil {
|
|
|
|
return nil, errors.NewWithMessage("logger is nil on <NewYoomoneyMockService>", errors.ErrInvalidArgs)
|
|
|
|
}
|
|
|
|
|
|
|
|
if deps.YoomoneyURL == nil {
|
|
|
|
return nil, errors.NewWithMessage("yoomoney urls is nil on <NewYoomoneyMockService>", errors.ErrInvalidArgs)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &YoomoneyMockService{
|
|
|
|
logger: deps.Logger,
|
|
|
|
yoomoneyURL: deps.YoomoneyURL,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (receiver *YoomoneyMockService) Register(host string) errors.Error {
|
|
|
|
return receiver.RegisterPayments(host)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (receiver *YoomoneyMockService) RegisterPayments(host string) errors.Error {
|
2023-06-20 08:25:43 +00:00
|
|
|
paymentsURL, parseErr := url.Parse(receiver.yoomoneyURL.Payments)
|
2023-06-19 19:39:34 +00:00
|
|
|
if parseErr != nil {
|
|
|
|
receiver.logger.Error("failed to parse payment url on <RegisterPayment> of <YoomoneyMockService>",
|
|
|
|
zap.Error(parseErr),
|
|
|
|
zap.String("url", receiver.yoomoneyURL.Payments),
|
|
|
|
)
|
|
|
|
|
|
|
|
return errors.NewWithError(fmt.Errorf("failed to parse payment url: %w", parseErr), errors.ErrInternalError)
|
|
|
|
}
|
|
|
|
|
2023-06-20 08:25:43 +00:00
|
|
|
hostURL, parseErr := url.Parse(host)
|
|
|
|
if parseErr != nil {
|
|
|
|
receiver.logger.Error("failed to parse payment url on <RegisterPayment> of <YoomoneyMockService>",
|
|
|
|
zap.Error(parseErr),
|
|
|
|
zap.String("url", receiver.yoomoneyURL.Payments),
|
|
|
|
)
|
|
|
|
|
|
|
|
return errors.NewWithError(fmt.Errorf("failed to parse payment url: %w", parseErr), errors.ErrInternalError)
|
|
|
|
}
|
|
|
|
|
|
|
|
if hostURL.Host != paymentsURL.Host {
|
2023-06-19 19:39:34 +00:00
|
|
|
return errors.NewWithMessage("yoomoney hosts not equal", errors.ErrInternalError)
|
|
|
|
}
|
|
|
|
|
2023-06-20 08:25:43 +00:00
|
|
|
if hostURL.Scheme != paymentsURL.Scheme {
|
|
|
|
return errors.NewWithMessage("yoomoney scheme not equal", errors.ErrInternalError)
|
|
|
|
}
|
|
|
|
|
2023-06-19 19:39:34 +00:00
|
|
|
mockClient := wiremock.NewClient(host)
|
|
|
|
|
|
|
|
rule := wiremock.
|
2023-06-20 08:25:43 +00:00
|
|
|
Post(wiremock.URLPathEqualTo(paymentsURL.Path)).
|
2023-06-19 19:39:34 +00:00
|
|
|
WillReturnJSON(
|
|
|
|
&yandex.Payment{
|
|
|
|
ID: "f32d71f4-214b-464b-94bb-f7b4d5299af0",
|
|
|
|
Status: yandex.PaymentStatusPending,
|
|
|
|
Amount: yandex.Amount{
|
|
|
|
Value: "150.00",
|
|
|
|
Currency: "RUB",
|
|
|
|
},
|
|
|
|
Confirmation: &yandex.ConfirmationRedirect{
|
|
|
|
Type: yandex.ConfirmationTypeRedirect,
|
|
|
|
ConfirmationURL: "https://yandex-redirect-url.com",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
map[string]string{"Content-Type": "application/json"},
|
|
|
|
200,
|
|
|
|
).
|
|
|
|
AtPriority(1)
|
|
|
|
|
|
|
|
if err := mockClient.StubFor(rule); err != nil {
|
2023-06-20 10:05:17 +00:00
|
|
|
receiver.logger.Error("failed to init payment mock handler on <RegisterPayment> of <YoomoneyMockService>",
|
|
|
|
zap.Error(err),
|
|
|
|
zap.String("payments full url", receiver.yoomoneyURL.Payments),
|
|
|
|
zap.String("payments path", paymentsURL.Path),
|
|
|
|
)
|
|
|
|
|
2023-06-19 19:39:34 +00:00
|
|
|
return errors.NewWithError(fmt.Errorf("failed to init payment mock handler: %w", err), errors.ErrInternalError)
|
|
|
|
}
|
|
|
|
|
2023-06-20 08:25:43 +00:00
|
|
|
isVerified, err := mockClient.Verify(wiremock.NewRequest("post", wiremock.URLPathEqualTo(paymentsURL.Path)), 0)
|
2023-06-19 19:39:34 +00:00
|
|
|
if err != nil {
|
2023-06-20 10:05:17 +00:00
|
|
|
receiver.logger.Error("failed to verify payment mock handler on <RegisterPayment> of <YoomoneyMockService>",
|
|
|
|
zap.Error(err),
|
|
|
|
zap.String("payments full url", receiver.yoomoneyURL.Payments),
|
|
|
|
zap.String("payments path", paymentsURL.Path),
|
|
|
|
)
|
|
|
|
|
2023-06-19 19:39:34 +00:00
|
|
|
return errors.NewWithError(fmt.Errorf("failed to verify payment mock handler: %w", err), errors.ErrInternalError)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isVerified {
|
|
|
|
return errors.NewWithMessage("payment mock handler not verified", errors.ErrInternalError)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|