Alodon's avatar

View is not loading :(

This is my route. Please let me know if I am doing anything wrong.

Route::get('/pay', 'PaymentController@index');

This is my controller

    public function index()
    {
        return view('pay');
    }
0 likes
20 replies
Alodon's avatar

I tried with this. but nothing happened.

    public function index()
    {
        return "Hello World";
    }
drewdan's avatar

Can you post the full controller please?

Alodon's avatar

Have a look

<?php
 
namespace App\Http\Controllers;
 
use Illuminate\Http\Request;
use Omnipay\Omnipay;
use App\Payment;
 
class PaymentController extends Controller
{
 
    public $gateway;
 
    public function __construct()
    {
        $this->gateway = Omnipay::create('PayPal_Rest');
        $this->gateway->setClientId(env('PAYPAL_CLIENT_ID'));
        $this->gateway->setSecret(env('PAYPAL_CLIENT_SECRET'));
        $this->gateway->setTestMode(true); //set it to 'false' when go live
    }
 
    public function index()
    {
        return "Hello World";
    }
 
    public function charge(Request $request)
    {
        if($request->input('submit'))
        {
            try {
                $response = $this->gateway->purchase(array(
                    'amount' => $request->input('amount'),
                    'currency' => env('PAYPAL_CURRENCY'),
                    'returnUrl' => url('paymentsuccess'),
                    'cancelUrl' => url('paymenterror'),
                ))->send();
          
                if ($response->isRedirect()) {
                    $response->redirect(); // this will automatically forward the customer
                } else {
                    // not successful
                    return $response->getMessage();
                }
            } catch(Exception $e) {
                return $e->getMessage();
            }
        }
    }
 
    public function payment_success(Request $request)
    {
        // Once the transaction has been approved, we need to complete it.
        if ($request->input('paymentId') && $request->input('PayerID'))
        {
            $transaction = $this->gateway->completePurchase(array(
                'payer_id'             => $request->input('PayerID'),
                'transactionReference' => $request->input('paymentId'),
            ));
            $response = $transaction->send();
         
            if ($response->isSuccessful())
            {
                // The customer has successfully paid.
                $arr_body = $response->getData();
         
                // Insert transaction data into the database
                $isPaymentExist = Payment::where('payment_id', $arr_body['id'])->first();
         
                if(!$isPaymentExist)
                {
                    $payment = new Payment;
                    $payment->payment_id = $arr_body['id'];
                    $payment->payer_id = $arr_body['payer']['payer_info']['payer_id'];
                    $payment->payer_email = $arr_body['payer']['payer_info']['email'];
                    $payment->amount = $arr_body['transactions'][0]['amount']['total'];
                    $payment->currency = env('PAYPAL_CURRENCY');
                    $payment->payment_status = $arr_body['state'];
                    $payment->save();
                }
         
                return "Payment is successful. Your transaction id is: ". $arr_body['id'];
            } else {
                return $response->getMessage();
            }
        } else {
            return 'Transaction is declined';
        }
    }
 
    public function payment_error()
    {
        return 'User is canceled the payment.';
    }
 
}

Alodon's avatar

And if I change it to this it works:

Route::get('/', 'PaymentController@index');
Alodon's avatar

No, This is my view:

@extends('layouts.public')

@section('title', 'Latest Blogs and trip')


@section('styles')

<link href="css/all.css" rel="stylesheet">

<!-- Bootstrap core CSS -->
<link href="css/main.min.css" rel="stylesheet">

<!-- Custom styles for this template -->
<link href="css/new.css" rel="stylesheet">

@endsection

@section('content')
        <div class="flex-center position-ref full-height">
  
            <div class="content">
            
            <form action="{{ url('charge') }}" method="post">
    <input type="text" name="amount" />
    {{ csrf_field() }}
    <input type="submit" name="submit" value="Pay Now">
</form>
            </div>
        </div>
        @section('js')




<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="./assets/js/vendor/jquery-slim.min.js"></script>
<script src="./js/popper.min.js" ></script>
<script src="./js/jquery.min.js" ></script>
<script src="./js/bootstrap.min.js" ></script>
<script src="./js/new.js" ></script>
<script src="./js/all.js" ></script>

<script>

$(document).ready(function(){
$("p img").addClass("important");
});

</script>
@endsection

@endsection
drewdan's avatar

What about:

Route::get('pay', 'PaymentController@index');

?

MichalOravec's avatar

@drewdan

Route::get('pay', 'PaymentController@index');

or

Route::get('/pay', 'PaymentController@index');

It doesn't matter, it's still same.

drewdan's avatar

@michaloravec - I felt silly immediately after suggesting it. Especially after looking at my own routes file :(

Do you have any other route names which may be conflicting?

Alodon's avatar

I have these four routes for this controller. I have searched.

Route::get('pay', 'PaymentController@index');
Route::post('charge', 'PaymentController@charge');
Route::get('paymentsuccess', 'PaymentController@payment_success');
Route::get('paymenterror', 'PaymentController@payment_error');
Alodon's avatar

Actually there are more than 300 routes on my website. I am attaching the routes that are out of the user and admin group.

Auth::routes();

Route::get('event-registration', 'OrderController@register');

//Route::post('payment', 'OrderController@order');

Route::get('/redirect', 'Auth\LoginController@redirect');

Route::get('/callback', 'Auth\LoginController@callback');

Route::get('/redirectfb', 'Auth\LoginController@redirectfb');

Route::get('/callbackfb', 'Auth\LoginController@callbackfb');

Route::get('/', 'GuestController@index')->name('welcome');

Route::get('/verify/logout', 'GuestController@verifyLogout')->name('verifyLogout');

Route::get('/banned/logout', 'GuestController@banned')->name('bannedLogout');

Route::get('/verify/user/{token}', 'GuestController@verify')->name('verify');

Route::get('/contact-us', 'GuestController@contact')->name('contact');

Route::post('/contact-us', 'GuestController@EmailContact')->name('GuestEmail');

Route::get('/payment-proof', 'GuestController@proof')->name('paymentProof');

Route::get('/blog', 'GuestController@tutorials')->name('tutorials');

Route::get('/discover/hill-stations', 'GuestController@discover1')->name('discover1');

Route::get('/blog/latest', 'GuestController@latestblogs')->name('latestblogs');

Route::get('/blog/mostpopular', 'GuestController@popularblogs')->name('popularblogs');

Route::get('/blog/todaytrends', 'GuestController@trendblogs')->name('trendblogs');

Route::get('/blog/this_week', 'GuestController@weekblogs')->name('weekblogs');

Route::get('/blog/this_month', 'GuestController@monthblogs')->name('monthblogs');

Route::get('/blog/this_year', 'GuestController@yearblogs')->name('yearblogs');

Route::get('/blog/most_view', 'GuestController@viewblogs')->name('viewblogs');

Route::get('/blog/most_shared', 'GuestController@shareblogs')->name('shareblogs');

Route::get('/blog/most_favorite', 'GuestController@favblogs')->name('favblogs');

Route::get('/forum', 'GuestController@forums')->name('forums');

Route::get('/search', 'GuestController@search')->name('search');

Route::get('/view/post/{slug}', 'GuestController@tutorialView')->name('viewPost');

Route::get('/view/forum/{slug}', 'GuestController@forumView')->name('viewForum');

Route::get('/view/package/{slug}', 'GuestController@packageView')->name('viewPackage');

Route::get('/{slug}', 'GuestController@PageView')->name('viewPage');

Route::get('user/profile', 'UserProfileController@index')->name('userProfile');

Route::get('paypal', 'PaymentController@index');
Route::post('charge', 'PaymentController@charge');
Route::get('paymentsuccess', 'PaymentController@payment_success');
Route::get('paymenterror', 'PaymentController@payment_error');
drewdan's avatar
Route::get('/{slug}', 'GuestController@PageView')->name('viewPage');

That one is the issue, pay is being matched to the slug

drewdan's avatar

If you move your Route::get('pay'.. before this route, I think it might work!

MichalOravec's avatar
Level 75

@alodon Put this route to the end of your route file.

Route::get('/{slug}', 'GuestController@PageView')->name('viewPage');
1 like
siangboon's avatar

perhaps, double check your web.php, full route list, whether any wildcard before the /pay

Route::get('/{model}', 'HomeController@index');

or show the route list may help other to understand more...

Please or to participate in this conversation.