gpt4 book ai didi

android - Flutter Stripe 在展示付款单时抛出 StripeException

转载 作者:行者123 更新时间:2023-12-04 23:37:45 26 4
gpt4 key购买 nike

我正在尝试使用 stripe_payment 包在我的 flutter 应用程序中实现 Stripe 支付系统。在我的代码中,我调用了 Stripe.instance.initPaymentSheet(...),但是当我尝试在几行之后调用 Stripe.instance.presentPaymentSheet(...) 时,我收到了这个错误:

flutter: 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))
这是我的代码:
Future<void> makePayment() async {
final url = Uri.parse(
'${firebaseFunction}');

final response =
await http.get(url, headers: {'Content-Type': 'application/json'});

this.paymentIntentData = json.decode(response.body);

await Stripe.instance.initPaymentSheet(
paymentSheetParameters: SetupPaymentSheetParameters(
paymentIntentClientSecret: paymentIntentData!['paymentIntent'],
applePay: true,
googlePay: true,
style: ThemeMode.dark,
merchantCountryCode: 'UK',
merchantDisplayName: 'Test Payment Service'));
setState(() {});

print('initialised');
try {
await Stripe.instance.presentPaymentSheet();
setState(() {
paymentIntentData = null;
});
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('Payment Successful!'),
));
} catch (e) {
print(e);
}
// await displayPaymentSheet();
}
这是我的 node.js 代码(通过 url 访问):
const functions = require("firebase-functions");

const stripe = require('stripe')(functions.config().stripe.testkey);

exports.stripePayment = functions.https.onRequest(async (req, res) => {
const paymentIntent = await stripe.paymentIntents.create({
amount: 170,
currency: 'usd'
},
function(err, paymentIntent) {
if (err != null) {
console.log(err);
} else {
res.json({
paymentIntent: paymentIntent.client_secret
})
}
})
})
当我尝试使用 presentPaymentSheet 方法时,为什么付款表没有初始化(或保持初始化)?

最佳答案

Paymentsheet 在 android 上工作,但对我来说在 iPhone 上不工作。
我花了几个小时才找到这个答案(也在苦苦挣扎)。
stripe 文档中需要更新,但是在初始化 Stripe 时,您需要初始化 Stripe.publishableKey 并且还要初始化 Stripe.merchantIdentifier
示例
首先,您需要在 main 函数中初始化 Stripe。 (如下图所示)。

void main() async {
WidgetsFlutterBinding.ensureInitialized();
Stripe.publishableKey = stripePublishableKey;
Stripe.merchantIdentifier = 'any string works';
await Stripe.instance.applySettings();
runApp(const App());
}

然后会出现付款单,但不注明 No payment sheet has been initialized yet

关于android - Flutter Stripe 在展示付款单时抛出 StripeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69151587/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com