PHP code example of ekolotech / mobilemoney-gateway

1. Go to this page and download the library: Download ekolotech/mobilemoney-gateway 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/ */

    

ekolotech / mobilemoney-gateway example snippets


interface CollectionGatewayInterface
{
    public function collect(CollectRequestBody $collectRequestBody): bool;
    public function collectReference(string $reference): array;
    public function balance(): array;
    public function isAccountIsActive(string $number): bool;
    public function getAccountBasicInfo(string $number): array;
}

class CollectRequestBody
{
    public function __construct(
        public readonly int    $amount, // Le montant
        public readonly string $number, // le numéro MoMo du client
        public readonly string $reference, // la référence de la transaction. Il doit être en version 4 du UUID
    )
    {
    }
}

interface DisbursementGatewayInterface
{
    public function disburse(DisburseRequestBody $disburseRequestBody) : bool;
    public function disburseReference(string $reference) : array;
    public function balance() : array;
    public function isAccountIsActive(string $number) : bool;
    public function getAccountBasicInfo(string $number) : array;
}

class DisburseRequestBody
{
    public function __construct(
        public readonly int    $amount, // Le montant
        public readonly string $number, // le numéro MoMo du bénéficiaire
        public readonly string $reference, // la référence de la transaction. Il doit être en version 4 du UUID
    )
    {
    }
}

/** 
 * @var CollectionGatewayInterface $collectionGateway 
 */
$collectionGateway = ApiGatewayFactory::loadMtnCollectionGateway(...);

/**
 * @var DisbursementGatewayInterface $disbursementGateway 
 */
$disbursementGateway = ApiGatewayFactory::loadMtnDisbursementGateway(...);

interface MtnApiAccessAndEnvironmentConfigInterface
{
    /** Methodes de configuration de l'environnement **/
    public function getBaseApiUrl(): string; // l'url de base de MTN MoMo API. Cette url varie selon l'environnement de sandbox ou de production
    public function isProd(): bool; // définition si le sandbox ou production. false pour le sandbox
    public function getCurrency(): string; // la devise de l'environnement, en €(EURO) pour le sandbox
    public function getProviderCallbackUrl(): string; // Votre end-point de callback. Exemple : https://mon-application.com/callback
    public function getProviderCallbackHost(): string; // Votre host de callback. Exemple : mon-application.com
    
    /** Methodes événementiel à écouter. Il est indispensable d'enregistrer ces données dans une base **/
    public function onApiUserCreated(string $apiUser): void; // Fournit le API User créé
    public function onApiKeyCreated(string $apiKey): void; // Fournit le API key
    public function onTokenCreated(MtnAccessToken $mtnAccessToken): void; // Fournit le token d'accès
    
    /** Methodes de configuration des données d'authentification MoMo API **/
    public function getMtnAuthenticationProduct() : MtnAuthenticationProduct; // Renvoie les clés d'authentification au produit (Collection ou Disbursement)
    public function getMtnAccessToken(): ?MtnAccessToken; // Renvoie le token d'accès
}

class MtnAuthenticationProduct
{
    public function __construct(
        private readonly string $subscriptionKeyOne,
        private readonly string $subscriptionKeyTwo,
        private ?string         $apiUser = null,
        private ?string         $apiKey = null
    )
    {
    }
}

class MtnAccessToken
{
    public function __construct(
        private readonly string $accessToken,
        private readonly string $tokenType,
        private readonly int    $expiresIn,
        private readonly bool   $isExpired = false,
    )
    {
    }
}

class CollectionGatewayServiceImpl implements MtnApiAccessAndEnvironmentConfigInterface 
{
    public function getBaseApiUrl(): string
    {
        return "https://sandbox.momodeveloper.mtn.com";
    }

    public function getProviderCallbackUrl(): string
    {
        return "https://sandbox.momodeveloper.mtn.com";
    }

    public function getProviderCallbackHost(): string
    {
        return "sandbox.momodeveloper.mtn.com";
    }

    public function isProd(): bool
    {
        return false;
    }

    public function getCurrency(): string
    {
        return Currency::EUR;
    }

    public function onApiUserCreated(string $apiUser): void
    {
        // TODO something such as save $apiUser in database
    }

    public function onApiKeyCreated(string $apiKey): void
    {
        // TODO something such as save $apiKey in database
    }

    public function onTokenCreated(MtnAccessToken $mtnAccessToken): void
    {
        // TODO something such as save $mtnAccessToken in database
    }
}

/** @var CollectionGatewayInterface $collectionGateway */
$collectionGateway = ApiGatewayFactory::loadMtnCollectionGateway(
    new CollectionGatewayServiceImpl()
);

$clientNumber = "066304925";
$collectReference = "a103dbea-d5f7-45f3-b2e5-e495904d44cb";

// Vérification si le numéro est enregistré et possède un compte mobile money
if (!$collectionGateway->isAccountIsActive($clientNumber)) {
    die("Le titulaire du compte n'a pas de compte MoMo enregistré");
}

// Données personnelles du client. Peut être utilisé pour confirmer le nom et prénom du client
$personalData = $collectionGateway->getAccountBasicInfo($clientNumber);

// Demande de paiement au client. En production une notification sera envoyé au client pour valider le paiement.
// Une fois le paiement valider par le client, le serveur MoMo API envoie le statut de la transaction sur votre end-point ou URL de callback
if (!$collectionGateway->collect(new CollectRequestBody(150, $clientNumber, $reference))) {
    die("Echec de la demande de paiement");
}

// Vérification du statut de la demande de paiement.
$referenceData = $collectionGateway->collectReference($collectReference);

// Solde du compte de paiement
$balance = $collectionGateway->balance();