2017 © Pedro Peláez
 

library laravel-chargebee-v2

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

image

mailforge/laravel-chargebee-v2

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

  • Thursday, August 17, 2017
  • by Mailforge
  • Repository
  • 0 Watchers
  • 0 Stars
  • 335 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 9 Forks
  • 0 Open issues
  • 3 Versions
  • 55 % Grown

The README.md

LaravelChargebee

Latest Version on Packagist ![Software License][ico-license] Build Status ![Total Downloads][ico-downloads], (*1)

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

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., (*3)

Install

If you haven't got an account on Chargebee, create one here., (*4)

Then require the package into your project via Composer:, (*5)

``` bash $ composer require tijmen-wierenga/laravel-chargebee, (*6)


Next, register the service provider in `config/app.php`: ``` php ['providers'] => [ TijmenWierenga\LaravelChargebee\ChargebeeServiceProvider::class ]

Add the LaravelChargebee traits to your model:, (*7)

``` php use TijmenWierenga\LaravelChargebee\Billable; use TijmenWierenga\LaravelChargebee\HandlesWebhooks;, (*8)

class User extends Eloquent {, (*9)

use Billable, HandlesWebhooks;

}, (*10)


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: ``` bash 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., (*11)

There are also a few environment variables that need to be added to the .env-file:, (*12)

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:, (*13)

CHARGEBEE_GATEWAY=stripe

Usage

Creating a new subscription:

You can create subscriptions in multiple ways: * Through Chargebee's Hosted Page * Through Stripe/Braintree's Javascript library, (*14)

Create a subscription with Chargebee's Hosted Page

Retrieve a hosted page URL:, (*15)

``` php $url = $user->subscription($planId)->withAddon($addonId)->getCheckoutUrl($embed);, (*16)


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** ``` php return redirect($url);

Return it as a variable in your view ``` php return view('subscribe')->with(compact($url));, (*17)


Next, render it in your view: ``` html <iframe src="{{ $url }}"></iframe>

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

Chargebee's Hosted Page example, (*19)

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:, (*20)

    // 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: ``` php $user->subscription()->registerFromHostedPage($request->id);, (*21)


The subscription is now registered in both Chargebee and your own application. I coulnd't be easier! #### Example ##### Subscription Controller ``` php <?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: * Stripe * Braintree, (*22)

To create a subscription with Braintree or Stripe you'll have to add a credit card token:, (*23)

``` php $user->subscription($plan)->create($creditcardToken);, (*24)


You can also add add-ons to a subscription: ``` php $user->subscription($plan) ->withAddon($addon) ->create($creditcardToken)

Or redeem a coupon:, (*25)

``` php $user->subscription($plan) ->coupon($coupon) ->create($creditcardToken), (*26)


### Changing plans ``` php // Get the subscription you want to change plans from $subscription = $user->subscriptions->first(); // Change the current plan $subscription->swap($planId);

Cancelling a subscription

``` php $subscription->cancel();, (*27)


## 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, (*28)


To run the PHPUnit tests, run the following composer command from the base directory: ``` bash $ composer run test

Security

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

Credits

License

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

The Versions

17/08 2017

dev-master

9999999-dev https://github.com/TijmenWierenga/LaravelChargebee

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

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel subscription cashier chargebee tijmenwierenga

17/08 2017

v0.3.1

0.3.1.0 https://github.com/TijmenWierenga/LaravelChargebee

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

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel subscription cashier chargebee tijmenwierenga

11/07 2016

v0.3

0.3.0.0 https://github.com/TijmenWierenga/LaravelChargebee

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

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel subscription cashier chargebee tijmenwierenga