PHP code example of patricpoba / mtn-momo-api-php

1. Go to this page and download the library: Download patricpoba/mtn-momo-api-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/ */

    

patricpoba / mtn-momo-api-php example snippets


use PatricPoba\MtnMomo\MtnConfig;

 
$config = new MtnConfig([ 
    // mandatory credentials
    'baseUrl'               => 'https://sandbox.momodeveloper.mtn.com', 
    'currency'              => 'EUR', 
    'targetEnvironment'     => 'sandbox', 

    // product specific blocks
    "collectionApiSecret"   => '3463953c31064e6e8ae634cd94f13c8c', 
    "collectionPrimaryKey"  => 'aadb6f286e95415db9024c7a4e2c6025', 
    "collectionUserId"      => 'b4d4019f-8617-4843-a4b8-ed90941747a3',
 
    "disbursementApiSecret" => '3463953c31064e6e8ae634cd94f13c8c', 
    "disbursementPrimaryKey"=> 'aadb6f286e95415db9024c7a4e2c6025', 
    "disbursementUserId"    => 'b4d4019f-8617-4843-a4b8-ed90941747a3',
 
    "remittanceApiSecret"   => '3463953c31064e6e8ae634cd94f13c8c', 
    "remittancePrimaryKey"  => 'aadb6f286e95415db9024c7a4e2c6025', 
    "remittanceUserId"      => 'b4d4019f-8617-4843-a4b8-ed90941747a3'
]);

use PatricPoba\MtnMomo\MtnConfig;

 
$config = new MtnConfig([ 
    // mandatory credentials
    'baseUrl'               => 'https://sandbox.momodeveloper.mtn.com', 
    'currency'              => 'EUR', 
    'targetEnvironment'     => 'sandbox', 

    // collection credentials
    "collectionApiSecret"   => '3463953c31064e6e8ae634cd94f13c8c', 
    "collectionPrimaryKey"  => 'aadb6f286e95415db9024c7a4e2c6025', 
    "collectionUserId"      => 'b4d4019f-8617-4843-a4b8-ed90941747a3'
]);


$collection = new MtnCollection($config); 

$params = [
    "mobileNumber"      => '233540000000', 
    "amount"            => '100', 
    "externalId"        => '774747234',
    "payerMessage"      => 'some note',
    "payeeNote"         => '1212'
];

$transactionId = $collection->requestToPay($params);

$transaction = $collection->getTransaction($transactionId);

   $transactionId = $collection->requestToPay($params);

   $transaction = $collection->getTransaction($transactionId);

   $transaction = $collection->getBalance();

   $transaction = $collection->accountHolderActive($mobileNumber);
 
use PatricPoba\MtnMomo\MtnConfig;
use PatricPoba\MtnMomo\MtnDisbursement;

 
$config = new MtnConfig([ 
    // mandatory credentials
    'baseUrl'               => 'https://sandbox.momodeveloper.mtn.com', 
    'currency'              => 'EUR', 
    'targetEnvironment'     => 'sandbox', 

    // disbursement credentials
    "disbursementApiSecret"   => '3463953c31064e6e8ae634cd94f13c8c', 
    "disbursementPrimaryKey"  => 'aadb6f286e95415db9024c7a4e2c6025', 
    "disbursementUserId"      => 'b4d4019f-8617-4843-a4b8-ed90941747a3'
]);

/**
 * setup disbursement config
 */
$disbursement = new MtnDisbursement($config); 

$params = [
    "mobileNumber"      => '233540000000', 
    "amount"            => '100', 
    "externalId"        => '774747234',
    "payerMessage"      => 'some note',
    "payeeNote"         => '1212'
];

/**
 * Transfer() is used to request a payment from a consumer (Payer). The payer will be asked to authorize the payment. The transaction is executed once the payer has authorized the payment. The transaction will be in status PENDING until it is authorized or declined by the payer or it is timed out by the system. 
 */
$transactionId = $disbursement->transfer($params);

/**
 * Status of the transaction can be validated by checking the `status` 
 * field on the result of `getTransaction()` method.
 */
$transaction = $disbursement->getTransaction($transactionId);

   $transactionId = $disbursement->transfer($params);

   $transaction = $disbursement->getTransaction($transactionId);

   $transaction = $disbursement->getBalance();

   $transaction = $disbursement->accountHolderActive($mobileNumber);
 
use PatricPoba\MtnMomo\MtnConfig;
use PatricPoba\MtnMomo\MtnRemittance;

 
$config = new MtnConfig([ 
    // mandatory credentials
    'baseUrl'               => 'https://sandbox.momodeveloper.mtn.com', 
    'currency'              => 'EUR', 
    'targetEnvironment'     => 'sandbox', 

    // disbursement credentials
    "remittanceApiSecret"   => '3463953c31064e6e8ae634cd94f13c8c', 
    "remittancePrimaryKey"  => 'aadb6f286e95415db9024c7a4e2c6025', 
    "remittanceUserId"      => 'b4d4019f-8617-4843-a4b8-ed90941747a3'
]);

/**
 * setup remittance config
 */
$remittance = new MtnRemittance($config); 

$params = [
    "mobileNumber"      => '233540000000', 
    "amount"            => '100', 
    "externalId"        => '774747234',
    "payerMessage"      => 'some note',
    "payeeNote"         => '1212'
];

/**  
 * Transfer operation is used to transfer an amount from the own account to a payee account.
 * Status of the transaction can validated by using the GET /transfer/{referenceId}
 */
$transactionId = $remittance->transfer($params);


/**
 * This operation is used to get the status of a transfer. X-Reference-Id 
 * that was passed in the post is used as reference to the request.
 */
$transaction = $remittance->getTransaction($transactionId);
  
  
/**
 * Get the balance of your disbursement account.
 */
$transaction = $disbursement->getBalance();
 

/**
 * Operation is used to check if an account holder is registered and active in the system.
 */
$transaction = $disbursement->accountHolderActive($mobileNumber);

bash
## On the command line, at the root of your project, run 
$ php ./vendor/patricpoba/mtn-momo-api-php/src/SandboxUserProvision.php -k '9cc70894a5d24dba8a8a50fcecbc0568' -c 'https://yourdomain.com'