TG settlement service
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
package notifications
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
messaging "github.com/tech/sendico/pkg/messaging/envelope"
|
||||
"github.com/tech/sendico/pkg/model"
|
||||
nm "github.com/tech/sendico/pkg/model/notification"
|
||||
"github.com/tech/sendico/pkg/mservice"
|
||||
)
|
||||
|
||||
type PaymentGatewayIntentNotification struct {
|
||||
messaging.Envelope
|
||||
payload model.PaymentGatewayIntent
|
||||
}
|
||||
|
||||
func (pgn *PaymentGatewayIntentNotification) Serialize() ([]byte, error) {
|
||||
data, err := json.Marshal(pgn.payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return pgn.Envelope.Wrap(data)
|
||||
}
|
||||
|
||||
type PaymentGatewayExecutionNotification struct {
|
||||
messaging.Envelope
|
||||
payload model.PaymentGatewayExecution
|
||||
}
|
||||
|
||||
func (pgn *PaymentGatewayExecutionNotification) Serialize() ([]byte, error) {
|
||||
data, err := json.Marshal(pgn.payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return pgn.Envelope.Wrap(data)
|
||||
}
|
||||
|
||||
func intentEvent() model.NotificationEvent {
|
||||
return model.NewNotification(mservice.PaymentGateway, nm.NAPaymentGatewayIntent)
|
||||
}
|
||||
|
||||
func executionEvent() model.NotificationEvent {
|
||||
return model.NewNotification(mservice.PaymentGateway, nm.NAPaymentGatewayExecution)
|
||||
}
|
||||
|
||||
func NewPaymentGatewayIntentEnvelope(sender string, intent *model.PaymentGatewayIntent) messaging.Envelope {
|
||||
var payload model.PaymentGatewayIntent
|
||||
if intent != nil {
|
||||
payload = *intent
|
||||
}
|
||||
return &PaymentGatewayIntentNotification{
|
||||
Envelope: messaging.CreateEnvelope(sender, intentEvent()),
|
||||
payload: payload,
|
||||
}
|
||||
}
|
||||
|
||||
func NewPaymentGatewayExecutionEnvelope(sender string, exec *model.PaymentGatewayExecution) messaging.Envelope {
|
||||
var payload model.PaymentGatewayExecution
|
||||
if exec != nil {
|
||||
payload = *exec
|
||||
}
|
||||
return &PaymentGatewayExecutionNotification{
|
||||
Envelope: messaging.CreateEnvelope(sender, executionEvent()),
|
||||
payload: payload,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package notifications
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
me "github.com/tech/sendico/pkg/messaging/envelope"
|
||||
ch "github.com/tech/sendico/pkg/messaging/notifications/paymentgateway/handler"
|
||||
np "github.com/tech/sendico/pkg/messaging/notifications/processor"
|
||||
"github.com/tech/sendico/pkg/mlogger"
|
||||
"github.com/tech/sendico/pkg/model"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type PaymentGatewayIntentProcessor struct {
|
||||
logger mlogger.Logger
|
||||
handler ch.PaymentGatewayIntentHandler
|
||||
event model.NotificationEvent
|
||||
}
|
||||
|
||||
func (pgp *PaymentGatewayIntentProcessor) Process(ctx context.Context, envelope me.Envelope) error {
|
||||
var msg model.PaymentGatewayIntent
|
||||
if err := json.Unmarshal(envelope.GetData(), &msg); err != nil {
|
||||
pgp.logger.Warn("Failed to decode payment gateway intent envelope", zap.Error(err), zap.String("topic", pgp.event.ToString()))
|
||||
return err
|
||||
}
|
||||
if pgp.handler == nil {
|
||||
pgp.logger.Warn("Payment gateway intent handler is not configured", zap.String("topic", pgp.event.ToString()))
|
||||
return nil
|
||||
}
|
||||
return pgp.handler(ctx, &msg)
|
||||
}
|
||||
|
||||
func (pgp *PaymentGatewayIntentProcessor) GetSubject() model.NotificationEvent {
|
||||
return pgp.event
|
||||
}
|
||||
|
||||
type PaymentGatewayExecutionProcessor struct {
|
||||
logger mlogger.Logger
|
||||
handler ch.PaymentGatewayExecutionHandler
|
||||
event model.NotificationEvent
|
||||
}
|
||||
|
||||
func (pgp *PaymentGatewayExecutionProcessor) Process(ctx context.Context, envelope me.Envelope) error {
|
||||
var msg model.PaymentGatewayExecution
|
||||
if err := json.Unmarshal(envelope.GetData(), &msg); err != nil {
|
||||
pgp.logger.Warn("Failed to decode payment gateway execution envelope", zap.Error(err), zap.String("topic", pgp.event.ToString()))
|
||||
return err
|
||||
}
|
||||
if pgp.handler == nil {
|
||||
pgp.logger.Warn("Payment gateway execution handler is not configured", zap.String("topic", pgp.event.ToString()))
|
||||
return nil
|
||||
}
|
||||
return pgp.handler(ctx, &msg)
|
||||
}
|
||||
|
||||
func (pgp *PaymentGatewayExecutionProcessor) GetSubject() model.NotificationEvent {
|
||||
return pgp.event
|
||||
}
|
||||
|
||||
func NewPaymentGatewayIntentProcessor(logger mlogger.Logger, handler ch.PaymentGatewayIntentHandler) np.EnvelopeProcessor {
|
||||
if logger != nil {
|
||||
logger = logger.Named("payment_gateway_intent_processor")
|
||||
}
|
||||
return &PaymentGatewayIntentProcessor{
|
||||
logger: logger,
|
||||
handler: handler,
|
||||
event: intentEvent(),
|
||||
}
|
||||
}
|
||||
|
||||
func NewPaymentGatewayExecutionProcessor(logger mlogger.Logger, handler ch.PaymentGatewayExecutionHandler) np.EnvelopeProcessor {
|
||||
if logger != nil {
|
||||
logger = logger.Named("payment_gateway_execution_processor")
|
||||
}
|
||||
return &PaymentGatewayExecutionProcessor{
|
||||
logger: logger,
|
||||
handler: handler,
|
||||
event: executionEvent(),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user