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
$checkout = $user->checkout('price_id');

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

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

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

echo $subscription->name;            // Get subscription name
echo $subscription->price;           // Get formatted price (e.g., "10.00 USD")
echo $subscription->interval;        // Get billing interval (e.g., "month")
echo $subscription->description;     // Get subscription description

// Check subscription status
if ($subscription->active()) {
    // Subscription is usable if any of:
    // - Status is active
    // - Currently on grace period after cancellation
}

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

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

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

$subscription->cancel();                    // Cancel subscription (End of period)
$subscription->change('new_price_id');      // Change subscription plan
$subscription->onGracePeriod();             // Check if scheduled for cancellation
$subscription->cancelled();                 // Check if cancelled
$subscription->active();                    // Check if subscription is active

// 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";
}

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 all orders for a user
$orders = $user->orders();

// Get orders with filters
$orders = $user->orders([
    'status' => 'completed',
    // other filters...
]);

// Get invoice URL for specific order
$invoiceUrl = $user->getInvoice('order_id');

// EventServiceProvider.php
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