PHP code example of preverus / preverus-php

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

    

preverus / preverus-php example snippets


use Preverus\Client;

$client = new Client($_ENV['PREVERUS_SERVER_KEY']);

$decision = $client->decisions()->evaluate([
    'event_type' => 'signup',
    'user_id' => 'acct_42',
    'ip' => $_SERVER['REMOTE_ADDR'] ?? null,
    'risk_session_token' => $_POST['preverus_risk_session_token'] ?? null,
    'fingerprint' => $_POST['preverus_fingerprint'] ?? null,
    'metadata' => [
        'email' => $_POST['email'] ?? null,
        'browser_session_event_id' => $_POST['preverus_browser_session_event_id'] ?? null,
    ],
], visitorId: $_POST['preverus_visitor_id'] ?? null);

if ($decision->isBlock()) {
    http_response_code(403);
    exit('Unable to create account.');
}

if ($decision->isReview()) {
    // Trigger step-up auth, hold the action, or route to manual review.
}

// Continue signup, login, checkout, withdrawal, or payout.

$client = new Client($_ENV['PREVERUS_SERVER_KEY'], [
    'endpoint' => 'https://api.preverus.com',
    'timeout_ms' => 1500,
    'connect_timeout_ms' => 500,
    'retries' => 2,
    'retry_delay_ms' => 150,
    'max_retry_delay_ms' => 1000,
]);

$decision = $client->decisions()->evaluate(
    input: [...],
    visitorId: $visitorId,
    idempotencyKey: 'signup:acct_42:' . bin2hex(random_bytes(16)),
);

$decision = $client->decisions()->evaluate([
    'event_type' => 'withdraw',
    'user_id' => 'acct_42',
    'ip' => '203.0.113.10',
    'risk_session_token' => 'signed_token_from_browser',
    'metadata' => [
        'payment_address' => '0xabc123',
    ],
], visitorId: 'v_abc123');

$decision->recommendedAction(); // allow, review, block, or deny
$decision->isAllow();
$decision->isReview();
$decision->isBlock();
$decision->toArray();

$event = $client->events()->create([
    'event_type' => 'login',
    'user_id' => 'acct_42',
    'ip' => '203.0.113.10',
    'fingerprint' => $fingerprint,
    'metadata' => [
        'email' => '[email protected]',
    ],
], visitorId: 'v_abc123');

$visitor = $client->visitors()->lookup(visitorId: 'v_abc123');
$visitor = $client->visitors()->lookup(fingerprint: 'fp_hash');

$result = $client->metadata()->lookup('email', '[email protected]');
$graph = $client->metadata()->graph('v_abc123');
$userRiskProfile = $client->lookupUserRiskProfile('acct_42');

$valid = $client->webhooks()->verify(
    rawBody: file_get_contents('php://input'),
    timestamp: $_SERVER['HTTP_X_FRAUD_WEBHOOK_TIMESTAMP'] ?? '',
    signatureHeader: $_SERVER['HTTP_X_FRAUD_WEBHOOK_SIGNATURE'] ?? '',
    secret: $_ENV['PREVERUS_WEBHOOK_SECRET'],
);

if (!$valid) {
    http_response_code(400);
    exit('Invalid signature');
}

$event = $client->webhooks()->constructEvent(
    rawBody: file_get_contents('php://input'),
    headers: [
        'X-Fraud-Webhook-Timestamp' => $_SERVER['HTTP_X_FRAUD_WEBHOOK_TIMESTAMP'] ?? '',
        'X-Fraud-Webhook-Signature' => $_SERVER['HTTP_X_FRAUD_WEBHOOK_SIGNATURE'] ?? '',
    ],
    secret: $_ENV['PREVERUS_WEBHOOK_SECRET'],
);

if (alreadyProcessed($event->id())) {
    http_response_code(204);
    exit;
}

$client->webhooks()->dispatch($event, [
    'decision.high_risk' => function ($event) {
        // Open a case, notify ops, or hold the account action.
    },
    '*' => function ($event) {
        // Optional catch-all handler.
    },
]);

use Preverus\Exceptions\ApiException;
use Preverus\Exceptions\NetworkException;

try {
    $decision = $client->decisions()->evaluate([...]);
} catch (ApiException $error) {
    // Authentication, validation, rate limit, or server response.
    $status = $error->statusCode();
    $code = $error->errorCode();
} catch (NetworkException $error) {
    // Timeout, DNS, TLS, or connection failure.
}
bash
composer