Flutter Stripe paymentsheet is opening webpage (hooks.stripe.com) while processing payments

1.2k Views Asked by At

I am developing a flutter application which uses stripe for payments. I am using https://pub.dev/packages/flutter_stripe for this.

Everything is working fine but whenever I initiate payments I always get a webpage middleware (Screenshots attached). What am I doing wrong?Stripe payment sheet and web middle ware :

Here is my implementation in Flutter

    Future<void> makePayment(String planName, String type) async { 
    Fluttertoast.showToast(msg: "initiating Payments, Please wait.");
   ApiProvider provider = ApiProvider();
    final tokenResponse = await provider
    .getPaymentToken(PlanPayment(planName: planName, type: type));
    if (tokenResponse != null) {`
    var _service = locator<NavigationService>();
    String secret = tokenResponse.clientSecret;

  // make a get call from this url
  Map<String, dynamic> paymentIntentData = Map();
  await payment.Stripe.instance.initPaymentSheet(
      paymentSheetParameters: payment.SetupPaymentSheetParameters(
    merchantCountryCode: 'IN',
    testEnv: true,
    paymentIntentClientSecret: secret,
    googlePay: true,
  ));
  try {
    // await Stripe.instance.handleCardAction(secret);
    await payment.Stripe.instance.presentPaymentSheet().then((value) {});
    await payment.Stripe.instance
        .confirmPaymentSheetPayment()
        .then((value) async {
      // await _service.pushNamed(paymentStatus, args: {'isSuccess': true});
    });
  } catch (e) {
    // await _service.pushNamed(paymentStatus, args: {'isSuccess': false});

    print("Stripe error" + e.toString());
  }

  await provider
      .confirmPayment(tokenResponse.transactionId)
      .then((value) async {
    await _service
        .pushReplacementNamed(paymentStatus, args: {"isSuccess": value});
  });
}

}

`

2

There are 2 best solutions below

2
On

Maybe you have a webhook put in your account?

1
On
        1)Provide valid Secret key 
        2)Provide valid Publisable key
        3)Update flutterstripe pacakge
        4)provide valid currency code to create stripe account country  
           ex :- stripe account create india to inr etc..
        
        
        5)Right Way to implemet
        
         - Main.dart to main method run app to implemet
        
               ex :--
                  Stripe.publishableKey = "your publishable key ";
        
         - create controller / method
        
        
        code:-
          Map<String, dynamic>? paymentIntentData;
        
        Future<void> makePayment({amount}) async {
            try {
              paymentIntentData =
                  await createPaymentIntent(amount: amount, currency: 'INR');
              if (paymentIntentData != null) {
                await Stripe.instance.initPaymentSheet(
                    paymentSheetParameters: SetupPaymentSheetParameters(
                  // applePay: true,
                  googlePay: const PaymentSheetGooglePay(merchantCountryCode: 'INR'),
                  merchantDisplayName: "PGA",
                  customerId: paymentIntentData!['customer'],
                  paymentIntentClientSecret: paymentIntentData!['client_secret'],
                  customerEphemeralKeySecret: paymentIntentData!['ephemeralkey'],
                ));
              }
        
              displayPaymentSheet();
            } catch (err) {
              logger.e(err);
            }
           
          }
        
          void displayPaymentSheet() async {
            try {
              await Stripe.instance.presentPaymentSheet();
              Get.snackbar("PaymentInfo", "Payment Successfully");
            } on Exception catch (e) {
              if (e is StripeException) {
                logger.e(e, "Error From Stripe");
              } else {
                logger.e(e, "Unforeseen error");
              }
            } catch (e) {
              logger.e("exeption === $e");
            }
           
          }
        
          var id = "";
          createPaymentIntent({amount, currency}) async {
            try {
              Map<String, dynamic> body = {
                'amount': calculateAmount(amount: amount),
                'currency': currency,
                'payment_method_types[]': 'card'
              };
              var response = await http.post(
                Uri.parse('https://api.stripe.com/v1/payment_intents'),
                headers: {
                  'Authorization':
                      'Bearer YourSecretKey',
                  'Content-Type': 'application/x-www-form-urlencoded'
                },
                body: body,
              );
              if (response.statusCode == 200) {
                var decode = jsonDecode(response.body);
                logger.e(decode);
        
                id = decode['id'];
                return decode;
              }
            } catch (e) {
              logger.e(e, "error charging user");
            }
           
          }
        
          calculateAmount({amount}) {
            logger.e(amount.round());
            final a = (int.parse(amount.toString())) * 100;
            logger.e(a.toString());
            update();
            return a.toString();
          }
        
        
 6)      How to  Access  Stripe payment :-
ex :- any button click 
        
          ontap : (){
    makePayment(
      amount: "200")
    }