Authenticating Stripe CLI0:00
Okay, I assume you have Stripe CLI installed and that you have created a Stripe account. Once you've done this, you can continue with your authentication. So go to your command line and enter stripe login. This will show you a pairing code and a URL that you have to open, and you have to verify that this pairing code matches up with what the browser will instruct you with. So there you go, you can see that our pairing code matches and that we want to give access to our webshop account, and we can simply say allow access. Now if we switch back to the CLI, you can see that it has successfully authenticated and this key is available for 90 days, at which point you will need to re-authenticate. So if you ever run the Stripe CLI command and you get an error, just type in Stripe
Installing Laravel Cashier0:46
and this key is available for 90 days, at which point you will need to re-authenticate. So if you ever run the stripe-cli command and you get an error, just type in stripe-cli login again to re-authenticate. Next we can install Cachier. So you can find the documentation by going to laravel.com and looking for Cachier, or you can go directly to this URL. So Cachier is a first-party package by Laravel that helps to interact with Stripe's API in a much more convenient way, somewhat that you're familiar with when working with Laravel. So let's install the composer dependency. Next we want to run the migrations because Cachier provides a couple of database migrations.
Configuring Billable Model1:27
So let's install the Composer dependency. Next we want to run the migrations because Cachier provides a couple of database migrations which are needed to track subscription and user data. So we can run the migrate command, there we go. Next we can continue by adding or configuring our billable model, in this case this is going to be on a User, and so we can actually follow this example and add the billable trait to our User model. There we go. Next we want to define our API keys, so let's copy and paste this and move over to our .env and copy and paste in our Stripe key secret and webhook secret.
Setting Stripe API Keys2:04
Next we want to define our API keys, so let's copy and paste this and move over to our .env and copy and paste in our STRIPE_KEY and STRIPE_SECRET. Next of course we need to make sure that we use the actual data from our account. So head over to your Stripe account, you can see from the dashboard there's a section here for developers so we can actually copy our key and our secret. The web secret is something we can set later on. So we're going to leave the currency to what we're using which is United States dollars which is the default. If you're using a different currency you can change all these things here. Now Stripe provides a service called Stripe Tax which allows you to automatically calculate.
Enabling Stripe Tax2:46
If you're using a different currency you can change all these things here. Now Stripe provides a service called Stripe Tax which allows you to automatically calculate taxes for invoices generated by Stripe. So we'll probably want to enable this if we have a, so we'll probably want to enable this so we make sure that the correct tax is applied when using Cashier. So we go to our AppServiceProvider and to our boot method we'll add the Cashier::calculateTax method. And as you can see once this is enabled any new subscriptions and one-off invoices that are generated will automatically receive tax calculations. For this feature to work properly your customer billing details such as customer name, address,
Setting Up Webhook Forwarding3:18
are generated will automatically receive tax calculations. For this feature to work properly your customer billing details such as customer name, address, etc. are needed and need to be synced by Stripe and you can see that there's a customer data synchronization and tax ID method offered by Cashier to accomplish this. So what we probably want to set up now are the webhooks. So as mentioned in the documentation as well you can use the Stripe CLI to help with running or receiving webhooks on your local environment. So to ensure that your application can handle Stripe webhooks be sure to configure your webhook URL in the Stripe portal. By default Cashier's webhook control responds to stripe/webhook.
webhook URL in the Stripe portal. By default Cashier's webhook control responds to Stripe slash webhook. If you want you can actually run php artisan cashier:webhook and it will install the necessary webhook for you. In our example we're using our local environment so we don't actually have to run this command we can use the Stripe CLI. So let's switch over to our terminal and enable local webhook forwarding. So to do this we simply type stripe listen, and we're going to use the --forward-to property to set where the webhook should be forwarded to. So that's the local URL for the store.
to set where the webhook should be forwarded to. So that's the local URL for the store. Next we want to use the route which is defined by Cashier which is stripe/webhook. Now for debugging purposes if you want to take a look at the actual request being sent over the webhook we can format the response in JSON to make it a bit more easier to read. So you can see that our webhook is ready to use and our webhook signing secret is the following. So we can simply copy this, move over to our .env file, and let's set our STRIPE_WEBHOOK_SECRET. Okay that's all set.
secret. Okay that's all set.
