Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
AG-Akash09 opened this issue Mar 26, 2024 · 3 comments

Comments

@AG-Akash09
Copy link

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
Copy link

warmkesselj commented Mar 26, 2024

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
Copy link
Author

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants