tmyers273 / laravel-stripe-billing
Laravel Stripe Billing
Requires
- php: >=7.0
- illuminate/console: ~5.5.0|~5.6.0|~5.7.0|~5.8.0|~6.0|~7.0
- illuminate/database: ~5.5.0|~5.6.0|~5.7.0|~5.8.0|~6.0|~7.0
- illuminate/events: ~5.5.0|~5.6.0|~5.7.0|~5.8.0|~6.0|~7.0
- illuminate/support: ~5.5.0|~5.6.0|~5.7.0|~5.8.0|~6.0|~7.0
- stripe/stripe-php: ^7.39.0
Requires (Dev)
- mockery/mockery: ^1.1
- orchestra/testbench: ^5.0
- phpunit/phpunit: ^8.0
README
版本
- 1.x - Laravel 5.1 -> 5.7
- 2.x - Laravel 5.8+
安装
通过 composer
composer require tmyers273/laravel-stripe-billing
通过 composer 安装包后,运行以下命令:php artisan vendor:publish
,然后从显示的列表中选择提供者:TMyers\StripeBilling\StripeBillingServiceProvider
。这会将迁移文件和配置复制到您的应用程序中。
之后运行迁移:php artisan migrate
用法
将Billable
特性添加到User模型中。
Stripe密钥
为了使用此包,您必须拥有一个Stripe密钥。它必须作为环境变量STRIPE_SECRET
存储,或者设置在config/services.php
中的stripe.secret
模型
- 产品(这是用于控制访问权限的父产品,例如Pro、Gold、Basic、Team等。)
- 价格(这定义了价格和试用期,例如pro_monthly_10、gold_yearly_9999等。)
- 订阅
- 卡
产品模型表示产品访问/权限参数,价格模型可选地属于产品模型,表示价格参数
公共API
StripeBilling静态助手
StripeBilling::createTestToken(); StripeBilling::setApiKey($apiKey); StripeBilling::setCurrency($currency);
Stripe客户
// Create a customer from token (new default card will be created) $user->retrieveOrCreateStripeCustomer($token); $user->retrieveStripeCustomer($token);
订阅
默认情况下,用户可以有多个订阅。但可以通过在config/stripe-billing.php
中将unique_active_subscription
设置为true
来更改此设置。
检查订阅
// Check if user is already subscribed to product // Accepts Price object, Product object, string (name of Product or Price) e.g. basic, basic_yearly_90 $user->isSubscribedTo($product); // Check if user is subscribed to a specific $price $user->isSubscribedStrictlyTo($price); // true or false $user->hasActiveSubscriptions(); // or for subscription // accepts Price|Product|string (name of Product or Price) $subscription->isFor($product);
检索订阅和链式方法
$user->getSubscriptionFor($product)->isActive(); $user->getSubscriptionFor('basic-monthly-10')->cancelNow(); // in the vast majority of cases your users will be only allowed // to have one active subscription, so use this method when applicable $user->getFirstActiveSubscription();
创建产品和订阅
// Create the plans $bronzePlan= Plan::create([ 'description' => 'Bronze Plan', 'name' => 'bronze', ]); // Create the Price $bronzeMonthly = Price::create([ 'product_id' => $bronzePlan->id, // parent plan id 'description' => 'Monthly Bronze Plan', 'name' => 'bronze_monthly_50.00', 'interval' => 'month', 'stripe_product_id' => 'bronze_monthly', // this needs to be created in Stripe first 'price' => 5000, 'active' => true, ]); // Accepts Plan object or string representing Plan name e.g. bronze_monthly_50.00 $user->subscribeTo($bronzeMonthly); // for already existing stripe customer $user->subscribeTo($bronzeMonthly, $token); // for user without created customer
列出订阅
$user->subscriptions; $user->activeSubscriptions;
取消订阅
$subscription->cancelAtPeriodEnd(); $subscription->cancelNow();
恢复订阅
恢复订阅仅在使用宽限期时才可能
$subscription->resume();
延长试用期
延长试用期有两种方式。
$subscription->trialEndAt($unixTimestamp);
$days = 10; $subscription->addDaysToTrial($days);
这将默认按比例计算为false
更改计划
// Accepts Plan object $subscription->changeTo($basicMonthlyPlan);
订阅作用域
Subscription::active()->get(); // get all active subscriptions Subscription::canceledAndArchived()->get(); // get all canceled and non active subscriptions
费用和信用卡
默认卡
$user->defaultCard;
从令牌添加卡
$card = $user->addCardFromToken($token);
这将创建一个Stripe客户,如果用户还没有分配,则默认来源将用于为用户创建默认信用卡,该信用卡将默认用于未来的交易。
如果用户已经为用户创建了Stripe客户并且用户已经有一个默认卡,则新卡将被添加为用户Stripe客户的另一个来源,并在本地数据库中创建一个新的Card记录。
将另一张卡设置为默认卡
$user->setCardAsDefault($card);
该方法默认使用\TMyers\StripeBilling\Models\Card::class
作为参数,并将该卡设置为该用户的默认卡。用户相应的Stripe客户的默认来源也得到更新。
检查用户是否已分配默认卡
$user->hasDefaultCard(); //true or false
或者,您可以传递一个\TMyers\StripeBilling\Models\Card::class
的实例来验证该特定卡是否是用户的默认卡
$user->hasDefaultCard($card); //true or false
移除卡
$user->removeCard($card);
卡辅助方法
$card->isOwnedBy($user); // true or false $card->isDefault(); // true or false
单次费用
当用户已分配默认卡时
$user->charge(2500); // Charge 25$
通过令牌收费
$user->chargeByToken(1799, 'some token from stripe.js'); // Charge 17.99$
通过非默认卡收费
/** * @param int $amount * @param Card $card * @param array $params * @return mixed */ $user->chargeCard(1799, $card); // Charge 17.99$
优惠券
优惠券可以是Stripe/Coupon或现有优惠券的字符串优惠券ID
$user->applyCoupon($coupon);
中间件
在HTTP Kernel.php
中注册
'subscription' => \TMyers\StripeBilling\Middleware\SubscriptionMiddleware::class,
中间件可以接受如下参数:subscription:basic,pro
- 这意味着任何拥有这些订阅的用户都可以通过中间件。当未使用参数时,它将寻找所有活跃的订阅,包括onTrial
或OnGracePeriod
订阅。
Blade指令
subscribed指令用于确定用户是否登录并订阅
@subscribed // do something @endsubscribed
unless_subscribed指令用于确定用户是否登录但没有订阅
@unless_subscribed // do something @endunless_subscribed
配置
'models' => [ 'owner' => 'App\User', 'subscription' => \TMyers\StripeBilling\Models\Subscription::class, 'pricing_plan' => \TMyers\StripeBilling\Models\Price::class, 'plan' => \TMyers\StripeBilling\Models\Plan::class, 'card' => \TMyers\StripeBilling\Models\Card::class, ], 'tables' => [ 'owner' => 'users', 'subscriptions' => 'subscriptions', 'pricing_plans' => 'pricing_plans', 'plans' => 'plans', 'cards' => 'cards', ], 'unique_active_subscription' => false, ];