@@ -15,6 +15,8 @@ import br.com.uol.pagseguro.plugpagservice.wrapper.PlugPagVoidData
15
15
import br.com.uol.pagseguro.plugpagservice.wrapper.PlugPagPrinterData
16
16
import br.com.uol.pagseguro.plugpagservice.wrapper.PlugPagPrinterListener
17
17
import br.com.uol.pagseguro.plugpagservice.wrapper.PlugPagPrintResult
18
+ import br.com.uol.pagseguro.plugpagservice.wrapper.PlugPagEventListener
19
+ import br.com.uol.pagseguro.plugpagservice.wrapper.PlugPagEventData
18
20
19
21
@DoNotStrip
20
22
class PlugpagNitro : HybridPlugpagNitroSpec () {
@@ -179,6 +181,182 @@ class PlugpagNitro : HybridPlugpagNitroSpec() {
179
181
}
180
182
}
181
183
184
+ override fun doPaymentWithEvents (
185
+ amount : Double ,
186
+ type : PaymentType ,
187
+ installmentType : InstallmentType ,
188
+ installments : Double ,
189
+ printReceipt : Boolean ,
190
+ userReference : String
191
+ ): Promise <PlugpagTransactionResult > {
192
+ return Promise .async {
193
+ withContext(Dispatchers .IO ) {
194
+ try {
195
+ initializePlugPag()
196
+
197
+ // Convert enum to PlugPag SDK constants
198
+ val paymentType = when (type) {
199
+ PaymentType .CREDIT -> PlugPag .TYPE_CREDITO
200
+ PaymentType .DEBIT -> PlugPag .TYPE_DEBITO
201
+ PaymentType .VOUCHER -> PlugPag .TYPE_VOUCHER
202
+ PaymentType .PIX -> PlugPag .TYPE_PIX
203
+ }
204
+
205
+ val installmentTypeInt = when (installmentType) {
206
+ InstallmentType .NO_INSTALLMENT -> PlugPag .INSTALLMENT_TYPE_A_VISTA
207
+ InstallmentType .SELLER_INSTALLMENT -> PlugPag .INSTALLMENT_TYPE_PARC_VENDEDOR
208
+ InstallmentType .BUYER_INSTALLMENT -> PlugPag .INSTALLMENT_TYPE_PARC_COMPRADOR
209
+ }
210
+
211
+ val plugPagPaymentData = PlugPagPaymentData (
212
+ paymentType,
213
+ amount.toInt(),
214
+ installmentTypeInt,
215
+ installments.toInt(),
216
+ userReference,
217
+ printReceipt
218
+ )
219
+
220
+ // Set up event listener for real-time payment events
221
+ var passwordCount = 0
222
+ plugPag.setEventListener(object : PlugPagEventListener {
223
+ override fun onEvent (plugPagEventData : PlugPagEventData ) {
224
+ val eventCode = plugPagEventData.eventCode
225
+ var message = plugPagEventData.customMessage ? : " "
226
+
227
+ // Handle specific events and enhance messages
228
+ when (eventCode) {
229
+ PlugPagEventData .EVENT_CODE_DIGIT_PASSWORD -> {
230
+ passwordCount++
231
+ message = when (passwordCount) {
232
+ 1 -> " Senha: *"
233
+ 2 -> " Senha: **"
234
+ 3 -> " Senha: ***"
235
+ 4 -> " Senha: ****"
236
+ 5 -> " Senha: *****"
237
+ 6 -> " Senha: ******"
238
+ else -> " Senha: ****"
239
+ }
240
+ emitPaymentEvent(1010.0 , message)
241
+ }
242
+ PlugPagEventData .EVENT_CODE_NO_PASSWORD -> {
243
+ passwordCount = 0
244
+ message = " Digite sua senha"
245
+ emitPaymentEvent(1011.0 , message)
246
+ }
247
+ else -> {
248
+ // Handle other events with generic messages
249
+ when {
250
+ message.contains(" cartão" , ignoreCase = true ) ||
251
+ message.contains(" card" , ignoreCase = true ) -> {
252
+ if (message.contains(" inserir" , ignoreCase = true ) ||
253
+ message.contains(" insert" , ignoreCase = true )) {
254
+ emitPaymentEvent(1004.0 , " Aguardando cartão..." )
255
+ } else if (message.contains(" remov" , ignoreCase = true ) ||
256
+ message.contains(" retire" , ignoreCase = true )) {
257
+ emitPaymentEvent(1030.0 , " Retire o cartão" )
258
+ } else {
259
+ emitPaymentEvent(1001.0 , message.ifEmpty { " Cartão detectado" })
260
+ }
261
+ }
262
+ message.contains(" processa" , ignoreCase = true ) ||
263
+ message.contains(" process" , ignoreCase = true ) -> {
264
+ emitPaymentEvent(1020.0 , message.ifEmpty { " Processando transação..." })
265
+ }
266
+ message.contains(" conecta" , ignoreCase = true ) ||
267
+ message.contains(" connect" , ignoreCase = true ) -> {
268
+ emitPaymentEvent(1021.0 , message.ifEmpty { " Conectando à rede..." })
269
+ }
270
+ message.contains(" envian" , ignoreCase = true ) ||
271
+ message.contains(" send" , ignoreCase = true ) -> {
272
+ emitPaymentEvent(1022.0 , message.ifEmpty { " Enviando dados..." })
273
+ }
274
+ message.contains(" aguard" , ignoreCase = true ) ||
275
+ message.contains(" wait" , ignoreCase = true ) -> {
276
+ emitPaymentEvent(1023.0 , message.ifEmpty { " Aguardando resposta..." })
277
+ }
278
+ message.contains(" aprovad" , ignoreCase = true ) ||
279
+ message.contains(" aprovad" , ignoreCase = true ) -> {
280
+ emitPaymentEvent(1031.0 , " Transação aprovada" )
281
+ }
282
+ message.contains(" negad" , ignoreCase = true ) ||
283
+ message.contains(" denied" , ignoreCase = true ) ||
284
+ message.contains(" recusad" , ignoreCase = true ) -> {
285
+ emitPaymentEvent(1032.0 , " Transação negada" )
286
+ }
287
+ else -> {
288
+ if (message.isNotEmpty()) {
289
+ emitPaymentEvent(1020.0 , message)
290
+ }
291
+ }
292
+ }
293
+ }
294
+ }
295
+ }
296
+ })
297
+
298
+ // Emit initial event
299
+ emitPaymentEvent(1004.0 , " Aguardando cartão..." )
300
+
301
+ val result = plugPag.doPayment(plugPagPaymentData)
302
+
303
+ // Clear event listener after payment with a no-op listener
304
+ try {
305
+ plugPag.setEventListener(object : PlugPagEventListener {
306
+ override fun onEvent (plugPagEventData : PlugPagEventData ) {
307
+ // No-op listener to clear events
308
+ }
309
+ })
310
+ } catch (e: Exception ) {
311
+ Log .w(TAG , " Could not clear event listener" , e)
312
+ }
313
+
314
+ val errorCode = when (result.result) {
315
+ PlugPag .RET_OK -> ErrorCode .OK
316
+ PlugPag .OPERATION_ABORTED -> ErrorCode .OPERATION_ABORTED
317
+ PlugPag .AUTHENTICATION_FAILED -> ErrorCode .AUTHENTICATION_FAILED
318
+ PlugPag .COMMUNICATION_ERROR -> ErrorCode .COMMUNICATION_ERROR
319
+ PlugPag .NO_PRINTER_DEVICE -> ErrorCode .NO_PRINTER_DEVICE
320
+ PlugPag .NO_TRANSACTION_DATA -> ErrorCode .NO_TRANSACTION_DATA
321
+ else -> ErrorCode .COMMUNICATION_ERROR
322
+ }
323
+
324
+ // Emit final event based on result
325
+ if (errorCode == ErrorCode .OK ) {
326
+ emitPaymentEvent(1031.0 , " Transação aprovada" )
327
+ } else {
328
+ emitPaymentEvent(1032.0 , " Transação negada" )
329
+ }
330
+
331
+ PlugpagTransactionResult (
332
+ result = errorCode,
333
+ errorCode = result.errorCode ? : " " ,
334
+ message = result.message ? : " " ,
335
+ transactionCode = result.transactionCode ? : " " ,
336
+ transactionId = result.transactionId ? : " " ,
337
+ hostNsu = result.hostNsu ? : " " ,
338
+ date = result.date ? : " " ,
339
+ time = result.time ? : " " ,
340
+ cardBrand = result.cardBrand ? : " " ,
341
+ bin = result.bin ? : " " ,
342
+ holder = result.holder ? : " " ,
343
+ userReference = result.userReference ? : " " ,
344
+ terminalSerialNumber = result.terminalSerialNumber ? : " " ,
345
+ amount = result.amount ? : " " ,
346
+ availableBalance = result.availableBalance ? : " " ,
347
+ cardApplication = result.cardApplication ? : " " ,
348
+ label = result.label ? : " " ,
349
+ holderName = result.holderName ? : " " ,
350
+ extendedHolderName = result.extendedHolderName ? : " "
351
+ )
352
+ } catch (e: Exception ) {
353
+ Log .e(TAG , " Error processing payment with events" , e)
354
+ throw Exception (" PAYMENT_WITH_EVENTS_ERROR: ${e.message ? : " Unknown error" } " )
355
+ }
356
+ }
357
+ }
358
+ }
359
+
182
360
override fun refundPayment (
183
361
transactionCode : String ,
184
362
transactionId : String ,
@@ -302,4 +480,17 @@ class PlugpagNitro : HybridPlugpagNitroSpec() {
302
480
plugPag = PlugPag (context)
303
481
}
304
482
}
483
+
484
+ private fun emitPaymentEvent (code : Double , message : String ) {
485
+ try {
486
+ // For now, we'll just log the events
487
+ // In the future, this could be enhanced to emit actual events to React Native
488
+ Log .d(TAG , " Payment Event - Code: $code , Message: $message " )
489
+
490
+ // TODO: Implement proper event emission for React Native
491
+ // This might require additional bridge setup or using a different approach for Nitro modules
492
+ } catch (e: Exception ) {
493
+ Log .e(TAG , " Error emitting payment event" , e)
494
+ }
495
+ }
305
496
}
0 commit comments