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

MohamedTammam's avatar

How to write tests for Laravel Cashier with Stripe one-time charge

Here's the part of the code that I want to test. I simplified it to make it more clear.

$request->validate([
            'payment_method' => 'required|string',
]);
$stripeCharge = $user->charge($price, $paymentMethod);
// The rest of order flow ...

Now in the test I need to send a POST request with a token as payment_method.

The question now is, how to generate a stripe payment token from tests to send it? or how to test that part of the code in general?

0 likes
1 reply
kevinbui's avatar

I am checking the ChargesTest.php class, looking at the very first test case: test_customer_can_be_charged. Apparently, we can pass whatever we want in the test environment.

Please or to participate in this conversation.