tmyers273/laravel-stripe-billing 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 tmyers273/laravel-stripe-billing 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 804
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 13
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-10-28