flutter 为什么不展示卡片

66bbxpm5  于 2023-02-20  发布在  Flutter
关注(0)|答案(1)|浏览(145)

我想集成条带支付,但收到此错误在调用presentPaymentOptions()之前,必须使用configureWithPaymentIntent()或configureWithSetupIntent()成功初始化流量控制器如何解决此错误以及它不显示任何卡

Center(
        child: ElevatedButton(
          onPressed: () {
            intpayment(email: "email,amount: 50.0);
            },
          child: Text("Pay20\$"),
        ),
      ),
Future<void> intpayment(
      {required String email, required double amount})async{
    try{
      final response= await http.post(Uri.parse("https://api.stripe.com/v1/payment_intents")
          ,body:{
            "receipt_email": email,
            "amount": amount.toInt().toString(),
            "currency": "usd"
          },
          headers: {
            'Authorization': 'Bearer ' + 'key',
            'Content-Type': 'application/x-www-form-urlencoded'
          }
      );
      final jsonresponse=jsonDecode(response.body);      Stripe.instance.initPaymentSheet(paymentSheetParameters: SetupPaymentSheetParameters(
        paymentIntentClientSecret: jsonresponse['paymentIntent'],
        merchantDisplayName: 'Zohaib',
      ));
      await Stripe.instance.presentPaymentSheet();
      Fluttertoast.showToast(
          msg: "payment successfully",
      );
    }
    catch(e){
      if (e is StripeException) {
        Fluttertoast.showToast(
            msg: "Stripe error $e",
        );
      }
      Fluttertoast.showToast(
          msg: "$e",
          toastLength: Toast.LENGTH_SHORT, );
    }
  }
fd3cxomn

fd3cxomn1#

您需要在服务器端创建PaymentIntent,而不是在flutter应用中。

final response= await http.post(Uri.parse("https://api.stripe.com/v1/payment_intents")
          ,body:{
            "receipt_email": email,
            "amount": amount.toInt().toString(),
            "currency": "usd"
          },
          headers: {
            'Authorization': 'Bearer ' + 'key',
            'Content-Type': 'application/x-www-form-urlencoded'
          }
      );

您不应该像上面的代码那样直接调用Stripe API,而应该调用自己的API并生成一个支付意向,然后将client_secret发送到flutter应用,否则您将暴露自己的密钥,从而获得对数据的访问权限。
完成服务器端部分后,下面将介绍其余部分。

相关问题