@@ -29,6 +29,8 @@ func NewManager(
29
29
}
30
30
}
31
31
32
+ type InvoiceEventFunc func (ctx context.Context , i * invoice.Invoice ) error
33
+
32
34
// Manager handle business logic related to payment gateway
33
35
type Manager struct {
34
36
config localconfig.Config
@@ -38,6 +40,11 @@ type Manager struct {
38
40
invoiceRepository datastore.InvoiceRepository
39
41
subscriptionRepository datastore.SubscriptionRepository
40
42
paymentConfigRepository datastore.PaymentConfigReader
43
+
44
+ invoiceCreatedCallback InvoiceEventFunc
45
+ invoiceProcessedCallback InvoiceEventFunc
46
+ invoiceFailedCallback InvoiceEventFunc
47
+ invoicePaidCallback InvoiceEventFunc
41
48
}
42
49
43
50
// MapMidtransTransactionStatusRepository mapping the midtrans transaction status repository
@@ -78,12 +85,32 @@ func (m *Manager) MustPaymentConfigReader(repo datastore.PaymentConfigReader) {
78
85
m .paymentConfigRepository = repo
79
86
}
80
87
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
+
81
108
func (m Manager ) charger (inv * invoice.Invoice ) invoice.PaymentCharger {
82
109
switch payment .NewGateway (inv .Payment .Gateway ) {
83
110
case payment .GatewayXendit :
84
111
return & xenditCharger {
85
- config : m .config .Xendit ,
86
- XenditGateway : m .xenditGateway ,
112
+ config : m .config .Xendit ,
113
+ XenditGateway : m .xenditGateway ,
87
114
}
88
115
case payment .GatewayMidtrans :
89
116
return & midtransCharger {
@@ -193,6 +220,17 @@ func (m *Manager) GenerateInvoice(ctx context.Context, gir *GenerateInvoiceReque
193
220
return nil , err
194
221
}
195
222
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
+
196
234
l .Info ().Msg ("invoice is created" )
197
235
return inv , nil
198
236
}
@@ -226,6 +264,17 @@ func (m *Manager) PayInvoice(ctx context.Context, pir *PayInvoiceRequest) (*invo
226
264
return nil , err
227
265
}
228
266
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
+
229
278
log .Info ().Msg ("invoice paid" )
230
279
231
280
return inv , nil
@@ -258,6 +307,17 @@ func (m *Manager) ProcessInvoice(ctx context.Context, invoiceNumber string) (*in
258
307
return nil , err
259
308
}
260
309
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
+
261
321
log .Info ().Msg ("invoice is processed" )
262
322
return inv , nil
263
323
}
@@ -289,6 +349,18 @@ func (m *Manager) FailInvoice(ctx context.Context, fir *FailInvoiceRequest) (*in
289
349
if err != nil {
290
350
return nil , err
291
351
}
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
+
292
364
log .Info ().Msg ("invoice is failed" )
293
365
return inv , nil
294
366
}
0 commit comments