#sayuru-flutter-pi

1 messages · Page 1 of 1 (latest)

high current
#

Hey there! When you remove an item from the cart, are you creating a new PI or updating the existing one?

#

Also, how are you initialising the Payment Sheet? Can you share the code

#

flutter_stripe is also a third-party library so we have limited knowledge of it here, but I'll help where I can!

gilded sage
#

Yeah I can share you the code

#

Here is the python backend which create payment intent after authorizing the user

#
    def __payment_handling(self, customer_id, uid):
        ephemeralKey = stripe.create_ephemeral_key(customer_id)
        total = db.get_total(uid)
        paymentIntent = stripe.create_payment_intent(customer_id, total)
        return jsonify(
            paymentIntent=paymentIntent.client_secret,
            ephemeralKey=ephemeralKey.secret,
            customer=customer_id,
            amount=paymentIntent.amount,
        )
 
#
    def create_ephemeral_key(self, customer_id):
        try:
            return stripe.EphemeralKey.create(
                stripe_version="2020-08-27",
                customer=customer_id,
            )
        except Exception as e:
            return e
#

``

#

``

#
    def create_payment_intent(self, customer_id, price):
        try:
            return stripe.PaymentIntent.create(
                amount=int(price*100),
                currency="usd",
                customer=customer_id,
                payment_method_types=["card"],
            )
        except Exception as e:
            return e
high current
#

I meant more the Flutter code

gilded sage
#

And after receive the json values from backend I init the paymet sheet

#
  void _createPaymentSheet(Map<String, dynamic> data) async {
    try {
      print(data['amount']);
      print(data);
      await stripe.initPaymentSheet(
        paymentSheetParameters: SetupPaymentSheetParameters(
          //main params
          merchantDisplayName: 'EatMoree',
          paymentIntentClientSecret: data['paymentIntent'],
          //customer params
          customerEphemeralKeySecret: data['ephemeralKey'],
          customerId: data['customer'],

          merchantCountryCode: 'SL',
        ),
      );
    } catch (e) {
      throw e;
    }
  }

#

Then I show the payment sheet with the following code

#
  Future _presentPaymentSheet(Emitter<CheckoutState> emit) async {
    try {
      await stripe.presentPaymentSheet();
      emit(CheckoutPaymentSuccess());
    } on StripeException catch (e) {
      final failure = StripeFailure(stripeFailureMsg: e.toString(), prefixMsg: STRIPE_FAILURE);
      emit(CheckoutPaymentUnsuccess(failure));
    } catch (e) {
      final failure = UnknownFailure(prefixMsg: e.toString(), unknownFailureMsg: UNKNOWN_FAILURE);
      emit(CheckoutPaymentUnsuccess(failure));
    }
  }
#

All these dart code called in this method

#
        await data.fold((failure) async => emit(CheckoutPaymentUnsuccess(failure)), (success) async {
          _createPaymentSheet(success);
          await _presentPaymentSheet(emit);
        });
high current
#

And are you creating a new PI or updating the existing one when you make a change in the cart?

gilded sage
#

I am creating payment intent for each time when user try to pay

#

All these codes called when user tap on order now button

high current
#

Have you considered update the existing PI amount parameter instead?

gilded sage
#

Ohh I solved that. It happend because I doesn't waited untill the future completes in the _create payment sheet method

#

My bad

high current
#

PErfect!

gilded sage
#

Thanks. And can you tell me Is it ok to create new payment intent each time

high current
#

It is yes, but you may just end up with a ton of incomplete payments in your Dashboard

#

Plus it's more state for you to manage in your application

gilded sage
#

Yes. It happened to me already. I will try to update existing PI instead of creating new one

#

Thanks your time

high current
#

Np!

gilded sage
#

And another small question. While reading the documentation it says If I update the payment-intent. I need to confirm the PaymentIntent again. And they said If you prefer to update and confirm at the same time, we recommend updating properties via the confirm API instead. But the problem is with the the confirm API there's no any parameter listed in the documentation to give the amount change.

high current
#

Well you wouldn't be confirming the PI at this stage so it's generally unrelated. They'd be 2 different actions:

#
  • Customer updates cart -> update the PI.
  • Confirm updated PI in Payment Sheet.
gilded sage
#

Yeah ok. I got it. Thank you again

high current
#

Sure, np!