ios Flutter Stripe在提交付款单时抛出StripeException

syqv5f0l  于 2022-12-15  发布在  iOS
关注(0)|答案(3)|浏览(444)

我尝试在flutter应用中使用stripe_payment包实现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方法时,为什么Payment Sheet没有初始化(或保持初始化)?

g0czyy6m

g0czyy6m1#

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

然后付款单将显示,但不显示No payment sheet has been initialized yet

pbossiut

pbossiut2#

如果有人在未来结束在这里,你已经尝试了一切,包括添加额外的设置在您的主文件中,也尝试改变您的Stripe.instance.initPaymentSheet字段中的Stripe.instance.initPaymentSheetnull或一些现有的ID。我发现,对于android它很容易工作,但与iOS它需要一个适当的customerId或空。

bzzcjhmw

bzzcjhmw3#

条带版本

flutter_stripe: ^7.0.0
await Stripe.instance.initPaymentSheet(
        paymentSheetParameters: SetupPaymentSheetParameters(
            merchantDisplayName: 'APP',
            paymentIntentClientSecret: model.clientSecret,
            customerEphemeralKeySecret: eph.secret,
            customerId: model.customer,
            style: ThemeMode.system,
            billingDetails: billingDetail,
            customFlow: true),
      );

添加属性“customFlow”解决了我的问题

customFlow: true

相关问题