1. Go to this page and download the library: Download moolre/moolre-php 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/ */
oolre\Client;
$client = new Client(
'your_public_key',
'your_account_number',
null,
Client::DEFAULT_BASE_URL,
60,
false,
'your_moolre_account_username'
);
$transaction = $client->verifyPayment($_GET['reference'] ?? '');
// Load the pending order from your database by reference first.
$order = [
'amount' => '120.00',
'currency' => 'GHS',
'email' => '[email protected]',
'account_number' => 'your_account_number',
];
if ($transaction->matchesPaymentDetails(
$order['amount'],
$order['currency'],
$order['email'],
$order['account_number']
)) {
// Mark the order, invoice, wallet top-up, or subscription as paid.
}
use Moolre\Client;
Route::post('/pay', function () {
$client = new Client(config('services.moolre.public_key'), config('services.moolre.account_number'));
$payment = $client->initiatePayment([
'reference' => Client::generateReference('order_' . auth()->id()),
'email' => auth()->user()->email,
'amount' => request('amount'),
'currency' => 'GHS',
'callback' => route('moolre.webhook'),
'redirect' => route('moolre.redirect'),
'expiration_time' => 10,
'metadata' => [
'customer_id' => (string) auth()->id(),
],
]);
return redirect()->away($payment->authorizationUrl());
});
Route::post('/moolre/webhook', function () {
$client = new Client(
config('services.moolre.public_key'),
config('services.moolre.account_number'),
null,
Client::DEFAULT_BASE_URL,
60,
false,
config('services.moolre.api_user')
);
$transaction = $client->verifyPayment(request('reference'));
abort_unless($transaction->isSuccessful(), 402);
// Update your local order here.
})->name('moolre.webhook');
Route::get('/moolre/redirect', function () {
$client = new Client(
config('services.moolre.public_key'),
config('services.moolre.account_number'),
null,
Client::DEFAULT_BASE_URL,
60,
false,
config('services.moolre.api_user')
);
$transaction = $client->verifyPayment(request('reference'));
abort_unless($transaction->isSuccessful(), 402);
// Show your receipt or success page here.
})->name('moolre.redirect');
namespace App\Controller;
use Moolre\Client;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
final class MoolreController extends AbstractController
{
#[Route('/pay', methods: ['POST'])]
public function pay(Request $request): RedirectResponse
{
$client = new Client(
$_ENV['MOOLRE_PUBLIC_KEY'],
$_ENV['MOOLRE_ACCOUNT_NUMBER']
);
$payment = $client->initiatePayment([
'reference' => Client::generateReference('order_' . $request->request->get('order_id')),
'email' => $request->request->get('email'),
'amount' => $request->request->get('amount'),
'currency' => 'GHS',
'callback' => $this->generateUrl('moolre_webhook', [], UrlGeneratorInterface::ABSOLUTE_URL),
'redirect' => $this->generateUrl('moolre_redirect', [], UrlGeneratorInterface::ABSOLUTE_URL),
'expiration_time' => 10,
'metadata' => [
'order_id' => (string) $request->request->get('order_id'),
],
]);
return new RedirectResponse($payment->authorizationUrl());
}
#[Route('/moolre/webhook', name: 'moolre_webhook', methods: ['POST'])]
public function webhook(Request $request): Response
{
$client = new Client(
$_ENV['MOOLRE_PUBLIC_KEY'],
$_ENV['MOOLRE_ACCOUNT_NUMBER'],
null,
Client::DEFAULT_BASE_URL,
60,
false,
$_ENV['MOOLRE_API_USER']
);
$payload = json_decode($request->getContent(), true) ?: $request->request->all();
$transaction = $client->verifyPayment((string) ($payload['reference'] ?? ''));
if (!$transaction->isSuccessful()) {
return new Response('Payment not successful.', 402);
}
// Update your local order here.
return new Response('Payment verified.');
}
#[Route('/moolre/redirect', name: 'moolre_redirect', methods: ['GET'])]
public function redirectResult(Request $request): Response
{
$client = new Client(
$_ENV['MOOLRE_PUBLIC_KEY'],
$_ENV['MOOLRE_ACCOUNT_NUMBER'],
null,
Client::DEFAULT_BASE_URL,
60,
false,
$_ENV['MOOLRE_API_USER']
);
$transaction = $client->verifyPayment((string) $request->query->get('reference'));
if (!$transaction->isSuccessful()) {
return new Response('Payment not successful.', 402);
}
return new Response('Payment receipt page.');
}
}
use Moolre\Client;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
$app->post('/pay', function (Request $request, Response $response) {
$data = (array) $request->getParsedBody();
$client = new Client($_ENV['MOOLRE_PUBLIC_KEY'], $_ENV['MOOLRE_ACCOUNT_NUMBER']);
$payment = $client->initiatePayment([
'reference' => Client::generateReference('order_' . ($data['order_id'] ?? '')),
'email' => $data['email'] ?? '',
'amount' => $data['amount'] ?? '',
'currency' => 'GHS',
'callback' => 'https://example.com/moolre/webhook',
'redirect' => 'https://example.com/moolre/redirect',
'expiration_time' => 10,
'metadata' => [
'order_id' => (string) ($data['order_id'] ?? ''),
],
]);
return $response
->withHeader('Location', $payment->authorizationUrl())
->withStatus(302);
});
$app->post('/moolre/webhook', function (Request $request, Response $response) {
$client = new Client(
$_ENV['MOOLRE_PUBLIC_KEY'],
$_ENV['MOOLRE_ACCOUNT_NUMBER'],
null,
Client::DEFAULT_BASE_URL,
60,
false,
$_ENV['MOOLRE_API_USER']
);
$payload = (array) $request->getParsedBody();
$transaction = $client->verifyPayment((string) ($payload['reference'] ?? ''));
if (!$transaction->isSuccessful()) {
$response->getBody()->write('Payment not successful.');
return $response->withStatus(402);
}
// Update your local order here.
$response->getBody()->write('Payment verified.');
return $response;
});
$app->get('/moolre/redirect', function (Request $request, Response $response) {
$client = new Client(
$_ENV['MOOLRE_PUBLIC_KEY'],
$_ENV['MOOLRE_ACCOUNT_NUMBER'],
null,
Client::DEFAULT_BASE_URL,
60,
false,
$_ENV['MOOLRE_API_USER']
);
$transaction = $client->verifyPayment((string) ($request->getQueryParams()['reference'] ?? ''));
if (!$transaction->isSuccessful()) {
$response->getBody()->write('Payment not successful.');
return $response->withStatus(402);
}
$response->getBody()->write('Payment receipt page.');
return $response;
});
namespace App\Controllers;
use CodeIgniter\Controller;
use Moolre\Client;
final class MoolreController extends Controller
{
public function pay()
{
$client = new Client(
getenv('MOOLRE_PUBLIC_KEY'),
getenv('MOOLRE_ACCOUNT_NUMBER')
);
$payment = $client->initiatePayment([
'reference' => Client::generateReference('order_' . $this->request->getPost('order_id')),
'email' => $this->request->getPost('email'),
'amount' => $this->request->getPost('amount'),
'currency' => 'GHS',
'callback' => site_url('moolre/webhook'),
'redirect' => site_url('moolre/redirect'),
'expiration_time' => 10,
'metadata' => [
'order_id' => (string) $this->request->getPost('order_id'),
],
]);
return redirect()->to($payment->authorizationUrl());
}
public function webhook()
{
$client = new Client(
getenv('MOOLRE_PUBLIC_KEY'),
getenv('MOOLRE_ACCOUNT_NUMBER'),
null,
Client::DEFAULT_BASE_URL,
60,
false,
getenv('MOOLRE_API_USER') ?: null
);
$transaction = $client->verifyPayment((string) $this->request->getPost('reference'));
if (!$transaction->isSuccessful()) {
return $this->response->setStatusCode(402)->setBody('Payment not successful.');
}
// Update your local order here.
return $this->response->setBody('Payment verified.');
}
public function redirectResult()
{
$client = new Client(
getenv('MOOLRE_PUBLIC_KEY'),
getenv('MOOLRE_ACCOUNT_NUMBER'),
null,
Client::DEFAULT_BASE_URL,
60,
false,
getenv('MOOLRE_API_USER') ?: null
);
$transaction = $client->verifyPayment((string) $this->request->getGet('reference'));
if (!$transaction->isSuccessful()) {
return $this->response->setStatusCode(402)->setBody('Payment not successful.');
}
return $this->response->setBody('Payment receipt page.');
}
}
use Moolre\Exceptions\MoolreException;
try {
$payment = $client->initiatePayment($payload);
} catch (MoolreException $exception) {
// Log the error and show a safe message to your user.
}