This episode is for members only

Sign up to access "Build a Pay Once For Access App" right now.

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

Transcript

00:00
We're now going to set up the Stripe PHP library or the Stripe PHP SDK and it kind of goes without
00:06
saying that you're going to need a Stripe account for this so if you don't already have one of course you're going to need to go ahead and sign up. You can see here that we're running under test data, I've toggled test mode and that works with the developers tab as well which we're going to grab our API keys from. We can see all of our test payments here and of course over on our
00:24
dashboard see everything that we need. So the first thing to do is just go ahead and install the Stripe PHP library so let's come down here and grab the composer command for this and let's close everything off open up a terminal tab and just go ahead and run this. Okay so now that we've done that what we need to do is use this by passing in if we just head down here our test key or our
00:49
production key depends on which environment you're working in. So this is how we set up a Stripe client but we need to do this really on Laravel's container so we can easily access it from anywhere we need. We don't just want to dump this in a controller we kind of want this to be a global thing. So one of the best ways to do this is to set up a service provider so we're going to go
01:10
ahead and make out a provider within Laravel and we're going to call this Stripe service provider and if we just open up that Stripe service provider you can see here that we have this register method this is where we're going to bind this to Laravel's container so we can grab it out wherever we need it. So to do this we're going to go ahead and say this app and singleton so we're
01:32
going to register this as a singleton and we're just going to give this a name we're not binding it to any kind of interface just yet we could do that later if things get a little bit more advanced but we have this closure here which we're going to return the new Stripe instance from which is what we see over in the docs here so we're going to go ahead and return a new Stripe client and
01:52
we're going to just go ahead and re-index our workspace so that gets pulled in so Stripe client and you can see that that comes from Stripe, Stripe client so you're going to need to add the import up here and then in here we just need our key now where's this going to come from we don't just want to dump it in here because it's going to change depending on the environment that
02:14
we're working on when we're on our local machine we're going to have a different key to production so the best thing to do here would be to create out some config within our app so if we come under our config directory and create out a stripe.php file we can actually create our stripe configuration file in here and that just keeps things nice and tidy so in here we're going to have a key which
02:37
is going to be our client side key and we'll implement that in a minute and our secret which will be the key that we use on the back end so the key is going to be used when we implement our client and secret is going to be used when we start to do things directly with the stripe api like create payment intents so these again we're not going to dump them directly inside of this config
02:59
we're actually going to put them inside of our environment file and that means that when we push this to production we can change around the environment variables that we need depending on the server that we're running on so we're going to go ahead and say stripe key and set that in a moment and stripe secret and again set that in a moment so we're going to go over to the stripe
03:20
dashboard come over to developers come over to api keys and we're going to grab our publishable key paste this in here and we're going to grab our secret key and just enter our password here and we're going to go ahead and grab that now and paste that into here making sure we get rid of that x so now we've got these two values we can reference these within config so to do that we use the env
03:43
helper stripe key and here again env helper and stripe secret so now we've got these nicely tucked away in their own configuration file which we can reference from here but we also have these in the env so if they change when we push this to a production server we can conveniently change these so we're going to use the config helper here to say stripe which is the name of the configuration
04:07
file and then secret which is the key inside of that that we need so now we're binding this to our container well not quite yet we need to come over to config an app and we need to come down to where we register our service providers let's come all the way down here and let's find our providers section which is just here and let's go ahead and add this under we could add this
04:28
under application service providers and just switch this out to stripe service provider so if we head over to our app now nothing really changes but now we can access that from our container and start to do whatever we want for it now we can test this out by coming over to the dashboard controller or just coming over to routes and web and we could just dump this
04:50
out in here so let's die dump on app and stripe and we should see that instance of the stripe client with all of our configuration so we have the stripe php sdk tucked away nicely over on our service container wherever we need it and we can now use this to start to create out a payment intent which is exactly what we're going to do in the next episode
15 episodes1 hr 11 mins

Overview

Let's do payments right with the Stripe Payment Intent API, and build an app where customers can pay to access a members area.

We'll cover setting up a Payment Intent, process payments correctly with authorization, handle declines, and securely respond to a Stripe webhook to upgrade a member.

This course is for you if:

  • You want to process payments to upgrade customers for access
  • You need to learn how to properly process one-off payments with Stripe
  • You need a refresher on the Payment Intents API
Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Comments

No comments, yet. Be the first to leave a comment.