refactored orchestrator and callbacks service to use pkg messsaging + envelope factory / handler
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
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 PaymentStatusUpdatedNotification struct {
|
||||
messaging.Envelope
|
||||
payload model.PaymentStatusUpdated
|
||||
}
|
||||
|
||||
func (psn *PaymentStatusUpdatedNotification) Serialize() ([]byte, error) {
|
||||
data, err := json.Marshal(psn.payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return psn.Envelope.Wrap(data)
|
||||
}
|
||||
|
||||
func paymentStatusUpdatedEvent() model.NotificationEvent {
|
||||
return model.NewNotification(mservice.PaymentOrchestrator, nm.NAUpdated)
|
||||
}
|
||||
|
||||
func NewPaymentStatusUpdatedEnvelope(sender string, status *model.PaymentStatusUpdated) messaging.Envelope {
|
||||
payload := model.PaymentStatusUpdated{}
|
||||
if status != nil {
|
||||
payload = *status
|
||||
}
|
||||
if strings.TrimSpace(payload.Type) == "" {
|
||||
payload.Type = model.PaymentStatusUpdatedType
|
||||
}
|
||||
return &PaymentStatusUpdatedNotification{
|
||||
Envelope: messaging.CreateEnvelope(sender, paymentStatusUpdatedEvent()),
|
||||
payload: payload,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package notifications
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
|
||||
me "github.com/tech/sendico/pkg/messaging/envelope"
|
||||
ch "github.com/tech/sendico/pkg/messaging/notifications/paymentorchestrator/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 PaymentStatusUpdatedProcessor struct {
|
||||
logger mlogger.Logger
|
||||
handler ch.PaymentStatusUpdatedHandler
|
||||
event model.NotificationEvent
|
||||
}
|
||||
|
||||
func (p *PaymentStatusUpdatedProcessor) Process(ctx context.Context, envelope me.Envelope) error {
|
||||
var msg model.PaymentStatusUpdated
|
||||
if err := json.Unmarshal(envelope.GetData(), &msg); err != nil {
|
||||
p.logger.Warn("Failed to decode payment status updated envelope", zap.Error(err), zap.String("topic", p.event.ToString()))
|
||||
return err
|
||||
}
|
||||
if strings.TrimSpace(msg.Type) == "" {
|
||||
msg.Type = model.PaymentStatusUpdatedType
|
||||
}
|
||||
if p.handler == nil {
|
||||
p.logger.Warn("Payment status updated handler is not configured", zap.String("topic", p.event.ToString()))
|
||||
return nil
|
||||
}
|
||||
return p.handler(ctx, &msg)
|
||||
}
|
||||
|
||||
func (p *PaymentStatusUpdatedProcessor) GetSubject() model.NotificationEvent {
|
||||
return p.event
|
||||
}
|
||||
|
||||
func NewPaymentStatusUpdatedProcessor(logger mlogger.Logger, handler ch.PaymentStatusUpdatedHandler) np.EnvelopeProcessor {
|
||||
if logger != nil {
|
||||
logger = logger.Named("payment_status_updated_processor")
|
||||
}
|
||||
return &PaymentStatusUpdatedProcessor{
|
||||
logger: logger,
|
||||
handler: handler,
|
||||
event: paymentStatusUpdatedEvent(),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package notifications
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/tech/sendico/pkg/model"
|
||||
)
|
||||
|
||||
type PaymentStatusUpdatedHandler = func(context.Context, *model.PaymentStatusUpdated) error
|
||||
@@ -0,0 +1,18 @@
|
||||
package notifications
|
||||
|
||||
import (
|
||||
messaging "github.com/tech/sendico/pkg/messaging/envelope"
|
||||
pinternal "github.com/tech/sendico/pkg/messaging/internal/notifications/paymentorchestrator"
|
||||
ch "github.com/tech/sendico/pkg/messaging/notifications/paymentorchestrator/handler"
|
||||
np "github.com/tech/sendico/pkg/messaging/notifications/processor"
|
||||
"github.com/tech/sendico/pkg/mlogger"
|
||||
"github.com/tech/sendico/pkg/model"
|
||||
)
|
||||
|
||||
func PaymentStatusUpdated(sender string, status *model.PaymentStatusUpdated) messaging.Envelope {
|
||||
return pinternal.NewPaymentStatusUpdatedEnvelope(sender, status)
|
||||
}
|
||||
|
||||
func NewPaymentStatusUpdatedProcessor(logger mlogger.Logger, handler ch.PaymentStatusUpdatedHandler) np.EnvelopeProcessor {
|
||||
return pinternal.NewPaymentStatusUpdatedProcessor(logger, handler)
|
||||
}
|
||||
29
api/pkg/model/payment_orchestrator_status.go
Normal file
29
api/pkg/model/payment_orchestrator_status.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package model
|
||||
|
||||
import "time"
|
||||
|
||||
const (
|
||||
PaymentStatusUpdatedType = "payment.status.updated"
|
||||
)
|
||||
|
||||
type PaymentStatusUpdated struct {
|
||||
EventID string `json:"event_id,omitempty"`
|
||||
Type string `json:"type,omitempty"`
|
||||
ClientID string `json:"client_id,omitempty"`
|
||||
OccurredAt time.Time `json:"occurred_at,omitempty"`
|
||||
PublishedAt time.Time `json:"published_at,omitempty"`
|
||||
Data PaymentStatusUpdatedData `json:"data"`
|
||||
}
|
||||
|
||||
type PaymentStatusUpdatedData struct {
|
||||
OrganizationRef string `json:"organization_ref,omitempty"`
|
||||
PaymentRef string `json:"payment_ref,omitempty"`
|
||||
QuotationRef string `json:"quotation_ref,omitempty"`
|
||||
ClientPaymentRef string `json:"client_payment_ref,omitempty"`
|
||||
IdempotencyKey string `json:"idempotency_key,omitempty"`
|
||||
State string `json:"state,omitempty"`
|
||||
PreviousState string `json:"previous_state,omitempty"`
|
||||
Version uint64 `json:"version,omitempty"`
|
||||
IsTerminal bool `json:"is_terminal"`
|
||||
Event string `json:"event,omitempty"`
|
||||
}
|
||||
Reference in New Issue
Block a user