Flutter Stripe 在显示付款单时抛出 StripeException

Zac*_*ain 15 android ios stripe-payments flutter flutter-dependencies

我正在尝试使用 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))
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

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();
  }
Run Code Online (Sandbox Code Playgroud)

这是我的 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
            })
        }
    })
})
Run Code Online (Sandbox Code Playgroud)

当我尝试使用 PresentPaymentSheet 方法时,为什么付款表没有初始化(或保持初始化状态)?

小智 37

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());
}

Run Code Online (Sandbox Code Playgroud)

然后付款单就会出现,没有说明No payment sheet has been initialized yet

  • 你绝对救了我!! (6认同)
  • IOS 不适合我 (3认同)