I'm setting up a stripe workflow using a stripe.js PaymentElement. The payment is able to clear, and redirect the user to the url given to stripe. However, when the backend queries the status of the paymentIntent, the status is "requires_payment_method" for several seconds.
The frontend is react, the backend is ruby on rails.
This is how the intent is created:
intent = Stripe::PaymentIntent.create( { amount: my_amount, currency: "usd", automatic_payment_methods: { enabled: true, }, metadata: { #... }, }, )render json: { client_secret: intent.client_secret, pi_id: intent.id }
This is the handler that sends the payment:
const stripe = useStripe();const elements = useElements();const handleSubmit: React.FormEventHandler = async (event) => { event.preventDefault(); // Stripe.js has not yet loaded. if (!stripe || !elements) return; const paymentElement = elements.getElement('payment'); if (!paymentElement) return; await stripe .confirmPayment({ elements, confirmParams: { return_url: my_url, }, });};
This is how the intent is queried on the controller for my_url
:
payment_intent = Stripe::PaymentIntent.retrieve({ id: params[:pi_id] })raise "Payment Error" unless payment_intent.status == "succeeded"
I would expect that after the user is forwarded to the new page, the payment intent would be in "succeeded" status. However, it is frequently in "requires_payment_method" status. Requerying the intent from IRB, the intent eventually resolves to "succeeded" after several seconds. Do I need to poll this every time?