package com.checkoutreactnativecomponents.components import android.content.Context import android.util.AttributeSet import androidx.compose.runtime.Composable import com.checkout.components.interfaces.api.PaymentMethodComponent import com.checkout.components.interfaces.model.UpdateDetails import com.checkoutreactnativecomponents.CheckoutManager import com.checkoutreactnativecomponents.interfaces.CheckoutComponent import kotlinx.coroutines.launch public abstract class CheckoutPaymentComponent : CheckoutComposeComponent { private var paymentComponent: PaymentMethodComponent? = null public constructor(context: Context) : super(context) public constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) public constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) final override suspend fun createComponent(config: HashMap): CheckoutComponent { val component = createPaymentComponent(config) paymentComponent = component return object : CheckoutComponent { @Composable override fun Render() { component.Render() } } } public fun submit() { scope.launch { try { paymentComponent?.submit() ?: initComponentError("submit") } catch (e: Exception) { val error = "${paymentComponent?.name} component submit failed" CheckoutManager.emitAndLogError(error, e) } } } public fun tokenize() { scope.launch { try { paymentComponent?.tokenize() ?: initComponentError("tokenize") } catch (e: Exception) { val error = "${paymentComponent?.name} component tokenize failed" CheckoutManager.emitAndLogError(error, e) } } } public fun update(amount: Int, currency: String? = null) { try { paymentComponent?.update(UpdateDetails(amount, currency)) ?: initComponentError("update") } catch (e: Exception) { val error = "${paymentComponent?.name} component update failed" CheckoutManager.emitAndLogError(error, e) } } private fun initComponentError( method: String, error: Throwable = NullPointerException(), ) { val message = "Error calling $method - PaymentMethodComponent is not initialized" CheckoutManager.emitAndLogError(message, error) } protected abstract suspend fun createPaymentComponent(config: HashMap): PaymentMethodComponent }