flutter-stripe / flutter_stripe

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

Payment Sheet Deferred Payment not able to show custom error thrown by Server in IntentCreationCallback. #1863

Closed SaddarTuri closed 4 weeks ago

SaddarTuri commented 2 months ago

When an error is thrown by the server during the payment intent creation, I want to display that specific error on the Payment Sheet. Currently, it only shows a generic Something went wrong message instead of the exact message I am providing in intentCreationCallback.

await Stripe.instance.intentCreationCallback( IntentCreationCallbackParams( error: StripeException( error: LocalizedErrorMessage(code: FailureCode.Failed, localizedMessage: 'Error', message: 'Error'))));

Does the flutter_stripe package have an option for displaying customized errors on the Payment Sheet?

remonh87 commented 2 months ago

In this particular case not. The paymentsheet will only display errors if they are relevant to the customer. It is up to the app developer to provide additional errors one way or the other. What do you exactly want to achieve?

SaddarTuri commented 2 months ago

In this particular case not. The paymentsheet will only display errors if they are relevant to the customer. It is up to the app developer to provide additional errors one way or the other. What do you exactly want to achieve?

I would like to outline the objective I aim to achieve. When a user inputs card details with an expired card number, the deferred payment process involves a confirmHandler that returns the payment method.

confirmHandler: (method, saveFuture) {
                  createPaymentIntent(method, amount, currencyCode);
                }),

In the confirmHandler, an API call is made to the server-side for creating a payment intent. The payment intent API returns an error message indicating that the Card is expired, I would like to display this error message on the payment sheet. However, I am currently unable to do so but the payment sheet show generic message as shown in screen shot below.

Here is my code of Payment Intent.

 /// Api Call for Payment Intent..
  Future<void> createPaymentIntent(
      PaymentMethod paymentMethod, int amount, String currencyCode) async {
    try {
      final request = StripePaymentEntity(
          paymentMethodId: paymentMethod.id,
          paymentMethodType:
              getPaymentMethodType(paymentMethod.paymentMethodType),
          currency: currencyCode,
          amount: amount,
          emailReceiptTo: props.value?.email ?? '',
          returnUrl: AppConstants.stripeReturnUrl,
          countryCode: paymentMethod.card.country ?? '');
      final params = AddStripePaymentUseCaseParams(request: request);
      final result = await addStripePaymentUseCase.execute(params);
      if (result != null) {
        await Stripe.instance.intentCreationCallback(
            IntentCreationCallbackParams(
                clientSecret: result.paymentIntentClientSecret));
      } else {
        await Stripe.instance.intentCreationCallback(
            IntentCreationCallbackParams(
                error: StripeException(
                    error: LocalizedErrorMessage(code: FailureCode.Failed))));
      }
    } catch (error) {
      if (error is StripeException) {
        await Stripe.instance
            .intentCreationCallback(IntentCreationCallbackParams(error: error));
      } else {
        await Stripe.instance.intentCreationCallback(
            IntentCreationCallbackParams(
                error: StripeException(
                    error: LocalizedErrorMessage(
                        code: FailureCode.Failed,
                        localizedMessage:
                            'Error message thrown by Payment Intent api',
                        message:
                            'Error message thrown by Payment Intent api'))));
      }
    }
  }
Screenshot 2024-07-26 at 12 07 46 PM
SaddarTuri commented 2 months ago

Hi @remonh87 Any update on this issue?

remonh87 commented 1 month ago

I cannot reproduce this issue anymore. I get a localised error description with the fact that the card is expired.