ruby-on-railsstripe-payments

When stripe checkout payment fails, it stays on the same page and displays a standard error message provided by stripe


I have integrated stripe checkout session with ruby.

    session = Stripe::Checkout::Session.create(
      payment_method_types: ['card'],
      line_items: [line_item],
      mode: 'payment',
      success_url: success_url,
      cancel_url: cancel_url
    )

When Payment is successful, it is redirected to the desired page (which is expected behaviour). When an error occurs say for example, when a payment is done with a card having insufficient amount, on Stripe Checkout page the error message is displayed to let the user know about the error. Instead of this, I need to redirect it to a different route, if a stripe payment fails with the error message. the success_url ad cancel_url are defined as;


    def success_url
        Rails.application.routes.url_helpers.payment_success_conference_url(
            @conference, booking_slug: @booking.slug)
    end

    def cancel_url
        Rails.application.routes.url_helpers.register_conference_url(@conference)
    end

Added basic flow for the the stripe checkout experience with proper urls to redirect.

added a method create_payment_from_stripe_session to added respective booking and payment in the database as;


    def create_payment_from_stripe_session(session)
        # Create a new payment object
        Payment.create!(
            booking_id: @booking.id,
            attendee_id: primary_attendee_id,
            stripe_session_id: session.id,
            amount_total: session.amount_total,
            currency: session.currency,
            payment_intent_id: session.payment_intent,
            customer_email: session.customer_email,
            success_url: session.success_url,
            cancel_url: session.cancel_url,
            payment_link: session.url
          )
    end

Once the stripe checkout page is rendered (in sandbox), I have tried to make a failed stripe payment using an Insufficient funds decline card, 4000000000009995.

After processing, the card was declined with error message, "Your card has been declined." I want it to redirect to another route that displays the payment and marks the payment created in database as failed. The stripe payment is updated as failed, but in database it is having status as pending. This should be failed as the payment didn't go through. I need to update the status of the payment that is created at the time of checkout-session creation. With current flow, when the payment is successfully completed, it is redirected to the desired route and all entities in the database is updated appropriately. But when the payment fails, payment remains in pending state.

One solution is to add webhook, but is there any other way to manage and update payment statuses in database with respect to the stripe payment.


Solution

  • Checkout Session is designed to redirect users only when a payment is successful. This way, when a payment fails, the users stays on the same page to allow them to retry the payment.

    So what you are trying to do (redirect when the payment fails) is not possible with Checkout Session.