main.dart
WidgetsFlutterBinding.ensureInitialized();
Stripe.publishableKey = stripePublishableKey;
Stripe.merchantIdentifier = 'emailID';
await Stripe.instance.applySettings();
启动支付
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.');
}
}
}
- 单击按钮时调用此confirmPayment((
- 它将等待初始化付款单
- 之后presentalPaymentSheet调用以呈现付款单
在安卓系统中,它运行良好。在iOS中,它显示错误,如:尚未初始化付款单
已经在插件Repo上提出了问题。https://github.com/flutter-stripe/flutter_stripe/issues/850
✅已解决:
好吧,问题出在我们的后端响应中当我们生成EphemeralKey时,我们的后端开发返回EphemeralKey["id"],而不是EphemeralKey["secret"]作为EphemeralKey。
我更改了EphemeralKey
从此paymentIntentDTO.setEphemeralKey(ephemeralKey.getId());
到这个paymentIntentDTO.setClientSecret(paymentIntent.getClientSecret());
请参阅ephemeralKey不同。ephemeralKey["id"]在安卓应用程序中可以很好地进行条纹支付,但在IOS上ephemeral Key["id"]将不起作用它显示Stripe的错误:尚未初始化付款单
我将ephemeralKey["id"]更改为ephemeralKey["secret"]作为ephemeralKey,并且现在它在两个设备上都工作良好。