Firebase with Stripe, hold on card/checking funds/checking balance before payment, react-native

43 views Asked by At

I'm struggling with checking if card has enough funds before payment. I am using Firebase with "Run Payments With Stripe"(Made by Stripe). I am able to save cards, create payment intent and for it pay later but I can't find a solution to grab confirmation that there are funds. I've tried with different properties in payment-intent like capture_method: "manual" etc. but I don't know how to grab (if it's possible) the confirmation or status: "requires_capture" from Stripe. Also I've tried with attaching card key/id to payment_method but all my payment intents ends up with status "requires_payment_method" - despite the fact, the method is attached to customer object. My payment flow:

  1. ADDING AND SAVING CARD:
 "const handleSaveCard = async () =\> {
   if (!paymentMethod?.complete) {
   Alert.alert('Pass all informations');
   return;
   }

   const { error, paymentMethod: newPaymentMethod } =
   await createPaymentMethod({
   paymentMethodType: 'Card',
   paymentMethodData: {
   paymentMethodId: paymentMethod.id,
   cvc: cardCVC,
   },
   });

   if (error) {
   console.log('Error creating payment method:', error);
   } else {
   navigation.navigate('PaymentMethods');
   if (
   newPaymentMethod &&
   !savedPaymentMethods.find((pm: any) =\> pm?.id === newPaymentMethod.id)
   ) {
   setSavedPaymentMethods((prev: any) =\> \[...prev, newPaymentMethod\]);
   const user = auth()?.currentUser?.uid;
   const userRef = await firestore().collection('passengers').doc(user);
   const userDoc = await userRef?.get();
   const userAsStripeCustomer = userDoc?.data()?.stripeId;
   await firestore()
   .collection('exampleCustomers')
   .doc(user)
   .collection('payment_methods')
   .add({
   paymentId: newPaymentMethod.id,
   cardBrand: newPaymentMethod.Card.brand,
   last4: newPaymentMethod.Card.last4,
   key: newPaymentMethod.id,
   });
   await firestore()
   .collection('passengers')
   .doc(user)
   .update({
   payment_method: newPaymentMethod.id,
   stripeId:
   userAsStripeCustomer !== undefined ? userAsStripeCustomer : '',
   });
   }
   }
   };
   "
  1. CREATE PAYMENT INTENT:
        createPaymentIntentStandard: async (paymentMethodId: string) => {
          const user = auth()?.currentUser?.uid;
          await firestore().collection('passengers').doc(user).update({
            payment_method: paymentMethodId,
          });
          await firestore()
            .collection('exampleCustomers')
            .doc(user)
            .collection('checkout_sessions')
            .add({
              email: loggedUser?.email,
              client: 'mobile',
              mode: 'payment',
              amount: priceStandardAsStripeAmout,
              currency: currency,
              paymentMethodType: 'Card',
              paymentMethod: paymentMethodId,
              payment_method: 'Card',
              done: false,
              setup_future_usage: 'off_session',
              capture_method: 'manual',
              captureMethod: 'manual',
              confirm: true,
              last4: lastFour,
              cardBrand: cardBrand,
              key: key,
              capture: false,
            });
        },
  1. PAYING
 handlePaymentWithSavedMethod: async (paymentMethodId: string) => {
          try {
            if (!paymentIntentClientSecret) {
              return;
            }
            const user = auth()?.currentUser?.uid;
            await firestore().collection('exampleCustomers').doc(user).update({
              payment_method: paymentMethodId,
            });

            const { error, paymentIntent } = await confirmPayment(
              paymentIntentClientSecret,
              {
                paymentMethodType: 'Card',
                paymentMethodData: {
                  paymentMethodId: paymentMethodId,
                },
              },
            );

            if (error) {
              console.log('Payment confirmation error', error);
            } else if (paymentIntent) {
              console.log('Payment successful', paymentIntent);
            }
          } catch (error) {
            console.log('Payment error', error);
          }
        },

I,ve tried with ideas from Stripe like for example: https://stripe.com/docs/payments/place-a-hold-on-a-payment-method#cancel-authorization but like I mentioned, I can't find anything to be sure that the card has funds. The goal is to be sure that there is a hold on card before navigating to next screen(after creating payment intent) or to just check that there is funds or not and inform the user about it.

Please, help :D

0

There are 0 answers