PHP code example of adilazhari / laravel-idempotency

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

    

adilazhari / laravel-idempotency example snippets


use AdilAzhari\LaravelIdempotency\Http\Middleware\IdempotencyMiddleware;
use Illuminate\Support\Facades\Route;

Route::post('/payments', CreatePaymentController::class)
    ->middleware(IdempotencyMiddleware::class);

use AdilAzhari\LaravelIdempotency\Queue\Middleware\JobIdempotencyMiddleware;

final class ChargeCustomer implements ShouldQueue
{
    public function __construct(
        private readonly Order $order,
    ) {}

    public function middleware(): array
    {
        return [new JobIdempotencyMiddleware(key: $this->order->id)];
    }

    public function handle(): void
    {
        // Charge the customer. If this job is retried or redelivered with
        // the same key and the same payload, handle() will not run again.
    }
}

use AdilAzhari\LaravelIdempotency\Support\IdempotencyContext;

public function middleware(): array
{
    return [new JobIdempotencyMiddleware(
        key: IdempotencyContext::current() ?? $this->order->id
    )];
}

return [

    'driver' => env('IDEMPOTENCY_STORE', 'cache'),

    'stores' => [

        'cache' => [
            'driver' => 'cache',
        ],

        'array' => [
            'driver' => 'array',
        ],

        'redis' => [
            'driver' => 'redis',
            'connection' => 'default',
            'prefix' => 'idempotency:',
        ],

        'database' => [
            'driver' => 'database',
        ],

    ],

    'header' => 'Idempotency-Key',

    'methods' => ['POST', 'PUT', 'PATCH', 'DELETE'],

    'key_max_length' => 255,

    'replay_header' => 'Idempotency-Replayed',

    'lock' => [
        'seconds' => 10,
    ],

    'expiration' => 86400,

    'jobs' => [
        'driver' => env('IDEMPOTENCY_JOB_STORE', env('IDEMPOTENCY_STORE', 'cache')),
        'expiration' => env('IDEMPOTENCY_JOB_EXPIRATION', 86400),
    ],

];

use AdilAzhari\LaravelIdempotency\Contracts\RequestFingerprinter;

$this->app->bind(
    RequestFingerprinter::class,
    CustomRequestFingerprinter::class,
);

use AdilAzhari\LaravelIdempotency\Contracts\IdempotencyStore;

$this->app->bind(
    IdempotencyStore::class,
    CustomIdempotencyStore::class,
);

use AdilAzhari\LaravelIdempotency\Contracts\IdempotencyLock;

$this->app->bind(
    IdempotencyLock::class,
    CustomIdempotencyLock::class,
);

'redis' => [

    'driver' => 'redis',

    'connection' => 'default',

    'prefix' => 'idempotency:',

],

use Illuminate\Support\Facades\Schedule;

Schedule::command('idempotency:prune')
    ->daily();
bash
php artisan vendor:publish --tag=idempotency-config
bash
php artisan vendor:publish --tag=idempotency-migrations

php artisan migrate
http
POST /payments

{
    "amount":1000
}
bash
php artisan vendor:publish --tag=idempotency-migrations

php artisan migrate
bash
php artisan idempotency:prune