W/System.err(24769): java.lang.IllegalStateException: Reply already submitted | Flutter Stripe Payment

Viewed 129

I am getting this error while running my Payment. I have removed the secret key. Just in case someone wants to test it they can by using their own secret key.

ERROR

W/System.err(24769): java.lang.IllegalStateException: Reply already submitted
W/System.err(24769):    at io.flutter.embedding.engine.dart.DartMessenger$Reply.reply(DartMessenger.java:431)
W/System.err(24769):    at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler$1.success(MethodChannel.java:267)
W/System.err(24769):    at com.facebook.react.bridge.Promise$1.run(Promise.java:26)
W/System.err(24769):    at android.os.Handler.handleCallback(Handler.java:883)
W/System.err(24769):    at android.os.Handler.dispatchMessage(Handler.java:100)
W/System.err(24769):    at android.os.Looper.loop(Looper.java:214)
W/System.err(24769):    at android.app.ActivityThread.main(ActivityThread.java:7356)
W/System.err(24769):    at java.lang.reflect.Method.invoke(Native Method)
W/System.err(24769):    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
W/System.err(24769):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

Payment page code import 'dart:convert'; import 'dart:developer';

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:flutter_stripe/flutter_stripe.dart';
import 'package:http/http.dart' as http;

import '../services/auth.dart';


class PaymentScreen extends StatelessWidget {
  const PaymentScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Stripe Payment',
      theme: ThemeData(
        primarySwatch: Colors.teal,
      ),
      home: const PaymentPage(),
    );
  }
}

class PaymentPage extends StatelessWidget {
  const PaymentPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Future<void> initPaymentSheet(context, {required String email, required int amount}) async {
      try {
        // 1. create payment intent on the server
        final response = await http.post(
            Uri.parse(
                'https://us-central1-stripe-checkout-flutter.cloudfunctions.net/stripePaymentIntentRequest'),
            body: {
              'email': email,
              'amount': amount.toString(),
            });

        final jsonResponse = jsonDecode(response.body);
        log(jsonResponse.toString());

        //2. initialize the payment sheet
        await Stripe.instance.initPaymentSheet(
          paymentSheetParameters: SetupPaymentSheetParameters(
            paymentIntentClientSecret: jsonResponse['Secret key goes here. Removed temporary'],
            merchantDisplayName: 'Flutter Stripe Store Demo',
            customerId: jsonResponse['customer'],
            customerEphemeralKeySecret: jsonResponse['ephemeralKey'],
            style: ThemeMode.light,
            testEnv: true,
            merchantCountryCode: 'SG',
          ),
        );

        await Stripe.instance.presentPaymentSheet();

        ScaffoldMessenger.of(context).showSnackBar(
          const SnackBar(content: Text('Payment completed!')),
        );
      } catch (e) {
        if (e is StripeException) {
          ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(
              content: Text('Error from Stripe: ${e.error.localizedMessage}'),
            ),
          );
        } else {
          ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(content: Text('Error: $e')),
          );
        }
      }
    }
    String currentUserEmail = (Auth().auth.currentUser as User).email.toString();
    return Scaffold(
      appBar: AppBar(
        title: const Text("Stripe Demo App"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
              style: ButtonStyle(
                foregroundColor: MaterialStateProperty.all<Color>(Colors.blue),
              ),
              onPressed: () async {
                await initPaymentSheet(context, email: currentUserEmail, amount: 200000);
              },
              child: const Text(
                'Buy Token 2000 USD',
                style: TextStyle(color: Colors.white),
              ),
            )
          ],
        ),
      ),
    );
  }
}

What can be the possible issue? Seems like my payment is passing through but there is something missing to handle the error or a function.

0 Answers
Related