【问题标题】:Flutter Stripe throws StripeException when Presenting Payment SheetFlutter Stripe 在展示付款单时抛出 StripeException
【发布时间】:2021-11-08 02:11:42
【问题描述】:

我正在尝试使用 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 方法时,为什么付款表没有初始化(或保持初始化)?

【问题讨论】:

  • @ZackHossian 你解决问题了吗?
  • @ZackHossain 你解决了吗,我也有同样的问题

标签: android ios flutter stripe-payments flutter-dependencies


【解决方案1】:

如果将来有人来到这里,并且您已经尝试了所有方法,包括在主文件中添加额外设置,请尝试将 Stripe.instance.initPaymentSheet 中的 customerId 字段更改为 null 或一些现有 ID。我发现对于 android,它很容易工作,但对于 iOS,它需要一个正确的 customerId 或 null。

【讨论】:

    【解决方案2】:

    Paymentsheet 可以在 android 上运行,但对我来说在 iPhone 上不可用。 我花了几个小时才找到这个答案(也在苦苦挣扎)。 stripe 文档中需要更新,但在初始化 Stripe 时,您需要初始化 Stripe.publishableKey 并初始化 Stripe.merchantIdentifier

    示例

    首先,您需要在主函数中初始化 Stripe。 (如下图所示)。

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

    然后会出现paymentsheet,不写No payment sheet has been initialized yet

    【讨论】:

    • 你绝对救了我!!
    猜你喜欢
    • 2022-01-16
    • 2022-06-14
    • 2021-12-01
    • 2023-02-20
    • 2020-03-14
    • 2018-09-12
    • 2017-04-16
    • 2023-04-11
    • 2015-03-01
    相关资源
    最近更新 更多