How to Integrate Instamojo Payment Gateway in Laravel 5

In this tutorial we going to learn How to Integrate Instamojo Payment Gateway in Laravel 5. Most of thing are already in Instamojo documentation we just going to see it in step so you will easily integrate without reading whole document.

Laravel has a package called “IndiPay – Indian Payment Gateways” which supports payment gateways like CCAvenue, PayUMoney, EBS, CitrusPay, Instamojo but in this tutorial without use package. In this example, I have all values written static in the input box.

Steps For How to Integrate Instamojo Payment Gateway in Laravel 5

Step 1. Create Instamojo account

Go to https://www.instamojo.com/ and sign up account.

Step 2. At time of sign up use your valid email.

Step 3. Fill all required business details.

Step 4. Add your bank detail.

Step 5. Create route in laravel

We are required only two route to call controller.

Route::get('subscribe', [
    'as' => 'subscribe',
    'uses' => 'SigninController@SubscribProcess'
]);

Route::get('response', [
    'as' => 'response',
    'uses' => 'SigninController@Response'
]);

Step 6. SigninController with SubscribProcess function

In this step you can need X-Api-Key and X-Auth-Token this key provide Instamojo.

public function SubscribProcess()
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://www.instamojo.com/api/1.1/payment-requests/');
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_HTTPHEADER,
            array("X-Api-Key:",
          "X-Auth-Token:"));
    $payload = Array(
        'purpose' => 'First Subscribe',
        'amount' => '500',
        'phone' => '0123456789',
        'buyer_name' => Auth::user()->name,
        'redirect_url' => 'http://yoururltoredirect/response/',
        'send_email' => true,
        'webhook' => '',
        'send_sms' => true,
        'email' => Auth::user()->email,
        'allow_repeated_payments' => false
    );

    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($payload));
    $response = curl_exec($ch);
    curl_close($ch); 

     $json_decode  = json_decode($response, true);

     $query_update =  DB::table('tbl_users')
        ->where('id', Auth::user()->id)
        ->update([
            'payment_id'  => $json_decode['payment_request']['id'],
        ]);

   
    $long_url = $json_decode['payment_request']['longurl'];
    header("Location:$long_url");
    exit();
}

Step 7. SigninController with Response function

In this step instamojo check success payment and cancel payment response.

public function Response(Request $request)
{
    if(!empty($request->get('payment_request_id')))
    {
        $payment_id  = $request->get('payment_request_id');
        $getdetail = DB::table('tbl_users')
            ->where('id','=', Auth::user()->id)
            ->select('*')
            ->first();   
        if($payment_id == $getdetail->payment_id)
        {
             dd('Success Payment!');
        }
        else
        {
             dd('Due to some error please try again.');
        }
    }
    else
    {
         dd('Due to some error please try again.');
    }
}

And if you like this tutorials please share it with your friends via Email or Social Media.

Leave a Reply

Your email address will not be published. Required fields are marked *