TG settlement service
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
package notifications
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
|
||||
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 ConfirmationRequestNotification struct {
|
||||
messaging.Envelope
|
||||
payload model.ConfirmationRequest
|
||||
}
|
||||
|
||||
func (crn *ConfirmationRequestNotification) Serialize() ([]byte, error) {
|
||||
data, err := json.Marshal(crn.payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return crn.Envelope.Wrap(data)
|
||||
}
|
||||
|
||||
type ConfirmationResultNotification struct {
|
||||
messaging.Envelope
|
||||
payload model.ConfirmationResult
|
||||
}
|
||||
|
||||
func (crn *ConfirmationResultNotification) Serialize() ([]byte, error) {
|
||||
data, err := json.Marshal(crn.payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return crn.Envelope.Wrap(data)
|
||||
}
|
||||
|
||||
func confirmationRequestEvent() model.NotificationEvent {
|
||||
return model.NewNotification(mservice.Notifications, nm.NAConfirmationRequest)
|
||||
}
|
||||
|
||||
func confirmationResultEvent(sourceService, rail string) model.NotificationEvent {
|
||||
action := strings.TrimSpace(sourceService)
|
||||
if action == "" {
|
||||
action = "unknown"
|
||||
}
|
||||
action = strings.ToLower(action)
|
||||
rail = strings.TrimSpace(rail)
|
||||
if rail == "" {
|
||||
rail = "default"
|
||||
}
|
||||
rail = strings.ToLower(rail)
|
||||
return model.NewNotification(mservice.Confirmations, nm.NotificationAction(action+"."+rail))
|
||||
}
|
||||
|
||||
func NewConfirmationRequestEnvelope(sender string, request *model.ConfirmationRequest) messaging.Envelope {
|
||||
var payload model.ConfirmationRequest
|
||||
if request != nil {
|
||||
payload = *request
|
||||
}
|
||||
return &ConfirmationRequestNotification{
|
||||
Envelope: messaging.CreateEnvelope(sender, confirmationRequestEvent()),
|
||||
payload: payload,
|
||||
}
|
||||
}
|
||||
|
||||
func NewConfirmationResultEnvelope(sender string, result *model.ConfirmationResult, sourceService, rail string) messaging.Envelope {
|
||||
var payload model.ConfirmationResult
|
||||
if result != nil {
|
||||
payload = *result
|
||||
}
|
||||
return &ConfirmationResultNotification{
|
||||
Envelope: messaging.CreateEnvelope(sender, confirmationResultEvent(sourceService, rail)),
|
||||
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/confirmations/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 ConfirmationRequestProcessor struct {
|
||||
logger mlogger.Logger
|
||||
handler ch.ConfirmationRequestHandler
|
||||
event model.NotificationEvent
|
||||
}
|
||||
|
||||
func (crp *ConfirmationRequestProcessor) Process(ctx context.Context, envelope me.Envelope) error {
|
||||
var msg model.ConfirmationRequest
|
||||
if err := json.Unmarshal(envelope.GetData(), &msg); err != nil {
|
||||
crp.logger.Warn("Failed to decode confirmation request envelope", zap.Error(err), zap.String("topic", crp.event.ToString()))
|
||||
return err
|
||||
}
|
||||
if crp.handler == nil {
|
||||
crp.logger.Warn("Confirmation request handler is not configured", zap.String("topic", crp.event.ToString()))
|
||||
return nil
|
||||
}
|
||||
return crp.handler(ctx, &msg)
|
||||
}
|
||||
|
||||
func (crp *ConfirmationRequestProcessor) GetSubject() model.NotificationEvent {
|
||||
return crp.event
|
||||
}
|
||||
|
||||
type ConfirmationResultProcessor struct {
|
||||
logger mlogger.Logger
|
||||
handler ch.ConfirmationResultHandler
|
||||
event model.NotificationEvent
|
||||
}
|
||||
|
||||
func (crp *ConfirmationResultProcessor) Process(ctx context.Context, envelope me.Envelope) error {
|
||||
var msg model.ConfirmationResult
|
||||
if err := json.Unmarshal(envelope.GetData(), &msg); err != nil {
|
||||
crp.logger.Warn("Failed to decode confirmation result envelope", zap.Error(err), zap.String("topic", crp.event.ToString()))
|
||||
return err
|
||||
}
|
||||
if crp.handler == nil {
|
||||
crp.logger.Warn("Confirmation result handler is not configured", zap.String("topic", crp.event.ToString()))
|
||||
return nil
|
||||
}
|
||||
return crp.handler(ctx, &msg)
|
||||
}
|
||||
|
||||
func (crp *ConfirmationResultProcessor) GetSubject() model.NotificationEvent {
|
||||
return crp.event
|
||||
}
|
||||
|
||||
func NewConfirmationRequestProcessor(logger mlogger.Logger, handler ch.ConfirmationRequestHandler) np.EnvelopeProcessor {
|
||||
if logger != nil {
|
||||
logger = logger.Named("confirmation_request_processor")
|
||||
}
|
||||
return &ConfirmationRequestProcessor{
|
||||
logger: logger,
|
||||
handler: handler,
|
||||
event: confirmationRequestEvent(),
|
||||
}
|
||||
}
|
||||
|
||||
func NewConfirmationResultProcessor(logger mlogger.Logger, sourceService, rail string, handler ch.ConfirmationResultHandler) np.EnvelopeProcessor {
|
||||
if logger != nil {
|
||||
logger = logger.Named("confirmation_result_processor")
|
||||
}
|
||||
return &ConfirmationResultProcessor{
|
||||
logger: logger,
|
||||
handler: handler,
|
||||
event: confirmationResultEvent(sourceService, rail),
|
||||
}
|
||||
}
|
||||
@@ -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