Call to private Instamojo\Instamojo::__construct() from context

1.1k views Asked by At
public function pay(Request $request){

$api = new \Instamojo\Instamojo(
       config('services.instamojo.api_key'),
       config('services.instamojo.auth_token'),
       config('services.instamojo.url')
   );

 try {
   $response = $api->paymentRequestCreate(array(
       "purpose" => "FIFA 16",
       "amount" => $request->amount,
       "buyer_name" => "$request->name",
       "send_email" => true,
       "email" => "$request->email",
       "phone" => "$request->mobile_number",
       "redirect_url" => "http://127.0.0.1:8000/pay-success"
       ));
        
       header('Location: ' . $response['longurl']);
       exit();
   }catch (Exception $e) {
    print('Error: ' . $e->getMessage());
  }
}

Error after submitting form on $api = new \Instamojo\Instamojo( line.

Error:- Call to private Instamojo\Instamojo::__construct()

1

There are 1 answers

10
Hari Darshan On

Correct usage is like this

$api = \Instamojo\Instamojo::init($authType, [
    'client_id' => '<clientId>',
    'client_secret' => '<clientSecret>',
    'username' => '<userName>',   // optional
    'password' => '<password>',   // optional
    'scope' => '<scope if any>'   // optional
], false);

try {
    $response = $api->createPaymentRequest(array(
        "purpose" => "FIFA 16",
        "amount" => $request->amount,
        "buyer_name" => "$request->name",
        "send_email" => true,
        "email" => "$request->email",
        "phone" => "$request->mobile_number",
        "redirect_url" => "http://127.0.0.1:8000/pay-success"
    ));

    header('Location: ' . $response['longurl']);
    exit();
} catch (Exception $e) {
    print('Error: ' . $e->getMessage());
}

$authType can be either app or user. If you want to use Test environment then pass third argument as true or false for Production

Note: There is an issue with Instamojo-php v1.0. Issue is InvalidRequestException.php exception class namespace is not correct. So, fix is to manually change the namespace of this exception class from Instamojo\Exception to Instamojo\Exceptions