flutter-stripe / flutter_stripe

Flutter SDK for Stripe.
https://pub.dev/packages/flutter_stripe
955 stars 526 forks source link

stripeSdk has not been initialized on Android #68

Closed amentone closed 3 years ago

amentone commented 3 years ago

I followed the example in order to do payments with webook and I believe I have not missed anything. When I call confirmPaymentMethod:

await Stripe.instance.confirmPaymentMethod(
    client_secret,
    PaymentMethodParams.card(),
);

I get this error:

Log error ```E/MethodChannel#flutter.stripe/payments( 9694): Failed to handle method call E/MethodChannel#flutter.stripe/payments( 9694): kotlin.UninitializedPropertyAccessException: lateinit property stripeSdk has not been initialized E/MethodChannel#flutter.stripe/payments( 9694): at com.flutter.stripe.StripeAndroidPlugin.onMethodCall(StripeAndroidPlugin.kt:44) E/MethodChannel#flutter.stripe/payments( 9694): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:233) E/MethodChannel#flutter.stripe/payments( 9694): at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85) E/MethodChannel#flutter.stripe/payments( 9694): at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:818) E/MethodChannel#flutter.stripe/payments( 9694): at android.os.MessageQueue.nativePollOnce(Native Method) E/MethodChannel#flutter.stripe/payments( 9694): at android.os.MessageQueue.next(MessageQueue.java:363) E/MethodChannel#flutter.stripe/payments( 9694): at android.os.Looper.loop(Looper.java:173) E/MethodChannel#flutter.stripe/payments( 9694): at android.app.ActivityThread.main(ActivityThread.java:8178) E/MethodChannel#flutter.stripe/payments( 9694): at java.lang.reflect.Method.invoke(Native Method) E/MethodChannel#flutter.stripe/payments( 9694): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513) E/MethodChannel#flutter.stripe/payments( 9694): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101) E/flutter ( 9694): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: PlatformException(error, lateinit property stripeSdk has not been initialized, null, kotlin.UninitializedPropertyAccessException: lateinit property stripeSdk has not been initialized E/flutter ( 9694): at com.flutter.stripe.StripeAndroidPlugin.onMethodCall(StripeAndroidPlugin.kt:44) E/flutter ( 9694): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:233) E/flutter ( 9694): at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85) E/flutter ( 9694): at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:818) E/flutter ( 9694): at android.os.MessageQueue.nativePollOnce(Native Method) E/flutter ( 9694): at android.os.MessageQueue.next(MessageQueue.java:363) E/flutter ( 9694): at android.os.Looper.loop(Looper.java:173) E/flutter ( 9694): at android.app.ActivityThread.main(ActivityThread.java:8178) E/flutter ( 9694): at java.lang.reflect.Method.invoke(Native Method) E/flutter ( 9694): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513) E/flutter ( 9694): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101) E/flutter ( 9694): ) E/flutter ( 9694): #0 JSONMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:164:7) E/flutter ( 9694): #1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:158:18) E/flutter ( 9694): E/flutter ( 9694): #2 MethodChannelStripe.initialise (package:stripe_platform_interface/src/method_channel_stripe.dart:36:5) E/flutter ( 9694): E/flutter ( 9694): #3 Stripe._initialise (package:flutter_stripe/src/stripe.dart:315:5) E/flutter ( 9694): E/flutter ( 9694): #4 Stripe.confirmPaymentMethod (package:flutter_stripe/src/stripe.dart:186:5) E/flutter ( 9694): ```

The thing is that the example works, my app doesn't. I've call also in my main.dart before runApp and public key already configured:

WidgetsFlutterBinding.ensureInitialized();
flutter doctor -v ``` [✓] Flutter (Channel stable, 2.2.0, on Linux, locale en_GB.UTF-8) • Flutter version 2.2.0 at /home/anmentone/lib/flutter • Framework revision b22742018b (10 days ago), 2021-05-14 19:12:57 -0700 • Engine revision a9d88a4d18 • Dart version 2.13.0 [✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3) • Android SDK at /home/anmentone/Android/Sdk • Platform android-30, build-tools 30.0.3 • Java binary at: /usr/local/android-studio/jre/bin/java • Java version OpenJDK Runtime Environment (build 11.0.8+0-b944-P17168821) • All Android licenses accepted. [✓] Chrome - develop for the web • Chrome at google-chrome [✓] Android Studio (version 4.2) • Android Studio at /usr/local/android-studio • Flutter plugin version 56.0.2 • Dart plugin version 202.8488 • Java version OpenJDK Runtime Environment (build 11.0.8+0-b944-P17168821) [✓] IntelliJ IDEA Community Edition (version 2021.1) • IntelliJ at /usr/local/idea-IC-211.7142.45 • Flutter plugin version 56.0.5 • Dart plugin version 211.7233 [✓] VS Code (version 1.56.2) • VS Code at /usr/share/code • Flutter extension version 3.22.0 [✓] Connected device (2 available) • POT LX1 (mobile) • 39V4C19A18025168 • android-arm64 • Android 10 (API 29) • Chrome (web) • chrome • web-javascript • Google Chrome 90.0.4430.212 • No issues found! ```

