承接 valentinfily/laravelchargebee 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

valentinfily/laravelchargebee

Composer 安装命令:

composer require valentinfily/laravelchargebee

包简介

A Laravel package which provides an easy way to handle billing and subscriptions.

README 文档

README

Latest Version on Packagist Software License Build Status Total Downloads

A Laravel package which provides an easy way to handle billing and subscriptions by making use of Chargebee's subscription software.

Introduction

LaravelChargebee (LC) is a membership management tool which handles your billing and recurring subscriptions with minimal effort. This easy-to-install package will cover the most important features for a successful subscription system:

  • Create recurring subscriptions in a very flexible way:
    • Add add-ons, coupons or even one-time charges
  • Change existing subscriptions
  • Cancel subscriptions
  • All of this through a fluent API or by using Chargebee's secure hosted pages.

Install

If you haven't got an account on Chargebee, create one here.

Then require the package into your project via Composer:

$ composer require tijmen-wierenga/laravel-chargebee

Next, register the service provider in config/app.php:

['providers'] => [
    TijmenWierenga\LaravelChargebee\ChargebeeServiceProvider::class
]

Add the LaravelChargebee traits to your model:

use TijmenWierenga\LaravelChargebee\Billable;
use TijmenWierenga\LaravelChargebee\HandlesWebhooks;

class User extends Eloquent {
    
    use Billable, HandlesWebhooks;

}

If you want to use the package's routes for handling webhooks, make sure you place the service provider before the Route Service Prodiver (App\Providers\RouteServiceProvider::class).

Next, run the following command in your terminal:

php artisan chargebee:install

This will copy and run the migrations necessary for the package to work. It also copies the configuration file to your config path.

There are also a few environment variables that need to be added to the .env-file:

CHARGEBEE_SITE=your-chargebee-site
CHARGEBEE_KEY=your-chargebee-token

If you want to use a different payment gateway, define your payment gateway details in the .env-file:

CHARGEBEE_GATEWAY=stripe

Usage

Creating a new subscription:

You can create subscriptions in multiple ways:

Create a subscription with Chargebee's Hosted Page

Retrieve a hosted page URL:

$url = $user->subscription($planId)->withAddon($addonId)->getCheckoutUrl($embed);

The $embed variable is a boolean value which describes whether or not you want to embed the hosted page as an i-frame.

You can now choose between redirecting the user to the hosted page, or send it to a view where you can render it:

Redirect

return redirect($url);

Return it as a variable in your view

return view('subscribe')->with(compact($url));

Next, render it in your view:

<iframe src="{{ $url }}"></iframe>

You can fully customize your hosted page on Chargebee, an example is shown below:

Chargebee's Hosted Page example

On success, Chargebee will redirect to their own success page by default, but to register the subscription in our own application, we need to redirect back to our application. To define this redirect, setup a callback route:

    // Define your callback URI's here
    'redirect' => [
        'success' => 'http://chargebee.app/success',
        'cancelled' => null,
    ],

Add the route and make a call to the registerFromHostedPage method from the controller:

$user->subscription()->registerFromHostedPage($request->id);

The subscription is now registered in both Chargebee and your own application. I coulnd't be easier!

Example

Subscription Controller
<?php

namespace App\Http\Controllers;

use App\User;

use App\Http\Requests;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class SubscriptionController extends Controller
{
    public function create()
    {
        // Authenticate a user or create one. Note that this is a dummy login. Not to be used in production.
        $user = User::first();
        Auth::login($user);

        // Create the embedded checkout form and return it to the view.
        $url = $user->subscription('cbdemo_hustle')->withAddon('cbdemo_conciergesupport')->getCheckoutUrl(true);
        return view('subscribe')->with(compact(['url', 'user']));
    }

    public function handleCallback(Request $request)
    {
        // Get the authenticated user. Again, this is dummy code just for demonstration.
        $user = User::first();
        
        // Attach the subscription to the user from the hosted page identifier.
        $user->subscription()->registerFromHostedPage($request->id);

        // Return the user to a success page.
        return view('subscribe')->with(compact(['user']));
    }
}

Subscriptions with Stripe/Braintree

In order to create subscriptions with Stripe and Braintree, you need to make use of their Javascript libraries. More info on subscribing with Stripe and Braintree can be found below:

To create a subscription with Braintree or Stripe you'll have to add a credit card token:

$user->subscription($plan)->create($creditcardToken);

You can also add add-ons to a subscription:

$user->subscription($plan)
    ->withAddon($addon)
    ->create($creditcardToken)

Or redeem a coupon:

$user->subscription($plan)
    ->coupon($coupon)
    ->create($creditcardToken)

Changing plans

// Get the subscription you want to change plans from
$subscription = $user->subscriptions->first();

// Change the current plan
$subscription->swap($planId);

Cancelling a subscription

$subscription->cancel();

Testing

If you want to run the unit tests for this package you need acquire test tokens from Stripe. To be able to fetch a test token create an .env-file in the base directory and add your stripe secret token:

STRIPE_SECRET=your-stripe-secret-key
CHARGEBEE_SITE=your-chargebee-site
CHARGEBEE_KEY=your-chargebee-token
CHARGEBEE_GATEWAY=stripe

To run the PHPUnit tests, run the following composer command from the base directory:

$ composer run test

Security

If you discover any security related issues, please email tijmen@floown.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

valentinfily/laravelchargebee 适用场景与选型建议

valentinfily/laravelchargebee 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 772 次下载、GitHub Stars 达 3, 最近一次更新时间为 2018 年 09 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「subscription」 「laravel」 「chargebee」 「cashier」 「TijmenWierenga」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 valentinfily/laravelchargebee 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 valentinfily/laravelchargebee 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 22
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-09-21