Guide

Laravel guide for wrapping the YKNPG flow in a controller.

Laravel guide for wrapping the YKNPG flow in a controller.

Prerequisites

  • PHP 8.1+, Composer, Laravel installer or laravel/laravel skeleton.
  • .env with YKNPG_API_TOKEN.
  1. Create project
  • composer create-project laravel/laravel demo-laravel
  • cd demo-laravel
  1. Add environment variables
  • File: .env
YKNPG_API_TOKEN=replace-with-your-token
YKNPG_BASE_URL=https://yknpg.ngoul.com/api/v1
  1. Create controller
  • File: app/Http/Controllers/PaymentController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class PaymentController extends Controller
{
    public function pay(Request $request)
    {
        $request->validate([
            'phone' => 'required|string',
            'amount' => 'required|integer|min:1',
        ]);

        $token = config('services.yknpg.token', env('YKNPG_API_TOKEN'));
        $base = config('services.yknpg.base', env('YKNPG_BASE_URL', 'https://yknpg.ngoul.com/api/v1'));
        $client = Http::withToken($token)->baseUrl($base)->timeout(10);

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

        $created = $client->post('/transactions/', $payload)->throw()->json();
        $paid = $client->post("/transactions/{$created['uuid']}/pay/")->throw()->json();

        $status = $paid['status'] ?? 'new';
        while (in_array($status, ['new', 'paying'])) {
            sleep(5);
            $status = $client->get("/transactions/{$created['uuid']}/")->throw()->json('status');
        }

        return response()->json([
            'instructions' => $created['instructions'] ?? null,
            'post_pay_instructions' => $paid['instructions'] ?? null,
            'final_status' => $status,
        ]);
    }
}
  1. Add route
  • File: routes/api.php
use App\Http\Controllers\PaymentController;
Route::post('/pay', [PaymentController::class, 'pay']);
  1. Optional config alias
  • File: config/services.php (add)
'yknpg' => [
    'token' => env('YKNPG_API_TOKEN'),
    'base' => env('YKNPG_BASE_URL', 'https://yknpg.ngoul.com/api/v1'),
],
  1. Run
  • php artisan serve
  • POST JSON {"phone": "...", "amount": 1000} to http://127.0.0.1:8000/api/pay.

Notes

  • Keep the token server-side; reuse the controller/service in jobs or queued flows if needed.