myfatoorah Payment Gateway Integration Laravel

  • 30-11-2022
  • 1132
  • Laravel 9
  • Haresh Chauhan

Watch Youtube Video (HINDI) :

How to install myfatoorah payment gateway in laravel; in this post, we will learn integration of myfatoorah payment in the laravel application using install composer, therefor we will install the composer require myfatoorah/laravel-package in our application.

myfatoorah payment gateway This post helps you to integrate laravel very simple and easy way I have written a post below. Therefore you just need to do a little configuration.

myfatoorah payment gateway integration we will simply see in this post with the configuration step-by-step example. also, code available you can copy and panel and add to your application.

Step 1. Install Composer

To install Myfatoorah in the laravel project, need to install laravel composer myfatoorah/laravel-package. use the below provided command in your application command prompt and install payment composer in the application.

composer require myfatoorah/laravel-package

Step 2. Publish Config

Publish the myfatoorah configuration file using the below-provided command in your project terminal.

php artisan vendor:publish --provider="MyFatoorah\LaravelPackage\MyFatoorahServiceProvider" --tag="myfatoorah"

After the published config file, go to config/myfatoorah.php and set your credential there, but the default test credential may be set already there. Goto your myfatoorah account and generate your API key credentials. Once you generate add to this configuration file.

Use true, if you are using test credentials, else add false and test mode keys and add production api_key in this configuration.

config/myfatoorah.php
<?php

return [

     /**
     * API Token Key
     * Live Token: https://myfatoorah.readme.io/docs/live-token
     * Test Token: https://myfatoorah.readme.io/docs/test-token
     */
     'api_key' => '',
     
     /**
     * Test Mode
     * Accepted value: 'true' for the test mode or 'false' for the live mode
     */
     'test_mode' => 'true',
     
     /**
     * Country ISO Code
     * Accepted value: KWT, SAU, ARE, QAT, BHR, OMN, JOD, or EGY.
     */
     'country_iso' => 'KWT'
];

Alos there are two links given for the test and live production documentation check, Please follow the provided link and configuration file and follow more deep details from there.

Step 3. Payment Controller

This is full payment confirmation for the myfatoorah payment, the construction method will retrieve data form the configuration file. In response, you will get a link for the payment just redirect or if you are using the API response link URL in response else redirect your application to the response link given.

Some little user details and redirect and cancel URLs also need you provided in the route file.

<?php

namespace App\Http\Controllers;

use MyFatoorah\Library\PaymentMyfatoorahApiV2;

class MyFatoorahController extends Controller {

    public $mfObj;

    /**
     * create MyFatoorah object
     */
    public function __construct() 
    {
        $this->mfObj = new PaymentMyfatoorahApiV2(
               config('myfatoorah.api_key'), 
               config('myfatoorah.country_iso'), 
               config('myfatoorah.test_mode')
          );
    }

    /**
     * Create MyFatoorah invoice
     *
     * @return \Illuminate\Http\Response
     */

    public function index() 
     {
        try {

          $paymentMethodId = 0; // 0 for MyFatoorah invoice or 1 for Knet in test mode

          $data = $this->mfObj->getInvoiceURL($this->getPayLoadData(), $paymentMethodId);

          return response()->json(['IsSuccess' => 'true', 'Message' => 'Invoice created successfully.', 'Data' => $data]);

        } catch (\Exception $e) {

          return response()->json(['IsSuccess' => 'false', 'Message' => $e->getMessage()]);
        }
     }

    /**
     * 
     * @param int|string $orderId
     * @return array
     */

    private function getPayLoadData($orderId = null) {

        $callbackURL = route('myfatoorah.callback');

        return [
            'CustomerName'       => 'FName LName',
            'InvoiceValue'       => '10',
            'DisplayCurrencyIso' => 'KWD',
            'CustomerEmail'      => 'test@test.com',
            'CallBackUrl'        => $callbackURL,
            'ErrorUrl'           => $callbackURL,
            'MobileCountryCode'  => '+965',
            'CustomerMobile'     => '12345678',
            'Language'           => 'en',
            'CustomerReference'  => $orderId,
            'SourceInfo'         => 'Laravel ' . app()::VERSION . ' - MyFatoorah Package ' . MYFATOORAH_LARAVEL_PACKAGE_VERSION
        ];
    }

    /**
     * Get MyFatoorah payment information
     * 
     * @return \Illuminate\Http\Response
     */
    public function callback() 
    {
        try {

          $data = $this->mfObj->getPaymentStatus(request('paymentId'), 'PaymentId');

          if ($data->InvoiceStatus == 'Paid') {

               $msg = 'Invoice is paid.';
          } else if ($data->InvoiceStatus == 'Failed') {

               $msg = 'Invoice is not paid due to ' . $data->InvoiceError;
          } else if ($data->InvoiceStatus == 'Expired') {

               $msg = 'Invoice is expired.';
          }
            
          return response()->json(['IsSuccess' => 'true', 'Message' => $msg, 'Data' => $data]);

        } catch (\Exception $e) {

          return response()->json(['IsSuccess' => 'false', 'Message' => $e->getMessage()]);
        }
    }
}

We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :