Integrate Polar with Laravel
In this guide, we’ll show you how to integrate Polar with Laravel.
Consider following this guide while using the Polar Sandbox Environment. This will allow you to test your integration without affecting your production data.
Polar Laravel Example App
We’ve created a simple example Laravel application that you can use as a reference.
Setting up environment variables
Polar API Key
To authenticate with Polar, you need create an access token, and supply it to Laravel using a POLAR_API_KEY
environment variable.
You can create an organization access token from your organization settings.
Fetching Polar Products for display
Creating the Products Controller
Go ahead and add the following entry in your routes/web.php
file:
Next up, create the ProductsController
class in the app/Http/Controllers
directory:
Displaying Products
Finally, create the products
view in the resources/views
directory:
Notice that we create a link to /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.
Generating Polar Checkout Sessions
This endpoint will be responsible for creating a new checkout session, redirecting the user to the Polar Checkout page & redirect back to a configured confirmation page.
Go ahead and create a new entry in your routes/web.php
file:
Next, create the CheckoutController
class in the app/Http/Controllers
directory:
We can now easily create a checkout session & redirect there by creating a link to /checkout?priceId={priceId}
. Just like we did when displaying the products above.
Upon Checkout success, the user will be redirected to the confirmation page.
Creating the Confirmation Page
Create a new entry in your routes/web.php
file:
Next, create the ConfirmationController
class in the app/Http/Controllers
directory:
The checkout is not considered “successful” yet however. It’s initially marked as confirmed
until you’ve received a webhook event checkout.updated
with a status set to succeeded
. We’ll cover this in the next section.
Handling Polar Webhooks
Polar can send you events about various things happening in your organization. This is very useful for keeping your database in sync with Polar checkouts, orders, subscriptions, etc.
Configuring a webhook is simple. Head over to your organization’s settings page and click on the “Add Endpoint” button to create a new webhook.
Tunneling webhook events to your local development environment
If you’re developing locally, you can use a tool like ngrok to tunnel webhook events to your local development environment. This will allow you to test your webhook handlers without deploying them to a live server.
Run the following command to start an ngrok tunnel:
Add Webhook Endpoint
- Point the Webhook to
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
. - Select which events you want to be notified about. You can read more about the available events in the Events section.
- Generate a secret key to sign the requests. This will allow you to verify that the requests are truly coming from Polar.
- Add the secret key to your environment variables.
Setting up the Webhook handler
First, we need to install the standard-webhooks package to properly decode the incoming webhook payloads.
Go and add a routes/api.php
file and add the following entry:
Make sure that it is included in the Bootstrap file.
We will use Spatie’s Webhook Client to handle the webhook events. It will automatically verify the signature of the requests, and dispatch the payload to a job queue for processing.
Let’s publish the config:
This will create a new file called webhook-client.php in the config folder.
We need to adjust it to properly verify the signature of the requests.
Preparing the database
By default, all webhook calls get saved into the database. So, we need to publish the migration that will hold the records. So run:
This will create a new migration file in the “database/migration” folder.
Then run php artisan migrate
to run the migration.
Setting up the queue system
Before we set up our job handler — let’s set up our queue system
Go to your “.env” file and set the QUEUE_CONNECTION=database — you can decide to use other connections like redis.
Let’s create our jobs table by running php artisan queue:table and then run the migration using php artisan migrate.
Create the Handlers
The next thing we do is to create a folder named Handler inside the app folder. Then inside this app/Handler, create two files which are
- PolarSignature.php
- ProcessWebhook.php
Inside app/Handler/PolarSignature.php, what we want to do is to validate that the request came from Polar. Add the code to that file.
Great. So the other file app/Handler/ProcessWebhook.php extends the ProcessWebhookJob class which holds the WebhookCall variables containing each job’s detail.
Our application is ready to receive webhook requests.
Don’t forget to run php artisan queue:listen
to process the jobs.
Tips
If you’re keeping track of active and inactive subscriptions in your database, make sure to handle the 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.
Notifying the client about the event
If you’re building a real-time application, you might want to notify the client about the event. On the confirmation-page, you can listen for the checkout.updated
event and update the UI accordingly when it reaches the succeeded status.
Polar Laravel Example App
We’ve created a simple example Laravel application that you can use as a reference
If you have issues or need support, feel free to join our Discord.
Was this page helpful?