Automating Stripe Checkout Payments with Laravel Dusk

stripe laravel dusk automation.

The simplest way to implement Stripe on your website is using Stripe Checkout, Accepting card payments via Stripe Checkout is two step process with a client side and server side process.

On the client side it involves putting up a code that includes HTML, Javascript and some CSS. When your customer enters their payment information, the card details are validated and tokenized for your server-side code to use.

Here is how the client side code looks

<form action="your-server-side-code" method="POST">
    src="" class="stripe-button"
    data-name="YourCompany Test"
    data-description="Example charge"

This code creates the following button on your website.

Livewire Component Library

Host Laravel Application on DigitalOcean

Use coupon 5balloons on this Cloudways Affiliate URL to get special discount.




Clicking on this button opens up a iFrame , where user can enter the payment information like Email, Card-Number, CVC, Expiry Date etc. Here is how the iframe modal looks like.

stripe checkout iframe modal

Note: This is how the default checkout modal looks, you may have extra fields enabled like zip code depending upon the data configuration in your script.

Let’s get into the Dusk Automation Part.

Since stripe checkout involves opening of an iFrame and putting data into it. We need to make dusk handle to switch into the iFrame before it starts filling out the data.

Dusk Test Code


/** @test */
public function it_asserts_that_user_can_pay_via_stripe(){
    $this->browse(function ($browser) {
                ->assertSee('Pay with Card')
                ->press('Pay with Card')
                ->withinFrame('iframe[name=stripe_checkout_app]', function($browser){
                   $browser->keys('input[placeholder="Email"]', '')
                       ->keys('input[placeholder="Card number"]', '4242 4242 4242 4242')
                       ->keys('input[placeholder="MM / YY"]', '0122')
                       ->keys('input[placeholder="CVC"]', '123')

Let’s understand the parts of this code


Once user press the Pay with Card button, we wait for the iframe to open up. waitFor method makes sure that we proceed only when the iframe is opened.

->withinFrame('iframe[name=stripe_checkout_app]', function($browser){

withinFrame is a great method provided by Laravel Dusk, This method switches the automation driver to the iFrame, performs the callback function code within the iFrame and then brings the driver to default window. withinFrame utilizes driver function switchTo ( $this->driver->switchTo()->frame($selector) ) to perform the switching.

Next, before filling out the payment information. I have given a pause(2000) of 2 seconds. This is because there were certain times driver started typing into the field before they appeared on the screen. Thus to make sure we have all the fields visible, I have added a pause before it starts filling details.

Next up we start putting data into the form fields.

$browser->keys('input[placeholder="Email"]', '')

Since the stripe form fields have a dynamic id filed we cannot make use of that in your tests. The only contant attribute in the fields are the placeholders. Thus I am making use of that to fill in the form details.

Once you have filled the form we hit the submit button press('button[type="submit"'), and wait for the iFrame to vanish waitUntilMissing('iframe[name=stripe_checkout_app]');

There you go. Automating Stripe Checkout Payments with Laravel Dusk.

Udemy Laravel Dusk Course

Laravel Dusk Beginner to Advanced

Video Course on Automation Testing, Web Scraping and Creating Bots using Dusk

> 60% OFF

Limited Time Offer

Learn More

Site Footer