|
| 1 | +package com.margelo.nitro.plugpagnitro |
| 2 | + |
| 3 | +import android.util.Log |
| 4 | +import com.facebook.react.bridge.Arguments |
| 5 | +import com.facebook.react.modules.core.DeviceEventManagerModule |
| 6 | +import com.margelo.nitro.NitroModules |
| 7 | +import br.com.uol.pagseguro.plugpagservice.wrapper.PlugPag |
| 8 | +import br.com.uol.pagseguro.plugpagservice.wrapper.PlugPagEventData |
| 9 | +import br.com.uol.pagseguro.plugpagservice.wrapper.PlugPagEventListener |
| 10 | + |
| 11 | +/** |
| 12 | + * Payment Event Handler for PlugPag Operations |
| 13 | + * Handles payment-related events and emits them to React Native |
| 14 | + */ |
| 15 | +class PaymentEventHandler(private val uiStateManager: UIStateManager) : PlugPagEventListener { |
| 16 | + |
| 17 | + private var countPassword = 0 |
| 18 | + private var messageCard = "" |
| 19 | + |
| 20 | + companion object { |
| 21 | + private const val TAG = "PlugpagPaymentEvents" |
| 22 | + private const val EVENT_PAYMENTS = "eventPayments" |
| 23 | + } |
| 24 | + |
| 25 | + override fun onEvent(plugPagEventData: PlugPagEventData) { |
| 26 | + try { |
| 27 | + // Handle events based on custom message since PlugPagEventData may not have public eventType |
| 28 | + val message = plugPagEventData.customMessage ?: "Payment processing..." |
| 29 | + |
| 30 | + when { |
| 31 | + message.contains("senha", ignoreCase = true) || |
| 32 | + message.contains("password", ignoreCase = true) -> { |
| 33 | + emitPaymentEvent("WAITING_PASSWORD", "Please enter your password") |
| 34 | + } |
| 35 | + message.contains("cartão", ignoreCase = true) || |
| 36 | + message.contains("card", ignoreCase = true) -> { |
| 37 | + emitPaymentEvent("WAITING_CARD", "Please insert or approximate your card") |
| 38 | + } |
| 39 | + message.contains("aprovado", ignoreCase = true) || |
| 40 | + message.contains("approved", ignoreCase = true) -> { |
| 41 | + countPassword = 0 |
| 42 | + emitPaymentEvent("TRANSACTION_APPROVED", message) |
| 43 | + } |
| 44 | + message.contains("erro", ignoreCase = true) || |
| 45 | + message.contains("error", ignoreCase = true) -> { |
| 46 | + emitPaymentEvent("TRANSACTION_ERROR", message) |
| 47 | + } |
| 48 | + message.contains("cancelado", ignoreCase = true) || |
| 49 | + message.contains("cancelled", ignoreCase = true) -> { |
| 50 | + emitPaymentEvent("TRANSACTION_CANCELLED", message) |
| 51 | + } |
| 52 | + else -> { |
| 53 | + emitPaymentEvent("PAYMENT_EVENT", message) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + } catch (e: Exception) { |
| 58 | + Log.e(TAG, "Error handling payment event: ${e.message}", e) |
| 59 | + emitPaymentEvent("PAYMENT_ERROR", "Error processing payment event: ${e.message}") |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Emit payment event to React Native |
| 65 | + */ |
| 66 | + private fun emitPaymentEvent(eventType: String, message: String, additionalData: Map<String, Any>? = null) { |
| 67 | + try { |
| 68 | + val context = NitroModules.applicationContext |
| 69 | + if (context != null) { |
| 70 | + val eventData = Arguments.createMap().apply { |
| 71 | + putString("eventType", eventType) |
| 72 | + putString("message", message) |
| 73 | + putDouble("timestamp", System.currentTimeMillis().toDouble()) |
| 74 | + |
| 75 | + additionalData?.forEach { (key, value) -> |
| 76 | + when (value) { |
| 77 | + is String -> putString(key, value) |
| 78 | + is Boolean -> putBoolean(key, value) |
| 79 | + is Int -> putInt(key, value) |
| 80 | + is Double -> putDouble(key, value) |
| 81 | + else -> putString(key, value.toString()) |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + (context as? com.facebook.react.bridge.ReactApplicationContext) |
| 87 | + ?.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java) |
| 88 | + ?.emit(EVENT_PAYMENTS, eventData) |
| 89 | + |
| 90 | + Log.d(TAG, "Payment event emitted: $eventType - $message") |
| 91 | + } else { |
| 92 | + Log.w(TAG, "Cannot emit payment event - no React context available") |
| 93 | + } |
| 94 | + } catch (e: Exception) { |
| 95 | + Log.e(TAG, "Error emitting payment event: ${e.message}", e) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Reset internal state |
| 101 | + */ |
| 102 | + fun reset() { |
| 103 | + countPassword = 0 |
| 104 | + messageCard = "" |
| 105 | + } |
| 106 | +} |
0 commit comments