Guide
PHP guide for creating and paying a YKNPG transaction (Guzzle).
PHP guide for creating and paying a YKNPG transaction (Guzzle).
Prerequisites
- PHP 8.1+, Composer.
.envholdingYKNPG_API_TOKEN.
- Init project
mkdir -p demo_php && cd demo_phpcomposer init --no-interaction
- Install dependencies
composer require guzzlehttp/guzzle vlucas/phpdotenv
- Add environment file
- File:
.env
YKNPG_API_TOKEN=replace-with-your-token
YKNPG_BASE_URL=https://yknpg.ngoul.com/api/v1
- Create script
- File:
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;
- Run
php transaction.php <phone> <amount>
Notes
- Keep the token out of version control; adjust
providerif you use another gateway.