DENNIS KITHINJI
DENNIS KITHINJI

Reputation: 236

I am trying to hit a certain local MPESA Payment API but am getting an error

I am trying to get a response from MPESA payment API using laravel but I am getting an error . My code is as below

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class MPESA_AUTH extends Controller
{
    public function Authorize(){

        $url = 'https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials';
        $CONSUMER_KEY= 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
        $CONSUMER_SECRET= 'xxxxxxxxxxxx';
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        $credentials = base64_encode($CONSUMER_KEY,$CONSUMER_SECRET);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Basic '.$credentials)); //setting a custom header
        curl_setopt($curl, CURLOPT_HEADER, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

        $curl_response = curl_exec($curl);

        $curl_json=json_decode($curl_response);
        return $curl_json;
    }
}

The error am getting is as below enter image description here

Upvotes: 1

Views: 562

Answers (2)

Destiny Maina
Destiny Maina

Reputation: 71

You should use a different function name in place of "Authorize". This is because "authorize" in controllers is a preserved name used in the parent class Controller.

Upvotes: 0

Faran Ali
Faran Ali

Reputation: 482

The Base controller uses Illuminate\Routing\Controller trait which has an 'authorize()' function. Your function declaration is clashing with it. Change your controller method name to anything else(other than 'authorize') and you should be good to go

Upvotes: 2

Related Questions