PHP code example of zayono / zayono-php

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

    

zayono / zayono-php example snippets


use Zayono\Zayono;

$zayono = new Zayono('zyn_test_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');

$payment = $zayono->payments->create([
    'amount'         => 5000,
    'currency'       => 'XOF',
    'description'    => 'T-shirt premium',
    'customer_email' => '[email protected]',
    'return_url'     => 'https://example.com/success',
    'cancel_url'     => 'https://example.com/cancel',
]);

header('Location: ' . $payment['checkout_url']);

$zayono = new Zayono(
    apiKey:     'zyn_test_xxxxx',
    baseUrl:    'https://backend.zayono.com/api/v1',  // default
    timeout:    30,                                    // seconds
    maxRetries: 3,                                     // 5xx + 429 + network
    logger:     $psrLogger,                            // optional PSR-3
);

$payment = $zayono->payments->create([
    'amount'         => 5000,
    'currency'       => 'XOF',
    'description'    => 'Order #1234',
    'customer_email' => '[email protected]',
    'metadata'       => ['order_id' => 'ORD-1234'],
]);

echo $payment['checkout_url'];

$payment = $zayono->payments->retrieve('019e5eaf-cb99-7351-a6d5-c219e28534db');

if ($payment['status'] === 'success') {
    // Deliver the order.
}

// Force a fresh check against the upstream aggregator:
$payment = $zayono->payments->verify('019e5eaf-cb99-7351-a6d5-c219e28534db');

$payout = $zayono->payouts->create([
    'amount'      => 10000,
    'currency'    => 'XOF',
    'operator'    => 'mtn_bj',
    'phone'       => '+22961000000',
    'description' => 'Payout for order ORD-1234',
]);

$payload   = file_get_contents('php://input');
$signature = $_SERVER['HTTP_X_ZAYONO_SIGNATURE'] ?? '';

if (!$zayono->webhooks->verify($payload, $signature, getenv('ZAYONO_WEBHOOK_SECRET'))) {
    http_response_code(401);
    exit('Invalid signature');
}

$event = json_decode($payload, true);

match ($event['event']) {
    'payment.succeeded' => handlePaymentSuccess($event['data']),
    'payment.failed'    => handlePaymentFailure($event['data']),
    default             => null,
};

foreach ($zayono->payments->list(['status' => 'success']) as $payment) {
    echo $payment['id'], PHP_EOL;
}

$page = $zayono->payments->listPage(['status' => 'success', 'per_page' => 50]);
foreach ($page['payments'] as $payment) {
    // …
}

$zayono->payments->create([
    'amount'           => 5000,
    'currency'         => 'XOF',
    'idempotency_key'  => '019e5eaf-cb99-4351-a6d5-c219e28534db',
]);

use Zayono\Exceptions\{
    AuthenticationException,
    ValidationException,
    RateLimitException,
    ResourceNotFoundException,
    ServerException,
    NetworkException,
};

try {
    $payment = $zayono->payments->create([/* … */]);
} catch (ValidationException $e) {
    foreach ($e->errors as $field => $messages) {
        echo "$field: ", implode(', ', $messages), PHP_EOL;
    }
} catch (AuthenticationException $e) {
    // 401: key revoked / wrong / expired
} catch (RateLimitException $e) {
    sleep($e->retryAfter ?? 1);
} catch (ResourceNotFoundException $e) {
    // 404
} catch (ServerException $e) {
    // 5xx after exhausting retries
} catch (NetworkException $e) {
    // No response received after retries
}

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

$log = new Logger('zayono');
$log->pushHandler(new StreamHandler('zayono.log'));

$zayono = new Zayono('zyn_test_xxxxx', logger: $log);

$zayono = new Zayono(
    apiKey:     'zyn_test_xxxxx',
    httpClient: $myPsr18Client,
);
bash
composer