Skip to content

Commit 91c8c37

Browse files
committed
feat: add optional callback handler
Signed-off-by: Imre Nagi <[email protected]>
1 parent 283fddc commit 91c8c37

File tree

2 files changed

+86
-2
lines changed

2 files changed

+86
-2
lines changed

manage/manage.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,18 @@ type invoiceI interface {
140140

141141
// FailInvoice make the invoice failed
142142
FailInvoice(ctx context.Context, fir *FailInvoiceRequest) (*invoice.Invoice, error)
143+
144+
// MustInvoiceCreatedEventFunc set event handler for emitting invoice created event
145+
MustInvoiceCreatedEventFunc(fn InvoiceEventFunc)
146+
147+
// MustInvoicePaidEventFunc set event handler for emitting invoice processed event
148+
MustInvoicePaidEventFunc(fn InvoiceEventFunc)
149+
150+
// MustInvoiceProcessedEventFunc set event handler for emitting invoice processed event
151+
MustInvoiceProcessedEventFunc(fn InvoiceEventFunc)
152+
153+
// MustInvoiceFailedEventFunc set event handler for emitting invoice failed event
154+
MustInvoiceFailedEventFunc(fn InvoiceEventFunc)
143155
}
144156

145157
type subscriptionI interface {

manage/manager.go

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ func NewManager(
2929
}
3030
}
3131

32+
type InvoiceEventFunc func(ctx context.Context, i *invoice.Invoice) error
33+
3234
// Manager handle business logic related to payment gateway
3335
type Manager struct {
3436
config localconfig.Config
@@ -38,6 +40,11 @@ type Manager struct {
3840
invoiceRepository datastore.InvoiceRepository
3941
subscriptionRepository datastore.SubscriptionRepository
4042
paymentConfigRepository datastore.PaymentConfigReader
43+
44+
invoiceCreatedCallback InvoiceEventFunc
45+
invoiceProcessedCallback InvoiceEventFunc
46+
invoiceFailedCallback InvoiceEventFunc
47+
invoicePaidCallback InvoiceEventFunc
4148
}
4249

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

88+
// MustInvoiceCreatedEventFunc set event handler for emitting invoice created event
89+
func (m *Manager) MustInvoiceCreatedEventFunc(fn InvoiceEventFunc) {
90+
m.invoiceCreatedCallback = fn
91+
}
92+
93+
// MustInvoicePaidEventFunc set event handler for emitting invoice processed event
94+
func (m *Manager) MustInvoicePaidEventFunc(fn InvoiceEventFunc) {
95+
m.invoicePaidCallback = fn
96+
}
97+
98+
// MustInvoiceProcessedEventFunc set event handler for emitting invoice processed event
99+
func (m *Manager) MustInvoiceProcessedEventFunc(fn InvoiceEventFunc) {
100+
m.invoiceProcessedCallback = fn
101+
}
102+
103+
// MustInvoiceFailedEventFunc set event handler for emitting invoice failed event
104+
func (m *Manager) MustInvoiceFailedEventFunc(fn InvoiceEventFunc) {
105+
m.invoiceFailedCallback = fn
106+
}
107+
81108
func (m Manager) charger(inv *invoice.Invoice) invoice.PaymentCharger {
82109
switch payment.NewGateway(inv.Payment.Gateway) {
83110
case payment.GatewayXendit:
84111
return &xenditCharger{
85-
config: m.config.Xendit,
86-
XenditGateway: m.xenditGateway,
112+
config: m.config.Xendit,
113+
XenditGateway: m.xenditGateway,
87114
}
88115
case payment.GatewayMidtrans:
89116
return &midtransCharger{
@@ -193,6 +220,17 @@ func (m *Manager) GenerateInvoice(ctx context.Context, gir *GenerateInvoiceReque
193220
return nil, err
194221
}
195222

223+
if m.invoiceCreatedCallback != nil {
224+
go func() {
225+
err := m.invoiceCreatedCallback(context.Background(), inv)
226+
if err != nil {
227+
l.Warn().
228+
Err(err).
229+
Msg("failed sending invoice created callback")
230+
}
231+
}()
232+
}
233+
196234
l.Info().Msg("invoice is created")
197235
return inv, nil
198236
}
@@ -226,6 +264,17 @@ func (m *Manager) PayInvoice(ctx context.Context, pir *PayInvoiceRequest) (*invo
226264
return nil, err
227265
}
228266

267+
if m.invoicePaidCallback != nil {
268+
go func() {
269+
err := m.invoicePaidCallback(context.Background(), inv)
270+
if err != nil {
271+
log.Warn().
272+
Err(err).
273+
Msg("failed sending invoice paid callback")
274+
}
275+
}()
276+
}
277+
229278
log.Info().Msg("invoice paid")
230279

231280
return inv, nil
@@ -258,6 +307,17 @@ func (m *Manager) ProcessInvoice(ctx context.Context, invoiceNumber string) (*in
258307
return nil, err
259308
}
260309

310+
if m.invoiceProcessedCallback != nil {
311+
go func() {
312+
err := m.invoiceProcessedCallback(context.Background(), inv)
313+
if err != nil {
314+
log.Warn().
315+
Err(err).
316+
Msg("failed sending invoice processed callback")
317+
}
318+
}()
319+
}
320+
261321
log.Info().Msg("invoice is processed")
262322
return inv, nil
263323
}
@@ -289,6 +349,18 @@ func (m *Manager) FailInvoice(ctx context.Context, fir *FailInvoiceRequest) (*in
289349
if err != nil {
290350
return nil, err
291351
}
352+
353+
if m.invoiceFailedCallback != nil {
354+
go func() {
355+
err := m.invoiceFailedCallback(context.Background(), inv)
356+
if err != nil {
357+
log.Warn().
358+
Err(err).
359+
Msg("failed sending invoice failed callback")
360+
}
361+
}()
362+
}
363+
292364
log.Info().Msg("invoice is failed")
293365
return inv, nil
294366
}

0 commit comments

Comments
 (0)