4

I want to implement Stripe to my Application.

To do this, I have this pattern :

The suer click on a product, it's linked to the BackEnd, who generate a PaymentIntent and send it to the FrontEnd. When my Front receive the Intent, he's asking card, numbers, and data to request Stripe's API.

BUT

After creating my Front, I have an issue and I can't compile my code...

SO, this is how my application is made :

in main.dart :

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  /// Initialize Stripe
  Stripe.publishableKey = "**********************************************";

.....
.....
.....
}

in product.dart (the products page) :


import 'package:flutter/material.dart';
import 'package:flutter_stripe/flutter_stripe.dart';

import '../../services/api.dart';

class ProductsPage extends StatefulWidget {
  const ProductsPage({Key? key}) : super(key: key);

  @override
  State<ProductsPage> createState() => _ProductsPageState();
}

class _ProductsPageState extends State<ProductsPage> {
  String? intentPayment;
  final productsList = [
    {
      "name": "Abonnement 30 jours",
      "description": "Abonnement test 30 jours",
      "price": 100,
      "duree": "30"
    },
    {
      "name": "Abonnement 60 jours",
      "description": "",
      "price": 180,
      "duree": "60"
    },
    {
      "name": "Abonnement 90 jours",
      "description": "Abonnement test 30 jours",
      "price": 250,
      "duree": "90"
    },
  ];

  Future<void> makePayment(product) async {
    try {
      intentPayment = await Api.getIntent(product);
      await Stripe.instance.initPaymentSheet(
          paymentSheetParameters: SetupPaymentSheetParameters(
        paymentIntentClientSecret: intentPayment!,
        // applePay: const PaymentSheetApplePay(merchantCountryCode: 'FR'),
        // googlePay: const PaymentSheetGooglePay(merchantCountryCode: 'FR'),
        style: ThemeMode.dark,
        merchantDisplayName: 'JobMe Test',
      ));
      displayPaymentSheet();
    } catch (e) {
      print("exception: $e");
    }
  }

  displayPaymentSheet() async {
    try {
      await Stripe.instance.presentPaymentSheet().then(((value) {
        showDialog(
            context: context,
            builder: (_) => AlertDialog(
                  content: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Row(
                        children: const [
                          Icon(
                            Icons.check_circle,
                            color: Colors.green,
                          ),
                          SizedBox(
                            width: 10,
                          ),
                          Text("Paiement effectué avec succès.")
                        ],
                      ),
                    ],
                  ),
                ));

        intentPayment = null;
      }));
    } on StripeException catch (e) {
      print("Stripe Exception: $e");
    } catch (e) {
      print("exception: $e");
    }
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: Scaffold(
            appBar: AppBar(
              backgroundColor: Colors.white,
            ),
            body: Center(
              child: Column(
                children: [
                  TextButton(
                      onPressed: () async {
                        await makePayment(productsList[0]);
                      },
                      child: const Text("Abonnement 1 mois. 100€")),
                  TextButton(
                      onPressed: () async {
                        await makePayment(productsList[1]);
                      },
                      child: const Text("Abonnement 2 mois. 180€")),
                  TextButton(
                      onPressed: () async {
                        await makePayment(productsList[2]);
                      },
                      child: const Text("Abonnement 3 mois. 250€")),
                ],
              ),
            )));
  }
}

And this is the error I get when I try to run the application :

C:\src\flutter\.pub-cache\hosted\pub.dartlang.org\stripe_android-5.1.0\android\src\main\kotlin\com\facebook\react\bridge\WritableNativeMap.java:22: error: no suitable constructor found for ReadableMap(HashMap<Object,Object>)

        super(new HashMap<>());
        ^
    constructor ReadableMap.ReadableMap(JSONObject) is not applicable      (argument mismatch; HashMap<Object,Object> cannot be converted to JSONObject)
    constructor ReadableMap.ReadableMap(Map<String,Object>) is not applicable      (argument mismatch; HashMap<Object,Object> cannot be converted to Map<String,Object>)
1 error

FAILURE: Build failed with an exception.

* What went wrong:Execution failed for task ':stripe_android:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

(My flutter doctor -v is fine, and I've already try to remove the build folder. I also switched FlutterActivity() to FlutterFragmentActivity() in MainActivity.kt)

Please help :)

1
  • Could you please share the full stackstrace error, that could help understanding the root issue. Where is that constructor function called from? Between, you can take a look at this Flutter example app as a complete working sample project: github.com/flutter-stripe/flutter_stripe/tree/main/example Commented Oct 12, 2022 at 16:19

2 Answers 2

8

Update the gradle version at android/gradle/wrapper/gradle-wrapper.properties

distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-all.zip

and update build gradle of root level to 7.2.1 at android/build.gradle

classpath 'com.android.tools.build:gradle:7.2.1'
Sign up to request clarification or add additional context in comments.

Comments

3

I finally found how to solve this issue.

I downgraded the flutter_stripe version from 5.1.0 to 4.0.0 and it's working perfectly fine now !

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.