3

In my flutter project, I'm attempting to integrate a Stripe payment using flutter_stripe package.
I've intialize and configured correctly. But when trying to present paymentSheet nothing showing or happen. There's also no error cause. As it stuck at the line and not run code after that. I've also tried plugin simple example code as well but not work as i want. Any help will be appreciated.

main.dart


  Stripe.publishableKey = StripeService.publishableKey;
  Stripe.merchantIdentifier = 'merchant.flutter.stripe.test';
  Stripe.urlScheme = 'flutterstripe';
  await Stripe.instance.applySettings();
  runApp(const MyApp());
}

service.dart

Future<void> initPaymentSheet() async {
try {
  // 1. create payment intent on the server
  final paymentSheetData = await createPaymentIntent("1200", 'usd');
  print("payment intent created");

  // create some billingdetails
  final billingDetails = BillingDetails(
    email: 'email@stripe.com',
    phone: '+48888000888',
    address: Address(
      city: 'Houston',
      country: 'US',
      line1: '1459  Circle Drive',
      line2: '',
      state: 'Texas',
      postalCode: '77063',
    ),
  ); // mocked data for tests

 // 2. initialize the payment sheet
  await Stripe.instance.initPaymentSheet(
      paymentSheetParameters: SetupPaymentSheetParameters(
    applePay: true,
    googlePay: true,
    style: ThemeMode.dark,
    testEnv: true,
    merchantCountryCode: 'US',
    merchantDisplayName: 'Prospects',
    customerId: paymentSheetData!['customer'],
    paymentIntentClientSecret: paymentSheetData['paymentIntent'],
    customerEphemeralKeySecret: paymentSheetData['ephemeralKey'],
  ));
  print("payment sheet created");

  await Stripe.instance.presentPaymentSheet();

  print("after payment sheet presented");
} on Exception catch (e) {
  if (e is StripeException) {
    print("Error from Stripe: ${e.error.localizedMessage}");
  } else {
    print("Unforeseen error: ${e}");
  }
  rethrow;
}

}

output

I/flutter (14987): payment intent created
I/flutter (14987): payment sheet created

Dr_Usman
  • 486
  • 4
  • 11
  • Are you getting the correct data from the back-end server? – vishnu anilkumar Dec 13 '21 at 14:39
  • I'd suggest you reach out to the developers/community via their github issues to see if they can suggest what might help here: https://github.com/flutter-stripe/flutter_stripe/issues – Nolan H Dec 13 '21 at 22:31
  • @vishnuanilkumar backend response https://textdoc.co/akteSsynvKdZREmP would u plz check and confirm if anything missing or is it correct? – Dr_Usman Dec 14 '21 at 06:15
  • @Dr_Usman, can you make sure 'customer', 'paymentIntent', 'ephemeralKey' are present in the response of createPaymentIntent. If 3 of them are it will work, also you need to create customer in stripe dashboard – vishnu anilkumar Dec 14 '21 at 11:22

2 Answers2

4

I got the solution, there's something incorrect parameter value while initialize the payment sheet

Wrong

paymentIntentClientSecret: paymentSheetData['paymentIntent'],

Correct

paymentIntentClientSecret: paymentIntentData!['client_secret'],
Muhammad Dyas Yaskur
  • 6,914
  • 10
  • 48
  • 73
Dr_Usman
  • 486
  • 4
  • 11
3

I check your backend response you are using "paymentIntent" instead of "client_secret", you should initialize your payment sheet as below

await Stripe.instance.initPaymentSheet(
  paymentSheetParameters: SetupPaymentSheetParameters(
    applePay: true,
    googlePay: true,
    style: ThemeMode.dark,
    testEnv: true,
    merchantCountryCode: 'US',
    merchantDisplayName: 'Prospects',
    customerId: paymentSheetData!['customer'],
    paymentIntentClientSecret: paymentSheetData['client_secret'],
    customerEphemeralKeySecret: paymentSheetData['ephemeralKey'],
));
Ruli
  • 2,592
  • 12
  • 30
  • 40
Kashif Niaz
  • 198
  • 1
  • 10