PHP code example of mafrasil / cashier-polar

1. Go to this page and download the library: Download mafrasil/cashier-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/ */

    

mafrasil / cashier-polar example snippets


use Mafrasil\CashierPolar\Concerns\Billable;

class User extends Authenticatable
{
    use Billable;
}

// Simple checkout with a product ID
$checkout = $user->checkout('product_id');

// With options
$checkout = $user->checkout('product_id', [
    'success_url' => route('checkout.success'),
    'metadata' => [
        'custom_field' => 'value'
    ]
]);

// Redirect to checkout
return redirect($checkout['url']);

// Get subscription details
$subscription = $user->subscription;

echo $subscription->name;            // Product name (e.g., "Pro")
echo $subscription->price;           // Formatted price (e.g., "15.00 USD")
echo $subscription->interval;        // Billing interval (e.g., "month")
echo $subscription->description;     // Product description
echo $subscription->product_id;      // Polar product ID

// Check subscription status
if ($subscription->active()) { // or $user->subscribed()
    // Subscription is usable if any of:
    // - Status is active or trialing
    // - Cancelled but on trial or grace period
}

// Subscription has been cancelled and on grace period (within billing period)
if ($subscription->onGracePeriod()) {}

// Subscription has been cancelled and grace period has ended
if ($subscription->cancelled()) {}

// Subscription has ended
if ($subscription->ended()) {}

// Period information
echo $subscription->currentPeriod();  // "2024-01-01 to 2024-02-01"
if ($subscription->withinPeriod()) {}

// Check specific product
if ($subscription->hasProduct('product_id')) {}

// Check specific price plan
if ($subscription->hasPlan('price_id')) {}

// Payment issues
if ($subscription->hasPaymentIssue()) {}

// Cancel at end of billing period
$subscription->cancel();

// Resume a cancelled subscription (before period ends)
$subscription->resume();

// Revoke immediately (cancel right now)
$subscription->revoke();

// Change to a different product
$subscription->change('new_product_id');

// Example usage
if ($subscription->onGracePeriod()) {
    echo "Subscription will cancel on " . $subscription->ends_at->format('Y-m-d');
} elseif ($subscription->cancelled()) {
    echo "Subscription has been cancelled";
} else {
    echo "Active " . $subscription->interval . " subscription";
}

// Check if subscription is on trial
if ($subscription->onTrial()) {
    echo "Trial ends on " . $subscription->trialEndDate(); // "2024-01-15"
    echo "Days left: " . $subscription->days_until_trial_ends;
}

// Check if trial has expired
if ($subscription->hasExpiredTrial()) {}

// End trial early and start paying immediately
$subscription->endTrial();

// Cancel during trial (won't convert to paid when trial ends)
$subscription->cancel();

// Revoke during trial (ends immediately)
$subscription->revoke();

// Trial dates
$subscription->trialStart();    // Carbon instance
$subscription->trialEnd();      // Carbon instance
$subscription->trialEndDate();  // "2024-01-15"

use Mafrasil\CashierPolar\Facades\CashierPolar;

// Get all products
$products = CashierPolar::products();

// Get specific product
$product = CashierPolar::product('product_id');

// Get products with filters
$products = CashierPolar::products([
    'is_archived' => true,
    // other filters...
]);

// Get paid invoices via the customer portal API
// Returns only paid orders with rich data: product, subscription,
// line items, invoice_number, is_invoice_generated, billing_reason, etc.
$invoices = $user->invoices();

// Filter invoices
$invoices = $user->invoices([
    'product_id' => 'product_id',
    'product_billing_type' => 'recurring', // or 'one_time'
    'subscription_id' => 'subscription_id',
    'query' => 'Pro Plan',
    'page' => 1,
    'limit' => 10,
    'sorting' => ['-created_at'],
]);

// Get all orders via the server-side API (ons()->paid()->get();

// Get the Polar customer portal URL for the user
$portalUrl = $user->customerPortalUrl();

// With a return URL (shows a back button in the portal)
$portalUrl = $user->customerPortalUrl(route('dashboard'));

// Redirect to the portal
return redirect($portalUrl);

'path' => 'custom/webhook/path'

use Mafrasil\CashierPolar\Events\SubscriptionCreated;
use Mafrasil\CashierPolar\Events\SubscriptionCanceled;
use Mafrasil\CashierPolar\Events\OrderCreated;

Event::listen(function (SubscriptionCreated $event) {
    $subscription = $event->subscription;
    $user = $subscription->billable;
    // Handle event
});
bash
composer  vendor:publish --tag="cashier-polar-migrations"
php artisan migrate

php artisan vendor:publish --tag="cashier-polar-config"
bash
php artisan cashier-polar:webhook-secret