Reputation: 626
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 protected]',
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
Upvotes: 3
Views: 7521
Reputation: 96
you need to run presentPaymentSheet function after initPaymentSheet function
await Stripe.instance.initPaymentSheet(
paymentSheetParameters: SetupPaymentSheetParameters(
customFlow: false,
merchantDisplayName: 'Flutter Stripe Store Demo',
googlePay: const PaymentSheetGooglePay(
merchantCountryCode: 'US',
testEnv: true,
),
style: ThemeMode.dark,
paymentIntentClientSecret: data['client_secret'],
),
);
await Stripe.instance.presentPaymentSheet();
Upvotes: 0
Reputation: 698
make sure to enter the Stripe.stripeAccountId value if you're using stripe connect, otherwise it'll error out.
Stripe.stripeAccountId = paymentDetails.stripe_account_id;
await Stripe.instance.applySettings();
if (Stripe.stripeAccountId == '') {
print(
'You need to connect your Stripe account via the dashboard.');
return;
}
```
Upvotes: 0
Reputation: 277
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'],
));
Upvotes: 5
Reputation: 626
I got the solution, there's something incorrect parameter value while initialize the payment sheet
paymentIntentClientSecret: paymentSheetData['paymentIntent'],
paymentIntentClientSecret: paymentIntentData!['client_secret'],
Upvotes: 4