
Polar Laravel Example App
We’ve created a simple example Laravel application that you can use as a reference. View Code on GitHubSetting up environment variables
Polar API Key
To authenticate with Polar, you need to create an access token, and supply it to Laravel using aPOLAR_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 yourroutes/web.php
file:
ProductsController
class in the app/Http/Controllers
directory:
Displaying Products
Finally, create theproducts
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.
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 yourroutes/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.
Creating the Confirmation Page
Create a new entry in yourroutes/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.
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.routes/api.php
file and add the following entry:
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: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
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 thesubscription.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 thecheckout.updated
event and update the UI accordingly when it reaches the succeeded status.