Search code examples
iosflutterstripe-paymentsflutter-iosflutter-stripe

No payment sheet has been initialized yet


main.dart

  WidgetsFlutterBinding.ensureInitialized();
  Stripe.publishableKey = stripePublishableKey;
  Stripe.merchantIdentifier = 'emailID';
  await Stripe.instance.applySettings();

Initiate Payment

Future<void> initPaymentSheet({
  required String? price,
}) async {
  try {
   await Stripe.instance.initPaymentSheet(
      paymentSheetParameters: SetupPaymentSheetParameters(
        // Main params
        paymentIntentClientSecret: clientSecret,
        customerId: customerId,
        customerEphemeralKeySecret: ephemeralKey,
        // Merchant Name
        merchantDisplayName: 'TEST',
        // Extra params
        applePay: const PaymentSheetApplePay(
          merchantCountryCode: 'US',
        ),
        googlePay: const PaymentSheetGooglePay(
          merchantCountryCode: 'US',
          testEnv: true,
        ),
        style: ThemeMode.system,
      ),
    );
  } catch (e) {
    rethrow;
  }
}


/// Display the payment sheet.
Future<void> confirmPayment({required String price}) async {
  try {
    await initPaymentSheet(price: price);
    // Present Payment sheet to user for payment
    await Stripe.instance.presentPaymentSheet();

    // Book the request.
    bookingRequest();
  } on Exception catch (e) {
    if (e is StripeException) {
      // displaySnackBar(
      //     'Error!', 'Error from Stripe: ${e.error.localizedMessage}');
      log('Error from Stripe: ${e.error.localizedMessage}');
      print('Error from Stripe: ${e.error.localizedMessage}');
      displaySnackBar('Error!', 'Something went wrong!, \nPlease try again.');
    } else {
      log('Error from Stripe: $e');
      displaySnackBar('Error!', 'Something went wrong!, \nPlease try again.');
    }
  }
}

  • This confirmPayment() is called on Button click.
  • It will wait for Init the payment sheet.
  • After that presentPaymentSheet called to present the Payment sheet.

In Android, It's working fine. In iOS, it's showing error like: No payment sheet has been initialized yet

Already Raised the Issue on the plugin Repo. https://github.com/flutter-stripe/flutter_stripe/issues/850


Solution

  • Solved:

    Okay, the problem was in our backend response when we were generating EphemeralKey, our backend dev was returning ephemeralKey["id"] instead of ephemeralKey["secret"] as a ephemeralKey.

    I changed the EphemeralKey

    from this paymentIntentDTO.setEphemeralKey(ephemeralKey.getId());

    to this paymentIntentDTO.setClientSecret(paymentIntent.getClientSecret());

    see ephemeralKey is different. ephemeralKey["id"] is work fine in android app for stripe payment but on IOS ephemeralKey["id"] will not working it was showing Error from Stripe: No payment sheet has been initialised yet

    i changed ephemeralKey["id"] to ephemeralKey["secret"] as a ephemeralKey and now it working fine on both device.