本文介绍了Flutter - Stripe Checkout 的 payment_method_types 数组中的无效数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using stripe checkout in flutter via webview along with some other plugins mentioned below. I have used the same code for another app and its totally working fine over there. While its showing this error in my current app:

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

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

Parameters which I'm passing here in reference to Stripe checkout doc:

 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 然后我会进一步使用它.

    This code is basically returning a sessionId which then I'm using further.

    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"],
          '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/',
        };
    
        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;
        }
      }
    }
    

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

    Any help will be appreciated, in case you need more details please let me know I will update.

    推荐答案

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

    You have to remove "payment_method_types": ["card"].A few months ago, it was mandatory to pass this field, but now it seems that it is not necessary. Here is an example of my code:

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

    这篇关于Flutter - Stripe Checkout 的 payment_method_types 数组中的无效数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 13:01