PHP code example of gr4vy / gr4vy-php

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

    

gr4vy / gr4vy-php example snippets


declare(strict_types=1);

h;

// Loaded the key from a file, env variable, 
// or anywhere else
$privateKey = "..."; 

$sdk = Gr4vy\SDK::builder()
    ->setId('example')
    ->setServer('sandbox')
    ->setSecuritySource(Auth::withToken($privateKey))
    ->setMerchantAccountId('default')
    ->build();

$response = $sdk->transactions->list();

if ($response->transactions !== null) {
    // handle response
}

use Gr4vy\Auth;
$token = Auth::getToken($privateKey),

use Gr4vy;
use Gr4vy\Auth;

// Loaded the key from a file, env variable, 
// or anywhere else
$privateKey = "..."; 

$sdk = Gr4vy\SDK::builder()
    ->setId('example')
    ->setServer('sandbox')
    ->setSecuritySource(Auth::withToken($privateKey))
    ->setMerchantAccountId('default')
    ->build();

$response = $sdk->checkout_sessions.create();

$token = $token = Auth::getEmbedToken(
    privateKey: $privateKey,
    expiresIn: '+1 hour',
    checkoutSessionId: $response->checkoutSession->id,
);

$sdk = Gr4vy\SDK::builder()
    ->setId('example')
    ->setServer('sandbox')
    ->setSecuritySource(Auth::withToken($privateKey))
    ->setMerchantAccountId('your-merchant-account-id')
    ->build();

use Gr4vy;

// Webhook payload and headers
$payload = "your-webhook-payload";
$secret = "your-webhook-secret";
$signatureHeader = "signatures-from-header";
$timestampHeader = "timestamp-from-header";
$timestampTolerance = 300; // optional, in seconds (default: 0)

try {
    Webhooks::verifyWebhook(
        secret: $secret$,
        payload: $payload,
        signatureHeader: $signatureHeader$,
        timestampHeader: $timestampHeader,
        timestampTolerance: $timestampTolerance
    );
}
catch(Throwable $th$) {
    // handle the exception
}

declare(strict_types=1);

\SDK::builder()
    ->setSecurity(
        '<YOUR_BEARER_TOKEN_HERE>'
    )
    ->setMerchantAccountId('default')
    ->build();

$accountUpdaterJobCreate = new Gr4vy\AccountUpdaterJobCreate(
    paymentMethodIds: [
        'ef9496d8-53a5-4aad-8ca2-00eb68334389',
        'f29e886e-93cc-4714-b4a3-12b7a718e595',
    ],
);

$response = $sdk->accountUpdater->jobs->create(
    accountUpdaterJobCreate: $accountUpdaterJobCreate
);

if ($response->accountUpdaterJob !== null) {
    // handle response
}

declare(strict_types=1);

\SDK::builder()
    ->setMerchantAccountId('default')
    ->setSecurity(
        '<YOUR_BEARER_TOKEN_HERE>'
    )
    ->build();



$response = $sdk->merchantAccounts->get(
    merchantAccountId: 'merchant-12345'
);

if ($response->merchantAccount !== null) {
    // handle response
}

declare(strict_types=1);

\SDK::builder()
    ->setMerchantAccountId('default')
    ->setSecurity(
        '<YOUR_BEARER_TOKEN_HERE>'
    )
    ->build();

$request = new Gr4vy\ListBuyersRequest(
    cursor: 'ZXhhbXBsZTE',
    search: 'John',
    externalIdentifier: 'buyer-12345',
);

$responses = $sdk->buyers->list(
    request: $request
);


foreach ($responses as $response) {
    if ($response->statusCode === 200) {
        // handle response
    }
}

declare(strict_types=1);

ls\Retry;

$sdk = Gr4vy\SDK::builder()
    ->setMerchantAccountId('default')
    ->setSecurity(
        '<YOUR_BEARER_TOKEN_HERE>'
    )
    ->build();

$request = new Gr4vy\ListBuyersRequest(
    cursor: 'ZXhhbXBsZTE',
    search: 'John',
    externalIdentifier: 'buyer-12345',
);

$responses = $sdk->buyers->list(
    request: $request,
    options: Utils\Options->builder()->setRetryConfig(
        new Retry\RetryConfigBackoff(
            initialInterval: 1,
            maxInterval:     50,
            exponent:        1.1,
            maxElapsedTime:  100,
            retryConnectionErrors: false,
        ))->build()
);


