我试图在Flutter中实现条纹支付方法,但它给出了一个问题flutter: Exception/DISPLAYPAYMENTSHEET==> StripeException(error: LocalizedErrorMessage(code: FailureCode.Failed, localizedMessage: No payment sheet has been initialized yet, message: No payment sheet has been initialized yet, stripeErrorCode: null, declineCode: null, type: null)) #0 MethodChannelStripe._parsePaymentSheetResult (package:stripe_platform_interface/src/method_channel_stripe.dart:310:11) #1 MethodChannelStripe.presentPaymentSheet (package:stripe_platform_interface/src/method_channel_stripe.dart:246:14) <asynchronous suspension> #2 Stripe.presentPaymentSheet (package:flutter_stripe/src/stripe.dart:367:12) <asynchronous suspension> #3 FutureExtensions.onError.<anonymous closure> (dart:async/future.dart:1049:15) <asynchronous suspension>
我检查了我在Android上的所有构建设置以及iOS设置,一切都很好。这里是我的条纹支付方式的代码
Future<void> makePayment() async {
try {
paymentIntentData = await createPaymentIntent('20', 'USD');
await Stripe.instance
.initPaymentSheet(
paymentSheetParameters: SetupPaymentSheetParameters(
paymentIntentClientSecret:
paymentIntentData != null ? ['client_secret'].join('') : null,
style: ThemeMode.light,
merchantDisplayName: 'John',
))
.then((value) {});
await displayPaymentSheet();
} catch (e) {
if (kDebugMode) {
print(e.toString());
}
}
}
displayPaymentSheet() async {
try {
await Stripe.instance.presentPaymentSheet().then((newValue) {
if (kDebugMode) {
print('payment intent${paymentIntentData!['id']}');
}
print('payment intent${paymentIntentData!['client_secret']}');
print('payment intent${paymentIntentData!['amount']}');
print('payment intent$paymentIntentData');
//orderPlaceApi(paymentIntentData!['id'].toString());
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text("Congratulation you have buy 20 Credits")));
paymentIntentData = null;
}).onError((error, stackTrace) {
if (kDebugMode) {
print('Exception/DISPLAYPAYMENTSHEET==> $error $stackTrace');
}
});
} on StripeException catch (e) {
if (kDebugMode) {
print('Exception/DISPLAYPAYMENTSHEET==> $e');
}
showDialog(
context: context,
builder: (_) => const AlertDialog(
content: Text("Cancelled "),
));
} catch (e) {
print('$e');
}
}
createPaymentIntent(String amount, String currency) async {
try {
Map<String, dynamic> body = {
'amount': calculateAmount(amount),
'currency': currency,
'payment_method_types[]': 'card'
};
var response = await http.post(
Uri.parse('https://api.stripe.com/v1/payment_intents'),
body: body,
headers: {
'Authorization':
'Bearer sk_test_key,
'Content-Type': 'application/x-www-form-urlencoded'
});
return jsonDecode(response.body.toString());
} catch (e) {
print(e.toString());
}
}
calculateAmount(String amount) {
final price = int.parse(amount) * 100;
return price.toString();
}
这是我主要文件
void main() async {
WidgetsFlutterBinding.ensureInitialized();
Stripe.publishableKey = 'pk_test_';
Stripe.merchantIdentifier = 'any string works';
await Stripe.instance.applySettings();
runApp(const MyApp());
}
请帮助我解决这个问题,它不能在真实的的设备上工作,不能在Android和iOS上工作。我检查了我在Android上的所有构建设置以及iOS设置,一切都很好。
1条答案
按热度按时间w1jd8yoj1#
我想你的顺序不对。你需要按照这个指南来做。
首先
await
创建PaymentIntent,然后await
initPaymentSheet
,其中您从PaymentIntent创建方法传递秘密,然后presentPaymentSheet
。