Flutter Stripe 不 show/present 支付 sheet
Flutter Stripe does not show/present payment sheet
在我的 flutter 项目中,我尝试使用 flutter_stripe 包集成 Stripe 支付。
我已经正确初始化和配置。但是当尝试呈现 paymentSheet 时,没有任何显示或发生。也没有错误原因。因为它停留在该行而不是之后的 运行 代码。我也尝试过插件简单的示例代码,但没有按我想要的那样工作。任何帮助将不胜感激。
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@stripe.com',
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): payment intent created
I/flutter (14987): payment sheet created
我找到了解决方案,初始化支付时参数值不正确sheet
错误
paymentIntentClientSecret: paymentSheetData['paymentIntent'],
正确
paymentIntentClientSecret: paymentIntentData!['client_secret'],
我检查了您使用的后端响应,您使用的是“paymentIntent”而不是“client_secret”,您应该按如下方式初始化您的付款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['client_secret'],
customerEphemeralKeySecret: paymentSheetData['ephemeralKey'],
));
在我的 flutter 项目中,我尝试使用 flutter_stripe 包集成 Stripe 支付。
我已经正确初始化和配置。但是当尝试呈现 paymentSheet 时,没有任何显示或发生。也没有错误原因。因为它停留在该行而不是之后的 运行 代码。我也尝试过插件简单的示例代码,但没有按我想要的那样工作。任何帮助将不胜感激。
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@stripe.com',
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): payment intent created
I/flutter (14987): payment sheet created
我找到了解决方案,初始化支付时参数值不正确sheet
错误
paymentIntentClientSecret: paymentSheetData['paymentIntent'],
正确
paymentIntentClientSecret: paymentIntentData!['client_secret'],
我检查了您使用的后端响应,您使用的是“paymentIntent”而不是“client_secret”,您应该按如下方式初始化您的付款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['client_secret'],
customerEphemeralKeySecret: paymentSheetData['ephemeralKey'],
));