flutter_stripe icon indicating copy to clipboard operation
flutter_stripe copied to clipboard

Stripe 'Pay' button is not working on Android device. App is getting crashed on clicking pay button

Open ayesha882 opened this issue 1 year ago • 6 comments

I am using stripe in my flutter project and my code is shown below. It is getting crashed on Android and working fine on iOS device on clicking Pay button.

 Future<void> initPaymentSheet(BuildContext context) async {
    isLoadingOther.value = true;
    try {
      Stripe.stripeAccountId = stripeAccountId.value;
    
      await Stripe.instance.applySettings();
      // var paymentMethods = await getPaymentMethods();
      var total = num.parse(amountTextEditingController.text).toInt();
      var currencyCode = CurrencyType.currencyByType(appService.currency);
      var billingDetails =
          BillingDetails(name: props.value?.payee, email: props.value?.email);

      await Stripe.instance.initPaymentSheet(
        paymentSheetParameters: SetupPaymentSheetParameters(
            customFlow: false,
            merchantDisplayName: props.value?.payee,
            intentConfiguration: IntentConfiguration(
                mode: IntentMode(
                  currencyCode: currencyCode,
                  amount: total,
                ),
                // paymentMethodTypes: paymentMethods,
                confirmHandler: (method, saveFuture) {
                  createPaymentIntent(method, total, currencyCode);
                }),
            allowsDelayedPaymentMethods: true,
            primaryButtonLabel:
                '${LocalizationConstants.payNow.tr} $outstandingPaymentString',
            style: ThemeMode.system,
            appearance: customPaymentSheetAppearance(),
            billingDetails: billingDetails,
            billingDetailsCollectionConfiguration:
                BillingDetailsCollectionConfiguration(
                    attachDefaultsToPaymentMethod: true)),
      );
      presentPaymentSheet(context);
    } catch (error) {
      isLoadingOther.value = false;
      if (error is StripeException){
        ScaffoldMessenger.of(context).showSnackBar(SnackBar(
            content: Text(error.error.localizedMessage ??
                LocalizationConstants.takePaymentFailedOrCancelled.tr),
            backgroundColor: Colors.red));
      } else {
        handleCallBack(AppConstants.cancel, context);
      }
    }
  }
Future<void> presentPaymentSheet(BuildContext context) async {
   try {
     isLoadingOther.value = false;
     //Display the payment sheet.
     await Stripe.instance.presentPaymentSheet();

     handleCallBack(AppConstants.success, context);
   } catch (error) {
     if (error is StripeException){
       ScaffoldMessenger.of(context).showSnackBar(SnackBar(
           content: Text(error.error.localizedMessage ??
               LocalizationConstants.takePaymentFailedOrCancelled.tr),
           backgroundColor: Colors.red));
     } else {
       handleCallBack(AppConstants.cancel, context);
     }
   }
 }

To Reproduce Steps to reproduce the behavior:

  1. Open stripe payment sheet.
  2. Add card details .
  3. Click on Pay button, the app will crashed with following stacktrace on Android device Screenshot 2024-07-02 at 6 12 20 PM

Expected behavior It should not crash

Smartphone / tablet

  • Device: Samsung Galaxy A05
  • OS: Android 14
  • Package version: flutter_stripe: ^10.2.0
  • Flutter version: '>=2.18.2 <3.0.0'

ayesha882 avatar Jul 02 '24 13:07 ayesha882

Can you double check that you have followed the instructions for installing on android? It seems like you miss some steps.

remonh87 avatar Jul 03 '24 19:07 remonh87

java.lang.RuntimeException: Methods marked with @UiThread must be executed on the main thread. Current thread: DefaultDispatcher-worker-6

I am also experiencing this issue while working on the payment sheet with deferred payments. I cross-checked it using the demo example, and the issue persists on the payment_sheet_deferred_screen.dart. When you click on "Pay Now," the confirmHandler crashes, as described above. I have been working on this for a while, and it was functioning perfectly on flutter_stripe version 10.1.1. However, after updating to version 10.2.0, this issue started occurring. My requirements for Android are correctly matched, but this problem is happening only on Android.

SaddarTuri avatar Jul 05 '24 07:07 SaddarTuri

@SaddarTuri I've the same problem, with same configuration (payment sheet, deferred) I'v tried 10.1.1, but it isn't wworking neither

pgiacomo69 avatar Jul 05 '24 08:07 pgiacomo69

@SaddarTuri I've the same problem, with same configuration (payment sheet, deferred) I'v tried 10.1.1, but it isn't wworking neither

Yes, you are right. For me, it was working perfectly on version 10.1.1, but after updating to version 10.2.0, the error occurred. The strange thing is that after downgrading back to the older version, the issue is occurring there as well.

SaddarTuri avatar Jul 05 '24 08:07 SaddarTuri

@SaddarTuri

  1. Flutter Pub Clean
  2. in pubspec.yaml put the version 10.1.1 (not ^10.1.1) for flutter_stripe package
  3. flutter pub get
  4. Modify the file pubspec.lock, and change the version for all stripe-relate packages from 10.2.0 to 10.1.1
  5. Flutter pub clean
  6. Flutter pub get

Now it works :)

pgiacomo69 avatar Jul 05 '24 09:07 pgiacomo69

@pgiacomo69 Thank you so much! I followed your steps, and it works perfectly now on version 10.1.1. I really appreciate your help! If you find any solution for the latest version, please share it in this thread.

SaddarTuri avatar Jul 05 '24 10:07 SaddarTuri

Good catch, thanks for reporting

Fixes with stripe_android package version 10.2.1

jonasbark avatar Jul 07 '24 11:07 jonasbark