Joy Awino
Joy Awino

Reputation: 11

Integrating Mpesa into Laravel 11 application

For Laravel users, open the Config/App.php file and add \Safaricom\Mpesa\MpesaServiceProvider::class under providers and 'Mpesa'=> \Safaricom\Mpesa\MpesaServiceProvider::class under aliases. How do I achieve this in Laravel 11. The above instruction only works if the Laravel version is 10 and below

I tried putting this in bootstrap/app.php and encountered an error bootstrap/app.php

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;

return Application::configure(basePath: dirname(__DIR__))
    ->withConfig(['aliases' => require __DIR__.'/../config/aliases.php'])
    ->withServiceProviders([
        \Safaricom\Mpesa\MpesaServiceProvider::class,
    // other service providers
    ])
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        api: __DIR__.'/../routes/api.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {
        //
    })
    ->withExceptions(function (Exceptions $exceptions) {
        //
    })->create();

And the following is the error

PS C:\xampp\htdocs\XWaste> php artisan serve
PHP Fatal error:  Uncaught Error: Call to undefined method Illuminate\Foundation\Configuration\ApplicationBuilder::withConfig() in C:\xampp\htdocs\XWaste\bootstrap\app.php:8
Stack trace:
#0 C:\xampp\htdocs\XWaste\artisan(12): require_once()
#1 {main}
  thrown in C:\xampp\htdocs\XWaste\bootstrap\app.php on line 8

Fatal error: Uncaught Error: Call to undefined method Illuminate\Foundation\Configuration\ApplicationBuilder::withConfig() in C:\xampp\htdocs\XWaste\bootstrap\app.php:8
Stack trace:
#0 C:\xampp\htdocs\XWaste\artisan(12): require_once()
#1 {main}
  thrown in C:\xampp\htdocs\XWaste\bootstrap\app.php on line 8<?php

Upvotes: 1

Views: 60

Answers (1)

Asad
Asad

Reputation: 27

In Laravel 11, withConfig() function will not work. You can define your aliases in a new service provider and then include it in the main application

public function register()
{
    $this->app->alias('Mpesa', \Safaricom\Mpesa\MpesaServiceProvider::class);
}

Upvotes: 0

Related Questions