braintree / braintree-android-drop-in

Braintree Drop-In SDK for Android
https://developers.braintreepayments.com/guides/drop-in/android/v2
MIT License
124 stars 78 forks source link

Unable to register DropInClient within coroutines or suspend functions or view model #467

Open AG-Akash09 opened 6 months ago

AG-Akash09 commented 6 months ago

Braintree SDK Version

6.15.0

Environment

Both

Android Version & Device

No response

Braintree dependencies

implementation 'com.braintreepayments.api:drop-in:6.15.0'
implementation 'com.braintreepayments.api:data-collector:4.41.0'

Describe the bug

Our network calls are seamlessly integrated with coroutines and view models. To retrieve the client token from our server, we employ a suspend function and observe its value in the onCreateView function. However, attempting to pass the client token to DropInClient results in a crash, accompanied by the error 'java.lang.IllegalStateException: LifecycleOwner MembershipFragment is attempting to register while current state is RESUMED. LifecycleOwners must call register before they are STARTED.' Although registering directly with the DropInClient within onCreateView using the tokenization key works flawlessly, we're constrained by the tokenization key's limitations, necessitating our reliance on the client token exclusively.

To reproduce

Code Snippet viewModel.paymentToken.observe(viewLifecycleOwner, Observer { Log.d("Payment Token ","$it") val fragmentActivity = requireActivity(); dropInClient = DropInClient(fragmentActivity,it) val dropInRequest = DropInRequest()

        dropInRequest.cardholderNameStatus = CardForm.FIELD_REQUIRED
        dropInRequest.maskSecurityCode = true
        dropInClient.launchDropIn(dropInRequest)
    })

Expected behavior

DropInClient should registered successfully

Screenshots

Screenshot 2024-03-26 at 9 08 21 PM
warmkesselj commented 6 months ago

Hey @AG-Akash09, thanks for the message. We’re aware of limitations with the current constructor and would need to make a breaking change to truly resolve this issue. We may investigate this further and publish a long term fix as part of a beta release of our next major version but this is not on our current roadmap.

In the meantime, the following workaround strategy may work:

val clientTokenProvider = ClientTokenProvider { callback ->
  viewModel.paymentToken.observe(viewLifecycleOwner, {
    callback.onSuccess(it)
  }
}
dropInClient = DropInClient(fragmentActivity, clientTokenProvider)
AG-Akash09 commented 6 months ago

Thanks for your active response. I have tried the workaround strategy, but still the same issue is encountered, i.e. "java.lang.IllegalStateException: LifecycleOwner com.andateknoloji.apps.nybox.MainActivity@d5a363d is attempting to register while current state is RESUMED. LifecycleOwners must call register before they are STARTED. at androidx.activity.result.ActivityResultRegistry.register(ActivityResultRegistry.java:123) at com.braintreepayments.api.DropInLifecycleObserver.onCreate(DropInLifecycleObserver.java:30) at androidx.lifecycle.FullLifecycleObserverAdapter.onStateChanged(FullLifecycleObserverAdapter.java:36) at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:360) at androidx.lifecycle.LifecycleRegistry.addObserver(LifecycleRegistry.java:202) at com.braintreepayments.api.DropInClient.addObserver(DropInClient.java:187) at com.braintreepayments.api.DropInClient.(DropInClient.java:181) at com.braintreepayments.api.DropInClient.(DropInClient.java:140)"

Screenshot 2024-03-27 at 10 27 29 AM
warmkesselj commented 6 months ago

@AG-Akash09 can you try removing the outer viewModel.paymentToken.observe(viewLifecycleOwner,