Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - Invalid array in payment_method_types of Stripe Checkout

What I'm doing

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"}}

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

Plugins:

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

Complete Code

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.

like image 609
Hamza Avatar asked Sep 07 '25 22:09

Hamza


1 Answers

This is what worked for me.

final body = {
      'payment_method_types[]': "card",
}
like image 63
Carlton Foster Avatar answered Sep 10 '25 01:09

Carlton Foster