Installing and configuring Laravel Cashier
Transcript was automatically generated and may be inaccurate.
-
–
Now that we have a Stripe account with a product and a pricing plan created, -
–
we can go ahead and start integrating it into our application. -
–
The first step is to install Cashier. -
–
Cashier is just a package, so we can install it using the Composer dependency -
–
manager. -
–
Open up a command prompt and run sail composer require laraval/cashier -
–
This will install the Cashier package and also add a number of migrations that -
–
need running, so let's migrate our database. -
–
This will add additional columns to our "users" table, as well as create tables -
–
to hold subscription-related data. -
–
Next, we need to add the Billable trait to our User model. -
–
This will add functionality to our User model for working with subscriptions -
–
for a particular customer instance. -
–
Open up your User model, add "Billable" to the list of traits being used. -
–
Next, we need to add our Stripe API keys. We need our publishable key and our -
–
secret key from Stripe. -
–
To find these, go back to your Stripe dashboard, click "Developer" in the top -
–
right, click the "API keys" tab, -
–
Ensure that we are viewing test data, and we need to copy these. -
–
First, copy the publishable key, open up your .env file, and at the bottom, -
–
create a new "STRIPE_KEY" secret, -
–
and paste the value. We need to reveal the secret key, copy its value, -
–
and paste that into a "STRIPE_SECRET" environment variable. -
–
To ensure the correct currency is used, we'll also need to set a couple of -
–
extra Cashier-specific environment variables. -
–
As I created the product and pricing as Pounds in my account, I'll need to set a -
–
"CASHIER_CURRENCY" environment variable and set its value to "gbp". -
–
So let's copy this, add it to our environment file, and change the value to "gbp". -
–
We can also copy this, and we can also set locale value to British English. -
–
A good idea when adding entries to your .env file is to also copy them to an .env.example file. -
–
So let's copy the Stripe and Cashier keys we've just created. -
–
Copy them, open our .env.example file, paste them at the bottom, and then remove -
–
their values. -
–
As this example file will be committed to our Git repository, so we don't want -
–
any live values in it. -
–
However, by adding these keys, a developer cloning the project or setting -
–
up on a new machine -
–
and see exactly what environment variables the application requires to function. -
–
Now that we've set up Stripe and installed and configured Cashier, we can move -
–
on to create subscriptions and charge customers for access.