In this guide, we’ll show you how to integrate Polar with Laravel.
POLAR_API_KEY
environment variable.
You can create an organization access token from your organization settings.
routes/web.php
file:
ProductsController
class in the app/Http/Controllers
directory:
products
view in the resources/views
directory:
/checkout
with a query parameter priceId
. This is the ID of the price that the user will be charged for when they click the “Buy” button. We will configure this route in the next section.
That’s it for the products page. You can now display the products to your users, and they will be able to buy them. Let’s now create the checkout endpoint.
routes/web.php
file:
CheckoutController
class in the app/Http/Controllers
directory:
/checkout?priceId={priceId}
. Just like we did when displaying the products above.
Upon Checkout success, the user will be redirected to the confirmation page.
routes/web.php
file:
ConfirmationController
class in the app/Http/Controllers
directory:
confirmed
until you’ve received a webhook event checkout.updated
with a status set to succeeded
. We’ll cover this in the next section.
your-app.com/api/webhook/polar
. This must be an absolute URL which Polar can reach. If you use ngrok, the URL will look something like this: https://<your-ngrok-id>.ngrok-free.app/api/webhook/polar
.routes/api.php
file and add the following entry:
php artisan migrate
to run the migration.
php artisan queue:listen
to process the jobs.
subscription.active
and subscription.revoked
events accordingly.
The cancellation of a subscription is handled by the subscription.canceled
event. The user has probably canceled their subscription before the end of the
billing period. Do not revoke any kind of access immediately, but rather wait
until the end of the billing period or when you receive the
subscription.revoked
event.
checkout.updated
event and update the UI accordingly when it reaches the succeeded status.