Thanks for your help!

amentone commented 3 years ago

When stripeSdk.initialise() is invoked, stripeSdk property has not yet initialized.

com/flutter/stripe/StripeAndroidPlugin.kt

44 when (call.method) {
45  "initialise" -> {
46    stripeSdk.initialise(
47      params = ReadableMap(call.arguments as JSONObject),
48      promise = Promise(result),
49    )
50  }

And this is strange because the initialization is made here:

com/flutter/stripe/StripeAndroidPlugin.kt

107 override fun onAttachedToActivity(binding: ActivityPluginBinding) {
108  stripeSdk = StripeSdkModule(ReactApplicationContext(binding), stripeSdkCardViewManager)
109 }

Is possibile that onAttachedToActivity is never triggered? Unfortunately I can't set breakpoints there, I tried with logging but nothing appear on the debug console.

jonasbark commented 3 years ago

Is your main activity based on FlutterFragmentActivity or FlutterActivity?

amentone commented 3 years ago

Sorry for my question, maybe I am not prepared for this. I'm using your plugin in a flutter application. FlutterActivity/FlutterFragmentAcitivity are useful to integrate Flutter within an Android app, is that my case? I have not written any main activity.

Thank you for your support, and sorry, I am trying to learn :)

jamesblasco commented 3 years ago

Noo need to worry! Thank you for creating this issue, you have been very helpful so far. By default, Flutter apps use FlutterActivity by default. You can find this in the file MainActivity.kt inside the android folder.

I think FlutterFragmentActivity is needed for the Stripe SDK to work, @jonasbark can you confirm this?. If this is the case we should add this to the docs.

The change should be quite easy, change FlutterActivity to FlutterFragmentAcitivity in MainActivity.kt

amentone commented 3 years ago

Ah ok! Thanks for this tip. I did a quick check and yes, my MainActivity is based on FlutterActivity while the MainActivity (from the example, that works) is based on FlutterFragmentActivity but I missed totally this thing.

amentone commented 3 years ago

Now it's working well.

Thank you guys!

giandifra commented 3 years ago

Hi, I have the same error, but changing my MainActivity.kt I receive errors from Firebase plugin!

Have you suggestion to solve this? There is another fix ?

Thanks

jamesblasco commented 3 years ago

Could you specify the error you are getting with the Firebase plugin?

giandifra commented 3 years ago

Could you specify the error you are getting with the Firebase plugin?

It's work now, I cleaned the project!

jamesblasco commented 3 years ago

I will close this issue now that is detailed inside the readme

jonasbark commented 3 years ago

I'll implement a runtime check so it's easier to read configuration issues

amentone commented 3 years ago

Hi guys, I'm back here writing again. I'm developing a plugin for flutter to insert card payment functionality inside my flutter app for mypos using mypos SDK (myPOS-Smart-SDK). My app is about "digital commercial documents" (receipt) and is used by vendors instead of a physical cash register.
I only use stripe for subscription renewal, which is required to use some services in the app. So these two plugins are always used separately but I'm having some unexpected issues. The functioning of the app is like a simple cash register, when there is a sale there is a request for payment and the customer can decide to pay by card using the integrated card reader of mypos. The payment handling is done with the plugin I'm developing that consumes the mypos API internally. The strange thing is that when the payment finishes and mypos close its activity, the app crashes throwing me this exception:

kotlin.UninitializedPropertyAccessException: lateinit property stripe has not been initialized ``` E/AndroidRuntime(25099): at android.os.Handler.dispatchMessage(Handler.java:102) E/AndroidRuntime(25099): at android.os.Looper.loop(Looper.java:135) E/AndroidRuntime(25099): at android.app.ActivityThread.main(ActivityThread.java:5254) E/AndroidRuntime(25099): at java.lang.reflect.Method.invoke(Native Method) E/AndroidRuntime(25099): at java.lang.reflect.Method.invoke(Method.java:372) E/AndroidRuntime(25099): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902) E/AndroidRuntime(25099): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697) E/AndroidRuntime(25099): Caused by: kotlin.UninitializedPropertyAccessException: lateinit property stripe has not been initialized E/AndroidRuntime(25099): at com.reactnativestripesdk.StripeSdkModule.access$getStripe$p(StripeSdkModule.kt:20) E/AndroidRuntime(25099): at com.reactnativestripesdk.StripeSdkModule$mActivityEventListener$1.onActivityResult(StripeSdkModule.kt:45) E/AndroidRuntime(25099): at com.facebook.react.bridge.BaseActivityEventListener.onActivityResult(BaseActivityEventListener.kt:15) E/AndroidRuntime(25099): at io.flutter.embedding.engine.FlutterEngineConnectionRegistry$FlutterEngineActivityPluginBinding.onActivityResult(FlutterEngineConnectionRegistry.java:739) E/AndroidRuntime(25099): at io.flutter.embedding.engine.FlutterEngineConnectionRegistry.onActivityResult(FlutterEngineConnectionRegistry.java:426) E/AndroidRuntime(25099): at io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onActivityResult(FlutterActivityAndFragmentDelegate.java:677) E/AndroidRuntime(25099): at io.flutter.embedding.android.FlutterFragment.onActivityResult(FlutterFragment.java:769) E/AndroidRuntime(25099): at io.flutter.embedding.android.FlutterFragmentActivity.onActivityResult(FlutterFragmentActivity.java:509) E/AndroidRuntime(25099): at android.app.Activity.dispatchActivityResult(Activity.java:6235) E/AndroidRuntime(25099): at android.app.ActivityThread.deliverResults(ActivityThread.java:3570) E/AndroidRuntime(25099): ... 10 more I/Process (25099): Sending signal. PID: 25099 SIG: 9 ```

