This episode is for members only

Sign up to access "Build a File Marketplace with Laravel" right now.

Get started
Already a member? Sign in to continue
Playing
04. Registering the Stripe PHP library

Episodes

0%
Your progress
  • Total: 3h 32m
  • Played: 0m
  • Remaining: 3h 32m
Join or sign in to track your progress

Transcript

00:00
From here on, we're going to start to make a bunch of requests the Stripe API to actually go through that onboarding process. So we need to put in the Stripe PHP library, get it into our application so we can use it really easily.
00:12
So the first thing that we're going to do is head over to the Stripe PHP library, and we're just going to go ahead and pull that in with Composer. We're going to close this off now because most of it now is pretty straightforward, but we're going to have to keep our Stripe account open and head over to this developer section where we can grab our API keys. So we're going to need our API keys under this test mode.
00:34
We obviously don't want to do this on production or live mode, and we can grab these and put these into our app. So this is usually where I start because it's the most boring part. We're going to go over and pull these into an EMV value just over here. So we're going to go ahead and say Stripe secret, or in this case, that's the publishable key.
00:52
I don't actually think we need the publishable key because we have no checkout on our page. So I'm going to go ahead and log in here and just grab the secret key. OK, so let's grab this secret here, and we're just going to add this to a EMV value called Stripe secret, and that's it. Now we're going to build out some config for this because we don't want to read these values directly from our EMV.
01:12
So let's go ahead and do that now while we're here. If we just head over to the config folder over in Laravel, let's go ahead and create our Stripe.php file. This is just going to return to us an array. So this is just how we build our config.
01:25
And we're just going to say secret in here and we're going to reference that Stripe secret value inside of our EMV. If you are new to Laravel, the reason that we do this is because in a different environment like production, this Stripe secret is going to be different. It's going to be your live version of the key. So this is just a really easy way to switch this out.
01:43
But we want to keep this in config so within Laravel we can access it a little bit more easily. OK, so now that we've done that, what we want to do is build out a service provider for Stripe. So again, we can do that with Artisan. So let's go ahead and make out a provider and we're going to call this Stripe service provider.
02:02
We're going to go ahead and register that under config and app. So if we just come down to our provider section here, we should find down here a package service providers. Or you can put this on the applications service providers. So let's just copy one of these down here.
02:19
Switch this out to Stripe service provider and we're done. So we can open up our Stripe service provider and under register we can bind Stripe to the container. Now we're going to do this in the most basic way possible because we're only going to be making a few API calls to Stripe. We're not going to get that advanced in this case, but you might want to change this around if you grow this app any bigger.
02:40
So we're going to go ahead and bind in just a string value in our container called Stripe. This just means that later on, what we can do is resolve this out of the container using app and Stripe. And then we can start to make API calls like this. We could say customers and create, for example.
02:57
This just makes it super easy to have this within our app so we don't have to pull it in in every single controller or action that we need to do. So to bind this to our container, we're going to create our closure here. Anything that we return from here is now going to be put into our container under this Stripe key that we can access anywhere in our app. So we're going to return a new instance of the Stripe client.
03:18
So let's go ahead and pull in the Stripe client. We might just need to re-index our workspace. And there it is. And then in here, all we need to do for our purposes right now is just pull in our secret key, which we know we added to Stripe config file under secret.
03:33
So we can just use the config helper here and say Stripe.secret. And that's pretty much it. That's all we need to do. We now have this in our app that we can start to use.

Episode summary

In this episode, we kick things off by integrating the Stripe PHP library into our Laravel project so we can start talking to the Stripe API. We begin by installing the library using Composer, which is always a necessary first step before any Stripe-related logic can happen in your app.

After pulling in the library, we head over to our Stripe dashboard (in test mode—never use live keys for testing!) and grab our secret API key. Instead of hardcoding this sensitive info, we pop it into our .env file under STRIPE_SECRET, and we talk about why this is important, especially when switching between local and production environments.

Next, we set up a config/stripe.php config file to cleanly manage our Stripe keys, making them easily swappable and centrally stored. Following best Laravel practices, we use config files so we never have to reference the .env variables directly throughout our project.

Then we create a StripeServiceProvider, register it, and use Laravel's service container to bind the Stripe client class. This way, we can easily resolve and use our Stripe instance anywhere in the app, without repetitive setup. We keep the setup basic since we only need a few API calls, but it's robust enough to handle more advanced usage later.

By the end of the episode, your Laravel project is all set up to make authenticated requests to Stripe, with everything nicely organized and ready for the next steps.

Episode discussion

No comments, yet. Be the first!