Integrating MTN Cameroon Mobile Money to Laravel Application (Momo)

Published 4 months ago by Malico
Share

Hello there, if you are reading this, that means you must be a PHP/Laravel Developer. In this lil write-up, I'll explain how you can integrate MTN Cameroon Mobile Money service to your web app.

Configuration

First, you have to install the Laravel Package for this API through Composer:

composer require malico/momo

Next I'll need you to publish the Configuration File for this Package using the command

php artisan vendor:publish --tag=momo-configuration

Run the php artisan migrate to migrate the transaction table to save all transactions details.

Visit MTN Developer Website to create a merchant/Corporate Account with MTN Cameroon.Update the configuration published in \config\momo.php with the credentials you used to create the account.

<?php
return [
	/**
      * Developer email used in registering account on 
      MTN Developer Website
      */
  'email' => env('MOMO_EMAIL'),  
    /**
      * Default Price to all the Site Transactions
      */    
  'default_price' => env('MOMO_DEFAULT_PRICE', 100),    
    /**    
      * To include the Momo\Support\MomoTransaction trait to
        your model that creates relationship between your
        model and Momo\Model\Transaction Model.     * Here you 
        specify the foreign_key as it is found on your model
        migration.     
      */    
  'foreign_key' => "momo_transaction_id"
];

Make sure you update your migrations to match the foreign key provided in configuration file in case you want to MomoTransaction trait to your Eloquent Model.

Payments

Within the controller, you can perform payment transactions.

<?php

// app/Http/Controllers/SalesController.php

namespace App\Http\Controllers;

use Malico\Momo\Momo;
use Illuminate\Http\Request;

class SalesController extends Controller
{
    public function index(Request $request)
    {
// ...
        $momo = new Momo($request->telephone_number);
        $transaction = $momo->pay();
  
    // $tranction is an Eloquent Model (\Momo\Model\Transaction)

        if($transaction->status){
              return $transaction;
            }
        }
}

You can also created a eloquent relationships between your Application models & MTN Transaction model.

* Make sure you have a column with your model migration with the foreign column name specified in the \config/momo.php file

<?php

// app/Sale.php

namespace App;

use Malico\Momo\Support\MomoTransaction;
use Illuminate\Database\Eloquent\Model;

class Sale extends Model
{
    use MomoTransaction;

    ...
}

Now within your controllers.

<?php
// SalesController.php
namespace App\Http\Controllers;

use App\Sale;
use Momo\Momo;
use Illuminate\Http\Request;

class SalesController extends Controller
{
    public function index()
    {
    	...
        $sale = new Sale();
        $sale->tel = $request->telephone_number;

        $momo = new Momo($request->telephone_number);

        // $price's the price of products or ...
        //
        $momo->price = $price ?? 200;
        $transaction = $momo->pay()
        $sale->momo_transaction_id = $transaction->id;

        $sale->save();

        if($sale->momo_transaction->status){
        	// :) Successful paid $price to your account
        	...

        } else {
        	// :( Transaction Error
        	...

        }

        ...

        return $sale::with('momo_transaction');
    }
}

Thanks.

Momo Package: https://github.com/malic0/momo