在我的 flutter 项目中,我尝试使用 flutter_stripe 包集成 Stripe 支付。
我已正确初始化并配置。但是,当尝试出示付款表时,没有任何显示或发生。也没有错误原因。因为它卡在线路上并且之后没有运行代码。我也尝试过插件简单的示例代码,但没有按我想要的方式工作。任何帮助将不胜感激。
main.dart
Stripe.publishableKey = StripeService.publishableKey;
Stripe.merchantIdentifier = 'merchant.flutter.stripe.test';
Stripe.urlScheme = 'flutterstripe';
await Stripe.instance.applySettings();
runApp(const MyApp());
}
服务.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;
}
}
输出
I/flutter (14987):已创建支付意图
I/flutter (14987):已创建付款表
我检查您的后端响应,您使用的是“ paymentIntent”而不是“ client_secret”,您应该如下初始化您的付款表
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'],
));
我已经解决了,初始化付款单时参数值不正确
paymentIntentClientSecret: paymentSheetData['paymentIntent'],
paymentIntentClientSecret: paymentIntentData!['client_secret'],
如果您使用的是 stripe connect,请务必输入 Stripe.stripeAccountId 值,否则会出错。
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;
}
```