Integrating MTN and Orange Money payments to your Laravel Application with Mesomb

7 months ago | 2 minute read | mesomb | Laravel | MTN | Orange | PHP

Mesomb + Laravel

In this article, we will see how you can add payments to your laravel application with the help of mesomb's payment gateway.

The good news is I already published a laravel package for it, so I'll just highlight the steps you need to follow to have it up and running.

Let's start with installing the package first - ­čĹë laravel-mesomb

composer require malico/laravel-mesomb

Next, we need to publish the configuration file, we have to fit in your API keys from your mesomb dashboard.

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

To obtain your Keys you need to sign up on https://mesomb.hachther.com/. After signup, Visit https://mesomb.hachther.com/en/backend/applications/ to view/create/edit your applications.

Click on New application to access the application creation form


After creating an application, click on API Keys to view keys. Copy them into .env the configuration file with the following keys. Mesomb API Documentation

MeSomb_APP_KEY= // application key
´╗┐MeSomb_API_KEY= // // api key
MeSomb_PIN=  // only if you will use mesomb deposition option

´╗┐Just one more thing, the package persists all transactions, to create those tables, run.

php artisan migrate

Setup complete.

Collecting Payments

Collecting payments is very easy. Within your controller, all you need to do is create a new instance of Malico\Mesomb\Payment class, with phone number and amount being first and second params respectively. To initiate a payment, call the pay() method of payment initiated. Something like...

<?php 
// OrderController.php´╗┐
´╗┐
use Malico\MeSomb\Payment;

class OrderController extends Controller {

    public function confirmOrder()
    {
        $request = new Payment('+23767xxxxxxx', 1000);

        $payment = $request->pay();

        if($payment->success){
            // Fire some event,Pay someone, Alert user
        } else {
            // fire some event, redirect to error page
        }

        // get Transactions details $payment->transactions
    }
}

Not the package depends on malico/mobile-cm-php, a PHP package that determines network provider i.e. if the phone number is ORANGE or MTN then best decides on request params.

The package also gives you the opportunity to associate each payment transaction to a particular model. This is particularly useful if you want to reference payments on later dates. An example is fetching payments for a particular order.

Within your model.

<?php 
// Order.php

use Malico\MeSomb\Helper\HasPayments;

class Order extends Model
{
    use HasPayments;
}

And then from your controller

<?php 
´╗┐// OrderController.php

class OrderController extends Controller {

    public function confirmOrder(){

        $order = Order::create(['amount' => 100]);

        $payment  = $order->payment('+23767xxxxxxx', $order->amount)->pay();

        if($payment->success){
            // Fire some event,Pay someone, Alert user
        } else {
            // fire some event, redirect to error page
        }

        // View Order payments via $order->payments

        // Get payment transaction with $payment->transaction

        return $payment;
    }
}

That was easy. Now you can collect payments on your platform without the unnecessary paperwork other payment gateways require.

Sending Payments

Mesomb also supports deposits (sending out payments), our package also provides provision for that. Make sure you have the application pin set up. Copy the APP Pin into your .env file.

<?php 
// PayOutController.php´╗┐
´╗┐
use Malico\MeSomb\Deposit;

class PayOutController extends Controller {

    public function confirmOrder(){
        $request = new Deposit('+23767xxxxxxx', 1000);

        $payment = $request->pay();

        if($payment->success){
            // Fire some event, send payout email
        } else {
            // fire some event, redirect to error page
        }

        // get Transactions details $payment->transactions
    }
}

Pretty cool right?...

Have fun with it. Any issues, I'll answer your question - Twitter: @yondifon