PHP code example of foxen / laravel-cancellation-tokens
1. Go to this page and download the library: Download foxen/laravel-cancellation-tokens 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/ */
foxen / laravel-cancellation-tokens example snippets
use Foxen\CancellationToken\Traits\HasCancellationTokens;
class Booking extends Model
{
use HasCancellationTokens;
}
$plainToken = $booking->createCancellationToken($user);
// Embed in a URL — the route only needs the token
$url = url('/booking/cancel/' . $plainToken);
// Send email containing $url...
use Foxen\CancellationToken\Facades\CancellationToken;
use Foxen\CancellationToken\Exceptions\TokenVerificationException;
Route::get('/booking/cancel/{token}', function (string $token) {
try {
$cancellationToken = CancellationToken::consume($token);
// Access the associated models
$booking = $cancellationToken->cancellable;
$user = $cancellationToken->tokenable;
// Perform the cancellation
$booking->cancel();
return view('booking.cancelled');
} catch (TokenVerificationException $e) {
// $e->reason is a TokenVerificationFailure enum:
// - NotFound — token doesn't exist
// - Expired — token has passed its expiry time
// - Consumed — token has already been used
return match ($e->reason) {
TokenVerificationFailure::Expired => response('This cancellation link has expired.'),
TokenVerificationFailure::Consumed => response('This booking has already been cancelled.'),
TokenVerificationFailure::NotFound => response('Invalid cancellation link.'),
};
}
});
$token = CancellationToken::verify($plainToken);
// $token->cancellable — the booking
// $token->tokenable — the user who requested cancellation
// Token is NOT consumed yet
use Foxen\CancellationToken\Rules\ValidCancellationToken;
class CancelBookingRequest extends FormRequest
{
public function rules(): array
{
return [
'token' => ['
use Foxen\CancellationToken\Rules\ValidCancellationToken;
$rule = new ValidCancellationToken;
// After validation, inspect the failure reason:
$rule->failureReason; // TokenVerificationFailure enum or null
use Foxen\CancellationToken\Facades\CancellationToken;
use Carbon\Carbon;
// Create with default expiry (7 days)
$token = CancellationToken::create($subscription, $admin);
// Create with custom expiry
$token = CancellationToken::create($order, $customer, Carbon::now()->addHours(24));
// Verify without consuming
$cancellationToken = CancellationToken::verify($token);
// Verify and consume (single-use)
$cancellationToken = CancellationToken::consume($token);
use Foxen\CancellationToken\Events\TokenConsumed;
use Foxen\CancellationToken\Events\TokenExpired;
// In a service provider's boot() method:
protected function boot(): void
{
Event::listen(TokenConsumed::class, function (TokenConsumed $event) {
$booking = $event->token->cancellable;
Log::info("Booking {$booking->id} was cancelled.");
});
Event::listen(TokenExpired::class, function (TokenExpired $event) {
// Alert the user that their cancellation link expired
$event->token->tokenable->notify(new CancellationLinkExpired(
$event->token->cancellable
));
});
}
use Illuminate\Support\Facades\Schedule;
Schedule::command('model:prune', [
'--model' => \Foxen\CancellationToken\Models\CancellationToken::class,
])->daily();
Schedule::command('model:prune')->daily();
use Foxen\CancellationToken\Facades\CancellationToken;
use Foxen\CancellationToken\Models\CancellationToken;
it('creates a cancellation token for the booking', function () {
$fake = CancellationToken::fake();
$booking = Booking::make(['id' => 1]);
$user = User::make(['id' => 1]);
$token = CancellationToken::create($booking, $user);
// Assert the token was created for the right models
$fake->assertTokenCreatedFor($booking, $user);
// Or just check the cancellable, ignoring the actor:
// $fake->assertTokenCreatedFor($booking);
});
it('consumes a token', function () {
$fake = CancellationToken::fake();
$booking = Booking::make(['id' => 1]);
$user = User::make(['id' => 1]);
$token = CancellationToken::create($booking, $user);
CancellationToken::consume($token);
$fake->assertTokenConsumed($token);
});
it('does not create tokens unnecessarily', function () {
$fake = CancellationToken::fake();
// No tokens created — assertion passes
$fake->assertNoTokensCreated();
});
use Foxen\CancellationToken\Models\CancellationToken;
// Create a valid, unexpired token
$token = CancellationToken::factory()->create();
// Create a consumed token
$token = CancellationToken::factory()->consumed()->create();
// Create an expired token
$token = CancellationToken::factory()->expired()->create();
// Associate with specific models
$token = CancellationToken::factory()
->for($booking, 'cancellable')
->for($user, 'tokenable')
->create();