110 lines
4.0 KiB
Go
110 lines
4.0 KiB
Go
![]() |
package client
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
|
|
||
|
"go.uber.org/zap"
|
||
|
"google.golang.org/grpc"
|
||
|
"google.golang.org/grpc/credentials/insecure"
|
||
|
"penahub.gitlab.yandexcloud.net/external/treasurer/internal/errors"
|
||
|
"penahub.gitlab.yandexcloud.net/external/treasurer/internal/models"
|
||
|
"penahub.gitlab.yandexcloud.net/external/treasurer/internal/proto/payment_callback"
|
||
|
)
|
||
|
|
||
|
type CallbackClientDeps struct {
|
||
|
Logger *zap.Logger
|
||
|
}
|
||
|
|
||
|
type CallbackClient struct {
|
||
|
logger *zap.Logger
|
||
|
}
|
||
|
|
||
|
func NewCallbackClient(deps CallbackClientDeps) (*CallbackClient, errors.Error) {
|
||
|
if deps.Logger == nil {
|
||
|
return nil, errors.NewWithMessage("logger is nil on <NewCallbackClient>", errors.ErrInvalidArgs)
|
||
|
}
|
||
|
|
||
|
return &CallbackClient{logger: deps.Logger}, nil
|
||
|
}
|
||
|
|
||
|
func (receiver *CallbackClient) SendOnSuccess(ctx context.Context, host string, event *models.Event) errors.Error {
|
||
|
connection, err := grpc.Dial(host, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||
|
if err != nil {
|
||
|
receiver.logger.Error("failed to connect on <SendOnSuccess> of <CallbackClient>", zap.Error(err), zap.String("host", host))
|
||
|
return errors.NewWithError(fmt.Errorf("failed connect to callback service: %w", err), errors.ErrInternalError)
|
||
|
}
|
||
|
defer func() {
|
||
|
if closeErr := connection.Close(); closeErr != nil {
|
||
|
receiver.logger.Error("failed to close connection on <SendOnSuccess> of <CallbackClient>", zap.Error(closeErr))
|
||
|
}
|
||
|
}()
|
||
|
|
||
|
client := payment_callback.NewPaymentCallbackServiceClient(connection)
|
||
|
|
||
|
if _, err := client.OnSuccess(ctx, &payment_callback.Event{
|
||
|
Key: event.Key,
|
||
|
Message: event.Message,
|
||
|
Payment: &payment_callback.Payment{
|
||
|
ID: event.Payment.ID,
|
||
|
UserID: event.Payment.UserID,
|
||
|
PaymentID: event.Payment.PaymentID,
|
||
|
IdempotencePaymentID: event.Payment.IdempotencePaymentID,
|
||
|
Amount: event.Payment.Amount,
|
||
|
Currency: event.Payment.Currency,
|
||
|
Type: string(event.Payment.Type),
|
||
|
Status: string(event.Payment.Status),
|
||
|
Completed: event.Payment.Completed,
|
||
|
},
|
||
|
}); err != nil {
|
||
|
receiver.logger.Error("failed to send success callback on <SendOnSuccess> of <CallbackClient>",
|
||
|
zap.Error(err),
|
||
|
zap.String("host", host),
|
||
|
zap.Any("event", event),
|
||
|
)
|
||
|
return errors.NewWithError(fmt.Errorf("failed to send success callback: %w", err), errors.ErrInternalError)
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (receiver *CallbackClient) SendOnFailure(ctx context.Context, host string, event *models.Event) errors.Error {
|
||
|
connection, err := grpc.Dial(host, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||
|
if err != nil {
|
||
|
receiver.logger.Error("failed to connect on <SendOnFailure> of <CallbackClient>", zap.Error(err), zap.String("host", host))
|
||
|
return errors.NewWithError(fmt.Errorf("failed connect to callback service: %w", err), errors.ErrInternalError)
|
||
|
}
|
||
|
defer func() {
|
||
|
if closeErr := connection.Close(); closeErr != nil {
|
||
|
receiver.logger.Error("failed to close connection on <SendOnFailure> of <CallbackClient>", zap.Error(closeErr))
|
||
|
}
|
||
|
}()
|
||
|
|
||
|
client := payment_callback.NewPaymentCallbackServiceClient(connection)
|
||
|
|
||
|
if _, err := client.OnFailure(ctx, &payment_callback.Event{
|
||
|
Key: event.Key,
|
||
|
Message: event.Message,
|
||
|
Payment: &payment_callback.Payment{
|
||
|
ID: event.Payment.ID,
|
||
|
UserID: event.Payment.UserID,
|
||
|
PaymentID: event.Payment.PaymentID,
|
||
|
IdempotencePaymentID: event.Payment.IdempotencePaymentID,
|
||
|
Amount: event.Payment.Amount,
|
||
|
Currency: event.Payment.Currency,
|
||
|
Type: string(event.Payment.Type),
|
||
|
Status: string(event.Payment.Status),
|
||
|
Completed: event.Payment.Completed,
|
||
|
},
|
||
|
}); err != nil {
|
||
|
receiver.logger.Error("failed to send failure callback on <SendOnFailure> of <CallbackClient>",
|
||
|
zap.Error(err),
|
||
|
zap.String("host", host),
|
||
|
zap.Any("event", event),
|
||
|
)
|
||
|
return errors.NewWithError(fmt.Errorf("failed to send failure callback: %w", err), errors.ErrInternalError)
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|