PHP code example of revaly / sdk

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

    

revaly / sdk example snippets




use Revaly\Sdk\Core\Model\CreditCard;
use Revaly\Sdk\Core\Model\PaymentMethod;
use Revaly\Sdk\Core\Model\PaymentRequest;
use Revaly\Sdk\Errors\OutcomeUnknownException;
use Revaly\Sdk\Errors\PermanentRejectionException;
use Revaly\Sdk\Errors\TransientFailureException;
use Revaly\Sdk\RapClient;
use Revaly\Sdk\Reconcile\Found;
use Revaly\Sdk\Reconcile\NotFoundYet;
use Revaly\Sdk\Reconcile\RapTransactionOutcome;
use Revaly\Sdk\Reconcile\ReconcilePolicy;

$client = new RapClient(
    // Enablement-issued sandbox-scoped key, sent as `Authorization: ApiKey <key>`.
    // Sandbox and live share the same URL — your key's scope selects the environment
    // (there is no separate sandbox host).
    apiKey: getenv('REVALY_API_KEY'),
);

$request = new PaymentRequest();
$request->setAmount(1999);                     // smallest currency unit (cents)
$request->setCurrency('USD');
$request->setMerchantTransactionId('order-1042-attempt-1'); // credential was
    // used, and it may reflect an Account Updater roll. Treat it as optional; absence
    // proves nothing.
} catch (PermanentRejectionException $e) {
    // Fix or decline — failing over reproduces the same rejection anywhere.
    echo "rejected [{$e->getStatusCode()}]: {$e->getApiError()} (ref {$e->getCorrelationId()})\n";
} catch (TransientFailureException $e) {
    // Definitively not processed — route to your own gateway immediately.
    chargeOnFallbackGateway($request);
} catch (OutcomeUnknownException $e) {
    // May have been processed — reconcile BEFORE acting (double-charge hazard).
    $verdict = $client->reconcile(
        $request->getMerchantTransactionId(),
        new ReconcilePolicy(
            maxAttempts: 5,
            overallBudgetSeconds: 30.0,
            initialDelaySeconds: 1.0,   // doubles each attempt, ±20% jitter
        ),
    );

    if ($verdict instanceof Found) {
        match ($verdict->getOutcome()) {
            RapTransactionOutcome::Approved => acknowledgeSuccess($verdict->getTransaction()),
            RapTransactionOutcome::Declined,
            RapTransactionOutcome::Error => decideOnFallbackPerYourRiskPolicy(),
            // Pending, Unknown, and future outcomes: hold conservatively.
            default => holdAndEscalate($verdict),
        };
    } elseif ($verdict instanceof NotFoundYet) {
        // NOT proof of absence — platform visibility is asynchronous and widest
        // exactly when RAP-core is degraded. Hold and escalate per your policy.
        holdAndEscalate($verdict);
    } else {
        // Default branch — REQUIRED. Verdicts are open for extension: SafeToFailover
        // arrives with platform P-2 as a minor release.
        holdAndEscalate($verdict);
    }
}

use Revaly\Sdk\RapClient;
use Revaly\Sdk\Testing\RapMockTransport;

$mock = new RapMockTransport();
$mock->charge()->returnsNotProcessed503();          // TransientFailure: fail over now
$mock->reconcile('order-1042-attempt-1')
    ->notFoundYet(2)                                // two 404s...
    ->thenFoundApproved();                          // ...then the record appears

$client = new RapClient(apiKey: 'sk-test-synthetic', transport: $mock);

// Drive your handler; then assert what it did:
$requests = $mock->getRequests();                   // recorded, with headers