PHP code example of itsjustmoha / laravel-x402

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

    

itsjustmoha / laravel-x402 example snippets


use Illuminate\Support\Facades\Route;

Route::get('/api/report', ReportController::class)->paid('$0.01');

// Middleware alias with parameters: x402:<price>[,<network>]
Route::post('/api/summarize', SummarizeController::class)->middleware('x402:0.05');

// Per-route options
Route::get('/api/weather', WeatherController::class)->paid('$0.001', [
    'description' => 'Current weather for a city',
    'mime_type' => 'application/json',       // default: application/json
    'network' => 'eip155:84532',             // default: config('x402.network')
    'pay_to' => '0x...',                     // default: config('x402.pay_to')
    'max_timeout_seconds' => 60,             // default: config('x402.max_timeout_seconds')
    'resource' => 'https://api.example.com/api/weather', // default: the request URL
    'discovery' => [/* see "Bazaar discovery" */],
]);

use Itsjustmoha\X402\Core\Facilitator\AuthHeadersProvider;
use Itsjustmoha\X402\Core\Http\HttpRequest;

final class CdpAuthHeaders implements AuthHeadersProvider
{
    public function headersFor(string $operation, HttpRequest $request): array
    {
        // $operation is "verify", "settle" or "supported"; $request has method + URL.
        return ['Authorization' => 'Bearer ' . $this->jwtFor($request->method, $request->url)];
    }
}

Event::listen(PaymentSettled::class, function (PaymentSettled $event) {
    Revenue::record($event->

Route::get('/api/weather', WeatherController::class)->paid('$0.001', [
    'description' => 'Current weather for a city',
    'discovery' => [
        'input' => ['city' => 'Berlin'],                       // example query params (or body for POST/PUT/PATCH)
        'input_schema' => ['properties' => ['city' => ['type' => 'string']], '
text
GET /api/report                          -> 402, PAYMENT-REQUIRED: base64(PaymentRequired)
GET /api/report + PAYMENT-SIGNATURE: ... -> server: POST {facilitator}/verify
                                            your controller runs
                                            server: POST {facilitator}/settle
                                         <- 200 + PAYMENT-RESPONSE: base64(SettlementResponse)