Where this SDK fits
Use the PHP SDK when your application has a PHP server that creates orders, redirects customers to hosted checkout, and handles payment confirmation. It works well for stores, invoices, membership payments, and any flow where your server owns fulfilment.
It is not a browser or mobile-app SDK. Browser and mobile apps should call your PHP backend for order creation and verification, rather than exposing server credentials in the client.
Create
Save a pending local order with a unique payment reference.
Redirect
Send the customer to the hosted Moolre checkout URL.
Verify
Check the transaction on your server before fulfilling the order.
Install with Composer
The SDK is framework-agnostic, so the same package works with raw PHP, Laravel, Symfony, Slim, CodeIgniter, and other PHP applications.
composer require moolre/moolre-phpPHP 7.4+
Required runtime
JSON extension
Required
cURL
Recommended; PHP streams fall back automatically
Configure the client
Store credentials in server-side environment variables. Live initiation needs a public key and account number. Sandbox initiation needs an account number and API user; verification always needs the API user.
<?phprequire __DIR__ . '/vendor/autoload.php';use Moolre\Client;$isProduction = getenv('MOOLRE_PRODUCTION') === 'true';$client = new Client( $isProduction ? (getenv('MOOLRE_PUBLIC_KEY') ?: '') : '', getenv('MOOLRE_ACCOUNT_NUMBER') ?: '', null, $isProduction ? Client::DEFAULT_BASE_URL : Client::SANDBOX_BASE_URL, 60, false, getenv('MOOLRE_API_USER') ?: '', $isProduction ? Client::DEFAULT_VERIFICATION_URL : Client::SANDBOX_VERIFICATION_URL,);Keep credentials off the client. Never expose your Moolre public key, account number, or API user in browser JavaScript, mobile app bundles, source control, or logs.
Create a hosted payment
Generate a unique reference and save the pending order before redirecting the customer. Use callback for server-to-server notification and redirect for the customer’s return page.
<?phpuse Moolre\Client;$reference = Client::generateReference('order_1001');// Save a pending order under $reference before redirecting the customer.$payment = $client->initiatePayment([ 'reference' => $reference, 'email' => 'customer@example.com', 'amount' => '120.00', 'currency' => 'GHS', 'callback' => 'https://example.com/moolre/webhook', 'redirect' => 'https://example.com/moolre/receipt', 'expiration_time' => 10, 'metadata' => [ 'order_id' => '1001', ],]);header('Location: ' . $payment->authorizationUrl(), true, 302);exit;Verify before fulfilment
A callback or redirect only tells your app to check a payment. Load the local pending order by its reference, then verify the transaction with Moolre before you mark it paid, credit a wallet, or deliver goods.
<?php$reference = $_POST['reference'] ?? $_GET['reference'] ?? '';$order = $orders->findPendingByReference($reference);if ($order === null) { throw new RuntimeException('Unknown payment reference.');}$transaction = $client->verifyPayment($reference);if (!$transaction->matchesAmountAndAccountNumber( (string) $order['amount'], (string) $order['account_number'],)) { throw new RuntimeException('Payment could not be verified for this order.');}// Make this update idempotent, then fulfil the order exactly once.$orders->markPaid($order['id'], $transaction->data());Safe verification checklist
- Look up the pending local order using the received reference.
- Call
verifyPayment()from your server. - Require
matchesAmountAndAccountNumber()before fulfilment. - Make the final order update idempotent so duplicate callbacks cannot fulfil it twice.
