/momopay

Paiement mobile au cameroun

Primary LanguagePHP

MOMOPAY

Packagist License Packagist Version (including pre-releases) Packagist Downloads

Description

A packages for mobile money payment in Cameroun.
Only Mtn Cameroon is supported

🛑 please wait for the first stable release before use this package

Installation

composer require nkaurelien/momopay

(optional) Add the service provider in config\app.php

    providers' => [
        #...
        \Nkaurelien\Momopay\Providers\MomopayServiceProvider::class,
    ]

(optional) Add the service facade in config\app.php

    aliases' => [
        #...
        'MomoPay' => \Nkaurelien\Momopay\Facades\MomoPay::class
    ]

Configuration

Add config to config/services.php


    'mtn' => [
        'currency' => env('MTN_MOMO_CURRENCY', 'XAF'),
        'go_live' => env('MTN_MOMO_GO_LIVE',false),
        'api_key' => env('MTN_MOMO_USER_API_KEY'),
        'reference_id' => env('MTN_MOMO_ID'),
        'subscription_key' => env('MTN_MOMO_KEY'),
        'payment_callback_route' => env('MTN_MOMO_CALLBACK_URL','payment.momo.callback'),
        'payment_callback_host' => env('MTN_MOMO_CALLBACK_HOST'),
        'notification_email' => env('MTN_MOMO_NOTIFICATION_EMAIL'),
    ],

Configuration description

  • reference_id : is the user id
  • subscription_key : is the Ocp-Apim-Subscription-Key
  • target_environment : can be sanbox or mtncameroon (when you go live)

Don't forget to cache the configurations again with the command php artisan config:cache

Add routes

Route::any('/payment/momo/callback', 'PaymentMomoController@callback')->name('payment.momo.callback');
Route::get('/payment/momo/transaction/{id}', 'PaymentMomoController@getPayment')->name('payment.momo.gettransaction');

Use in controller

First inject the repository class

    private $paymentMomoRepository;
    public function __construct(PaymentMomoRepository $paymentMomoRepository){ #...   

Then consume repository instance to implement your payment logic

    $momoRequestToPayDto = new \Nkaurelien\Momopay\Fluent\MomoRequestToPayDto;
    $momoRequestToPayDto->amount = 100;
    $momoRequestToPayDto->payeeNote = '';
    $momoRequestToPayDto->payerMessage = '';
    $momoRequestToPayDto->externalId = 'my_product_id';
    $momoRequestToPayDto->payer->telephone = '2376XXXXXXXX';
    
    # optional
    $momoRequestToPayDto->currency = 'XAF'; // Use EUR when you are in sandbox mode

    $refId = \Ramsey\Uuid\Uuid::uuid4()->toString();

    $requestToPayResult = $this->paymentMomoRepository->requestToPay($momoRequestToPayDto, $refId);

If your prefer the facade instead of injection

    #...
    $requestToPayResult = \Nkaurelien\Momopay\Facades\MomoPay::requestToPay($momoRequestToPayDto, $refId);

Capture events

You can listen to :

  • Nkaurelien\Momopay\Events\PaymentAccepted is fired after the success of request to pay

Todo

  • Create payment exceptions class
  • Create payment events
  • Email notification on payment success
  • Add orange money payment method

Useful links