我已经创建了一个用于条纹支付的测试演示,但是当我通过单击按钮进行测试时。我得到一个
API 密钥是必需的,但我已经给出了。请帮助我找出错误。最有可能的是我从主函数调用按键的方式
import 'dart:convert';
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:flutter_stripe/flutter_stripe.dart';
import 'package:http/http.dart' as http;
Future<void> main() async {
Stripe.publishableKey =
"pk_test_51JT7jkCTAUDjRNFVfafy4Gskx1KzUNk8nPj8T51zzCPE18fA17DOFO6MqSZVTCxhVCSWGwouDSe0yjcObAznHLW600VBoGyDcg";
await Stripe.instance.applySettings();
runApp(const PaymentScreen());
}
class PaymentScreen extends StatelessWidget {
const PaymentScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Stripe Payment',
theme: ThemeData(
primarySwatch: Colors.teal,
),
home: const PaymentPage(),
);
}
}
class PaymentPage extends StatelessWidget {
const PaymentPage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
Future<void> initPaymentSheet(context, {required String email, required int amount}) async {
try {
// 1. create payment intent on the server
final response = await http.post(
Uri.parse(
'https://us-central1-stripe-checkout-flutter.cloudfunctions.net/stripePaymentIntentRequest'),
body: {
'email': email,
'amount': amount.toString(),
});
final jsonResponse = jsonDecode(response.body);
log(jsonResponse.toString());
//2. initialize the payment sheet
await Stripe.instance.initPaymentSheet(
paymentSheetParameters: SetupPaymentSheetParameters(
paymentIntentClientSecret: jsonResponse['paymentIntent'],
merchantDisplayName: 'Flutter Stripe Store Demo',
customerId: jsonResponse['customer'],
customerEphemeralKeySecret: jsonResponse['ephemeralKey'],
style: ThemeMode.light,
testEnv: true,
merchantCountryCode: 'SG',
),
);
await Stripe.instance.presentPaymentSheet();
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Payment completed!')),
);
} catch (e) {
if (e is StripeException) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Error from Stripe: ${e.error.localizedMessage}'),
),
);
} else {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Error: $e')),
);
}
}
}
return Scaffold(
appBar: AppBar(
title: const Text("Stripe Demo App"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
style: ButtonStyle(
foregroundColor: MaterialStateProperty.all<Color>(Colors.blue),
),
onPressed: () async {
await initPaymentSheet(context, email: "[email protected]", amount: 200000);
},
child: const Text(
'Buy Token 2000 USD',
style: TextStyle(color: Colors.white),
),
)
],
),
),
);
}
}
您可以测试代码。除此之外没有任何错误
虽然我通常建议您将其作为 issue 与
flutter_stripe
存储库归档,但您可以考虑与他们的 example 相比,您是否可能在初始化中遗漏了一行:
void main() async {
WidgetsFlutterBinding.ensureInitialized(); // <- you don't have this line
// set the publishable key for Stripe - this is mandatory
Stripe.publishableKey = stripePublishableKey;
runApp(PaymentScreen());
}
付款无法成功解决我在 Stripe 中遇到的类似问题: 所以我在初始化 flutter 实例之前添加了merchantID和urlScreme。它将验证同一商家
Stripe.publishableKey = "pk_test_RS3cXiyeMHhEmpqnojBPuukSZQdjA7S00GE5LjhtqLongSecureKey342423";
Stripe.merchantIdentifier = "acct_1IL78e48329489014f";
Stripe.stripeAccountId = "acct_1IL78e48329489014f";
Stripe.urlScheme = "flutterstripe";
await Stripe.instance.applySettings();
我也有同样的问题。经过大量研究后,我发现我们正在使用的包
flutter_stripe
并不完全适用于 Web,并且由于 Web 平台导致条带初始化失败,因此会产生冲突。如突出显示的 [Stripe_flutter]
将以下内容添加到您的 pubspec 中。并重新启动应用程序
flutter_stripe_web: ^5.2.0