我有一个 Flutter 应用程序,我想向其中添加 Stripe,以便可以向用户收费。我已将 Stripe 添加到注册页面,以便新用户注册时可以输入付款信息。
这是我尝试进入 Stripe 屏幕的代码:
onPressed: () async {
setState(() {
showSpinner = true;
});
try {
final newUser =
await _auth.createUserWithEmailAndPassword(
email: email, password: password);
if (newUser != null) {
Navigator.of(context).pushReplacement(MaterialPageRoute(
builder: (context) => const StripePaymentScreen()));
Navigator.of(context).pushReplacement(MaterialPageRoute(
builder: (context) => const VerifyEmailScreen()));
} else {
setState(() {
registrationFail = true;
});
}
setState(() {
showSpinner = false;
});
} on FirebaseAuthException catch (error) {
因此,如果应用程序的用户帐户创建成功,我想转到 Stripe 屏幕,但它执行代码但屏幕不显示。我希望显示 Stripe 屏幕,让用户输入他们的信息,然后代码可以继续并发送验证电子邮件。
我做错了什么?
这是 Stripe 代码:
class StripePaymentScreen extends StatefulWidget {
const StripePaymentScreen({super.key});
@override
_StripePaymentScreenState createState() => _StripePaymentScreenState();
}
class _StripePaymentScreenState extends State<StripePaymentScreen> {
Map<String, dynamic>? paymentIntent;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Stripe Payment'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextButton(
child: const Text('Make Payment'),
onPressed: () async {
await makePayment();
Navigator.pop(context); // return to User Registration Screen
},
),
],
),
),
);
}
Future<void> makePayment() async {
try {
paymentIntent = await createPaymentIntent('100', 'USD');
//STEP 2: Initialize Payment Sheet
await Stripe.instance
.initPaymentSheet(
paymentSheetParameters: SetupPaymentSheetParameters(
paymentIntentClientSecret: paymentIntent![
'client_secret'], //Gotten from payment intent
style: ThemeMode.dark,
merchantDisplayName: 'Ikay'))
.then((value) {});
//STEP 3: Display Payment sheet
displayPaymentSheet();
} catch (err) {
throw Exception(err);
}
}
displayPaymentSheet() async {
try {
await Stripe.instance.presentPaymentSheet().then((value) {
showDialog(
context: context,
builder: (_) => const AlertDialog(
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
Icon(
Icons.check_circle,
color: Colors.green,
size: 100.0,
),
SizedBox(height: 10.0),
Text("Payment Successful!"),
],
),
));
paymentIntent = null;
}).onError((error, stackTrace) {
throw Exception(error);
});
} on StripeException catch (e) {
//print('Error is:---> $e');
const AlertDialog(
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
Row(
children: [
Icon(
Icons.cancel,
color: Colors.red,
),
Text("Payment Failed"),
],
),
],
),
);
} catch (e) {
print('$e');
}
}
createPaymentIntent(String amount, String currency) async {
try {
//Request body
Map<String, dynamic> body = {
'amount': calculateAmount(amount),
'currency': currency,
};
//Make post request to Stripe
var response = await http.post(
Uri.parse('https://api.stripe.com/v1/payment_intents'),
headers: {
'Authorization': 'Bearer ${dotenv.env['STRIPE_SECRET']}',
'Content-Type': 'application/x-www-form-urlencoded'
},
body: body,
);
return json.decode(response.body);
} catch (err) {
throw Exception(err.toString());
}
}
calculateAmount(String amount) {
final calculatedAmout = (int.parse(amount)) * 100;
return calculatedAmout.toString();
}
}
如果您已经创建了用户,那么您打算导航到付款屏幕,但您没有。
if (newUser != null) {
Navigator.of(context).pushReplacement(MaterialPageRoute(
builder: (context) => const StripePaymentScreen()));
Navigator.of(context).pushReplacement(MaterialPageRoute(
builder: (context) => const VerifyEmailScreen()));
}
导航到付款屏幕后,您将导航更改为验证电子邮件地址屏幕,只需导航到付款:
if (newUser != null) {
Navigator.of(context).pushReplacement(MaterialPageRoute(
builder: (context) => const StripePaymentScreen()));
}