Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

webrobert's avatar

Stripe Elements re mount?

I have a multi step form. I want to allow the user to go back and edit change the credit card, not sure how to achieve this.

Presently I use an init to render the elements when the step is chosen. That seems to work fine. I even added a @if to just display visa ...2342 when the user goes back having already completed the card . I want to call a changeCard method reset card and let the user enter a new card.

Something is a miss. The elements do re-render, but fail to submit correctly and stripe gives a generic error.

I'm sure it's my lack of js knowledge, or something with stripe js not allowing reuse of the elements?

0 likes
2 replies
LaryAI's avatar
Level 58

To address the issue of re-mounting Stripe Elements in a multi-step form, you need to ensure that the Stripe Elements are properly initialized and mounted each time the user navigates back to the credit card step. The problem you're encountering might be due to the reuse of the same paymentElement instance without reinitializing it properly. Here's a solution to handle this scenario:

  1. Ensure Proper Initialization: Each time you want to allow the user to change the card, you should create a new instance of the Stripe Elements and mount it.

  2. Unmount and Re-mount: When the user decides to change the card, unmount the existing element and create a new one.

Here's how you can modify your code to achieve this:

Key Points:

  • Re-initialization: By calling initializeStripeElements() each time you need to mount the element, you ensure that a fresh instance is created.
  • Unmounting: Before creating a new instance, always unmount the existing one to avoid conflicts.
  • Event Listeners: Ensure that your form submission logic is correctly handling the Stripe payment method creation and any errors that might occur.

This approach should help you manage the Stripe Elements lifecycle more effectively in a multi-step form scenario.

webrobert's avatar

ok, this seems to work... it allows the user to go back and enter a new card. I used an event to load stripe and dynamically pass in the intentSecret so it was fresh.

Let me know if I missed something?

public function changeCard()
{
    $this->creditCard->reset();
    $this->intentClientSecret = (new StripeService)->setupIntent()['client_secret'];
    $this->dispatch('loadStripe', clientSecret: $this->intentClientSecret);
}

public function completeDetails()
{
    $this->form->validate();

    $this->showSection = 'billing';
    $this->dispatch('loadStripe', clientSecret: $this->intentClientSecret);
}

and in the blade...

Please or to participate in this conversation.