Flutter Stripe iOS端支付面板未初始化报错问题排查
问题描述
使用flutter_stripe包集成Stripe支付功能时,仅iOS设备运行抛出异常,安卓设备运行正常。
报错信息
Unhandled Exception: StripeException(error: LocalizedErrorMessage(code: FailureCode.Failed, localizedMessage: No payment sheet has been initialized yet, message: No payment sheet has been initialized yet, stripeErrorCode: null, declineCode: null, type: null))
核心报错提示:尚未初始化支付面板
现有实现代码
services.dart 支付逻辑代码
payNow() async { try{ Map<String, dynamic> stripeData = widget.paymentData; print("stripeData===== ${stripeData['stripeCustomerId']}"); Stripe.publishableKey = Constants.stripeKey; Stripe.merchantIdentifier = Constants.merchantId; await Stripe.instance.applySettings(); await Stripe.instance.initPaymentSheet( paymentSheetParameters: SetupPaymentSheetParameters( paymentIntentClientSecret: stripeData['paymentIntentSecret'], merchantDisplayName: 'Courses', customerId: stripeData['stripeCustomerId'], customerEphemeralKeySecret: stripeData['ephemeralKey'], applePay: true), ); await Stripe.instance.presentPaymentSheet(); widget.submitData(); } on Exception catch (e) { if (e is StripeException) { print("Error from Stripe: ${e.error.localizedMessage}"); } else { print("Unforeseen error: $e"); } rethrow; } }
main.dart 初始化代码
void main() async { WidgetsFlutterBinding.ensureInitialized(); Stripe.publishableKey = Constants.stripeKey; Stripe.merchantIdentifier= Constants.merchantId; Stripe.urlScheme = 'flutterstripe'; await Stripe.instance.applySettings(); runApp(MyApp()); }
排查修复方案
该问题为iOS端专属配置/代码写法错误导致,按以下顺序检查修复即可:
- 补全iOS原生URL Scheme配置:打开iOS项目的
Info.plist文件,在LSApplicationQueriesSchemes数组下添加stripe字段,同时在CFBundleURLTypes中配置URL标识为你代码中定义的flutterstripe。缺少该配置时iOS端无法正常接收Stripe的初始化回调,安卓不受该配置影响。 - 修正Apple Pay参数写法:当前代码中
applePay: true是旧版本flutter_stripe的废弃写法,新版本要求传入PaymentSheetApplePay配置对象,传布尔值会导致iOS端初始化流程静默失败,安卓因不触发Apple Pay逻辑不会报错。修正后的初始化代码段如下:
await Stripe.instance.initPaymentSheet( paymentSheetParameters: SetupPaymentSheetParameters( paymentIntentClientSecret: stripeData['paymentIntentSecret'], merchantDisplayName: 'Courses', customerId: stripeData['stripeCustomerId'], customerEphemeralKeySecret: stripeData['ephemeralKey'], // 替换为你商户对应的国家代码,例如美国为US、中国为CN applePay: const PaymentSheetApplePay(merchantCountryCode: 'CN'), ), );
- 删除payNow方法中重复的Stripe配置逻辑:你已经在main函数中完成了Stripe全局初始化,payNow方法里重复赋值
publishableKey、merchantIdentifier并调用applySettings()会重置Stripe实例状态,直接清空iOS端已经生成的支付面板实例。 - 校验Apple Pay权限:确认你使用的
merchantIdentifier已经在Apple开发者后台开启了Apple Pay能力,同时在Xcode的Signing & Capabilities页面添加Apple Pay能力并选中对应merchantId,权限缺失也会导致iOS端支付面板初始化失败。
所有修改完成后执行flutter clean,清空iOS端DerivedData缓存后重新编译运行即可。
内容的提问来源于stack exchange,提问作者Anjali
相关产品推荐
相关产品推荐

