Purchase access to watch this video.

Unlock course for £159

Already purchased access? Login

Installing and configuring Laravel Cashier

  1. Now that we have a Stripe account with a product and a pricing plan created,
  2. we can go ahead and start integrating it into our application.
  3. The first step is to install Cashier.
  4. Cashier is just a package, so we can install it using the Composer dependency
  5. manager.
  6. Open up a command prompt and run sail composer require laraval/cashier
  7. This will install the Cashier package and also add a number of migrations that
  8. need running, so let's migrate our database.
  9. This will add additional columns to our "users" table, as well as create tables
  10. to hold subscription-related data.
  11. Next, we need to add the Billable trait to our User model.
  12. This will add functionality to our User model for working with subscriptions
  13. for a particular customer instance.
  14. Open up your User model, add "Billable" to the list of traits being used.
  15. Next, we need to add our Stripe API keys. We need our publishable key and our
  16. secret key from Stripe.
  17. To find these, go back to your Stripe dashboard, click "Developer" in the top
  18. right, click the "API keys" tab,
  19. Ensure that we are viewing test data, and we need to copy these.
  20. First, copy the publishable key, open up your .env file, and at the bottom,
  21. create a new "STRIPE_KEY" secret,
  22. and paste the value. We need to reveal the secret key, copy its value,
  23. and paste that into a "STRIPE_SECRET" environment variable.
  24. To ensure the correct currency is used, we'll also need to set a couple of
  25. extra Cashier-specific environment variables.
  26. As I created the product and pricing as Pounds in my account, I'll need to set a
  27. "CASHIER_CURRENCY" environment variable and set its value to "gbp".
  28. So let's copy this, add it to our environment file, and change the value to "gbp".
  29. We can also copy this, and we can also set locale value to British English.
  30. A good idea when adding entries to your .env file is to also copy them to an .env.example file.
  31. So let's copy the Stripe and Cashier keys we've just created.
  32. Copy them, open our .env.example file, paste them at the bottom, and then remove
  33. their values.
  34. As this example file will be committed to our Git repository, so we don't want
  35. any live values in it.
  36. However, by adding these keys, a developer cloning the project or setting
  37. up on a new machine
  38. and see exactly what environment variables the application requires to function.
  39. Now that we've set up Stripe and installed and configured Cashier, we can move
  40. on to create subscriptions and charge customers for access.

Resources

Connect GitHub