PHP code example of linkthrow / laravel-billing

1. Go to this page and download the library: Download linkthrow/laravel-billing library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

linkthrow / laravel-billing example snippets


use LinkThrow\Billing\CustomerBillableTrait;

class User extends Eloquent
{
    use CustomerBillableTrait;

}

public function subscriptionmodels()
{
    // Return an Eloquent relationship.
    return $this->hasMany('Website');
    
    // Or, return an array or collection of models.
    return Website::where('user_id', $this->id)->get();
    
    // Or, return an array of collections.
    return array(
        Website::where('user_id', $this->id)->get(),
        Domain::where('user_id', $this->id)->get(),
    );
}

use LinkThrow\Billing\SubscriptionBillableTrait;

class Website extends Eloquent
{
    use SubscriptionBillableTrait;

}

public function customermodel()
{
    // Return an Eloquent relationship.
    return $this->belongsTo('User', 'user_id');
    
    // Or, Return an Eloquent model.
    return User::find($this->user_id);
}

'customer_models' => array('User')

'subscription_models' => array('Website')

$user = User::find(1);

$user->billing()->withCardToken('token')->create();

$user->billing()->withCardToken('token')->withCoupon('code')->create();

$user->billing()->withCardToken('token')->create(array(
    'email' => $email,
));

$user->billing()->withCardToken('token')->withCoupon('code')->update();

$user->billing()->delete();

$card = $user->creditcards()->create('credit_card_token');

$card->update(array(
    'exp_month' => '01',
    'exp_year'  => '2017',
));

$card->delete();

// Get all customer credit cards.
$cards = $user->creditcards()->get();

// Get the first card for a customer.
$card = $user->creditcards()->first();

// Find a card by it's ID.
$card = $user->creditcards()->find('card_id');

echo $card->id;
echo "{$card->brand} xxxx-xxxx-xxxx-{$card->last4} Exp: {$card->exp_month}/{$card->exp_year}"

$invoices = $user->invoices()->get();

$invoice = $user->invoices()->first();

$invoice = $user->invoices()->find('invoice_id');

$invoice->id;
$invoice->date;
$invoice->amount;
$invoice->items();
//...

$invoice->render();

if ($user->readyForBilling()) {
    //
}

$user = User::find(1);
$website = Website::find(1);

$user->subscriptions('monthly')->create($website);

$user->subscriptions('monthly')->withCoupon('code')->create($website);

$user->subscriptions('monthly')->withCardToken('token')->create($website);

$user->subscriptions('monthly')->withCard('card_id')->create($website);

$website->subscription('monthly')->create();

$website->subscription('monthly')->withCoupon('code')->withCardToken('token')->create();

protected $cardUpFront = false;

$website->billing_trial_ends_at = Carbon::now()->addDays(14);

$website->save();

$user->billing()->withCardToken('token')->withSubscriptions()->create();

$website->subscription('premium')->swap();

$website->subscription()->increment();

// Add five to the subscription's current quantity...
$website->subscription()->increment(5);

$website->subscription()->decrement();

// Subtract five from the subscription's current quantity...
$website->subscription()->decrement(5);

$website->subscription()->cancel();

$website->subscription('monthly')->resume();

$website->subscription('monthly')->withCardToken('token')->resume();

$user->subscriptions('free-plan')->isFree()->create($website);

$website->subscription('free-plan')->isFree()->swap();

if ($website->subscribed()) {
    //
}

if ($website->billingIsActive()) {
    //
}

if ($website->onTrial()) {
    //
}

if ($website->canceled()) {
    //
}

if ($website->onGracePeriod()) {
    //
}

if ($website->everSubscribed()) {
    //
}

$customer = $website->customer();

$subscriptions = $user->subscriptions()->get();

$charge = $user->charges()->create(499);

$charge = $user->charges()->withCardToken('token')->create(499);

$charge = $user->charges()->withCard('card_id')->create(499);

$charge = $user->charges()->create(499, array('capture' => false));

$charge->capture();

$charge = $user->charges()->create(499, array('capture' => false));

$charge->capture(array('amount' => 399));

$charge->refund();

$charge->refund(array('amount' => 399, 'reason' => '...'));

$charges = $user->charges()->get();

$charge = $user->charges()->first();

$charge = $user->charges()->find('charge_id');

$invoice = $charge->invoice();

// Stripe.
Route::post('stripe/webhook', 'LinkThrow\Billing\Gateways\Stripe\WebhookController@handleWebhook');

// Braintree.
Route::post('braintree/webhook', 'LinkThrow\Billing\Gateways\Braintree\WebhookController@handleWebhook');

class WebhookController extends LinkThrow\Billing\Gateways\Stripe\WebhookController
{
    public function handleChargeDisputeCreated($payload)
    {
        // Handle The Event
    }
}

Website::trialWillEnd(function ($website, $args = array()) {
    Log::info('Trial will end in ' . array_get($args, 'days') . ' day(s).');
});
console
$ php artisan config:publish linkthrow/laravel-billing