/* * Copyright (c) 2023 Adyen N.V. * * This file is open source and available under the MIT license. See the LICENSE file for more info. */ package com.adyenreactnativesdk.component.googlepay import android.util.Log import androidx.fragment.app.FragmentManager import com.adyen.checkout.components.core.CheckoutConfiguration import com.adyen.checkout.components.core.PaymentMethod import com.adyen.checkout.components.core.action.Action import com.adyen.checkout.googlepay.GooglePayComponent import com.adyen.checkout.googlepay.GooglePayComponentState import com.adyen.checkout.sessions.core.CheckoutSession import com.adyen.checkout.ui.core.AdyenComponentView import com.adyenreactnativesdk.AdyenCheckout import com.adyenreactnativesdk.R import com.adyenreactnativesdk.component.base.BaseComponentFragment import com.adyenreactnativesdk.component.base.ComponentData import com.adyenreactnativesdk.component.base.ModuleException class GooglePayFragment( private val configuration: CheckoutConfiguration, paymentMethod: PaymentMethod, session: CheckoutSession? ) : BaseComponentFragment(paymentMethod, session) { override fun setupComponent(componentData: ComponentData) { val session = session val component = (if (session == null) componentData.callback?.let { GooglePayComponent.PROVIDER.get( this, componentData.paymentMethod, configuration, it, ) } else componentData.sessionCallback?.let { GooglePayComponent.PROVIDER.get( this, session, componentData.paymentMethod, configuration, it ) }) ?: throw ModuleException.WrongFlow() this.component = component AdyenCheckout.setComponent(component) view?.findViewById(R.id.component_view) ?.attach(component, this) ?: { Log.e(TAG, FRAGMENT_ERROR) } viewModel.componentStarted() } companion object { internal const val TAG = "GooglePayFragment" private var googlePayScreenVisible = false fun show( fragmentManager: FragmentManager, configuration: CheckoutConfiguration, paymentMethod: PaymentMethod, session: CheckoutSession? ) { googlePayScreenVisible = false GooglePayFragment(configuration, paymentMethod, session).show(fragmentManager, TAG) } fun handle(fragmentManager: FragmentManager, action: Action) { handle(fragmentManager, action, TAG) } fun hide(fragmentManager: FragmentManager) { hide(fragmentManager, TAG) } } override fun runComponent() { if (!googlePayScreenVisible) { component?.startGooglePayScreen( requireActivity(), GooglePayModule.GOOGLEPAY_REQUEST_CODE ) googlePayScreenVisible = true } } }