This left me surprised as I am not using Stripe in this segment.

Seeing the message error, I actually tried using stripe before I could make a payment with the mypos API, and it went like this... After making the payment I get a black screen but the app doesn't crash and this is the error message:

Activity com.anmentone.app.MainActivity has leaked IntentReceiver ``` E/ActivityThread(23524): android.app.IntentReceiverLeaked: Activity com.anmentone.app.MainActivity has leaked IntentReceiver com.reactnativestripesdk.StripeSdkModule$mPaymentSheetReceiver$1@3d98bd7f that was originally registered here. Are you missing a call to unregisterReceiver()? E/ActivityThread(23524): at android.app.LoadedApk$ReceiverDispatcher.(LoadedApk.java:903) E/ActivityThread(23524): at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:704) E/ActivityThread(23524): at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1650) E/ActivityThread(23524): at android.app.ContextImpl.registerReceiver(ContextImpl.java:1630) E/ActivityThread(23524): at android.app.ContextImpl.registerReceiver(ContextImpl.java:1624) E/ActivityThread(23524): at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:489) E/ActivityThread(23524): at com.facebook.react.bridge.ReactComponentActivityWrapper.registerReceiver(ReactComponentActivityWrapper.java:39) E/ActivityThread(23524): at com.reactnativestripesdk.StripeSdkModule.initialise(StripeSdkModule.kt:240) E/ActivityThread(23524): at com.flutter.stripe.StripeAndroidPlugin.onMethodCall(StripeAndroidPlugin.kt:44) E/ActivityThread(23524): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:233) E/ActivityThread(23524): at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85) E/ActivityThread(23524): at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:818) E/ActivityThread(23524): at android.os.MessageQueue.nativePollOnce(Native Method) E/ActivityThread(23524): at android.os.MessageQueue.next(MessageQueue.java:143) E/ActivityThread(23524): at android.os.Looper.loop(Looper.java:122) E/ActivityThread(23524): at android.app.ActivityThread.main(ActivityThread.java:5254) E/ActivityThread(23524): at java.lang.reflect.Method.invoke(Native Method) E/ActivityThread(23524): at java.lang.reflect.Method.invoke(Method.java:372) E/ActivityThread(23524): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902) E/ActivityThread(23524): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697) ```
Device info myPOS Smart N5 Operating system | Android 5.1 -- | -- Processor | 4 Core CPU + Secure CPU Memory | 8GB FLASH, 1GB RAM Display | 5.5’ (720 x 1280) Colorful LCD capacitive touchscreen with signature capture functionality Printer | High-Speed Thermal PrinterPaper Roll Width/Diameter: 58mm/40mm Communications | 4G LTE/ Wi-Fi / Bluetooth Camera | 5MP - rear -facing Data Capture | 1D Barcode scanner (optional)2MP camera -front-facing (optional) GPS Tracking | GPS / BEIDOU / GLONASS Card Readers | Magstripe: Triple track, high coercivity, bi-directionalSmart Card: EMV & PBOC approvedContactless : NFC 13.56MHz, support ISO14443, Type A/B/C, MifareCard (optional) Card Slots | PSAM: 2SIM: 1SD: 1 Battery | Li-ion 4300mAhInput : 100-240V, 50-60HZOutput: 5V/2A Multi-media | Video, Audio Dimensions | L × W × H = 186mm × 82mm × 64mm , 417g Device Ports | 1 Micro-USB OTG Environmental | Operating Temperature: 0°C ̴ 50°C (32°F ̴ 122°F)  Storage Temperature: -20°C ̴ 60°C (-4°F ̴ 158°F) Relative Humidity: 5% ̴ 95% (Non-Condensing) Optional Base | Charging Base / Wi-Fi Base: Wi-Fi, integrated charging Certifications | PCI 4.X, EMV, PayPass, PayWave certified Languages | English, German, Bulgarian, Czech, Greek, Spanish, French, Croatian, Italian, Dutch, Portugal, Romanian, Swedish, Hungarian, Icelandic, Latvian, Polish, Slovenian, Norwegian

I emphasize that Stripe has nothing to do with these payments. I use it elsewhere to be able to renew the subscription which allows you to use premium features.

I'm trying to go deeper but I can't understand the problem. If you have any ideas I would be very grateful. Thank you!

jonasbark commented 3 years ago

This already provides plenty of information, thank you! We will have a look.