PHP code example of bgultekin / cashier-fastspring

1. Go to this page and download the library: Download bgultekin/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/ */

    

bgultekin / cashier-fastspring example snippets


'providers' => array(
    // ...
    Bgultekin\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',
    '\Bgultekin\CashierFastspring\Http\Controllers\WebhookController@handleWebhook'
)->name('fastspringWebhook');

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

protected $listen = [
    // some others
    'Bgultekin\CashierFastspring\Events\SubscriptionCanceled' => [
        'Bgultekin\CashierFastspring\Listeners\SubscriptionStateChanged'
    ],
    'Bgultekin\CashierFastspring\Events\SubscriptionDeactivated' => [
        'Bgultekin\CashierFastspring\Listeners\SubscriptionDeactivated'
    ],
    'Bgultekin\CashierFastspring\Events\SubscriptionPaymentOverdue' => [
        'Bgultekin\CashierFastspring\Listeners\SubscriptionStateChanged'
    ],
    'Bgultekin\CashierFastspring\Events\OrderCompleted' => [
        'Bgultekin\CashierFastspring\Listeners\OrderCompleted'
    ],
    'Bgultekin\CashierFastspring\Events\SubscriptionActivated' => [
        'Bgultekin\CashierFastspring\Listeners\SubscriptionActivated'
    ],
    'Bgultekin\CashierFastspring\Events\SubscriptionChargeCompleted' => [
        'Bgultekin\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
    'Bgultekin\CashierFastspring\Events\SubscriptionCanceled' => [
        'Your\Lovely\Listener'
    ]
];
bash
php artisan vendor:publish