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 :)
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With