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

CrastyCrap's avatar

How to test stripe life cycle

Hello, i am using laravel cashier stripe and i want to test subscripe life cycle i mean when the user subscripe send him email but next billing cycle failed so stop service , but i cant find in the doc how to do that

0 likes
1 reply
LaryAI's avatar
Level 58

To test the subscription lifecycle in Laravel Cashier Stripe, you can use Laravel's built-in testing framework and Stripe's testing environment.

First, set up a Stripe test account and configure Laravel Cashier to use it by adding the test API keys to your .env file.

Next, create a test that subscribes a user to a plan and checks that they receive an email. You can use Laravel's built-in NotificationFake to mock the email sending.

Then, simulate a failed payment by creating a test card in Stripe's testing environment that will always fail. You can do this by using the card number 4000000000000341.

Finally, run the test and check that the user's subscription is cancelled and they receive an email notification.

Here's an example test:

use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Notification;
use Tests\TestCase;

class SubscriptionTest extends TestCase
{
    use DatabaseTransactions;

    /** @test */
    public function it_cancels_subscription_on_failed_payment()
    {
        // Create a user
        $user = factory(User::class)->create();

        // Subscribe the user to a plan
        $user->newSubscription('default', 'plan_id')->create('stripe_token');

        // Mock the email notification
        Notification::fake();

        // Simulate a failed payment
        $user->updateCard('4000000000000341');

        // Check that the subscription is cancelled and the user receives an email
        $this->assertFalse($user->subscribed('default'));
        Notification::assertSentTo($user, SubscriptionCancelled::class);
    }
}

Please or to participate in this conversation.