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