1. Go to this page and download the library: Download lepresk/momo-api 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/ */
lepresk / momo-api example snippets
use Lepresk\MomoApi\MomoApi;
// Simple fluent configuration
$collection = MomoApi::collection([
'environment' => 'sandbox', // or 'mtncongo', 'mtnuganda', etc.
'subscription_key' => 'YOUR_SUBSCRIPTION_KEY',
'api_user' => 'YOUR_API_USER',
'api_key' => 'YOUR_API_KEY',
'callback_url' => 'https://yourdomain.com/callback'
]);
// Quick payment - 3 parameters
$paymentId = $collection->quickPay('1000', '242068511358', 'ORDER-123');
// Check payment status
$transaction = $collection->getPaymentStatus($paymentId);
if ($transaction->isSuccessful()) {
echo "Payment of {$transaction->getAmount()} received!";
}
use Lepresk\MomoApi\MomoApi;
use Lepresk\MomoApi\Models\TransferRequest;
$disbursement = MomoApi::disbursement([
'environment' => 'sandbox',
'subscription_key' => 'YOUR_SUBSCRIPTION_KEY',
'api_user' => 'YOUR_API_USER',
'api_key' => 'YOUR_API_KEY',
'callback_url' => 'https://yourdomain.com/callback'
]);
// Transfer money to a beneficiary
$transfer = TransferRequest::make('5000', '242068511358', 'SALARY-001');
$transferId = $disbursement->transfer($transfer);
// Check transfer status
$result = $disbursement->getTransferStatus($transferId);
use Lepresk\MomoApi\MomoApi;
use Lepresk\MomoApi\Support\Uuid;
$momo = MomoApi::create(MomoApi::ENVIRONMENT_SANDBOX);
$subscriptionKey = 'YOUR_SANDBOX_SUBSCRIPTION_KEY';
// 1. Create API User
$uuid = Uuid::v4();
$callbackHost = 'https://yourdomain.com/callback';
$apiUser = $momo->sandbox($subscriptionKey)->createApiUser($uuid, $callbackHost);
// 2. Create API Key
$apiKey = $momo->sandbox($subscriptionKey)->createApiKey($apiUser);
// Now use these credentials for Collection/Disbursement
use Lepresk\MomoApi\MomoApi;
use Lepresk\MomoApi\Models\PaymentRequest;
$collection = MomoApi::collection([
'environment' => 'mtncongo',
'subscription_key' => env('MOMO_SUBSCRIPTION_KEY'),
'api_user' => env('MOMO_API_USER'),
'api_key' => env('MOMO_API_KEY'),
'callback_url' => 'https://yourdomain.com/webhook/momo'
]);
// Custom payment request
$request = new PaymentRequest(
amount: '2500',
currency: 'XAF',
externalId: 'ORDER-456',
payer: '242068511358',
payerMessage: 'Payment for order #456',
payeeNote: 'Thank you for your purchase'
);
$paymentId = $collection->requestToPay($request);
// Get account balance
$balance = $collection->getBalance();
echo "Available: {$balance->getAvailableBalance()} {$balance->getCurrency()}";
use Lepresk\MomoApi\MomoApi;
use Lepresk\MomoApi\Models\TransferRequest;
use Lepresk\MomoApi\Models\RefundRequest;
$disbursement = MomoApi::disbursement([...config...]);
// Transfer
$transfer = new TransferRequest(
amount: '10000',
currency: 'XAF',
externalId: 'PAYOUT-789',
payee: '242068511358',
payerMessage: 'Monthly salary',
payeeNote: 'Salary payment for June'
);
$transferId = $disbursement->transfer($transfer);
// Refund
$refund = RefundRequest::make('1000', $originalTransactionId, 'REFUND-123');
$refundId = $disbursement->refund($refund);
// Check balance
$balance = $disbursement->getBalance();
use Ramsey\Uuid\Uuid;
// Derive it from your own key, so the same line always gets the same id
$referenceId = Uuid::uuid5(MY_NAMESPACE_UUID, "{$batchRef}-{$lineNo}")->toString();
$disbursement->transfer($transfer, $referenceId);
$status = $disbursement->getTransferStatus($referenceId);
// Airtel takes it as $transactionId, the externalId it returns
$airtelDisbursement->transfer('10000', '068511358', 'PAY-001', $referenceId);
$payload = json_decode(file_get_contents('php://input'), true) ?? [];
// Look the order up in your own records; never trust the amount in the callback
$order = $orders->findByExternalId($payload['externalId'] ?? '');
if ($order === null) {
http_response_code(404);
exit;
}
// Ask MTN, with the reference id stored when the payment was requested
$transaction = $collection->getPaymentStatus($order->momoReferenceId);
if ($transaction->isSuccessful()
&& $transaction->getExternalId() === $order->id
&& (float) $transaction->getAmount() === (float) $order->amount
) {
$order->markPaid(); // make this idempotent: MTN may call more than once
} elseif ($transaction->isFailed()) {
$order->markFailed((string) $transaction->getReason());
}
http_response_code(200);
$payload = json_decode(file_get_contents('php://input'), true) ?? [];
$order = $orders->findByExternalId($payload['transaction']['id'] ?? '');
if ($order === null) {
http_response_code(404);
exit;
}
// Ask Airtel; the callback's status_code is not proof of payment
$transaction = $airtelCollection->getPaymentStatus($order->id);
if ($transaction->isSuccessful()) {
$order->markPaid();
} elseif ($transaction->isFailed()) {
$order->markFailed((string) $transaction->getMessage());
}
http_response_code(200);
use Lepresk\MomoApi\Exceptions\ResourceNotFoundException;
use Lepresk\MomoApi\Exceptions\InternalServerErrorException;
use Lepresk\MomoApi\Models\ErrorReason;
try {
$paymentId = $collection->quickPay('1000', '242068511358', 'ORDER-999');
} catch (ResourceNotFoundException $e) {
// Payment not found
echo "Error: " . $e->getMessage();
} catch (InternalServerErrorException $e) {
// Server error
echo "Server error, please retry";
}
// Check error reason from transaction
$transaction = $collection->getPaymentStatus($paymentId);
if ($transaction->isFailed()) {
$reason = $transaction->getReason();
// isPayerFundingFailure() also covers LOW_BALANCE_OR_PAYEE_LIMIT_REACHED_OR_NOT_ALLOWED,
// which MTN Congo returns instead of NOT_ENOUGH_FUNDS
if ($reason !== null && $reason->isPayerFundingFailure()) {
echo "Insufficient funds or limit reached";
} elseif ($reason !== null) {
echo "Failed: {$reason}"; // "[COULD_NOT_PERFORM_TRANSACTION]"
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.