1. Go to this page and download the library: Download danestves/laravel-polar library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
danestves / laravel-polar example snippets
return [
/*
|--------------------------------------------------------------------------
| Polar Access Token
|--------------------------------------------------------------------------
|
| The Polar access token is used to authenticate with the Polar API.
| You can find your access token in the Polar dashboard > Settings
| under the "Developers" section.
|
*/
'access_token' => env('POLAR_ACCESS_TOKEN'),
/*
|--------------------------------------------------------------------------
| Polar Webhook Secret
|--------------------------------------------------------------------------
|
| The Polar webhook secret is used to verify that the webhook requests
| are coming from Polar. You can find your webhook secret in the Polar
| dashboard > Settings > Webhooks on each registered webhook.
|
| We (the developers) recommend using a single webhook for all your
| integrations. This way you can use the same secret for all your
| integrations and you don't have to manage multiple webhooks.
|
*/
'webhook_secret' => env('POLAR_WEBHOOK_SECRET'),
/*
|--------------------------------------------------------------------------
| Polar Url Path
|--------------------------------------------------------------------------
|
| This is the base URI where routes from Polar will be served
| from. The URL built into Polar is used by default; however,
| you can modify this path as you see fit for your application.
|
*/
'path' => env('POLAR_PATH', 'polar'),
/*
|--------------------------------------------------------------------------
| Default Redirect URL
|--------------------------------------------------------------------------
|
| This is the default redirect URL that will be used when a customer
| is redirected back to your application after completing a purchase
| from a checkout session in your Polar account.
|
*/
'redirect_url' => null,
/*
|--------------------------------------------------------------------------
| Currency Locale
|--------------------------------------------------------------------------
|
| This is the default locale in which your money values are formatted in
| for display. To utilize other locales besides the default "en" locale
| verify you have to have the "intl" PHP extension installed on the system.
|
*/
'currency_locale' => env('POLAR_CURRENCY_LOCALE', 'en'),
];
use Danestves\LaravelPolar\Billable;
class User extends Authenticatable
{
use Billable;
}
use Illuminate\Http\Request;
Route::get('/customer-portal', function (Request $request) {
return $request->user()->redirectToCustomerPortal();
});
$url = $user->customerPortalUrl();
$order->status;
$order->paid();
if ($order->hasProduct('product_id_123')) {
// ...
}
if ($order->hasPrice('price_id_123')) {
// ...
}
if ($user->hasPurchasedProduct('product_id_123')) {
// ...
}
if ($user->hasPurchasedPrice('price_id_123')) {
// ...
}
use Illuminate\Http\Request;
Route::get('/subscribe', function (Request $request) {
return $request->user()->subscribe('product_id_123');
});
$subscription = $user->subscription();
if ($user->subscribed()) {
// ...
}
if ($user->subscription()->valid()) {
// ...
}
if ($user->subscription()->hasProduct('product_id_123')) {
// ...
}
if ($user->subscription()->hasPrice('price_id_123')) {
// ...
}
if ($user->subscribedToPrice('price_id_123')) {
// ...
}
if ($user->subscribed('swimming')) {
// ...
}
if ($user->subscribedToPrice('price_id_123', 'swimming')) {
// ...
}
if ($user->subscription()->cancelled()) {
// ...
}
if ($user->subscription()->onGracePeriod()) {
// ...
}
if ($user->subscription()->pastDue()) {
// ...
}
// Get all active subscriptions...
$subscriptions = Subscription::query()->active()->get();
// Get all of the cancelled subscriptions for a specific user...
$subscriptions = $user->subscriptions()->cancelled()->get();
if ($user->subscription()->onGracePeriod()) {
// ...
}
$user->subscription()->resume();
namespace App\Listeners;
use Danestves\LaravelPolar\Events\WebhookHandled;
class PolarEventListener
{
/**
* Handle received Polar webhooks.
*/
public function handle(WebhookHandled $event): void
{
if ($event->payload['type'] === 'subscription.updated') {
// Handle the incoming event...
}
}
}
namespace App\Providers;
use App\Listeners\PolarEventListener;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Danestves\LaravelPolar\Events\WebhookHandled;
class EventServiceProvider extends ServiceProvider
{
protected $listen = [
WebhookHandled::class => [
PolarEventListener::class,
],
];
}