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
05. Creating a payment intent

Transcript

00:00
So we're going to look now at creating out a payment intent ready for our customer to make
00:05
a payment. Now you can use the charges API if you want to, to create a very, very simple charge. You gather on the client side the card information, then you get a key which you can pass through to your back end and then make a simple charge. Now this flow using the charges API is a lot simpler, but it doesn't come with the benefits of the payment intents API.
00:28
Now you can read all about this over on the Stripe documentation, but the reason that we're covering the payment intents API is a couple of reasons. The main reason is that we can use strong customer authentication, so we can use 3D secure in here. We don't have issues like double charges as well, and really everything is just handled for you. It really works in a much better way.
00:51
The downside is it's a little bit more tricky to set up, but of course this is what we're covering in this course, so we're going to go through everything together. So go ahead and give that a read if you want to. Let's head over to our app and start to create out our payments page. So we're not going to implement the card form just yet, we're just going to create a controller out
01:10
which will initiate a payment intent. So let's go ahead and make a controller in here and just call this payment index controller, and let's go ahead and open up that payment index controller, and let's again create out an invoke magic method in here and just die dump and do something. Let's head over to our web routes and let's again just create out a
01:34
route in here, payment index controller, and we're not going to apply any kind of middleware or anything to this just yet. We do actually want to make sure that we're authenticated, so let's actually add that middleware in there now. Of course, we can't make a payment here if we're not authenticated, so we'll just use Laravel's auth middleware. Okay, so we can now head over
01:56
to the payments page and we just see die dump ABC. Now this is where we're going to create the payment intent, so let's look at how to do that. Let's go and create out a payment intent variable. That's really important because we're going to need to be able to pass the payment intent secret through to our view, which we're then going to use within our card form so
02:20
Stripe knows which payment intent the user is paying with or paying for, and here we're going to go ahead and access our Stripe SDK on our container. We're going to use this to access the payment intent part of the Stripe API, and we're going to go ahead and create a payment intent. Now at the moment, that's not quite going to work. We'll probably see an error here with
02:44
some missing information. Of course, we've got a missing required parameter amount. This is where up front that we define everything that we need from currency to amount to the metadata that we want to include as part of this payment intent, and we are going to be doing that because that's really, really important. Let's start with the basics and let's start with the
03:04
amount. I want this to be $100, so I'm going to go ahead and define this as 10,000 cents. We're going to go ahead and choose the currency in here, which I'm going to, of course, choose as dollars, and then we're going to go ahead and include a really important key here, setup future usage. We don't need to do this because it's not a subscription, so we're just
03:28
going to go ahead and define this as on session. Now, of course, all of this is in the documentation, so if you have slightly different needs, you can go ahead and change that around. Now, before we add anything else here, let's just talk a little bit about how this is going to work. The first thing that we do is set up a payment intent. That goes ahead and defines all of the
03:46
things that we've looked at, like the amount, the currency. Over on Stripe, that will create the payment intent. What we then do is we create a card form and we give the payment intent client secret, which we pass through to that. Now, once a user has made a payment, what we then do is we listen with a webhook on our side for a successful payment intent. That means that the user has
04:13
definitely made a payment and everything has gone through. What we can then do is on our side, update the user. Now, this part here, where we set up a payment intent, what we really need to do is provide the user ID, because when we make a payment and we receive the webhook, the data in this webhook will then contain the user ID. When we get that data back from that webhook, we can
04:39
then look that user up on our end and update the user. That's just a really basic way of explaining that, but let's actually look at it if that didn't make sense. We're going to go ahead and define in here metadata, which is pretty much a way of defining any kind of data within a payment intent that we need. We're just going to provide the user ID, because by the time our Stripe webhook
05:02
hits our server, we need to know the user who has made this payment. What we can do is just cast this to a string, just to be safe here, and go ahead and from our request, which we haven't pulled in yet, access the currently authenticated user, and we can go ahead and grab the ID. Okay, so let's head over and just give this payments page a refresh again. You'll notice
05:26
there's a slight delay there, because obviously we have to hit the Stripe API, but if we now head over to our test data here and we access our payment section, just give that a refresh real quick. That did come up. You can see that we've got this incomplete payment for $100. We get the payment intent key here. We don't have a customer in here, because we're not passing that data
05:48
through. You can do that if you want to, but this is pretty much what we have, and you can see that we've got that metadata there as well. What we now need to do is from this payment intent, access that key, which we're then going to pass through to the card form, and the card form will make the payment, and it will update the status of this payment intent to complete. You can see
06:09
I've got a few succeeded complete payments in here already. Then we'll go ahead and set up our webhook to notify our app that this particular payment intent has been successful. Let's head back over to our app and just take a look at this data really quickly. Let's go ahead and die dump on the payment intent that we assigned this to, and then we'll cover in the next part
06:30
how to hook this up to our form. You can see here we've got pretty much most of the information that we saw over on the Stripe dashboard, but we've got this client secret. That's that value that we want to pass through to our card form. So in the next episode, we are going to set up our card form passing that client secret in.
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.