foreach ($responses as $response) {
    if ($response->statusCode === 200) {
        // handle response
    }
}

declare(strict_types=1);

ls\Retry;

$sdk = Gr4vy\SDK::builder()
    ->setRetryConfig(
        new Retry\RetryConfigBackoff(
            initialInterval: 1,
            maxInterval:     50,
            exponent:        1.1,
            maxElapsedTime:  100,
            retryConnectionErrors: false,
        )
  )
    ->setMerchantAccountId('default')
    ->setSecurity(
        '<YOUR_BEARER_TOKEN_HERE>'
    )
    ->build();

$request = new Gr4vy\ListBuyersRequest(
    cursor: 'ZXhhbXBsZTE',
    search: 'John',
    externalIdentifier: 'buyer-12345',
);

$responses = $sdk->buyers->list(
    request: $request
);


foreach ($responses as $response) {
    if ($response->statusCode === 200) {
        // handle response
    }
}

declare(strict_types=1);

ors;

$sdk = Gr4vy\SDK::builder()
    ->setMerchantAccountId('default')
    ->setSecurity(
        '<YOUR_BEARER_TOKEN_HERE>'
    )
    ->build();

try {
    $accountUpdaterJobCreate = new Gr4vy\AccountUpdaterJobCreate(
        paymentMethodIds: [
            'ef9496d8-53a5-4aad-8ca2-00eb68334389',
            'f29e886e-93cc-4714-b4a3-12b7a718e595',
        ],
    );

    $response = $sdk->accountUpdater->jobs->create(
        accountUpdaterJobCreate: $accountUpdaterJobCreate
    );

    if ($response->accountUpdaterJob !== null) {
        // handle response
    }
} catch (errors\Error400Throwable $e) {
    // handle $e->$container data
    throw $e;
} catch (errors\Error401Throwable $e) {
    // handle $e->$container data
    throw $e;
} catch (errors\Error403Throwable $e) {
    // handle $e->$container data
    throw $e;
} catch (errors\Error404Throwable $e) {
    // handle $e->$container data
    throw $e;
} catch (errors\Error405Throwable $e) {
    // handle $e->$container data
    throw $e;
} catch (errors\Error409Throwable $e) {
    // handle $e->$container data
    throw $e;
} catch (errors\HTTPValidationErrorThrowable $e) {
    // handle $e->$container data
    throw $e;
} catch (errors\Error425Throwable $e) {
    // handle $e->$container data
    throw $e;
} catch (errors\Error429Throwable $e) {
    // handle $e->$container data
    throw $e;
} catch (errors\Error500Throwable $e) {
    // handle $e->$container data
    throw $e;
} catch (errors\Error502Throwable $e) {
    // handle $e->$container data
    throw $e;
} catch (errors\Error504Throwable $e) {
    // handle $e->$container data
    throw $e;
} catch (errors\APIException $e) {
    // handle default exception
    throw $e;
}

declare(strict_types=1);

\SDK::builder()
    ->setServer('sandbox')
    ->setId('<id>')
    ->setMerchantAccountId('default')
    ->setSecurity(
        '<YOUR_BEARER_TOKEN_HERE>'
    )
    ->build();

$accountUpdaterJobCreate = new Gr4vy\AccountUpdaterJobCreate(
    paymentMethodIds: [
        'ef9496d8-53a5-4aad-8ca2-00eb68334389',
        'f29e886e-93cc-4714-b4a3-12b7a718e595',
    ],
);

$response = $sdk->accountUpdater->jobs->create(
    accountUpdaterJobCreate: $accountUpdaterJobCreate
);

if ($response->accountUpdaterJob !== null) {
    // handle response
}

declare(strict_types=1);

\SDK::builder()
    ->setServerURL('https://api.example.gr4vy.app')
    ->setMerchantAccountId('default')
    ->setSecurity(
        '<YOUR_BEARER_TOKEN_HERE>'
    )
    ->build();

$accountUpdaterJobCreate = new Gr4vy\AccountUpdaterJobCreate(
    paymentMethodIds: [
        'ef9496d8-53a5-4aad-8ca2-00eb68334389',
        'f29e886e-93cc-4714-b4a3-12b7a718e595',
    ],
);

$response = $sdk->accountUpdater->jobs->create(
    accountUpdaterJobCreate: $accountUpdaterJobCreate
);

if ($response->accountUpdaterJob !== null) {
    // handle response
}
bash
composer