< go back to home

Stripe V3 in PHP Tutorial

October 14, 2019

Stripe is a payment platform that caters in making it easy to implement for software developers. It released a new checkout version in April 2019. I had a hard time finding tutorials for it in PHP so I am writing this to help others. This is a step-by-step walkthrough on checkout to payment.

Note that though I am using Laravel in this demo, this should easily translate to other PHP frameworks. I'm just taking advantage of things that are already setup like Composer, ENV file and routes. With that said, let us begin.

Start by setting up Laravel so we have a common ground. Refer to this documentation for the installation. You may use "php artisan serve" command for quick webserver. If all went well, you'll see welcome page like the image below.

Welcome page for a fresh Laravel install.

If you haven't done so, sign-up to Stripe. In the dashboard account, you'll find your test "Publishable key" and "Secret key".

Stripe dashboard.

Place "Publishable key" and "Secret key" at the end of ".env" file. If ".env" file does not exist yet, create one by copying ".env.example" as ".env". Remember to put your own keys here.

Also, make sure to have an account name by clicking "Unanamed Account" in the top left panel and entering any name.

Install Stripe PHP library using Composer by running the command.

Create checkout controller using Artisan command. This will generate a file in "app/Http/Controllers/CheckoutController.php"

Open the empty controller class and add lines of code below to have a Stripe checkout. This is based in this Stripe documentation for checkout.

The Session::create() function contains the parameters that would generate a session key which will be used in the frontend. It would contain the information in the array parameters especially the amount to be paid so it would handle those transactions by just using the session key. You can easily imagine the information in "line_items" parameter comming from database table of cart for example.

Note that "success_url" and "cancel_url" should point to the URL route that would be redirected based on wether the payment is successful or not. The domain "" is just where my local web server is being served. In your case, this might be "http://localhost".

Next is to create the view file for the frontend in 'resources/views/checkout.blade.php This is based in Stripe checkout documentation step 2.

Finally, add the checkout route in "routes/web.php".

You should now see a simple button when you visit checkout page, e.g. "http://localhost/checkout". It is just a simple button as this is demo only but, you can put additional information in this page, usually an address form and summary of items to be purchase.

Click the checkout button. It will redirect you to Stripe's own payment page.

Fill up the form with test data. You may use the test value "4242 4242 4242 4242" for card information. Click the "Pay button" and you will be redirected to the "success-payment" page that was defined in the backend as "success_url" parameter when creating session.

You now have an end-to-end implementation of Stripe in PHP. In Part 2, I will show how to use Stripe webhooks. This is useful for use cases like processing your own database to set order status once Stripe is successful in payment.:w