Search code examples
phplaravel-5.3csrfpayment-gatewaypaytm

TokenMismatchException in VerifyCsrfToken.php line 67: while integrating paytm with laravel 5.3


token mismatch exception in verifyCsrfToken

I had integrated paytm by referring "https://github.com/anandsiddharth/laravel-paytm-wallet" with laravel 5.3 and when doing payment it redirect to paytm site and when it redirect back to my site it gives error "TokenMismatchException in VerifyCsrfToken.php line 67"

View.php

               <form action="{{action('OrderController@order')}}" method="post" name="payuForm" >
                  <input type="hidden" name="_token" value="{{ csrf_token() }}">
                  <input type="hidden" name="firstname" id="firstname" value="{{$data1->first_name}}" />
                  <input type="hidden" name="email" id="email" value="{{$data1->email}}" />
                  <input type="text"   name="amount" value="<?php echo $data[0]->cost?>" readonly/>
                  <input type="text"   name="productinfo" value="<?php echo $data[0]->mid?>" readonly />
                  <input type="text"   name="phone" value="{{$data1->mobile}}" readonly/>
                  <center><button class="btn btn-colored btn-theme-colored text-uppercase pay">Pay</button></center>
                </form>

Controller.php

<?php

namespace App\Http\Controllers;

use PaytmWallet;
use Illuminate\Http\Request;
use App\EventRegistration;
use Session;
use DB;

class OrderController extends Controller
{

    /**
     * Redirect the user to the Payment Gateway.
     *
     * @return Response
     */
    public function register()
    {
        return view('register');
    }

    /**
     * Redirect the user to the Payment Gateway.
     *
     * @return Response
     */
    public function order(Request $request)
    {

        $input = $request->all();
        //pri($input); die();
        $input['order_id'] = str_random(16);
        $input['fee'] = $input['amount'];
        Session::put('packid', $input['productinfo']);
        EventRegistration::create($input);

        $payment = PaytmWallet::with('receive');
        $payment->prepare([
          'order' => $input['order_id'],
          'user' => $input['firstname'],
          'mobile_number' => $input['phone'],
          'email' => $input['email'],
          'amount' => $input['fee'],
          'callback_url' => url('payment/status')
        ]);
        //pri($payment);exit;
        return $payment->receive();
    }

    /**
     * Obtain the payment information.
     *
     * @return Object
     */
    public function paymentCallback()
    {

        $transaction = PaytmWallet::with('receive');

        $response = $transaction->response();
        $order_id = $transaction->getOrderId();

        if($transaction->isSuccessful()){
            dd('payment Done');
        }else if($transaction->isFailed()){
           dd('payment Failed');
        }
    }    
}

Routes.php

Route::get('event-registration', 'OrderController@register');
Route::post('payment', 'OrderController@order');
Route::post('payment/status', 'OrderController@paymentCallback');

Solution

  • hey @Deepak Patel i search lots of information related your question.i found one solution for you. version 5.1 Laravel's VerifyCsrfToken middleware allows to routes.you need to add the routes to $except array in your App\Http\MiddlewareVerifyCsrfToken class:

    <?php namespace App\Http\Middleware;
    
    use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;
    
    class VerifyCsrfToken extends BaseVerifier
    {
      protected $except = [
        'your-route-name/*',
      ];
    }
    

    it's disable csrf in laravel for specific route.Hope it's work.