mafrasil/cashier-polar
Composer 安装命令:
composer require mafrasil/cashier-polar
包简介
Laravel Cashier integration for Polar.sh subscription billing services
README 文档
README
Disclaimer
Note: This is not an official Laravel package. This is a community-built package following Laravel Cashier principles.
Introduction
Cashier Polar provides an expressive, fluent interface to Polar's subscription billing services. It handles almost all of the boilerplate subscription billing code you are dreading writing.
Table of Contents
Requirements
- PHP 8.3+
- Laravel 10.0+ / 11.0+
- Polar account and API credentials (https://polar.sh)
Installation
composer require mafrasil/cashier-polar php artisan vendor:publish --tag="cashier-polar-migrations" php artisan migrate php artisan vendor:publish --tag="cashier-polar-config"
Configuration
POLAR_API_KEY=your-api-key POLAR_ORGANIZATION_ID=your-organization-id POLAR_WEBHOOK_SECRET=your-webhook-secret POLAR_SANDBOX=true # Set to false for production
Generate a webhook secret or grab it from your Polar Webhook settings:
php artisan cashier-polar:webhook-secret
Basic Usage
Setup Billable Model
use Mafrasil\CashierPolar\Concerns\Billable; class User extends Authenticatable { use Billable; }
Create a Checkout Session
// 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']);
Access Subscriptions
// 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()) {}
Manage Subscriptions
// 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"; }
Trials
Trials are configured on your products in the Polar dashboard. When a customer subscribes to a product with a trial, the subscription will have trialing status.
// 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"
The active() method returns true for trialing subscriptions, so $user->subscribed() works seamlessly whether the user is on a trial or a paid plan.
Products and Pricing
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... ]);
Orders and Invoices
In Polar, orders represent payments/invoices. Each time a customer pays (initial purchase, subscription renewal, plan change), an order is created.
// 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 (includes all statuses) $orders = $user->orders(); // Generate an invoice PDF for an order (must be called before retrieving) // This triggers Polar to create the invoice PDF — returns 202 (accepted) $user->generateInvoice('order_id'); // Get invoice PDF URL for a specific order // The invoice must be generated first, otherwise this returns null $invoiceUrl = $user->getInvoice('order_id'); // Access local transaction records (stored via webhooks) $transactions = $user->transactions()->get(); // Only completed/paid local transactions $transactions = $user->transactions()->paid()->get();
Important: Polar requires invoices to be generated before they can be retrieved. Call
generateInvoice()first, wait a few seconds, then callgetInvoice(). For a reliable approach, listen to theorder.updatedwebhook and check theis_invoice_generatedfield before retrieving.
Customer Portal
// 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);
Webhook Events
The package automatically handles these webhook events:
checkout.createdcheckout.updatedorder.createdsubscription.createdsubscription.updatedsubscription.activesubscription.revokedsubscription.canceled
Webhook Configuration
By default, Cashier Polar listens for webhooks at /webhooks/polar. You can customize this path in your config/cashier-polar.php configuration file:
'path' => 'custom/webhook/path'
Make sure to configure your webhook URL in your Polar dashboard to match your application's webhook endpoint:
- URL:
https://your-domain.com/webhooks/polar(or your custom path) - Secret: Use the value from your
POLAR_WEBHOOK_SECRETenvironment variable
The package automatically validates webhook signatures to ensure they come from Polar.
Local Testing with Ngrok
For local development, you can use Ngrok to create a secure tunnel to your local application:
ngrok http 8000
Then use the generated Ngrok URL (e.g., https://random-string.ngrok.io/webhooks/polar) as your webhook endpoint in the Polar dashboard.
Listen for Events
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 });
Testing
composer test
Credits
License
The MIT License (MIT). Please see License File for more information.
mafrasil/cashier-polar 适用场景与选型建议
mafrasil/cashier-polar 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 188 次下载、GitHub Stars 达 5, 最近一次更新时间为 2024 年 12 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「billing」 「subscriptions」 「polar」 「mafrasil」 「cashier-polar」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mafrasil/cashier-polar 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mafrasil/cashier-polar 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mafrasil/cashier-polar 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel Cashier provides an expressive, fluent interface to Braintree's subscription billing services.
Metamel Addresses is a polymorphic Laravel package, for address book management. You can add addresses to any eloquent model with ease.
Handling Appstore server to server notifications
A package provide pricing plans for Laravel.
A PHP (and Laravel) package to interface with the Snipcart api.
统计信息
- 总下载量: 188
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 30
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-12-21