PHP code example of 22digital / laravel-cashier-fastspring

1. Go to this page and download the library: Download 22digital/laravel-cashier-fastspring 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/ */

    

22digital / laravel-cashier-fastspring example snippets


'providers' => array(
    // ...
    TwentyTwoDigital\CashierFastspring\CashierServiceProvider::class,
)

use Laravel\Cashier\Billable;

class User extends Authenticatable
{
    use Billable;
}

'fastspring' => [
    'model' => App\User::class,
    'username' => env('FASTSPRING_USERNAME'),
    'password' => env('FASTSPRING_PASSWORD'),
    'store_id' => env('FASTSPRING_STORE_ID'),

    // strongly recommend to set hmac secret in webhook configuration
    // to prevent webhook spoofing
    'hmac_secret' => env('FASTSPRING_HMAC_SECRET')
],

Route::post(
    'fastspring/webhook',
    '\TwentyTwoDigital\CashierFastspring\Http\Controllers\WebhookController@handleWebhook'
)->name('fastspringWebhook');

protected $except = [
    'fastspring/*',
];

protected $listen = [
    // some others
    'TwentyTwoDigital\CashierFastspring\Events\SubscriptionCanceled' => [
        'TwentyTwoDigital\CashierFastspring\Listeners\SubscriptionStateChanged'
    ],
    'TwentyTwoDigital\CashierFastspring\Events\SubscriptionDeactivated' => [
        'TwentyTwoDigital\CashierFastspring\Listeners\SubscriptionDeactivated'
    ],
    'TwentyTwoDigital\CashierFastspring\Events\SubscriptionPaymentOverdue' => [
        'TwentyTwoDigital\CashierFastspring\Listeners\SubscriptionStateChanged'
    ],
    'TwentyTwoDigital\CashierFastspring\Events\OrderCompleted' => [
        'TwentyTwoDigital\CashierFastspring\Listeners\OrderCompleted'
    ],
    'TwentyTwoDigital\CashierFastspring\Events\SubscriptionActivated' => [
        'TwentyTwoDigital\CashierFastspring\Listeners\SubscriptionActivated'
    ],
    'TwentyTwoDigital\CashierFastspring\Events\SubscriptionChargeCompleted' => [
        'TwentyTwoDigital\CashierFastspring\Listeners\SubscriptionChargeCompleted'
    ]
];

// we create session and return it to frontend to care
$builder = Auth::user()->newSubscription('default', $selectedPlan);
$session = $builder->create();

// we create session and return it to frontend to care
$builder = Auth::user()->newSubscription('default', $selectedPlan);
$session = $builder->create();

$builder = Auth::user()->newSubscription('default', $selectedPlan)
    ->withCoupon('free-ticket-to-Mars')
    ->quantity(1); // yeap no ticket for returning
$session = $builder->create();

$apiResponse = Auth::user()->createAsFastspringCustomer();

$apiResponse = Auth::user()->updateAsFastspringCustomer();

if ($user->subscribed('default')) {
    //
}

$subscription = $user->subscription('default');

// check if you should serve or not
$subscription->valid();

// check if its state is active
$subscription->active();

// check if its state is deactived
$subscription->deactivated();

// check if its state is overdue
$subscription->overdue();

// alias: onTrial(). check if its state is trial
$subscription->trial();

// alias: canceled(), onGracePeriod(). check if its state is canceled
$subscription->cancelled();

if ($user->subscribedToPlan('monthly', 'default')) {
    //
}

$user = App\User::find(1);

$user->subscription('default')->swap('provider-plan-id', $prorate, $quantity, $coupons);

$user->subscription('default')->cancel();

$user->subscription('default')->cancelNow();

$user->subscription('default')->resume();

$activePeriod = $user->subscription('default')->activePeriodOrCreate();

// if you don't want to create an active subscription period immediately when no exist
// you can use activePeriod method as below
// you can set a cron job for creation of new periods
// or do it in your way
$activePeriod = $user->subscription('default')->activePeriod();

$redirectURI = $user->accountManagementURI();

protected $listen = [
    // some others
    'TwentyTwoDigital\CashierFastspring\Events\SubscriptionCanceled' => [
        'Your\Lovely\Listener'
    ]
];
bash
php artisan vendor:publish