
In this blog post, we will walk through integrating the Waafi Pay payment gateway with Laravel 11. We will cover setting up a service class, binding it to a service provider, and using it within a controller to handle payments efficiently.
Prerequisites
Before we begin, make sure you have:
- A Laravel 11 application set up
- Composer installed
- Waafi Pay API credentials (merchant ID, API key, and base URL)
Step 1: Install Guzzle HTTP Client
Waafi Pay API requests are made using HTTP, so we need to install Guzzle:
composer require guzzlehttp/guzzle
Step 2: Create the WaafiPay Service Class
Laravel 11 does not include the app/Services directory by default, so we need to create it manually:
mkdir app/Services
Then, create a new file inside app/Services called WaafiPayService.php:
<?php
namespace App\Services;
use GuzzleHttp\Client;
class WaafiPayService
{
protected $client;
protected $baseUrl;
protected $merchantId;
protected $apiKey;
public function __construct()
{
$this->client = new Client();
$this->baseUrl = config('services.waafipay.base_url');
$this->merchantId = config('services.waafipay.merchant_id');
$this->apiKey = config('services.waafipay.api_key');
}
public function initiatePayment($orderDetails)
{
$url = $this->baseUrl . '/v1/payments';
$payload = [
'merchant_id' => $this->merchantId,
'order_id' => $orderDetails['order_id'],
'amount' => $orderDetails['amount'],
'currency' => $orderDetails['currency'],
'callback_url' => route('waafipay.callback'),
'customer_phone' => $orderDetails['phone'],
'api_key' => $this->apiKey,
];
$response = $this->client->post($url, ['json' => $payload]);
return json_decode($response->getBody()->getContents(), true);
}
}
Step 3: Create a Service Provider
Now, we need to bind the service to Laravel’s service container by creating a provider:
php artisan make:provider WaafiPay
Open app/Providers/WaafiPay.php and update the file with the following code:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Services\WaafiPayService;
class WaafiPay extends ServiceProvider
{
/**
* Register services.
*/
public function register(): void
{
$this->app->singleton(WaafiPayService::class, function ($app) {
return new WaafiPayService();
});
}
/**
* Bootstrap services.
*/
public function boot(): void
{
//
}
}
Step 4: Register the Provider in config/app.php
To ensure Laravel loads our custom service provider, add it to the providers array in config/app.php:
'providers' => [
// Other service providers
App\Providers\WaafiPay::class,
],
Step 5: Configure API Credentials
Next, add your Waafi Pay credentials in config/services.php:
'waafipay' => [
'base_url' => env('WAAFIPAY_BASE_URL', 'https://api.waafipay.net'),
'merchant_id' => env('WAAFIPAY_MERCHANT_ID'),
'api_key' => env('WAAFIPAY_API_KEY'),
],
Then, define these values in your .env file:
WAAFIPAY_BASE_URL=https://api.waafipay.net
WAAFIPAY_MERCHANT_ID=your_merchant_id
WAAFIPAY_API_KEY=your_api_key
Step 6: Use the WaafiPay Service in a Controller
Now, let’s create a controller to handle payments:
php artisan make:controller PaymentController
Open app/Http/Controllers/PaymentController.php and add the following code:
<?php
namespace App\Http\Controllers;
use App\Services\WaafiPayService;
use Illuminate\Http\Request;
class PaymentController extends Controller
{
protected $waafiPayService;
public function __construct(WaafiPayService $waafiPayService)
{
$this->waafiPayService = $waafiPayService;
}
public function pay(Request $request)
{
$orderDetails = [
'order_id' => $request->order_id,
'amount' => $request->amount,
'currency' => 'USD',
'phone' => $request->phone,
];
$response = $this->waafiPayService->initiatePayment($orderDetails);
// Handle response
return redirect($response['payment_url'] ?? '/');
}
public function callback(Request $request)
{
// Handle the callback from WaafiPay
}
}
Step 7: Define Routes
In routes/web.php, define the routes for payment processing:
use App\Http\Controllers\PaymentController;
Route::post('/pay', [PaymentController::class, 'pay'])->name('waafipay.pay');
Route::get('/callback', [PaymentController::class, 'callback'])->name('waafipay.callback');
Step 8: Testing the Integration
You can test your integration by making a POST request to /pay with the required parameters:
{
"order_id": "123456",
"amount": 50.00,
"currency": "USD",
"phone": "252612345678"
}
If everything is set up correctly, your application should send a payment request to Waafi Pay, and you should be redirected to the payment URL.
Conclusion
Integrating Waafi Pay with Laravel 11 involves setting up a service class, binding it in a service provider, and using it within a controller. This structured approach ensures better maintainability and scalability of your payment system.
Now that you have successfully integrated Waafi Pay, you can start accepting payments securely in your Laravel application!
For any questions or feedback, feel free to leave a comment. Happy coding!