Flutter集成Stripe Google Pay生产环境支付失败求助
Flutter Stripe集成Google Pay生产环境报错问题
我使用flutter_stripe插件集成Google Pay支付功能,测试环境(设置testEnv: true并使用测试密钥)可正常运行,但切换至生产环境(调用v1/payment_intents API时使用生产密钥,testEnv: false)后无法正常工作,出现报错。
错误截图:
相关代码
startGooglePay方法
Future<void> startGooglePay() async { final googlePaySupported = await Stripe.instance.isGooglePaySupported(IsGooglePaySupportedParams(testEnv: false)); if (googlePaySupported) { try { // 1. 从后端获取Intent Client Secret final response = await fetchPaymentIntentClientSecret(); print(response); final clientSecret = response['client_secret']; // 2. 展示Google Pay面板 await Stripe.instance.initGooglePay(GooglePayInitParams( merchantName: "Test Name", countryCode: 'US', testEnv: false )); await Stripe.instance.presentGooglePay(PresentGooglePayParams( clientSecret: clientSecret, currencyCode: "GBP" )); await Stripe.instance.initPaymentSheet(paymentSheetParameters: SetupPaymentSheetParameters( googlePay: PaymentSheetGooglePay(merchantCountryCode: 'US'), paymentIntentClientSecret: response['client_secret'], setupIntentClientSecret: response['client_secret'], customerEphemeralKeySecret: response['client_secret'], customerId: PLPrefrence.getPrefValue(key: PLPrefrence.user_code).toString() )); await Stripe.instance.retrievePaymentIntent(response['client_secret']).then((value) { print(value.id); print(value.status); print(value.confirmationMethod.name); }); } catch (e) { print('ErrorStripePrint : ${e}'); errorDialog(errorMessage: "$e"); } } else { errorDialog(errorMessage: "该设备不支持Google Pay"); } }
payment_intents API调用方法
Future<Map<String, dynamic>> fetchPaymentIntentClientSecret() async { final url = Uri.parse('https://api.stripe.com/v1/payment_intents'); Map<String, dynamic> body = { 'amount': calculateAmount(amount: _plController.isUseWalletbalance.value ? _plController.processedAmount.value : widget.grandTotal.toString()).toString(), 'currency': "GBP", 'payment_method_types[]': 'card', }; final response = await http.post( url, headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'Authorization': 'Bearer ${PLKeys.stripeSK_Live}', }, body: body, ); return json.decode(response.body); }
问题排查与解决方案
1. 修正Google Pay地区与货币不匹配问题
代码中initGooglePay的countryCode设为US,但支付货币是GBP(英镑),地区与货币不兼容会导致生产环境验证失败。将地区代码改为GB:
await Stripe.instance.initGooglePay(GooglePayInitParams( merchantName: "Test Name", countryCode: 'GB', // 匹配GBP货币 testEnv: false ));
同时调整PaymentSheet中的merchantCountryCode为GB。
2. 清理PaymentSheet冗余参数
initPaymentSheet中同时传入了paymentIntentClientSecret、setupIntentClientSecret和customerEphemeralKeySecret,这三个参数互斥(分别对应支付Intent、设置Intent、客户临时密钥场景),当前是支付流程,仅保留paymentIntentClientSecret和必要配置:
await Stripe.instance.initPaymentSheet(paymentSheetParameters: SetupPaymentSheetParameters( googlePay: PaymentSheetGooglePay(merchantCountryCode: 'GB'), paymentIntentClientSecret: response['client_secret'], merchantDisplayName: "Test Name" ));
3. 完善PaymentIntent创建参数
生产环境下,创建PaymentIntent时建议明确指定google_pay为支付方式类型,避免兼容性问题:
Map<String, dynamic> body = { 'amount': calculateAmount(...).toString(), 'currency': "GBP", 'payment_method_types[]': ['card', 'google_pay'], // 同时支持card和google_pay };
4. 检查Stripe生产环境配置
- 登录Stripe Dashboard,进入支付方式设置,确保Google Pay在生产环境已启用。
- 确认Stripe账户已完成合规审核(如商户信息验证、域名验证),未处于限制状态。
- 确保使用的是有效的生产环境Secret Key(格式为
sk_live_xxx)。
5. 验证设备与支付账户状态
- 生产环境需使用真实银行卡,测试卡无法用于生产环境支付。
- 确保设备所在地区支持Google Pay,且Google Pay应用已登录有效账户并绑定合规银行卡。
内容的提问来源于stack exchange,提问作者Alpit Panchal
相关产品推荐
相关产品推荐

