PHP code example of dunopay / php-sdk

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

    

dunopay / php-sdk example snippets


use DunoPay\DunoPayClient;

// Plateforme unique dunopay.com — le mode (live/sandbox) est déterminé par la clé.
// Les clés de test (sk_sandbox_…) fonctionnent immédiatement ; les clés live
// nécessitent un compte validé.
$dunopay = new DunoPayClient('sk_live_xxxxx');

// Optionnel : passer le mode explicitement à l'initialisation — garde-fou qui
// lève une exception si le mode ne correspond pas au préfixe de la clé.
$dunopay = new DunoPayClient('sk_sandbox_xxxxx', mode: 'sandbox');

$transaction = $dunopay->createTransaction([
    'amount'       => 5000,                      // XOF, entier
    'currency'     => ['code' => 'XOF'],
    'description'  => 'Commande #42',
    'callback_url' => 'https://boutique.example/webhooks/dunopay',
    'return_url'   => 'https://boutique.example/merci',
    'customer'     => [
        'firstname' => 'Awa',
        'lastname'  => 'Diallo',
        'email'     => '[email protected]',
    ],
    'custom_metadata' => ['order_id' => '42'],
], idempotencyKey: 'order-42'); // rejoué sans doublon en cas de retry

// Rediriger le client vers la page de paiement hébergée :
header('Location: ' . $transaction['payment_url']);

$tx = $dunopay->getTransaction($transaction['id']);
if ($tx['status'] === 'approved') { /* commande payée */ }

use DunoPay\Webhook;
use DunoPay\DunoPayException;

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

try {
    $event = Webhook::constructEvent($payload, $signature, $webhookSecret);
} catch (DunoPayException $e) {
    http_response_code(403);
    exit;
}

match ($event['event']) {
    'transaction.approved' => markOrderPaid($event['transaction']['reference']),
    'transaction.declined' => markOrderFailed($event['transaction']['reference']),
    'refund.approved'      => markOrderRefunded($event['refund']['transaction_id']),
    default                => null,
};

http_response_code(200);
bash
composer