تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Introducing Stripe Webhooks0:00

Now that we have completed our checkout, we can move on to dealing with webhooks being sent by Stripe. So whenever a customer pays, Stripe will send us a webhook informing us that a payment was successful. Because we're using Cashier, it already ships with a bunch of helpers out of the box that will help us to handle webhooks sent by Stripe. So let's open up our EventServiceProvider and register a new listener. So Cashier has an event called webhookReceived.

Creating Webhook Listener0:34

and register a new listener. So Cashier has an event called webhookReceive. So we're going to listen to this event and register a new listener to handle this event. Let's open up our terminal and use php artisan make:listener to create a new listener called StripeEventListener. Now that this is created, we can update our EventServiceProvider to call this listener whenever the webhookReceived event is fired. Let's open up our listener. In this case, we can remove the constructor because we don't need it.

Handling Checkout Event1:10

Let's open up our listener. In this case, we can remove the constructor because we don't need it. Let's update the handle method so we can update this type in from object to the webhook.receives.event. Now before we move on, let's quickly take a look at the Stripe documentation and take a look at the different events. In our case, we want to listen for the checkout.session.completed event. This will occur whenever a checkout session has been successfully completed. So the webhook.receives.event has a payload property,

This will occur whenever a checkout session has been successfully completed. So the webhook receives event has a payload property, and this payload property will contain an array with the entire webhook. So now what we need to do is to check the payload type and to check whether this is the correct event that we want to listen to. So in this case, we saw event.payload.type, and we'll make sure that it is equal to checkout.session.completed. Now to get a better understanding of the payload itself, we're going to trigger a new project. So head over to the browser, click checkout,

Triggering Test Checkout2:06

we're going to trigger a new project. So head over to the browser, click checkout, and in this case, we're going to successfully place an order. Fill out the form with some dummy information and use the credit card number 42424242, et cetera, as this will instruct Stripe to fake a payment, which is going to be set as a successful payment. You can enter any year, just make sure that it is in the future and any security code that you want. Next, click pay to complete the checkout.

Inspecting Webhook Payload2:34

and any security code that you want. Next, click pay to complete the checkout. So if we now switch back to our editor and take a look at the tab where we had the stripe listen commands running, you can see all the different webhooks being sent by Stripe, and it includes the entire payload in JSON. So let's copy and paste the JSON from our latest checkout.session.completed event. Let's create a new scratch trial, and let's format the JSON so we can take a look at how our payload looks right at this moment.

Let's create a new scratch trial, and let's format the JSON so we can take a look at how our payload looks right at this moment. So as you can see, it includes a bunch of different information like the subtotal, the amountTotal, whether tax was applied, whether promotionCodes were allowed, the currency of the payment. It contains our customerDetails like our address. It contains our email, our name, phone, our shippingInformation. You can also see the totalAmount, which has been applied as a discount, shipping, or for tax purposes. So we are going to use this webhookData to create an order on our site.

Planning Order Persistence3:31

shipping, or for tax purposes. So we are going to use this webhook data to create an order on our site. So we're going to create a database table, which contains our orders, our order items. And this allows us to, for example, have a dashboard where a customer can see all his or her pending orders. We can use it to send out an email, saying like, hey, thank you for your order. This is everything you've just ordered. So let's move on to the next video.

This is everything you've just ordered. So let's move on to the next video.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