Flutter - Stripe Checkout 的 payment_method_types 数组中的无效数组

问题描述

我在做什么

我正在通过 webview 以及下面提到的其他一些插件Flutter 中使用条带结帐。我为另一个应用程序使用了相同的代码,它在那里完全正常工作。虽然它在我当前的应用中显示了这个错误

{"error":{"message":"Invalid array","param":"payment_method_types","type":"invalid_request_error"}}

在这里传递的参数参考 Stripe checkout 文档:

 final body = {
      'payment_method_types': ["card"],'line_items': [
        {
          'name': "$serviceName",'amount': price.round(),"currency": "usd",'quantity': 1,}
      ],'mode': 'payment','success_url': 'https://success.com/{CHECKOUT_SESSION_ID}','cancel_url': 'https://cancel.com/',};

插件

  • webview_Flutter: ^2.0.8
  • dio: ^4.0.0
  • js: ^0.6.3

完整代码

这段代码基本上返回了一个 sessionId,然后我会进一步使用它。

class StripeServer {
  final String serviceName;
  final int price;

  StripeServer({this.serviceName,this.price});

  Future<String> createCheckout() async {
    final auth = "Basic " + base64Encode(utf8.encode(dotenv.env['secretKey']));
    final body = {
      'payment_method_types': ["card"],};

    try {
      final result = await dio().post(
        "https://api.stripe.com/v1/checkout/sessions",data: body,options: Options(
          headers: {HttpHeaders.authorizationHeader: auth},contentType: "application/x-www-form-urlencoded",),);
      return result.data['id'];
    } on dioError catch (e) {
      print(e.response);
      throw e;
    }
  }
}

任何帮助将不胜感激,如果您需要更多详细信息,请告诉我我会更新。

解决方法

您必须删除 "payment_method_types": ["card"]。 几个月前,必须通过此字段,但现在似乎没有必要。这是我的代码示例:

    var amountInt = (amount * 100.toDouble()).toInt();
    try {
      var data = {"amount": amountInt,"currency":"eur"}; //,"payment_method_types[]": ["card"]};
     
      Response response = await Dio().post("https://api.stripe.com/v1/payment_intents",data: data,options:
          Options(contentType:Headers.formUrlEncodedContentType,headers: {
              'Authorization': 'Bearer ${secret}',}
          ),);
      _currentSecret = response.data["client_secret"];
      return response.data["id"];
    } catch (err) {
      print('err charging user: ${err.toString()}');
    }