danestves/laravel-polar adds Polar checkouts, subscriptions and webhooks to a Laravel app. A Billable trait gives your models methods for checkout, plan changes and the Customer Portal, and the package stores subscriptions and orders in your database from webhooks.
This provider is not maintained or officially supported by Polar. Use at your own discretion. If you have questions about the provider, please contact the project maintainer.
Installation
Step 1: You can install the package via composer:Terminal
:install:
Terminal
Terminal
Terminal
Terminal
Terminal
Usage
Access token
Create an organization access token in Settings → Access Tokens, or runpolar auth token, and add it to .env:
- https://sandbox.polar.sh/dashboard/ORG_SLUG/settings/access-tokens (Sandbox)
- https://polar.sh/dashboard/ORG_SLUG/settings/access-tokens (Production)
Terminal
Webhook secret
Create a webhook endpoint in Settings → Webhooks and copy its secret.- https://sandbox.polar.sh/dashboard/ORG_SLUG/settings/webhooks (Sandbox)
- https://polar.sh/dashboard/ORG_SLUG/settings/webhooks (Production)
order.createdorder.updatedorder.refundedsubscription.createdsubscription.updatedsubscription.activesubscription.canceledsubscription.revokedbenefit_grant.createdbenefit_grant.updatedbenefit_grant.revoked
Terminal
Billable trait
Add theBillable trait to the model that pays, usually User:
Polar script
To use the embedded checkout, include Polar’s script with the@polarEmbedScript directive inside the <head> tag of your views.
Webhooks
This package includes a webhook handler that will handle the webhooks from Polar.Webhooks and CSRF protection
Incoming webhooks should not be affected by CSRF protection. To prevent this, add your webhook path to the except list of yourApp\Http\Middleware\VerifyCsrfToken middleware:
polar/* in your application’s bootstrap/app.php file:
Commands
This package includes a list of commands that you can use to retrieve information about your Polar account.Checkouts
Single payments
To create a checkout to show only a single payment, pass a single items to the array of products when creating the checkout.checkout method.
Custom price
You can override the price of a product using thecharge method.
Embedded checkout
Instead of redirecting the user, create the checkout link, pass it to the page and use the package’s Blade component:Prefill customer information
You can override the user data using the following methods in your models provided by theBillable trait.
Redirects after purchase
You can redirect the user to a custom page after the purchase using thewithSuccessUrl method:
checkout_id={CHECKOUT_ID} query parameter to the URL to retrieve the checkout session id:
Custom metadata and customer metadata
You can add custom metadata to the checkout session using thewithMetadata method:
withCustomerMetadata method:
Reserved keywords
When working with custom data, this library has a few reserved terms.billable_idbillable_typesubscription_type
Customers
Customer Portal
Customers can update their personal information, such as name and email address, in the Customer Portal. To redirect customers to this portal, call theredirectToCustomerPortal() method on your billable model (e.g., the User model).
Orders
Retrieving orders
You can retrieve orders by using theorders relationship on the billable model:
Check order status
You can check the status of an order by using thestatus attribute:
Order model:
Subscriptions
Creating subscriptions
To start a subscription, pass the product ID to a new subscription checkout on your billable model:SubscriptionCreated event webhook connects it to your billable model in the database. You may then get the subscription from your billable model:
Checking subscription status
Once a consumer has subscribed to your services, you can use a variety of methods to check on the status of their subscription. The most basic example is to check if a customer has a valid subscription.valid method:
Cancelled status
To see if a user has cancelled their subscription, you can use the cancelled method:onGracePeriod check.
Past due status
If a recurring payment fails, the subscription will become past due. This indicates that the subscription is still valid, but your customer’s payments will be retried automatically over a 21-day period.Subscription scopes
There are several subscription scopes available for querying subscriptions in specific states:Changing plans
When a consumer is on a monthly plan, they may desire to upgrade to a better plan, alter their payments to an annual plan, or drop to a lower-cost plan. In these cases, you can allow them to swap plans by giving a different product id to theswap method:
swapAndInvoice method instead.
Multiple subscriptions
In certain situations, you may wish to allow your consumer to subscribe to numerous subscription kinds. For example, a gym may provide a swimming and weight lifting subscription. You can let your customers subscribe to one or both. To handle the various subscriptions, you can offer a type of subscription as the second argument when creating a new one:Cancelling subscriptions
To cancel a subscription, call thecancel method.
onGracePeriod method:
cancel method always cancels at the end of the period. To resume a subscription while it’s still in its grace period, use the resume method.
Handling webhooks
Polar can send webhooks to your app, allowing you to react. The package handles most of the work: once the webhook endpoint is configured, it listens for incoming events and updates your database. Subscribe the endpoint to all the events listed above.Webhook events
Danestves\LaravelPolar\Events\BenefitGrantCreatedDanestves\LaravelPolar\Events\BenefitGrantUpdatedDanestves\LaravelPolar\Events\BenefitGrantRevokedDanestves\LaravelPolar\Events\OrderCreatedDanestves\LaravelPolar\Events\OrderRefundedDanestves\LaravelPolar\Events\SubscriptionActiveDanestves\LaravelPolar\Events\SubscriptionCanceledDanestves\LaravelPolar\Events\SubscriptionCreatedDanestves\LaravelPolar\Events\SubscriptionRevokedDanestves\LaravelPolar\Events\SubscriptionUpdated
$model object and an event $payload. The subscription events also include the $subscription object. These can be accessed via the public properties.
If you wish to respond to these events, you must establish listeners for them. For example, you may wish to react when a subscription is updated.
EventServiceProvider:
Testing
Terminal

