Guide

Guide PHP pour créer et payer une transaction YKNPG (Guzzle).

Guide PHP pour créer et payer une transaction YKNPG (Guzzle).

Pré-requis

  • PHP 8.1+, Composer.
  • .env contenant YKNPG_API_TOKEN.
  1. Initialiser le projet
  • mkdir -p demo_php && cd demo_php
  • composer init --no-interaction
  1. Installer les dépendances
  • composer require guzzlehttp/guzzle vlucas/phpdotenv
  1. Ajouter le fichier d'environnement
  • Fichier : .env
YKNPG_API_TOKEN=remplacez-par-votre-jeton
YKNPG_BASE_URL=https://yknpg.ngoul.com/api/v1
  1. Créer le script
  • Fichier : transaction.php
<?php
require __DIR__ . '/vendor/autoload.php';

use Dotenv\Dotenv;
use GuzzleHttp\Client;

$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();

$token = $_ENV['YKNPG_API_TOKEN'] ?? null;
$base = $_ENV['YKNPG_BASE_URL'] ?? 'https://yknpg.ngoul.com/api/v1';
if (!$token) {
    echo "Set YKNPG_API_TOKEN in .env\n";
    exit(1);
}

$phone = $argv[1] ?? null;
$amount = $argv[2] ?? null;
if (!$phone || !$amount) {
    echo "Usage: php transaction.php <phone> <amount>\n";
    exit(1);
}

$client = new Client([
    'base_uri' => $base,
    'headers' => ['Authorization' => "Bearer $token"],
    'timeout' => 10,
]);

$payload = [
    'amount' => (int) $amount,
    'provider' => 'ORANGE_MONEY',
    'user_infos' => ['number' => $phone],
    'callback_url' => 'http://example.com',
    'callback_method' => 'POST',
    'your_message' => 'pay this',
    'your_order_ref' => 'demo-1',
    'provider_fees_on_customer' => true,
];

$create = $client->post('/transactions/', ['json' => $payload])->getBody()->getContents();
$createData = json_decode($create, true);
echo "Gateway instructions: " . ($createData['instructions'] ?? '') . PHP_EOL;

$pay = $client->post("/transactions/{$createData['uuid']}/pay/")->getBody()->getContents();
$payData = json_decode($pay, true);
echo "Post-pay instructions: " . ($payData['instructions'] ?? '') . PHP_EOL;

$status = $payData['status'] ?? 'new';
while (in_array($status, ['new', 'paying'], true)) {
    sleep(5);
    $poll = $client->get("/transactions/{$createData['uuid']}/")->getBody()->getContents();
    $pollData = json_decode($poll, true);
    $status = $pollData['status'] ?? $status;
    echo "Current status: $status" . PHP_EOL;
}
echo "Final status: $status" . PHP_EOL;
  1. Exécuter
  • php transaction.php <phone> <amount>

Remarques

  • Gardez le jeton hors du contrôle de version ; ajustez provider si vous utilisez une autre passerelle.