Skip to content

Commit

Permalink
feat: add optional callback handler
Browse files Browse the repository at this point in the history
Signed-off-by: Imre Nagi <[email protected]>
  • Loading branch information
imrenagi committed Mar 14, 2022
1 parent 283fddc commit 91c8c37
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
12 changes: 12 additions & 0 deletions manage/manage.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,18 @@ type invoiceI interface {

// FailInvoice make the invoice failed
FailInvoice(ctx context.Context, fir *FailInvoiceRequest) (*invoice.Invoice, error)

// MustInvoiceCreatedEventFunc set event handler for emitting invoice created event
MustInvoiceCreatedEventFunc(fn InvoiceEventFunc)

// MustInvoicePaidEventFunc set event handler for emitting invoice processed event
MustInvoicePaidEventFunc(fn InvoiceEventFunc)

// MustInvoiceProcessedEventFunc set event handler for emitting invoice processed event
MustInvoiceProcessedEventFunc(fn InvoiceEventFunc)

// MustInvoiceFailedEventFunc set event handler for emitting invoice failed event
MustInvoiceFailedEventFunc(fn InvoiceEventFunc)
}

type subscriptionI interface {
Expand Down
76 changes: 74 additions & 2 deletions manage/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func NewManager(
}
}

type InvoiceEventFunc func(ctx context.Context, i *invoice.Invoice) error

// Manager handle business logic related to payment gateway
type Manager struct {
config localconfig.Config
Expand All @@ -38,6 +40,11 @@ type Manager struct {
invoiceRepository datastore.InvoiceRepository
subscriptionRepository datastore.SubscriptionRepository
paymentConfigRepository datastore.PaymentConfigReader

invoiceCreatedCallback InvoiceEventFunc
invoiceProcessedCallback InvoiceEventFunc
invoiceFailedCallback InvoiceEventFunc
invoicePaidCallback InvoiceEventFunc
}

// MapMidtransTransactionStatusRepository mapping the midtrans transaction status repository
Expand Down Expand Up @@ -78,12 +85,32 @@ func (m *Manager) MustPaymentConfigReader(repo datastore.PaymentConfigReader) {
m.paymentConfigRepository = repo
}

// MustInvoiceCreatedEventFunc set event handler for emitting invoice created event
func (m *Manager) MustInvoiceCreatedEventFunc(fn InvoiceEventFunc) {
m.invoiceCreatedCallback = fn
}

// MustInvoicePaidEventFunc set event handler for emitting invoice processed event
func (m *Manager) MustInvoicePaidEventFunc(fn InvoiceEventFunc) {
m.invoicePaidCallback = fn
}

// MustInvoiceProcessedEventFunc set event handler for emitting invoice processed event
func (m *Manager) MustInvoiceProcessedEventFunc(fn InvoiceEventFunc) {
m.invoiceProcessedCallback = fn
}

// MustInvoiceFailedEventFunc set event handler for emitting invoice failed event
func (m *Manager) MustInvoiceFailedEventFunc(fn InvoiceEventFunc) {
m.invoiceFailedCallback = fn
}

func (m Manager) charger(inv *invoice.Invoice) invoice.PaymentCharger {
switch payment.NewGateway(inv.Payment.Gateway) {
case payment.GatewayXendit:
return &xenditCharger{
config: m.config.Xendit,
XenditGateway: m.xenditGateway,
config: m.config.Xendit,
XenditGateway: m.xenditGateway,
}
case payment.GatewayMidtrans:
return &midtransCharger{
Expand Down Expand Up @@ -193,6 +220,17 @@ func (m *Manager) GenerateInvoice(ctx context.Context, gir *GenerateInvoiceReque
return nil, err
}

if m.invoiceCreatedCallback != nil {
go func() {
err := m.invoiceCreatedCallback(context.Background(), inv)
if err != nil {
l.Warn().
Err(err).
Msg("failed sending invoice created callback")
}
}()
}

l.Info().Msg("invoice is created")
return inv, nil
}
Expand Down Expand Up @@ -226,6 +264,17 @@ func (m *Manager) PayInvoice(ctx context.Context, pir *PayInvoiceRequest) (*invo
return nil, err
}

if m.invoicePaidCallback != nil {
go func() {
err := m.invoicePaidCallback(context.Background(), inv)
if err != nil {
log.Warn().
Err(err).
Msg("failed sending invoice paid callback")
}
}()
}

log.Info().Msg("invoice paid")

return inv, nil
Expand Down Expand Up @@ -258,6 +307,17 @@ func (m *Manager) ProcessInvoice(ctx context.Context, invoiceNumber string) (*in
return nil, err
}

if m.invoiceProcessedCallback != nil {
go func() {
err := m.invoiceProcessedCallback(context.Background(), inv)
if err != nil {
log.Warn().
Err(err).
Msg("failed sending invoice processed callback")
}
}()
}

log.Info().Msg("invoice is processed")
return inv, nil
}
Expand Down Expand Up @@ -289,6 +349,18 @@ func (m *Manager) FailInvoice(ctx context.Context, fir *FailInvoiceRequest) (*in
if err != nil {
return nil, err
}

if m.invoiceFailedCallback != nil {
go func() {
err := m.invoiceFailedCallback(context.Background(), inv)
if err != nil {
log.Warn().
Err(err).
Msg("failed sending invoice failed callback")
}
}()
}

log.Info().Msg("invoice is failed")
return inv, nil
}
Expand Down

0 comments on commit 91c8c37

Please sign in to comment.