flutterdartexceptionstripe-paymentsflutter-stripe

How to catch stripe errors on flutter?


I'm creating a subscription based platform, so I integrated the stripe subscription payment, if user is able to enter all the card data and proceed with the payment successfully, nothing goes wrong. But if user cancels the payment by pressing the cross of the paymentIntentSheet the following errors were sent: If customFlow: true is sent "The payment option selection flow has been canceled" If customFlow: false is sent "The payment has been canceled"

And the worst part of it is that even with a try catch statement the error was sent and the app crashes.

This is my code:

try {
      await Stripe.instance.presentPaymentSheet();
    } on StripeException catch (e) {
      print('Error is:---> $e');
      String ss = "exception 3 :$e";
    } catch (e) {
      print(e);
    }

And the creation of the intent is:

await Stripe.instance.initPaymentSheet(
        paymentSheetParameters: SetupPaymentSheetParameters(
            customFlow: false,
            //returnURL: 'flutterstripe://redirect',
            primaryButtonLabel: 'Añadir tarjeta',
            style: ThemeMode.light,
            merchantDisplayName: 'BitMasters',
            customerId: customerId,
            setupIntentClientSecret: paymentIntentClientSecret,
            allowsDelayedPaymentMethods: true),
      );

When I run and reproduce the error it shows like this:

Exception message


Solution

  • According to the API reference, presentPaymentSheet throws an exception for failure. Therefore, you need to implement a try-a-catch and handle failures.

    Here's the sample code about handling exception raised from presentPaymentSheet