tmyers273/laravel-stripe-billing
Composer 安装命令:
composer require tmyers273/laravel-stripe-billing
包简介
Laravel Stripe Billing
README 文档
README
Versions
- 1.x - Laravel 5.1 -> 5.7
- 2.x - Laravel 5.8+
Installation
Via composer:
composer require tmyers273/laravel-stripe-billing
After package is installed via composer, run the following command:
php artisan vendor:publish
and then pick Provider: TMyers\StripeBilling\StripeBillingServiceProvider from the displayed list.
This will copy migrations files and config into your app.
Afterwards run the migrations:
php artisan migrate
Usage
Add Billable trait to the User model.
Stripe Secret
In order to use this package you must posses a Stripe Secret Key.
It must be stored as an environment variable STRIPE_SECRET and/or set in config/services.php under stripe.secret
Models
- Product (this is the parent product used to control access rights - e.g. Pro, Gold, Basic, Team etc.)
- Price (this defines price and trial period e.g. pro_monthly_10, gold_yearly_9999 etc.)
- Subscription
- Card
Product model represents product access/privileges parameters Price model optionally belongs to Product model and represents price parameters
Public API
StripeBilling static helper
StripeBilling::createTestToken(); StripeBilling::setApiKey($apiKey); StripeBilling::setCurrency($currency);
Stripe customer
// Create a customer from token (new default card will be created) $user->retrieveOrCreateStripeCustomer($token); $user->retrieveStripeCustomer($token);
Subscriptions
By default users can have multiple subscriptions.
But this can be changed by setting unique_active_subscription to true in config/stripe-billing.php
Check subscription
// 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);
Retrieve subscriptions and chain methods
$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 products and subscriptions
// 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
List subscriptions
$user->subscriptions; $user->activeSubscriptions;
Cancel subscriptions
$subscription->cancelAtPeriodEnd(); $subscription->cancelNow();
Resuming subscription
Resuming subscription is possible only as long as it is on grace period
$subscription->resume();
Extend trial
Extending the trial is done in two ways.
$subscription->trialEndAt($unixTimestamp);
$days = 10; $subscription->addDaysToTrial($days);
This will default prorate to false
Changing plan
// Accepts Plan object $subscription->changeTo($basicMonthlyPlan);
Subscription scopes
Subscription::active()->get(); // get all active subscriptions Subscription::canceledAndArchived()->get(); // get all canceled and non active subscriptions
Charges and credit cards
Default card
$user->defaultCard;
Add card from token
$card = $user->addCardFromToken($token);
This will create a stripe customer if user does not have one assigned yet. The default source of the new Stripe customer will be used to create the default credit card for the user, which will be used by default for future transactions.
If Stripe customer already exists for the user and the user already has a default card, the new card will be added as a new source for the users's Stripe Customer and a new Card record will be created in the local database.
Set another card as default
$user->setCardAsDefault($card);
That method by default takes \TMyers\StripeBilling\Models\Card::class as an argument and makes the card
the default card for that user. User's corresponding Stripe Customer's default source also gets updated.
Check if user already has a default card assigned
$user->hasDefaultCard(); //true or false
Or you can pass an instance of \TMyers\StripeBilling\Models\Card::class to verify
if that particular card is the default one for the user:
$user->hasDefaultCard($card); //true or false
Remove card
$user->removeCard($card);
Card helper methods
$card->isOwnedBy($user); // true or false $card->isDefault(); // true or false
Single charges
When user already has a default card assigned:
$user->charge(2500); // Charge 25$
Charge by token
$user->chargeByToken(1799, 'some token from stripe.js'); // Charge 17.99$
Charge via non-default card
/** * @param int $amount * @param Card $card * @param array $params * @return mixed */ $user->chargeCard(1799, $card); // Charge 17.99$
Coupons
Coupon can be either a Stripe/Coupon or a string coupon ID of an existing coupon
$user->applyCoupon($coupon);
Middleware
Register in HTTP Kernel.php
'subscription' => \TMyers\StripeBilling\Middleware\SubscriptionMiddleware::class,
The middleware can take parameters like so: subscription:basic,pro - that means that
users with any of these subscriptions can pass the middleware. When used without parameters it will
just look for any active including onTrial or OnGracePeriod subscriptions
Blade directives
subscribed directive determines if user is logged in and subscribed
@subscribed // do something @endsubscribed
unless_subscribed directive determines if user is logged in and not subscribed
@unless_subscribed // do something @endunless_subscribed
Config
'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, ];
tmyers273/laravel-stripe-billing 适用场景与选型建议
tmyers273/laravel-stripe-billing 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 804 次下载、GitHub Stars 达 3, 最近一次更新时间为 2018 年 10 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「payments」 「stripe」 「laravel」 「billing」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 tmyers273/laravel-stripe-billing 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tmyers273/laravel-stripe-billing 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 tmyers273/laravel-stripe-billing 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Flexible payment integration for Laravel, lunarphp, supporting multiple payment providers.
Dealing with payments through the Egyptian payment gateway PayMob
Librería para la gestión sencilla de pagos mediante TPV Redsys y Paypal
Rich payment solutions for Laravel framework. Paypal, payex, authorize.net, be2bill, omnipay, recurring paymens, instant notifications and many more
Alfabank REST API integration
统计信息
- 总下载量: 804
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-10-28